Cross-Chain Value Transfer
Last updated
Last updated
This section will explain how to enable ERC-20 value transfer between Baobab network and your ServiceChain with the provided test code. You will add KLAY to the operator account and deploy bridge and ERC-20 contracts. Then you will register the contract address on SCN. And you will test an ERC-20 value transferring.
We assume that you installed the ServiceChain, and connected the ServiceChain to the Baobab EN by the instructions in Connecting to Baobab.
Clone the repository servicechain-value-transfer-examples.
Install Node.js
(v14) and npm
(How to install)
This example leverages two packages, axios and caver-js, which both support v14.
Connect to the SCN and check the account addresses by executing subbridge.parentOperator
and subbridge.childOperator
.
subbridge.parentOperator
and subbridge.childOperator
must have enough KLAY to send a transaction. Note that subbridge.parentOperator
is an account on the Baobab network, and subbridge.childOperator
is an account on the ServiceChain network. Create a test account on a Baobab Wallet and get test KLAY from the faucet. Then send some KLAY to the parentOperator
. childOperator
has to get KLAY from the test account generated by homi
(Refer to EN Setup and SCN Connection Guide).
Check if the operator accounts have enough balance. You can query from the console of the SCN node where the subbridge is installed as follows:
Connect to the SCN and prepare the node environment for contract deployment. Clone the repository servicechain-value-transfer-examples.
In this step, we would deploy both the bridge contract and token contract in the parent as well as the child chain. Token contracts are for mint/transfer test and bridge contracts are used to listen/handle value transfer requests.
On a text editor, edit the bridge_info.json
as below.
Replace url
in the child
section (SCN node on ServiceChain network) with your SCN node IP and the proper port number from RPC_PORT
in kscnd.conf
.
Replace child.key
with testkey1
that was generated by homi
.
Set child.operator
to the subbridge.childOperator
address that we examined in the previous step.
Replace url
in the parent
section (EN node on Baobab network) with your EN node IP and the proper port number from RPC_PORT
in kend.conf
.
Replace parent.key
with the private key of the test account created from Baobab Wallet in the previous step.
Set parent.operator
as the subbridge.parentOperator
of the previous step.
Perform the token deployment by running the command node erc20-deploy.js
. This script deploys both the bridge contract and the token contract, and it outputs API usage to initialize bridge pair.
Perform token transfer with the command node erc20-transfer-1step.js
. This one-step token transfer requires modification of an ERC-20 token implementation. If you don't want to modify the token contract or you have a token contract that is already deployed, please refer to ERC-20 Token Transfer (two-step).
If the result is alice balance: 100
, then it has been executed successfully.
Run erc20-transfer-2step.js for the two-step transfer example. With this two-step token transfer example, unmodified ERC-20 token contracts can be used. The two-step transfer consists of two function calls: (1) approve the bridge contract first, and then (2) call the contract function requestERC20Transfer()
. We do not deploy contracts in this section since we already deployed both bridge and token contracts. You must deploy first if you didn't deploy them. You can deploy the contract using node erc20-deploy.js
.
KIP-7 is a token standard compatible with ERC-20. We can call requestERC20Transfer()
function to a KIP-7 token contract to transfer KIP-7 tokens between a parent chain and a child chain. In the case of sending KIP-7 tokens via the ERC-20 interface, we call the approve()
function to allow the bridge to send the tokens on behalf of the transaction sender. Then call the requestERC20Transfer()
function. The below command deploys the bridge contract and a KIP-7 contract.
The below command is an example of sending KIP-7 tokens using the ERC-20 interface with requestERC20Transfer()
.
Please refer service-chain-value-transfer-example for the other cases.
Currently, the bridge contract provided by the Klaytn team supports only requestERC20Transfer()
and requestERC721Transfer()
for token transfer. The corresponding request functions for KIP-7 and KIP-17 will be supported soon. Before the implementation is done, as you can see above, you can transfer KIP-7 tokens using ERC-20 interfaces.
The workflow for ERC-721, KIP-17, and KLAY is the same as above. erc721
, kip17
, and klay
directories contain corresponding example source code.