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

@web3-guru/loom-x

v0.5.1

Published

[![npm version](https://badge.fury.io/js/%40web3-guru%2Floom-x.svg)](https://badge.fury.io/js/%40web3-guru%2Floom-x)

Downloads

2

Readme

LoomX

npm version

Extended JS library for 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 @web3-guru/loom-x.js

or

npm install @web3-guru/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 "@web3-guru/loom-x/dist";

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 "@web3-guru/loom-x/dist";

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 "@web3-guru/loom-x/dist";

const loomx = new LoomX(ethereumPrivateKey, loomPrivateKey);

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

import LoomX from "@web3-guru/loom-x/dist";

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.loom.hasAccountMapping(loomx.ethereum.address);
if (!mapped) {
    await loomx.loom.mapAccounts(loomx.ethereum.signer, loomx.ethereum.address, loomx.loom.address);
}

Map contracts

ERC20/ERC721 contracts must be mapped prior to using them.

import { Address } from "@web3-guru/loom-x/dist";

// Your contract address on Ethereum
const ethereumAddress = Address.createEthereumAddress("0x...");
// Tx hash used when deploying your contract on Ethereum
const ethereumTxHash = "0x...";
// Your contract address on Loom
const loomAddress = Address.createLoomAddress("0x...");

await loomx.loom.mapContracts(loomx.ethereum, ethereumAddress, ethereumTxHash, loomAddress);

Deposit ETH/ERC20

You can easily deposit ETH and ERC20 assets using LoomX.

ETH

import { BigNumberUtils } from "@web3-guru/loom-x/dist";

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

ERC20

import { Address, BigNumberUtils } from "@web3-guru/loom-x/dist";

const assetAddress = Address.createEthereumAddress("0x..."); // ERC20 Address
const gatewayAddress = Address.createEthereumAddress(loomx.ethereum.getTransferGateway().address);
const amount = BigNumberUtils.toBigNumber(10**18);
const approveTx = await loomx.ethereum.approveERC20Async(assetAddress, gatewayAddress, amount);
await approveTx.wait();
const depositTx = await loomx.ethereum.depositERC20Async(assetAddress, 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 "@web3-guru/loom-x/dist";

const amount = BigNumberUtils.toBigNumber(10**18); // 1 ETH
const ethereumGateway = loomx.ethereum.getTransferGateway();
const myEthereumAddress = loomx.ethereum.address;
const assetAddress = Address.createEthereumAddress(Constants.ZERO_ADDRESS); // ERC20 Address

// Send tx to Loom Network
const tx1 = await loomx.loom.withdrawETHAsync(amount, ethereumGateway);
await tx1.wait();

// Listen to the withdrawal signature
const signature = await loomx.loom.listenToTokenWithdrawal(assetAddress, myEthereumAddress);

// Send tx to Ethereum Network
const tx2 = await loomx.ethereum.withdrawETHAsync(amount, signature);
await tx2.wait();

ERC20

import { BigNumberUtils } from "@web3-guru/loom-x/dist";

const assetAddress = Address.createEthereumAddress("0x..."); // ERC20 Address
const myEthereumAddress = loomx.ethereum.address;
const amount = BigNumberUtils.toBigNumber(10**18);

// Send tx to Loom Network
const tx1 = await loomx.loom.withdrawERC20Async(assetAddress, amount);
await tx1.wait();

// Listen to the withdrawal signature
const signature = await loomx.loom.listenToTokenWithdrawal(assetAddress, myEthereumAddress);

// Send tx to Ethereum Network
const tx2 = await loomx.ethereum.withdrawERC20Async(assetAddress, amount, signature);
await tx2.wait();

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

Handling Pending Withdrawal

If Loom.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

// Check if you have a pending receipt
const nonce = await loomx.ethereum.getWithdrawalNonceAsync();
if (nonce) {
    // Get pending withdrawal receipt with the nonce
    const receipt = await loomx.getPendingETHWithdrawalReceipt(nonce);
    // Withdraw pending ETH
    const tx = await ethereum.withdrawETHWithReceiptAsync(receipt);
    if (tx) {
        await tx.wait();
    }
}

ERC20

// Check if you have a pending receipt
const nonce = await loomx.ethereum.getWithdrawalNonceAsync();
if (nonce) {
    const assetAddress = Address.createEthereumAddress("0x..."); // ERC20 Address
    // Get pending withdrawal receipt with the nonce
    const receipt = await loomx.getPendingERC20WithdrawalReceipt(nonce);
    // Withdraw pending ERC20
    const tx = await ethereum.withdrawERC20WithReceiptAsync(assetAddress, receipt);
    if (tx) {
        await tx.wait();
    }
}

Author

👤 @web3.guru