Example usage: Bridge from EVM to Chromia and vice versa
Bridge from EVM to Chromia
-
Approve token spending
Ensure the user approves token spending by the token bridge:
const approvalResponse = await bcl.approveDepositAmount(BigInt(10));
-
Bridge from EVM to Chromia
To bridge tokens from EVM to Chromia, use the
depositToEvmBridgeContract
method, specifying the number of tokens to bridge:const contractTransactionResponse = await bcl.depositToEvmBridgeContract(BigInt(100));
If the user has created an FT4 account and linked it to their EVM address, funds for foreign tokens will be minted directly to that account. For native tokens, funds will be transferred from the bridge account to the user's account.
If the user hasn't created an account, or has created one but hasn't linked it to their EVM address, funds for foreign tokens will be minted to the pool account instead. For native tokens, funds will be transferred from the bridge account to the pool account. In this case, the user must create and link an FT4 account. Once this is done, the funds will be transferred from the pool account to the user's account.
-
Link EVM account
After a deposit, link the EVM account with the corresponding FT4 account created during the deposit process. Provide the
evmKeyStore
you created earlier:const accountLinkingResponse = await bcl.linkEvmEoaAccount(evmKeyStore);
noteIf the EVM account is linked, it will be returned to the
accountLinkingResponse
. An EVM account can have multiple FT4 accounts linked to it.
Bridge from Chromia to EVM
-
Bridge from Chromia
Call the
bridgeFromChromia
method with the amount and the asset ID:// Network ID does not need to be provided as it will be fetched from the provider
const transactionResponse = await bcl.bridgeFromChromia(BigInt(10), Buffer.from("YOUR_ASSET_ID", "hex"));
Request withdrawal from EVM bridge
-
Create a pending withdrawal request
This needs to be accepted by the user to start the withdrawal process using the
requestEvmWithdraw
method:const erc20WithdrawalInfo = await bcl.getErc20WithdrawalByTransactionRid(
transactionResponse.receipt.transactionRid,
opIndex
);
// Get event proof for withdrawal
const eventProof = await bcl.getWithdrawRequestEventProof(erc20WithdrawalInfo.event_hash);
// Request withdrawal
const requestedWithdraw = await bcl.requestEvmWithdraw(eventProof); -
Check withdrawal status
Depending on the bridge contract configuration, the user must wait a certain number of blocks on EVM to complete their withdrawal. This can be done using the
getPendingWithdrawFromProof
method:const { block_number } = await getPendingWithdrawFromProof(eventProof);
Once the
block_number
has been reached on the target EVM chain, the user can withdraw their tokens:const withdrawal = await bcl.evmWithdraw(eventProof.leaf as Buffer);
Additional methods
getErc20Deposits(filter?: DepositFilter, pageSize?: number, pageCursor?: string)
: Returns all deposits specified by the filter.getErc20Withdrawals(filter?: WithdrawFilter, pageSize?: number, pageCursor?: string)
: Returns all withdrawals from the EVM bridge specified by the filter.setBlockchainRid(blockchainRid: Buffer)
: Sets the blockchain RID.