npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dnext-tech/loom-x

v0.1.0

Published

Extended JavaScript library to interact with Loom Network.

Downloads

4

Readme

LoomX

Extended JavaScript library to interact with Loom Network.

Why LoomX?

Loom Network has its own JavaScript library loom-js which offers a variety of features which are needed when developing on Loom Network. LoomX wraps loom-js but focuses on core features such as depositing and withdrawing assets to offer an easier way of interacting with Loom Network.

Install

yarn add @dnext-tech/loom-x.js

or

npm install @dnext-tech/loom-x.js

Getting Started

Loom Network is a DPOS sidechain that guarantees sub-second confirmation. Most of the transactions will happen on Loom Network. However, your assets need to be transferred from Ethereum Network. So loom-x interacts with both networks.

Selecting network

First of all, you need to select which network to use for both Ethereum and Loom Network

import { EthereumNetwork, LoomNetwork } from "@dnext-tech/loom-x";

EthereumNetwork.setCurrent(EthereumNetwork.mainnet);
// or
EthereumNetwork.setCurrent(EthereumNetwork.ropsten);

LoomNetwork.setCurrent(LoomNetwork.plasma); // mainnet
// or
LoomNetwork.setCurrent(LoomNetwork.extdev); // testnet

Create private keys

You need to create private keys for both Ethereum Network and Loom Network.

import { CryptoUtils } from "@dnext-tech/loom-x";

const ethereumPrivateKey = CryptoUtils.createEthereumPrivateKey();
// save your ethereum private key
const loomPrivateKey = CryptoUtils.createLoomPrivateKey();
// save your loom private key

Create LoomX

If you have private keys, you can create an LoomX.

import LoomX from "@dnext-tech/loom-x";

const loomx = new LoomX(ethereumPrivateKey, loomPrivateKey);

or, you can create LoomX using 12-words mnemonic.

import LoomX from "@dnext-tech/loom-x";

const loomx = LoomX.fromMnemonic("glove amused flock sight want basic course invite chase paper crater defense"); // example mnemonic

Map accounts

Your accounts in Ethereum Network and Loom Network must be mapped before deposit/withdrawal of assets.

const mapped = await loomx.loomChain.hasMapping(loomx.ethereumChain.address);
if (!mapped) {
    await loomx.loomChain.mapAccounts(loomx.ethereumChain.signer, loomx.ethereumChain.address, loomx.loomChain.address);
}

Deposit ETH/ERC20

You can easily deposit ETH and ERC20 assets using LoomX.

ETH

import { BigNumberUtils } from "@dnext-tech/loom-x";

const amount = BigNumberUtils.toBigNumber(10**18); // 1 ETH
const tx = await loomx.ethereumChain.depositETHAsync(amount);
await tx.wait();

ERC20

import { BigNumberUtils } from "@dnext-tech/loom-x";

const asset = new ERC20Asset("DAIToken", "DAI", 18, "0x...", "0x..."); // DAIToken
const gateway = loomx.ethereumChain.getTransferGateway();
const amount = BigNumberUtils.toBigNumber(10**18); // 1 DAI
const approveTx = await loomx.ethereumChain.approveERC20Async(asset, gateway.address, amount);
await approveTx.wait();
const depositTx = await loomx.ethereumChain.depositERC20Async(asset, amount);
await depositTx.wait();

After 10 blocks of confirmation, transfer gateway oracle generates same amount of assets in Loom Network.

Withdraw ETH/ERC20

ETH and ERC20 assets in Loom Network can be withdrawn to Ethereum Network.

ETH

import { BigNumberUtils, Constants } from "@dnext-tech/loom-x";

const amount = BigNumberUtils.toBigNumber(10**18); // 1 ETH
const ethereumGateway = loomx.ethereumChain.getTransferGateway().address;
const myEthereumAddress = loomx.ethereumChain.getAddress().toLocalAddressString();
// Call to Loom Network
const tx1 = await loomx.loomChain.withdrawETHAsync(amount, ethereumGateway);
await tx1.wait();
// Listen to the withdrawal signature
const signature = await loomx.loomChain.listenToTokenWithdrawal(Constants.ZERO_ADDRESS, myEthereumAddress);
// Call to Ethereum Network
const tx2 = await loomx.ethereumChain.withdrawETHAsync(amount, signature);
await tx2.wait();

ERC20

import { BigNumberUtils } from "@dnext-tech/loom-x";

const asset = new ERC20Asset("DAIToken", "DAI", 18, "0x...", "0x..."); // DAIToken
const amount = BigNumberUtils.toBigNumber(10**18); // 1 DAI
// Call to Loom Network
const tx1 = await loomx.loomChain.withdrawERC20Async(asset, amount);
await tx1.wait();
// Listen to the withdrawal signature
const signature = await loomx.loomChain.listenToTokenWithdrawal(asset.ethereumAddress.toLocalAddressString(), myEthereumAddress);
// Call to Ethereum Network
const tx2 = await loomx.ethereumChain.withdrawERC20Async(asset, amount, signature);
await tx2.wait();

LoomChain.listenToWithdrawal() waits for 120 seconds then it times out if no withdrawal signature is generated.

Handling Pending Withdrawal

If LoomChain.listenToWithdrawal() times out after 120 seconds or you couldn't properly withdraw your assets, your withdrawal will be in pending state so you need to handle this manually.

ETH

import { BigNumberUtils } from "@dnext-tech/loom-x";
import { bytesToHexAddr } from "loom-js/dist/crypto-utils";

// Check if you have a pending receipt
const nonce = await loomx.ethereumChain.getWithdrawalNonceAsync();
if (nonce) {
    // Get pending withdrawal receipt with the nonce
    const receipt = await loomx.getPendingETHWithdrawalReceipt(nonce);
    // Withdraw pending ETH
    const tx = await ethereumChain.withdrawETHAsync(
        BigNumberUtils.toBigNumber(receipt.tokenAmount.toString()),
        bytesToHexAddr(receipt.oracleSignature)
    );
    await tx.wait();
}

ERC20

import { BigNumberUtils } from "@dnext-tech/loom-x";
import { bytesToHexAddr } from "loom-js/dist/crypto-utils";

// Check if you have a pending receipt
const nonce = await loomx.ethereumChain.getWithdrawalNonceAsync();
if (nonce) {
    const asset = new ERC20Asset("DAIToken", "DAI", 18, "0x...", "0x..."); // DAIToken
    // Get pending withdrawal receipt with the nonce
    const receipt = await loomx.getPendingERC20WithdrawalReceipt(nonce);
    // Withdraw pending ERC20
    const tx = await ethereumChain.withdrawERC20Async(
        asset,
        BigNumberUtils.toBigNumber(receipt.tokenAmount.toString()),
        bytesToHexAddr(receipt.oracleSignature)
    );
    await tx.wait();
}

Author

👤 @YoonjaeYoo