Interact with ThunderCore Bridge

Since the main function of the bridge is to transfer assets across different networks, the way to interact with ThunderCore bridge is quite straightforward.

The only thing that you have to do is to transfer supported tokens to the bridge address directly.

Supported tokens can be found in the following documents:

Transfer Native Token to ThunderCore

In the ThunderCore Bridge, we currently support transfering ETH from Ethereum, BNB from BSC, and HT from HECO to ThunderCore. Here's an example how it works:

let transaction = {
  to: FOREIGN_BRIDGE_ADDRESS,
  data: '',
  value: VALUE,
  gasPrice: GAS_PRICE,
  gas: GAS_LIMIT,
};

const result = await web3.eth.sendTransaction(transaction);
console.log(`transactionHash: ${result.transactionHash}`);

If you need to transfer tokens to a custom recipient in ThunderCore, replace the data part in transaction like as shown below

const ERC20 = new web3.eth.Contract(ERC20ABI, FOREIGN_TOKEN_ADDRESS)
const data = await ERC20.methods.transfer(FOREIGN_BRIDGE_ADDRESS, VALUE).encodeABI({from: SENDER_ADDRESS});

// CUSTOM_RECIPIENT should start with 0x
data += `000000000000000000000000${CUSTOM_RECIPIENT.slice(2)}`;

Transfer ERC20 to ThunderCore

To transfer ERC20 tokens to ThunderCore, try the example shown below

const ERC20 = new web3.eth.Contract(ERC20ABI, FOREIGN_TOKEN_ADDRESS);
const data = await ERC20.methods.transfer(FOREIGN_BRIDGE_ADDRESS, VALUE).encodeABI({from: SENDER_ADDRESS})

const transaction = {
    gasPrice: GAS_PRICE,
    data,
    gas: GAS_LIMIT,
    from: SENDER_ADDRESS,
    to: FOREIGN_TOKEN_ADDRESS,
    value: '0',
};

await web3.eth.sendTransaction(transaction);

The corresponding tokens will be transfered to SENDER_ADDRESS in a while on foreign chain.

If you need to transfer tokens to a custom recipient in ThunderCore, add the recipient behind the data part before you send the transaction.

// CUSTOM_RECIPIENT should start with 0x
data += `000000000000000000000000${CUSTOM_RECIPIENT.slice(2)}`;

Transfer TT20 to Foreign

On ThunderCore, most of the bridge token are ERC677, we use transferAndCall function to call onTokenTransfer in bridge contract. Here's an example:

const ERC677 = new web3.eth.Contract(ERC677ABI, HOME_TOKEN_ADDRESS);
let data = await ERC677.methods.transferAndCall(HOME_BRIDGE_ADDRESS, VALUE, "0x").encodeABI({from: SENDER_ADDRESS})

// if you want to send the token to custom address in foreign chain, CUSTOM_RECIPIENT should start with 0x
if (CUSTOM_RECIPIENT) {
    data += `000000000000000000000000${CUSTOM_RECIPIENT.slice(2)}`;
}

let transaction = {
  to: HOME_TOKEN_ADDRESS,
  data,
  value: "0",
  gasPrice: GAS_PRICE,
  gas: GAS_LIMIT,
};

await web3.eth.sendTransaction(transaction);

and part of ERC677ABI is

[
  {
    "constant": false,
    "inputs": [
      {
        "name": "_to",
        "type": "address"
      },
      {
        "name": "_value",
        "type": "uint256"
      },
      {
        "name": "_data",
        "type": "bytes"
      }
    ],
    "name": "transferAndCall",
    "outputs": [
      {
        "name": "",
        "type": "bool"
      }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

Last updated