bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 37쪽 중 16쪽
antleSepolia',
chainId: 5003,
urls: {
apiURL: 'https://api-sepolia.mantlescan.xyz/api',
browserURL: 'https://sepolia.mantlescan.xyz/',
},
},
],
},
};
export default config;
```
## Verify your contracts[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/hardhat#verify-your-contracts) <a href="#verify-your-contracts" id="verify-your-contracts"></a>
Currently hardhat supports two methods of contract validation:
#### Use `--deployment-id` to name your deployment.
You can use a flag to name your contract (whatever you want) during deployment, and then you can run the `ignition verify` task and pass the deployment Id to verify your contracts.
1. Deploy your contract like this:
```bash
npx hardhat ignition deploy ./ignition/modules/<Your-Contract>.ts --network mantleSepolia --deployment-id <Contract-Deployment-ID>
```
2. Verify your contract like this:
<pre class="language-bash"><code class="lang-bash"><strong>npx hardhat ignition verify <Contract-Deployment-ID>
</strong></code></pre>
#### Use `--verify` to verify your contract during deployment:
You can add a flag to verify your contract during deployment
```
npx hardhat ignition deploy ./ignition/modules/<Your-Contract>.ts --network mantleSepolia --verify
```
### **Sample Outputs**
```sh
Successfully submitted source code for contract
contracts/Lock.sol:Lock at 0x76B6a0F36CC71d49792eC1740892E8bc9AA7814c
for verification on the block explorer. Waiting for verification result...
Successfully verified contract Lock on the block explorer.
https://sepolia.mantlescan.xyz/address/0x76B6a0F36CC71d49792eC1740892E8bc9AA7814c#code
```
Now you can head over to [Mantle blockchain explorer](https://mantlescan.xyz/) to check the contract status.
# Use Foundry to Verify Smart Contracts
To verify your contracts using Foundry, we need to verify the contract with the help of Mantle's explorer. It is assumed that you have already completed the deployment of the contract. If not, you can review this document to complete the contract deployment.
## Verify your contract[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/foundry#verify-your-contract) <a href="#verify-your-contract" id="verify-your-contract"></a>
Run the following command in your terminal to verify your contract.
```sh
forge verify-contract --verifier-url "https://api.etherscan.io/v2/api?chainid=5003" --etherscan-api-key <YOUR_MANTLESCAN_API_KEY> --compiler-version "v0.8.23+commit.f704f362" <contract address> src/Contract_Name.sol:Contract_Name --watch
```
{% hint style="info" %}
Make sure to configure `--compiler-version` and `--num-of-optimizations` correctly! And you can modify the `--constructor-args` if needed. And make sure you set the `--chain` and `--verifier-url` correctly.
{% endhint %}
### **Sample Outputs**
```sh
Start verifying contract `0xad4736AE3DB1E71375a27b7d8A634994921E3c04` deployed on mainnet
Submitting verification for [src/Counter.sol:Counter] 0xad4736AE3DB1E71375a27b7d8A634994921E3c04.
Submitted contract for verification:
Response: `OK`
GUID: `d5s2dhvr62tahsjez9cypxkxdcmpjagkkxxuqcp6dkyaizbp8n`
URL:
https://etherscan.io/address/0xad4736ae3db1e71375a27b7d8a634994921e3c04
Contract verification status:
Response: `NOTOK`
Details: `Pending in queue`
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
```
<br>
# Use Explorer to Verify Smart Contracts
There are multiple methods of verification available via [Mantle Explorer](https://mantlescan.xyz/verifyContract).
## Via Source Code[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/explorer#via-source-code) <a href="#via-source-code" id="via-source-code"></a>
### Plugging Code Into Explorer[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/explorer#plugging-code-into-explorer) <a href="#plugging-code-into-explorer" id="plugging-code-into-explorer"></a>
Once you have obtained the source code, fill in the contract address, contract name, the flattened code, and other fields on [Mantle Explorer](https://mantlescan.xyz/verifyContract) and proceed with verifying your contract.
{% hint style="info" %}
Make sure to select the correct compiler for your contract, the proper EVM version and the optimization number.
{% endhint %}
<figure><img src="https://docs-v2.mantle.xyz/assets/images/explorer-3-9893936ecddd85016c449f96b7f3a985.png" alt=""><figcaption></figcaption></figure>
## Via Standard Input JSON[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/explorer#via-standard-input-json) <a href="#via-standard-input-json" id="via-standard-input-json"></a>
### Get JSON file[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/explorer#get-json-file) <a href="#get-json-file" id="get-json-file"></a>
{% tabs %}
{% tab title="Compiled Using Remix" %}
1. The file can be found under the **"Solidity Compiler"** tab that can be accessed by clicking on the left navigate bar.
<img src="https://docs-v2.mantle.xyz/assets/images/explorer-4-eb99e50581b67f4a3c7f2ce943b096cf.png" alt="" height="589" width="361">
2. Click on the **Compilation Details** button to bring up a menu from where you can copy the `COMPILERINPUT` code, as shown below.
<img src="https://docs-v2.mantle.xyz/assets/images/explorer-5-7a9fd705b6a549cba5041f677a8835b7.png" alt="" height="409" width="840">
{% endtab %}
{% tab title="Compiled Using Hardhat" %}
1. Once you've successfully compiled your contract code, you'll be able to find a JSON file (with a long name in hexadecimal) in the **"./artifacts/build-info"** directory, as shown below.
<img src="https://docs-v2.mantle.xyz/assets/images/explorer-6-9e1abff10dbf84ed104f15104c53aa01.png" alt="" height="252" width="620">
2. Within this JSON file, you'll need to copy the `input` field, as shown below, and then paste the copied code into a **new** empty JSON file. You can import this file to the Explorer later.
<figure><img src="https://3885809119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQliZlRNwHhmzesDU1Ksq%2Fuploads%2F0NA1XA2wMxma2M3lZaE6%2Fimage.png?alt=media&token=df53befb-f0b6-49ab-aa2a-6640c0d75c53" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% tab title="Compiled Using Foundry" %}
1. With Foundry, you'll need to run the following command to obtain the input and output information for your contract code.
```sh
forge build --build-info
```
**Note:**
If you get this response, please run `forge clean` and then rerun the command.
```sh
[⠒] Compiling...No files changed, compilation skipped
[⠑] Compiling...
```
2. You'll then be able to find a JSON file (with a long name in hexadecimal) in the **"./out/build-info"** directory, as shown below. Copy the `input` field and paste it into a new empty JSON file, which you can then import to the explorer.
<figure><img src="https://docs-v2.mantle.xyz/assets/images/explorer-8-8be230c85ee3972884c948d1926b97f6.png" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}
### Importing JSON file to Explorer[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/verify/explorer#importing-json-file-to-explorer) <a href="#importing-json-file-to-explorer" id="importing-json-file-to-explorer"></a>
Once you've obtained the standard input JSON values in a file, you can import it to the explorer, as shown below.
Choose "Solidity (Standard-Json-Input)" as the compiler type. 
<figure><img src="https://docs-v2.mantle.xyz/assets/images/explorer-11-b97590c291ac8713469c3111eab3eea4.png" alt=""><figcaption></figcaption></figure>
Upload the json file and verify it.
<figure><img src="https://3885809119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQliZlRNwHhmzesDU1Ksq%2Fuploads%2Fbe0yiM6g7sAxKzT9zlUx%2Fimage.png?alt=media&token=be4f6c42-4711-408a-a5b6-05c57b2d72ea" alt=""><figcaption></figcaption></figure>
# How to Use Mantle SDK
The Mantle SDK implements methods to make contract calls for both:
* **Deposit**
* Locking `$MNT`/ERC20/ERC721 tokens on layer 1 (L1) and minting equivalent tokens on layer 2 (L2)
* **Withdrawal**
* Locking `$MNT`/ERC20/ERC721 tokens on L2 and minting equivalent tokens on L1
* **Sending Messages**
* Contracts on Mantle can invoke contract functions on Ethereum, and vice versa.
### Deposit[](https://docs-v2.mantle.xyz/devs/dev-guides/tools/sdk#deposit) <a href="#deposit" id="deposit"></a>
`$MNT`/`$ETH`/ERC20/ERC721 token deposits into L2 from the Ethereum mainnet can be triggered using the following SDK methods.
| **Token Type** | **SDK Methods** |
| -------------- | --------------- |
| $ETH | `depositETH` |
| $MNT | `depositMNT` |
| ERC20 | `depositERC20` |
| ERC721 | `depositERC721` |
{% hint style="danger" %}
You must first approve the Mantle Bridge to use the amount of `$MNT`/`$ETH`/`ERC20`/`ERC721` tokens that you are moving to L2, or the deposit will fail.
{% endhint %}
{% hint style="info" %}
`$ETH` deposits can alternatively be triggered by sending $ETH directly to the [L1StandardBridge](https://github.com/mantlenetworkio/mantle/blob/main/packages/contracts/contracts/L1/messaging/L1StandardBridge.sol). Your account will be funded with the corresponding amount once the transaction is detected and finalized.
{% endhint %}
### Withdrawal[](https://docs-v2.mantle.xyz/devs/dev-guides/tools/sdk#withdrawal) <a href="#withdrawal" id="withdrawal"></a>
`$MNT`/`$ETH`/ERC20/ERC721 token withdrawals from L2 to the Ethereum mainnet can be triggered using the following SDK methods.
{% hint style="info" %}
All withdrawals use `$MNT` for gas fees.
{% endhint %}
| **Token Type** | **SDK Methods** | **L2 Token Address** |
| -------------- | ---------------- | -------------------------------------------- |
| $MNT | `withdrawMNT` | `0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000` |
| $ETH | `withdrawERC20` | `0xdEAddEaDdeadDEadDEADDEAddEADDEAddead1111` |
| ERC20 | `withdrawERC20` | - |
| ERC721 | `withdrawERC721` | - |
### Sending Messages[](https://docs-v2.mantle.xyz/devs/dev-guides/tools/sdk#sending-messages) <a href="#sending-messages" id="sending-messages"></a>
Contracts on Mantle can invoke contract functions on Ethereum, and vice versa. This enables dApps on Mantle and Ethereum to interact with each other. Here's a [step-by-step](https://github.com/mantlenetworkio/mantle-tutorial/tree/main/cross-dom-comm#communication-between-contracts-on-l1-and-l2) guide on how to enable communication between L1 and L2.
# Bridging MNT with the Mantle SDK
{% hint style="info" %}
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).
{% endhint %}
This tutorial teaches you how to use the Mantle SDK to transfer MNT between Layer 1 (L1) and Layer 2 (L2). Check the complete code [here](https://github.com/mantlenetworkio/mantle-tutorial/blob/main/cross-dom-bridge-mnt/index.js).
## Setup[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#setup) <a href="#setup" id="setup"></a>
1. Ensure your computer has:
* [`git`](https://git-scm.com/downloads)
* [`node`](https://nodejs.org/en/)
* [`yarn`](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable)
* [`docker`](https://www.docker.com/products/docker-desktop/)
2. Start L1 and L2 environments. Currently, we support the local environment or the testnet environment. If you want to deploy your own L1 and L2, please follow the instructions below.
```sh
git clone https://github.com/mantlenetworkio/mantle-v2.git
cd mantle/ops
make up
# check status
make ps
```
**We highly recommend using the testnet environment, which means you can skip this step, you can apply your own L1 RPC** [**here**](https://www.alchemy.com/) **and replace the L1 RPC URL in the `.env` file.**
3. Clone this repository and enter it.
```sh
git clone https://github.com/mantlenetworkio/mantle-tutorial.git
cd mantle-tutorial/cross-dom-bridge-mnt
```
4. Install the necessary packages.
```sh
yarn
```
## Run the Sample Code[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#run-the-sample-code) <a href="#run-the-sample-code" id="run-the-sample-code"></a>
The sample code is in the `index.js`, the whole execution flow will automatically start after running it.
### Node Environment[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#node-environment) <a href="#node-environment" id="node-environment"></a>
If you want to test by using your own nodes, you should configure the missing or changing environment variables in file `.env.local.tmp` then use `yarn local` to execute `index.js`.
If you want to have a test in our testnet network you should do the same for `.env.testnet.tmp` and then use `yarn testnet` to execute `index.js`.
## How Does It Work?[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#how-does-it-work) <a href="#how-does-it-work" id="how-does-it-work"></a>
### Import the Necessary Libraries[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#import-the-necessary-libraries) <a href="#import-the-necessary-libraries" id="import-the-necessary-libraries"></a>
```javascript
const ethers = require('ethers');
const mantleSDK = require('@mantleio/sdk');
const fs = require('fs');
```
In this tutorial, we initialize the required 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 Mantle SDK, which facilitates cross-chain transactions between Layer 1 (L1) and Layer 2 (L2) blockchains. It abstracts away complexities, making it easier to perform operations like depositing and withdrawing assets.
* `fs`: The Node.js `fs` module for file system operations. It may be used later in the code for reading or writing files, although it's not explicitly used in the provided snippet.
### Network Configuration and Wallet Setup[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#network-configuration-and-wallet-setup) <a href="#network-configuration-and-wallet-setup" id="network-configuration-and-wallet-setup"></a>
Next, the code defines some configuration parameters. We fetch the specified network and wallet configurations from the `.env` file.
```javascript
const L1TestERC20 = JSON.parse(fs.readFileSync('TestERC20.json'));
const l1MntAddr = process.env.L1_MNT;
const l2MntAddr = process.env.L2_MNT;
const key = process.env.PRIV_KEY;
```
* `L1TestERC20`: A JSON object representing the L1 Test ERC20 contract.
* `l1MntAddr`: The address of the L1 MNT token.
* `l2MntAddr`: The address of the L2 MNT token.
* `key`: The private key retrieved from the environment variables.
```javascript
// Global variable because we need them almost everywhere
let crossChainMessenger;
let ourAddr; // Our address
```
* `crossChainMessenger`: A global variable initialized later in the `setup` function, representing the Mantle SDK's `CrossChainMessenger` object.
* `ourAddr`: A variable that will store the user's address.
Then create wallet objects by passing the private key and RPC addresses as parameters for L1 and L2.
```javascript
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);
```
### Setup CrossChainMessenger Object[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#setup-crosschainmessenger-object) <a href="#setup-crosschainmessenger-object" id="setup-crosschainmessenger-object"></a>
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.
```javascript
const setup = async () => {
ourAddr = l1Wallet.address;
crossChainMessenger = new mantleSDK.CrossChainMessenger({
l1ChainId: process.env.L1_CHAINID,
l2ChainId: process.env.L2_CHAINID,
l1SignerOrProvider: l1Wallet,
l2SignerOrProvider: l2Wallet,
bedrock: true,
});
l1Mnt = new ethers.Contract(l1MntAddr, L1TestERC20.abi, l1Wallet);
};
```
### Report the Balances[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#report-the-balances) <a href="#report-the-balances" id="report-the-balances"></a>
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.
```javascript
const reportBalances = async () => {
const l1Balance = (await l1Mnt.balanceOf(ourAddr)).toString().slice(0, -18);
const l2Balance = (await crossChainMessenger.l2Signer.getBalance())
.toString()
.slice(0, -18);
console.log(`Token on L1:${l1Balance} Token on L2:${l2Balance}`);
};
```
### Deposit[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#deposit) <a href="#deposit" id="deposit"></a>
The `depositMNT` function deposits 1 MNT token to L2 via the Mantle bridge. 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.
To show that the deposit actually happened we show before and after balances.
```javascript
console.log('#################### Deposit MNT ####################');
await reportBalances();
```
We need to make sure the allowance is approved. We can do that by sending an approval transaction on L1.
```javascript
// Need the l2 address to know which bridge is responsible
const allowanceResponse = await crossChainMessenger.approveERC20(
l1MntAddr,
l2MntAddr,
depositToken,
);
await allowanceResponse.wait();
```
`crossChainMessenger.depositMNT()` creates and sends the deposit transaction on L1.
```javascript
console.log(`Time so far ${(new Date() - start) / 1000} seconds`);
const response = await crossChainMessenger.depositMNT(depositToken);
```
Of course, it takes time for the transaction to actually be processed on L1.
```javascript
console.log(`Deposit transaction hash (on L1): ${response.hash}`);
await response.wait();
```
After the transaction is processed on L1 it needs to be picked up by an off-chain service and relayed to L2. To show that the deposit actually happened we need to wait until the message is relayed.
```javascript
console.log('Waiting for status to change to RELAYED');
console.log(`Time so far ${(new Date() - start) / 1000} seconds`);
await crossChainMessenger.waitForMessageStatus(
response.hash,
mantleSDK.MessageStatus.RELAYED,
);
```
Once the message is relayed the balance change on L2 is practically instantaneous. We can just report the balances and see that the L2 balance rose by 1.
```javascript
await reportBalances();
console.log(`depositMNT took ${(new Date() - start) / 1000} seconds\n`);
```
### Withdraw[](https://docs-v2.mantle.xyz/devs/dev-guides/how-to/bridge-mnt#withdraw) <a href="#withdraw" id="withdraw"></a>
This function shows how to withdraw MNT from L2 to L1. For deposits, it was enough to transfer 1 MNT to show that the L2 balance increases. However, in the case of withdrawals the withdrawing account needs to be paid for finalizing the message, which costs more than that.
To show that the withdrawal actually happened we show before and after balances.
```javascript
console.log('#################### Withdraw MNT ####################');
await reportBalances();
```
Then we withdraw the token from L2.
```javascript
const response = await crossChainMessenger.withdrawMNT(withdrawToken);
console.log(`Transaction hash (on L2): ${response.hash}`);
await response.wait();
```
We need to wait until the message is ready to prove.
```javascript
console.log('Waiting for status to be READY_TO_PROVE');
console.log(`Time so far 프로젝트로 돌아가기