Viewing Transactions between Layers
If you want to test the functionality of the Mantle SDK, we recommend using the testnet environment instead of the mainnet environment. The challenge period will be much shorter in the testnet (~40 minutes) than in the mainnet (7 days).
In this tutorial, we'll be going over how to use the Mantle SDK to view transactions passed between layer 2 (L2) and layer 1 (L1). This function is particularly useful for dApps that make contract calls between Mantle Network and Ethereum.
Prerequisites
Before you begin, ensure that your computer has the following installed:
Setup
-
Clone the tutorial repository:
git clone https://github.com/mantlenetworkio/mantle-tutorial.git
cd mantle-tutorial/sdk-view-tx -
Install necessary dependencies:
yarn
Running the Sample Code
The sample code is provided in index.js.
Execution Environment
For testing in the Mantle third party rpc network, configure the missing or changing environment variables in the file .env.testnet.tmp. Then use the following command:
yarn testnet
Understanding the Code
Libraries Initialization
In this tutorial, we initialize the following libraries:
-
ethers: A JavaScript library for interacting with the Ethereum blockchain. It provides an easy-to-use interface for tasks like creating wallets, sending transactions, and interacting with smart contracts. -
mantleSDK: The Mantleio SDK, which facilitates cross-chain transactions between L1 and L2 blockchains. It abstracts away complexities, making it easier to perform operations like depositing and withdrawing assets.
const ethers = require('ethers');
const mantleSDK = require('@mantleio/sdk');
Configuration Parameters
The code defines some configuration parameters:
// Global variable because we need them almost everywhere
let crossChainMessenger;
const key = process.env.PRIV_KEY;
const l1RpcProvider = new ethers.providers.JsonRpcProvider(process.env.L1_RPC);
const l2RpcProvider = new ethers.providers.JsonRpcProvider(process.env.L2_RPC);
const l1Wallet = new ethers.Wallet(key, l1RpcProvider);
const l2Wallet = new ethers.Wallet(key, l2RpcProvider);
key: The private key retrieved from the environment variables.l1RpcProviderandl2RpcProvider: Environment variables representing the RPC providers for the L1 and L2 blockchains.l1Walletandl2Wallet: Wallets for the L1 and L2, respectively.
Setup and Initialization
The setup function initializes parameters needed for transfers.
Firstly, we need to create the CrossChainMessenger object that we use to query the transactions.
const setup = async () => {
crossChainMessenger = new mantleSDK.CrossChainMessenger({
l1ChainId: process.env.L1_CHAINID,
l2ChainId: process.env.L2_CHAINID,
l1SignerOrProvider: l1Wallet,
l2SignerOrProvider: l2Wallet,
});
};
Get Token Symbol
To get the token symbol, we need to get the symbol from the L1 contract.
// Only the part of the ABI we need to get the symbol
const ERC20ABI = [
{
constant: true,
inputs: [],
name: 'symbol',
outputs: [
{
name: '',
type: 'string',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
];
const getSymbol = async (l1Addr) => {
if (l1Addr == '0x0000000000000000000000000000000000000000') return 'ETH';
if (l1Addr == '0x1A4b46696b2bB4794Eb3D4c26f1c55F9170fa4C5') return 'MNT';
const l1Contract = new ethers.Contract(
l1Addr,
ERC20ABI,
crossChainMessenger.l1SignerOrProvider,
);
return await l1Contract.symbol();
};
Describe Transactions
To describe a transaction, we need to get the amount and the status of the transaction, we can also get token symbol by using the getSymbol function we defined earlier.
// Describe a cross domain transaction, either deposit or withdrawal
const describeTx = async (tx) => {
console.log(`tx:${tx.transactionHash}`);
// Assume all tokens have decimals = 18
console.log(`\tAmount: ${tx.amount / 1e18} ${await getSymbol(tx.l1Token)}`);
console.log(
`\tRelayed: ${
(await crossChainMessenger.getMessageStatus(tx.transactionHash)) ==
mantleSDK.MessageStatus.RELAYED
}`,
);
};
Conclusion
Congrates! You have completed the tutorial on how to view transactions between L1 and L2 by using Mantle SDK. For more information, check out the Mantle tutorial.