Intermediate

 

Cross-Chain Value Transfer

Welcome to our tutorial on cross-chain value transfer on the Klaytn blockchain network. In Klaytn, cross-chain value transfer refers to the process of transferring value or assets between different blockchains. This allows for greater interoperability between different blockchain networks and opens up new possibilities for decentralized applications and services. In this tutorial, we will explore the process of cross-chain value transfer on the Klaytn network, including the necessary software and configuration steps, and best practices for implementing cross-chain value transfer in your applications. Whether you are a developer, researcher, or simply a blockchain enthusiast, this tutorial will provide valuable insights and hands-on experience in implementing cross-chain value transfer on the Klaytn network.

This section will explain how to enable ERC-20 value transfer between the 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 transfer.

ERC-20 Token Transfer (one-step)

Step 1: Add KLAY to the operator accounts.

Connect to the SCN and check the account addresses by executing subbridge.parentOperator and subbridge.childOperator.

$ kscn attach --datadir ~/data
> subbridge.childOperator
"0x10221f7f9355017cd0c11444e7eecb99621bacce"
> subbridge.parentOperator
"0x3ce216beeafc62d20547376396e89528e1d778ca"

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 .

$ kscn account import ~/homi-output/keys_test/testkey1
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {80119c31cdae67c42c8296929bb4f89b2a52cec4}
$ kscn attach --datadir ~/data
> personal.unlockAccount("80119c31cdae67c42c8296929bb4f89b2a52cec4")
Unlock account 80119c31cdae67c42c8296929bb4f89b2a52cec4
Passphrase:
True
> klay.sendTransaction({from:"80119c31cdae67c42c8296929bb4f89b2a52cec4", to:subbridge.childOperator, value: web3.toPeb(1000, "KLAY")})
"0x84caab84ebf0c4bb4ecf0a7849f1de3e479f1863a95f70c51047a7ca7bc64b33"

Check if the operator accounts have enough balance. You can query from the console of the SCN node where the sub bridge is installed as follows:

> klay.getBalance(subbridge.childOperator)
1e+21
> subbridge.childOperatorBalance
1e+21
> subbridge.parentOperatorBalance
1e+18

Step 2: Deploy Contracts

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 tests and bridge contracts are used to listen/handle value transfer requests.

$ git clone https://github.com/klaytn/servicechain-value-transfer-examples
$ cd servicechain-value-transfer-examples
$ npm install
$ cd erc20

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.

{
     "child" : {
         "url": "http://192.168.0.1:7551",
         "key": "0x66cb283353589a10866b58d388e9d956e5a9c873a8c78fa4411d460c19c494ea",
         "operator": "0x10221f7f9355017cd0c11444e7eecb99621bacce"
     },
     "parent" : {
         "url": "http://192.168.0.5:8551",
         "key": "0x26f4b5ac42ceabcfd3b23a991fdbfc792d10ce700a99582fdf9185a8f163b790",
         "operator": "0x3ce216beeafc62d20547376396e89528e1d778ca"
     }
 }

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 the bridge pair.

$ node erc20-deploy.js
------------------------- erc20-deploy START -------------------------
> info.bridge: 0xEa024d8101E112330f2d7B1a7e7932034E206721
> info.token: 0xbE641028610F628835C36F12bE62d54d74308D70
> info.bridge: 0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463
> info.token: 0x52F8Fa79Fa6D37b18b7AC8f9Ca835373f3C9270f
> subbridge.registerBridge("0xEa024d8101E112330f2d7B1a7e7932034E206721", "0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463")
> subbridge.subscribeBridge("0xEa024d8101E112330f2d7B1a7e7932034E206721", "0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463")
> subbridge.registerToken("0xEa024d8101E112330f2d7B1a7e7932034E206721", "0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463", "0xbE641028610F628835C36F12bE62d54d74308D70", "0x52F8Fa79Fa6D37b18b7AC8f9Ca835373f3C9270f")
------------------------- erc20-deploy END -------------------------

Step 3: Token transfer

Cross-Chain Value Transfer

This section will explain how to enable ERC-20 value transfer between the 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 transfer.

Prerequisites

ERC-20 Token Transfer (one-step)

Step 1: Add KLAY to the operator accounts.

Connect to the SCN and check the account addresses by executing subbridge.parentOperator and subbridge.childOperator.

$ kscn attach --datadir ~/data
> subbridge.childOperator
"0x10221f7f9355017cd0c11444e7eecb99621bacce"
> subbridge.parentOperator
"0x3ce216beeafc62d20547376396e89528e1d778ca"

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).

$ kscn account import ~/homi-output/keys_test/testkey1
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {80119c31cdae67c42c8296929bb4f89b2a52cec4}
$ kscn attach --datadir ~/data
> personal.unlockAccount("80119c31cdae67c42c8296929bb4f89b2a52cec4")
Unlock account 80119c31cdae67c42c8296929bb4f89b2a52cec4
Passphrase:
True
> klay.sendTransaction({from:"80119c31cdae67c42c8296929bb4f89b2a52cec4", to:subbridge.childOperator, value: web3.toPeb(1000, "KLAY")})
"0x84caab84ebf0c4bb4ecf0a7849f1de3e479f1863a95f70c51047a7ca7bc64b33"

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:

