Skip to main content

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));
  • Deposit to Chromia EVM bridge

    Use the depositToEvmBridgeContract method, specifying the number of tokens to bridge:

    const contractTransactionResponse = await bcl.depositToEvmBridgeContract(BigInt(100));
  • 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);
    note

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