Skip to main content

BridgeERC20

The BridgeERC20 class facilitates the bridging of fungible tokens across different chains.

To bridge any holographic asset to another network, a three-step process is followed:

  1. Bridge Out - Users initiate a bridge out request to the HolographBridge contract on the source chain.
  2. Message Layer - The message layer is responsible for transmitting the message from the source chain to the destination chain.
  3. Bridge In - Once the message layer successfully delivers the message, the HolographBridge contract on the destination chain finalizes the bridging process.

This class provides the bridgeOut function, which initiates the entire bridging process.

1. Constructor Parameters

bridgeERC20Input

  • Type:
    BridgeERC20Inputrequired

The input object containing all the information to bridge the token.

type BridgeERC20Input = {
sourceChainId: number
destinationChainId: number
contractAddress: `0x${string}`
amount: bigint
wallet: HolographWallet
from?: `0x${string}`
to?: `0x${string}`
}
  • Validation:
    • sourceChainId:
      numberrequired
      - must be the chain ID where the token amount is located.
    • destinationChainId:
      numberrequired
      - must be a chain ID of a network to which the token contract is already deployed.
    • contractAddress:
      0x${string}required
      - must be the token contract address.
    • amount:
      bigintrequired
      - must be the amount of tokens you want to bridge.
    • wallet:
      HolographWalletrequired
      - must be the wallet that is going to execute the transaction.
    • from:
      0x${string}default: wallet.account.address
      - must be the tokens owner's address.
    • to:
      0x${string}default: from
      - must be the tokens owner's address or another address to transfer ownership.

gasSettings

  • Type:
    GasSettings

This is an optional input object that allows users to override the source gasPrice and gasLimit in Wei, providing greater control over these values.

type GasSettings = {
gasPrice?: bigint
gasLimit?: bigint
}

Example:

src/holograph/bridge-erc20.ts
import { BridgeERC20 } from "@holographxyz/sdk"
import { networks } from "@holographxyz/networks"

import { wallet } from "./config"

const bridgeERC20 = new BridgeERC20(
{
sourceChainId: networks.polygon.chain,
destinationChainId: networks.ethereum.chain,
contractAddress: "0x1234567890abcdef1234567890abcdef12345678", // ERC20 token contract address.
amount: BigInt(100000),
wallet,
},
{
gasPrice: BigInt(132000000000),
gasLimit: BigInt(650000),
}
)

2. Properties

contractAddress

  • Type:
    0x${string}

The token contract address.

amount

  • Type:
    bigint

The number of tokens to be bridged.

from

  • Type:
    0x${string}

The tokens owner's address.

to

  • Type:
    0x${string}

The token owner's address or another address to transfer ownership.

destinationChainId

  • Type:
    number

The chain ID of the network to which the token will be bridged.

sourceChainId

  • Type:
    number

The chain ID where the token is located.


3. Methods

createInitCode()

This is a static class method utilized by the getInitCode and can also be envoked without instantiating the class. It returns the bridge out payload.

Params:

  • from:
    0x${string}required
    - The address of the tokens owner on the source chain.
  • to:
    0x${string}required
    - The address of the wallet to which the tokens will be transferred on the destination chain.
  • amount:
    bigintrequired
    - The number of tokens to be bridged.

Returns:

Promise<0x${string}>

Example:

import { BridgeERC20 } from "@holographxyz/sdk"

const from = "0xB1113E02F232d00bCf060c2c70A9b60d09309501"
const to = "0xB1113E02F232d00bCf060c2c70A9b60d09309501"

const bridgeOutPayload = await BridgeERC20.createInitCode(from, to, amount)

getInitCode()

Retrieve the initialization payload required for the bridgeOut function. This hexadecimal string encodes from, to, and amount parameters.

Returns:

Promise<0x${string}>

Example:

const bridgeOutPayload = await bridgeERC20.getInitCode()

bridgeOut()

Makes the bridgeOut request.

Params:

  • walletOverride:
    HolographWallet
    - The option overrides the configured wallet.
  • destinationChainId:
    number
    - The option overrides the configured destination chain.

Returns:

Promise<Transaction>

Example:

import { networks } from "@holographxyz/networks"

import { anotherWallet } from "./config"

const tx = await bridgeERC20.bridgeOut()

// With overrides:
const anotherTx = await bridgeERC20.bridgeOut(anotherWallet, networks.arbitrumOne.chain)