> klay.getBalance(subbridge.childOperator)1e+21
> subbridge.childOperatorBalance
1e+21
> subbridge.parentOperatorBalance
1e+18

Step 2: Deploy Contracts

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.

$ git clone https://github.com/klaytn/servicechain-value-transfer-examples
$ cd servicechain-value-transfer-examples
$ npm install
$ cd erc20

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.

{
"child" : {
         "url": "http://192.168.0.1:7551",
         "key": "0x66cb283353589a10866b58d388e9d956e5a9c873a8c78fa4411d460c19c494ea",
         "operator": "0x10221f7f9355017cd0c11444e7eecb99621bacce"
     },
     "parent" : {
         "url": "http://192.168.0.5:8551",
         "key": "0x26f4b5ac42ceabcfd3b23a991fdbfc792d10ce700a99582fdf9185a8f163b790",
         "operator": "0x3ce216beeafc62d20547376396e89528e1d778ca"
     }
 }

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.

$ node erc20-deploy.js
------------------------- erc20-deploy START -------------------------
> info.bridge: 0xEa024d8101E112330f2d7B1a7e7932034E206721
> info.token: 0xbE641028610F628835C36F12bE62d54d74308D70
> info.bridge: 0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463
> info.token: 0x52F8Fa79Fa6D37b18b7AC8f9Ca835373f3C9270f
> subbridge.registerBridge("0xEa024d8101E112330f2d7B1a7e7932034E206721", "0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463")
> subbridge.subscribeBridge("0xEa024d8101E112330f2d7B1a7e7932034E206721", "0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463")
> subbridge.registerToken("0xEa024d8101E112330f2d7B1a7e7932034E206721", "0xA5af6Ffe13b367626B5AdF827DdE7438E3Db4463", "0xbE641028610F628835C36F12bE62d54d74308D70", "0x52F8Fa79Fa6D37b18b7AC8f9Ca835373f3C9270f")
------------------------- erc20-deploy END -------------------------

Step 3: Token transfer

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).

$ node erc20-transfer-1step.js
------------------------- erc20-transfer-1step START -------------------------
alice balance: 0
requestValueTransfer..
alice balance: 100
------------------------- erc20-transfer-1step END -------------------------

If the result is alice balance: 100, then it has been executed successfully.

ERC-20 Token Transfer (two-step)

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.

$ node erc20-transfer-2step.js
> ------------------------- erc20-transfer-2step START -------------------------
> alice balance: 100
> requestValueTransfer..
> alice balance: 200
------------------------- erc20-transfer-2step END -------------------------

KIP-7 Token Transfer via ERC-20 Interface (two-step)

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.

$ node kip7-deploy.js
> ------------------------- kip7-deploy START -------------------------
> info.bridge: 0x04e929Cd2A08acd28a210369407D8Ca237Edd8FE
> info.token: 0xE0E2fC6C7d1eB069153E0c12a4C87B01586b39e7
> info.bridge: 0xEb502159A4B4E876B1cb423f250DCC0d276e01b6
> info.token: 0xd4f02Ca1d49674056A9ec78fbBDc9e1e97726A4F
> subbridge.registerBridge("0x04e929Cd2A08acd28a210369407D8Ca237Edd8FE", "0xEb502159A4B4E876B1cb423f250DCC0d276e01b6")
> subbridge.subscribeBridge("0x04e929Cd2A08acd28a210369407D8Ca237Edd8FE", "0xEb502159A4B4E876B1cb423f250DCC0d276e01b6")
> subbridge.registerToken("0x04e929Cd2A08acd28a210369407D8Ca237Edd8FE", "0xEb502159A4B4E876B1cb423f250DCC0d276e01b6", "0xE0E2fC6C7d1eB069153E0c12a4C87B01586b39e7", "0xd4f02Ca1d49674056A9ec78fbBDc9e1e97726A4F")
------------------------- kip7-deploy END -------------------------

The below command is an example of sending KIP-7 tokens using the ERC-20 interface with requestERC20Transfer().

$ node kip7-transfer-2step-erc20-interface.js
> ------------------------- kip7-transfer-2step-erc20-interface START -------------------------
> alice balance: 0
> requestValueTransfer..
> alice balance: 100
> ------------------------- kip7-transfer-2step-erc20-interface END -------------------------

Native Support for KIP-7 and KIP-17 (To Be Implemented)

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.

Value Transfer for ERC-721, KIP-17, and KLAY

The workflow for ERC-721, KIP-17, and KLAY is the same as above. erc721, kip17, and klay directories contain corresponding example source codes.

With this, you complete this workshop successfully!!