The TokenBridgeHomeSide API is used to bridge tokens into the layer 2 network in which the Card Protocol runs. The TokenBridgeHomeSide API can be obtained from getSDK() with a Web3 instance that is configured to operate on a layer 2 network (like Gnosis Chain or Sokol).

Example

import { getSDK } from "@cardstack/cardpay-sdk";
let web3 = new Web3(myProvider); // Layer 2 web3 instance
let tokenBridge = await getSDK('TokenBridgeHomeSide', web3);

Hierarchy

  • TokenBridgeHomeSideClass

Implements

Constructors

Properties

layer2Web3: default
layer2Signer?: Signer

Methods

  • Returns

    the minimum and maximum withdrawal limits as a string in token units (we assume 18 decimals, i.e. wei) for bridging a token to layer 1. This method is invoked with the layer 2 CPXD token address of the CPXD token being withdrawn.

    Example

    let { min, max } = await tokenBridge.getWithdrawalLimits(daiTokenAddress);
    

    Parameters

    • tokenAddress: string

    Returns Promise<{
        min: string;
        max: string;
    }>

  • This call will invoke the token bridge contract to relay tokens from a layer 2 safe into the account specified in layer 1.

    Example

    let result = await tokenBridge.relayTokens(
    layer2SafeAddress,
    tokenAddress,
    layer1RecipientAddress,
    amountInWei
    );

    Parameters

    • txnHash: string

    Returns Promise<SuccessfulTransactionReceipt>

  • Parameters

    • safeAddress: string
    • tokenAddress: string
    • recipientAddress: string
    • amount: string
    • Optional txnOptions: TransactionOptions
    • Optional contractOptions: ContractOptions

    Returns Promise<SuccessfulTransactionReceipt>

  • This call waits for the token bridge validators to perform their necessary signatures on the token bridge request from layer 2 to layer 1. After the bridge validators have signed the bridging request, this call will return a messageId, encodedData, and signatures for the bridging request. These items can then be used to claim the bridged tokens in layer 1.

    This method is invoked with:

    • The block height of layer 2 before the relayTokens call was initiated on the home side of the bridge. Get it with await layer2Web3.eth.getBlockNumber()
    • The layer 2 transaction hash for the bridging transaction (the result of TokenBridgeHomeSide.relayTokens).

    Example

    let {
    messageId,
    encodedData,
    signatures
    } = await tokenBridge.waitForBridgingValidation(fromBlock, txnHash);

    Parameters

    • fromBlock: string
    • bridgingTxnHash: string

    Returns Promise<BridgeValidationResult>

  • This call will listen for a TokensBridgedToSafe event emitted by the TokenBridge home contract that has a recipient matching the specified address. The starting layer 2 block height should be captured before the call to relayTokens is made to begin bridging. It is used to focus the search and avoid matching on a previous bridging for this user.

    Returns

    promise that includes a web3 transaction receipt for the layer 2 transaction, from which you can obtain the transaction hash, ethereum events, and other details about the transaction https://web3js.readthedocs.io/en/v1.3.4/web3-eth-contract.html#id37.

    Remarks

    We use the subgraph to act as our indicator that bridging has completed, as this is the same mechanism that is populating the card wallet's displayed token balances

    Example


    let txnReceipt = await tokenBridge.waitForBridgingToLayer2Completed(
    recipientAddress
    startingBlockHeight,
    );

    Parameters

    • recipientAddress: string

      The address of the layer 2 account that will own the resulting safe (passed as receiver to relayTokens call)

    • fromBlock: string

      The block height of layer 2 before the relayTokens call was initiated on the foreign side of the bridge. Get it with await layer2Web3.eth.getBlockNumber()

    Returns Promise<SuccessfulTransactionReceipt>

Generated using TypeDoc