This guide outlines the process of utilizing Wischain's bridge for ERC20 tokens that require custom functionality via the Custom Gateway.
Do You Need Custom Logic?
In many cases, you may not need to use the Custom Gateway to add additional features to your token or its bridging logic. For further details, please refer to the article on ETH and ERC20 Token Bridge.
Step 1: Deploy a Token on Sepolia
To begin, you'll need a token to bridge. There is no specific ERC20 implementation required for compatibility with Layer 2 (L2). If you already possess a token, you can skip this step. However, if you're creating a new token, you can deploy a simple ERC20 token using the following contract, which mints 1 million tokens to the deployer upon launch:
Step 2: Deploy the Counterpart Token on Wischain Sepolia Testnet
Next, create a counterpart token on Wischain to represent the original token on Sepolia. This token can implement custom logic that mirrors the L1 token or introduce additional features.
For this to function correctly:
The token must implement the IWischainStandardERC20 interface to ensure compatibility with the bridge.
The contract should provide the gateway address and counterpart token addresses (the L1 token just deployed) through the gateway() and counterpart() functions. Additionally, it must allow the L2 gateway to call the token's mint() and burn() functions during deposits and withdrawals.
Here’s an example of a token that is compatible with the bridge:
To incorporate your token into the L2CustomERC20Gateway contract on Wischain and the L1CustomERC20Gateway contract on Sepolia, you need to contact the Wischain team. Additionally, follow the guidelines provided in the token lists repository to list your token on the Wischain official bridge frontend.
Step 4: Deposit Tokens
Once your token receives approval from the Wischain team, you can deposit it from Layer 1. First, approve the L1CustomGateway contract address on Sepolia (e.g., 0x......). Then, deposit tokens by calling the depositERC20 function from the L1CustomGateway contract. This can be accomplished using the Wischain bridge UI, Etherscan Sepolia, or a smart contract.
Step 5: Withdraw Tokens
To send tokens back from L2 to L1, you will follow similar steps. Begin by approving the L2CustomGateway address (e.g., 0x.....) and then withdraw the tokens by calling the withdrawERC20 function from the L2CustomGateway contract.
Alternative Approach: Launching and Setting Up a Custom L1 Gateway Contract
While adding your token to the Wischain official bridge is the recommended method for bridging tokens to and from Wischain, you can also opt for launching a custom gateway. This method will require deploying an L1CustomERC20Gateway contract on Layer 1 and an L2CustomERC20Gateway on Layer 2.
Launch an L1 Custom Gateway
To begin, deploy the following contract on Sepolia:
After deploying the contracts, call the following functions to initialize them and bind them to the corresponding tokens and the gateway on the opposite side of the bridge.
Call the initialize function on the MyL1Gateway contract with the following parameters:
_counterpart: The address of MyL2Gateway deployed on Wischain.
_router: Set this to 0x........, the L1GatewayRouter contract address on Sepolia.
_messenger: Set this to 0x......, the L1WischainMessenger contract address on Sepolia.
A custom gateway can host multiple token bridges. In this example, we will enable bridging only between L1Token and L2Token by calling the updateTokenMapping function on the MyL1Gateway contract with the following parameters:
_l1Token: The address of the L1Token contract launched on Sepolia.
_l2Token: The address of the L2Token contract launched on Wischain.
Setup Your Gateway Contract on Wischain
Now, switch to the Wischain network and initialize MyL2Gateway, following similar steps:
Call the initialize function from MyL2Gateway:
_counterpart: The address of MyL1Gateway deployed on Sepolia.
_router: Set this to 0x........., the L2GatewayRouter contract address on Wischain.
_messenger: Set this to 0x......., the L2WischainMessenger contract address on Wischain.
Call updateTokenMapping on the MyL2Gateway contract:
_l2Token: The address of the L2Token contract launched on Wischain.
_l1Token: The address of the L1Token contract launched on Sepolia.
Bridging Tokens
You can now call depositERC20 from MyL1Gateway and withdrawERC20 from MyL2Gateway, just as you would with the official Wischain bridge. This allows for seamless token transfers between Layer 1 and Layer 2 within the Wischain ecosystem.