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
<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.cjs.js | built for node, remove node-built modules such as crypto. |
dist/aelf-cross-chain.umd.js | built for browser, 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.
<script src="https://unpkg.com/aelf-sdk-cross-chain@lastest/dist/aelf-cross-chain.umd.js"></script>
<script>
</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.cjs.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.cjs.js')
})
]
})
Basic Demo
const AElf = require('aelf-sdk');
const Wallet = AElf.wallet;
const {
CrossChain
} = require('aelf-sdk-cross-chain');
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'));
const receiveInstance = new AElf(new AElf.providers.HttpProvider('http://127.0.0.1:8001'));
async function init() {
const crossChainInstance = new CrossChain({
AElfUtils: AElf.utils,
sendInstance,
receiveInstance,
wallet,
});
await crossChainInstance.init();
const {
crossTransferTxId
} = await crossChainInstance.send({
to: receiveAddress,
symbol: 'ELF',
amount: 1,
memo: 'Hello World'
});
const receiveInfo = await crossChainInstance.receive({
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.
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
});
And you can get more information from the source code.