Bridging $MNT using Mantle SDK
Deposit and withdraw $MNT using the SDK
Mantle v2 Tectonic has been released, please move to the new documentation!
This tutorial demonstrates how to use the Mantle SDK to deposit and withdraw $MNT tokens between Mantle and Ethereum.
Set up local environment
Make sure you have the following tools installed in your local environment.
Let's start by fetching the example JS scripts that we’ll work with and use to make SDK invocations from the Mantle Github. Clone the repository containing the sample scripts by executing the following command in your project directory.
Next, we can use yarn
to download the SDK along with all the necessary dependencies, as shown below. All the dependencies are defined in the yarn.lock
file, so we can just run yarn
in the ./cross-dom-bridge-mnt
directory.
We'll need a .env
file from where we can add and modify wallet and network settings. The main directory contains two .env
files, where .env.local
specifies the configuration for a local environment, while .env.testnet
specifies the configuration to connect to testnet.
All the necessary contracts addresses are already included in the respective .env
files, so you can specify your preferred L1 RPC endpoint and your wallet private key to start sending transactions.
Let's take a look at the main script.
Analyzing and modifying the sample script
The index.js
script containing the code we need is located in the ./mantle-tutorial/cross-dom-bridge-eth
directory. By default, it is configured to run on a local test environment. You can run L1 and L2 instances on your system and start deploying contracts to test your applications. You can make a copy of the index.js
file before we start modifying it if you want to try that out.
Check out the tutorial here that demonstrates the same bridging functionality on a private network.
Importing necessary libraries
This code does not need to be changed. We import three libraries, and the .env
configuration file we created earlier.
dotenv
: The.env
file containing wallet and network configurationethers
: The Ethers.js library comes handy with wallet and contract operations@mantlenetwork/sdk
: Mantle SDK instancefs
: File system module to read the contract ABI from a JSON file
Generating contract bytecode from ABI
We don't need to modify this either. The contents of the JSON file containing the contract ABI are stored in TestERC20.json
which we will be using later.
Network configuration and wallet setup
We fetch the specified network and wallet configurations from the .env
file, and create wallet objects by passing the private key and RPC addresses as parameters for L1 and L2.
CrossChainMessenger object
The CrossChainMessenger
object calls the cross chain messenger contracts on L1 and L2 to transfer assets. Here we instantiate the object with chain IDs, wallet objects, and contract objects.
Reporting balances
The reportBalances
function fetches L1 and L2 wallet balances and prints them out. We'll use this method to keep track balance change after deposit and withdraw operations.
Deposit function
The depositMNT
function deposits 1 $MNT token to L2 via the Mantle bridge. The deposit transaction is sent using the depositERC20
method, which is picked up by an off-chain service and relayed to L2. The asynchronous function prints out the transaction hash and waits for the message to get relayed. Finally, we display the updated $MNT balance on L1 and L2.
Withdraw function
Similarly, the withdrawMNT
function withdraws 1 $MNT token from L2 via the Mantle bridge. The function prints out the transaction hash. The transaction then goes into a challenge period. Once it is ready for relay, it is picked up by an off-chain service to be relayed to L1. Finally, we display the updated $MNT balance on L1 and L2.
Invoking deposit and withdraw functions
We write a main()
where we call the functions to perform configuration, deposit, and withdraw operations.
Running the script
Once the configuration is ready, you can run the script using the yarn testnet
command. The script will automatically select the testnet configuration to perform both deposit and withdraw operations in the index.js
script. If you want to run the script locally, you can run yarn local
.
Conclusion
You can use this code to test out the token bridging mechanism via SDK on Mantle testnet and start integrating it to your applications.
Last updated