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

aelf-sdk-cross-chain

v1.0.14

Published

sdk for cross chain

Downloads

838

Readme

aelf-sdk-cross-chain.js

Introduction

A JS sdk aims to provide cross chain tool.

Please pay attention that some function of aelf-sdk.js is the input params of the aelf-sdk-cross-chain.js.

If we want use aelf-sdk-cross-chain.js, we need import/require aelf-sdk.js at first.

Installation

Script

<!-- minified version with UMD module -->
<script src="https://unpkg.com/aelf-sdk-cross-chain@lastest/dist/aelf-cross-chain.umd.js"></script>

Npm

npm install aelf-sdk-cross-chain

Yarn

yarn add aelf-sdk-cross-chain

Library files

In our dist directory, we support different packages for different platforms, such as Node and Browser.

packages | usage ---|--- dist/aelf-cross-chain.umd.js | built for browser & node, add some node modules by webpack

You can choose any packages based on what you need, for examples:

if you are new to FrontEnd, you can use window.AElfCrossChain by add a script tag in your html files.

<!-- minified version with UMD module -->
<script src="https://unpkg.com/aelf-sdk-cross-chain@lastest/dist/aelf-cross-chain.umd.js"></script>
<script>
  // window.AElfCrossChain
</script>

if you want to use a bundle system such as webpack or rollup, and build your applications for Node.js and Browsers, just import the specified version of package files.

For browser usage and use UMD

Webpack:

module.exports = {
  // ...
  resolve: {
    alias: {
      'aelf-sdk-cross-chain': 'aelf-sdk-cross-chain/dist/aelf-cross-chain.umd.js'
    }
  }
}

Rollup:

const alias = require('rollup-plugin-alias');

rollup({
  // ...
  plugins: [
    alias({
      'aelf-sdk-cross-chain': require.resolve('aelf-sdk-cross-chain/dist/aelf-cross-chain.umd.js')
    })
  ]
})

For Node.js usage and use commonjs module system

Webpack:

module.exports = {
  // ...
  resolve: {
    alias: {
      'aelf-sdk-cross-chain': 'aelf-sdk-cross-chain/dist/aelf-cross-chain.umd.js'
    }
  }
}

Rollup:

const alias = require('rollup-plugin-alias');

rollup({
  // ...
  plugins: [
    alias({
      'aelf-sdk-cross-chain': require.resolve('aelf-sdk-cross-chain/dist/aelf-cross-chain.umd.js')
    })
  ]
})

Basic Demo

/**
 * @author huangzonghze
 * @description cross chain demo
 */

/* eslint-env node */
const AElf = require('aelf-sdk');
const Wallet = AElf.wallet;

const {
  CrossChain
} = require('aelf-sdk-cross-chain');

// address: 2hxkDg6Pd2d4yU1A16PTZVMMrEDYEPR8oQojMDwWdax5LsBaxX
const defaultPrivateKey = 'bdb3b39ef4cd18c2697a920eb6d9e8c3cf1a930570beb37d04fb52400092c42b';
const receiveAddress = '2hxkDg6Pd2d4yU1A16PTZVMMrEDYEPR8oQojMDwWdax5LsBaxX';
const wallet = Wallet.getWalletByPrivateKey(defaultPrivateKey);

const sendInstance = new AElf(new AElf.providers.HttpProvider('http://127.0.0.1:8000')); // a chain
const receiveInstance = new AElf(new AElf.providers.HttpProvider('http://127.0.0.1:8001')); // another chain

async function init() {
  const crossChainInstance = new CrossChain({
    AElfUtils: AElf.utils,
    sendInstance,
    receiveInstance,
    wallet,
    mainChainId: 9992731,
    issueChainId: 9992731
  });
  // await crossChainInstance.init();
  // Can accelerate the process.
  await crossChainInstance.init({
    contractAddresses: {
      tokenContractAddressSend: '7RzVGiuVWkvL4VfVHdZfQF2Tri3sgLe9U991bohHFfSRZXuGX',
      crossChainContractAddressSend: '2snHc8AMh9QMbCAa7XXmdZZVM5EBZUUPDdLjemwUJkBnL6k8z9',
      tokenContractAddressReceive: 'JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE',
      crossChainContractAddressReceive: '2SQ9LeGZYSWmfJcYuQkDQxgd3HzwjamAaaL4Tge2eFSXw2cseq'
    },
    chainIds: {
      chainIdSend: 'tDVV',
      chainIdReceive: 'AELF'
    }
  });

  const {
    crossTransferTxId
  } = await crossChainInstance.send({
    to: receiveAddress,
    symbol: 'ELF',
    amount: 1,
    memo: 'Hello World'
  });

  // const registerMsg = await crossChainInstance.tokenCrossChainInstance.checkRegister();
  // const result = await crossChainInstance.isChainReadyToReceive({
  //   crossTransferTxId: '560f44f346b23eb455fb43b37f8ae1ef28636c5a1fc3a9aed3c55f475c82d0aa'
  // });

  const receiveInfo = await crossChainInstance.receive({
    // crossTransferTxId: 'cad78d4697f23ec34d03956ab17c0c8443d97277f7590d2b178d714d4a9682d3'
    crossTransferTxId
  });

  return receiveInfo;
}

init().then(result => {
  console.log('init result: ', result);
}).catch(error => {
  console.log('init error: ', error);
});

Use tokenCrossChainBasic

crossChain of aelf-sdk-cross-chain is a wrap of tokenCrossChainBasic.

tokenCrossChainBasic only provide basic methods init, send and recevie.

It means we need write our own logic to ensure whether the chains are ready to recevie the cross chain transaction.

crossChain provide a simple logic to solve this situation.

// Demo
const tokenCrossChainInstance = new TokenCrossChainBasic({
  AElfUtils: AElf.Utils,
  sendInstance: sendInstance,
  receiveInstance: receiveInstance
});
const contractsAndChainIds = await tokenCrossChainInstance.init({
  wallet: wallet
});

const params = {
  to: receiveAddress,
  symbol: 'ELF',
  amount: 1,
  memo: 'Hello World'
};
const sendInfo = await tokenCrossChainInstance.send(contractsAndChainIds, params);

const receiveInfo = await tokenCrossChainInstance.receive({
  crossTransferTxId // sendInfo.crossTransferTxId
});

// const registerMsg = await crossChainInstance.tokenCrossChainInstance.checkRegister();
const registerMsg = await tokenCrossChainInstance.checkRegister();

And you can get more information from the source code.