bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 37쪽 중 34쪽
web.com/solidity/base-contracts/erc20base), [ERC721](https://portal.thirdweb.com/solidity/base-contracts/erc721base), or [ERC1155](https://portal.thirdweb.com/solidity/base-contracts/erc1155base)
5. Add any desired [extensions](https://portal.thirdweb.com/solidity/extensions)
3. Once created, navigate to your project’s directory and open in your preferred code editor. If you open the `contracts` folder, you will find your smart contract; this is your smart contract written in Solidity. 
4. The following is code for an ERC721Base contract without specified extensions. It implements all of the logic inside the [`ERC721Base.sol`](https://github.com/thirdweb-dev/contracts/blob/main/contracts/base/ERC721Base.sol) contract; which implements the [`ERC721A`](https://github.com/thirdweb-dev/contracts/blob/main/contracts/eip/ERC721A.sol) standard.
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@thirdweb-dev/contracts/base/ERC721Base.sol";
contract Contract is ERC721Base {
constructor(
string memory _name,
string memory _symbol,
address _royaltyRecipient,
uint128 _royaltyBps
) ERC721Base(_name, _symbol, _royaltyRecipient, _royaltyBps) {}
}
```
This contract inherits the functionality of ERC721Base through the following steps:
* Importing the ERC721Base contract
* Inheriting the contract by declaring that our contract is an ERC721Base contract
* Implementing any required methods, such as the constructor.
5. After modifying your contract with your desired custom logic, you're ready to deploy it to Mantle using [Deploy](https://portal.thirdweb.com/deploy).
{% hint style="info" %}
You can also navigate to the [thirdweb Explore page](https://thirdweb.com/explore) and choose from a collection of pre-built contracts to use and deploy. For more information on different contracts available on Explore, check out [thirdweb’s documentation.](https://portal.thirdweb.com/pre-built-contracts)
{% endhint %}
### Deploying Contract
`deploy` allows you to deploy a smart contract to Mantle Network without configuring RPC URLs, exposing your private keys, writing scripts, and other additional setup such as verifying your contract.
1. To deploy your smart contract using deploy, navigate to the root directory of your project and run:
```bash
npx thirdweb deploy
```
Executing this command will trigger the following actions:
* Compiling all the contracts in the current directory.
* Providing the option to select which contract(s) you wish to deploy.
* Uploading your contract source code (ABI) to [IPFS](https://docs.ipfs.tech/concepts/what-is-ipfs/).
2. When it is completed, it will open a dashboard interface to finish filling out the parameters for our contract.
* `_name`: contract name
* `_symbol`: symbol or "ticker"
* `_royaltyRecipient`: wallet address to receive royalties from secondary sales
* `_royaltyBps`: basis points (bps) that will be given to the royalty recipient for each secondary sale, e.g. 500 = 5%
3. Select Mantle Testnet as the network if you're deploying to the testnet, or Mantle Mainnet if you're deploying to production.
4. Manage additional settings on your contract’s dashboard as needed such as uploading NFTs, configuring permissions, and more.
{% hint style="info" %}
For additional information on `deploy`, refer to [thirdweb’s documentation](https://portal.thirdweb.com/deploy). You can reach out thirdweb support at <http://support.thirdweb.com/>.
{% endhint %}
# Verifying Contracts
Different ways to verify your contracts on Mantle Network
Verifying a contract means making its source code public, along with the compiler settings you used, which allows anyone to compile it and compare the generated bytecode with the one that is deployed on-chain.
## Using Hardhat
To verify your contracts using Hardhat, you'll need an [Etherscan API key](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics) and custom network configuration in your `hardhat.config.ts` file. Feel free to refer to the configuration from [Mantle Hardhat Starter Kit](https://github.com/mantlenetworkio/mantle-tutorial/tree/main/mantle-hardhat-starter-kit).
You'll need to modify the `hardhat.config.ts` configuration to include `customChains` before moving forward.
```typescript
const config: HardhatUserConfig = {
solidity: "0.8.19", // solidity version
defaultNetwork: "mantleTest",
networks: {
mantle: {
url: "https://rpc.mantle.xyz", //mainnet
accounts: [process.env.ACCOUNT_PRIVATE_KEY ?? ''],
},
mantleTest: {
url: "https://rpc.testnet.mantle.xyz", // testnet
accounts: [process.env.ACCOUNT_PRIVATE_KEY ?? '']
}
},
etherscan: {
apiKey: process.env.API_KEY,
customChains: [
{
network: "mantleTest",
chainId: 5001,
urls: {
apiURL: "https://explorer.testnet.mantle.xyz/api",
browserURL: "https://explorer.testnet.mantle.xyz"
}
}
]
},
};
```
Run the following command to verify the contract located in the **"contracts"** directory:
{% code overflow="wrap" %}
```bash
npx hardhat verify --network <network> DEPLOYED_CONTRACT_ADDRESS "Constructor arguments"
```
{% endcode %}
Here's a sample request and the command line output it produces:
```
npx hardhat verify --network mantleTest 0x4ECd62E55...
Successfully submitted source code for contract
contracts/Storage.sol:Storage at 0x4ECd62E55...
for verification on the block explorer. Waiting for verification result...
Successfully verified contract Storage on the block explorer.
https://explorer.testnet.mantle.xyz/address/0x4ECd62E554dF15d3E9A69b97A127A89155E84E18#code
```
{% hint style="info" %}
If you see an error that says **"Hardhat found multiple contracts in the project..."**, see the fix on the [troubleshooting page](/network/network/for-devs/troubleshooting#contract-verification-error-hardhat-found-multiple-contracts-in-the-project-your-mytokencontract-and).
{% endhint %}
## Using Foundry
 Use the following `forge` command to verify the contract you've deployed on Mantle Testnet:
```bash
forge verify-contract --verifier blockscout --watch \
--verifier-url "https://explorer.testnet.mantle.xyz/api?module=contract&action=verify" \
--compiler-version "v0.8.19+commit.7dd6d404" \
--num-of-optimizations 200 \
--constructor-args $(cast abi-encode <constructor> <param>)
--chain 5001 \
<your contract address> <path>:<contract>
```
## Using Remix IDE
1. Start by installing and activating the "Etherscan - Contract Verification" plugin in your Remix IDE from the plugin manager that can be accessed by clicking the <img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FsqEtM08KX0g8tuK6NXcL%2Fimage.png?alt=media&token=fd868613-f800-475a-af4b-32bd3ba23e98" alt="" data-size="line"> icon
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FsaKwZDthR67GjwaoIW4Q%2Fimage.png?alt=media&token=4cd1560e-932f-430a-85f2-459aafe1022b" alt=""><figcaption></figcaption></figure>
2. Once activated, you can access the plugin by clicking on the <img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FS22UDsWa0j31ZC1SxgRS%2Fimage.png?alt=media&token=90ac3c9b-5b11-49a2-a3e4-30ace5429b97" alt="" data-size="line"> icon. Enter your Etherscan API key and save the setting
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FhQADI1bitVrfIDjwsIAV%2Fimage.png?alt=media&token=e2d6cb0b-3066-4b73-9120-e5ef985914e4" alt=""><figcaption></figcaption></figure>
3. Select the **"Injected Provider - MetaMask"** environment to connect to Mantle Testnet under the **"Deploy and Run Transactions"** tab
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FVl6K13qohzeXa0SEGNcK%2Fimage.png?alt=media&token=eef7eefd-dcb4-40d2-983a-ac304737815f" alt=""><figcaption></figcaption></figure>
4. Now navigate to the **"Etherscan - Contract Verification"** tab, select the contract you want to verify, and enter the constructor parameters (in hexadecimal), and the contract address. Then click the <img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FN0dYm4J2aztj2WJtMRdd%2Fimage.png?alt=media&token=754dcd63-de71-4e4b-a1bc-841d1e60bf75" alt="" data-size="line"> button
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FKn0jLekRLL76ycE1jjvU%2Fimage.png?alt=media&token=ed17602c-c9b9-43ec-91de-69502bf52b3b" alt=""><figcaption></figcaption></figure>
#### Obtaining Constructor Parameters
Consider the following sample contract code:
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Name {
constructor(string memory name) {}
}
```
In order to obtain the hex encoded constructor parameters, first you'll need to compile your code. Then, navigate to the **"Deploy and Run Transactions"** tab, enter the string parameter in the field next to the **"Deploy"** button, and then bring up the drop-down menu. 
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2F6JcgSPbq3XvlTNHEGcXv%2Fimage.png?alt=media&token=d3d85273-0f8f-4c1e-a47e-723dbdc50b95" alt=""><figcaption></figcaption></figure>
Click on the <img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FFRCefPUZFXeY5dak9fEs%2Fimage.png?alt=media&token=3560df9a-c737-44ff-b1e4-d03f9b140b5f" alt="" data-size="line"> button to copy the hex encoded parameter value, and continue with the verification process.
## Using Explorer
There are multiple methods of verification available via Mantle Explorer. 
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FTQLxl58xUWI52AQd9A8O%2Fimage.png?alt=media&token=1a48d84a-e907-4c23-8c13-add5aa96530d" alt=""><figcaption></figcaption></figure>
Let's go over the steps involved in using the most popular methods.
### Via Flattened Source Code
You can use Remix, or any other tool, to flatten your contract code.
#### Flatten Using Remix
1. In Remix, right click on the contract file and click on **"Flatten"**.
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2Fu4qNlK5CKQGHijtq8AVM%2Fimage.png?alt=media&token=9a96e728-eb7b-4f8b-b99e-0bb0b8baf045" alt="" width="491"><figcaption></figcaption></figure>
2. Flattening a contract with imports will generate a new Solidity file with the flattened code that you can copy, as shown below.
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2F96GV2wfbKEoNWkb28xnJ%2Fimage.png?alt=media&token=4af4100c-24ff-4289-934b-f9de872faac2" alt="" width="563"><figcaption></figcaption></figure>
#### Flatten Using Hardhat
If you're using Hardhat to flatten your contract, you can use the following command:
```bash
npx hardhat flatten contracts/Airdrop.sol > asAirdropFlatten.sol
```
You can copy the flattened contract code from the newly generated file.
#### Flatten Using Foundry
If you're using Foundry to flatten your contract, you can use the following command:
```bash
forge flatten --output src/Storage.flattened.sol src/Storage.sol
```
You can copy the flattened contract code from the newly generated file.
#### Plugging Flattened Code Into Explorer
Once you have obtained the flattened code, fill in the contract address, contract name, the flattened code, and other fields on Mantle Explorer and proceed with verifying your contract.
{% hint style="info" %}
Make sure to select the correct EVM version
{% endhint %}
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FtxkPqafhUDP4ZfbEPPgM%2Fimage.png?alt=media&token=0004d3b9-b89e-4ed7-93bd-0d34b1e00791" alt="" width="563"><figcaption></figcaption></figure>
### Via Standard Input JSON
You can obtain the standard input JSON file by compiling your contract using any of the tools we've used so far. Let's see where the input value code can be found in each case.
#### Compiled Using Remix
The file can be found under the **"Solidity Compiler"** tab that can be accessed by clicking on the <img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FboLEJb0dEHaNaEtZPuGK%2Fimage.png?alt=media&token=8497549c-e434-496b-81ef-82edc0750136" alt="" data-size="line"> button. Click on the <img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2Fr8BSEYIOxpXRsZd1Qyks%2Fimage.png?alt=media&token=7bd4ea96-4e53-44ea-ac9a-210f8d6cc195" alt="" data-size="line"> button to bring up a menu from where you can copy the **"COMPILERINPUT"** code, as shown below.
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FMPSn502a6RhgX64U4fAa%2Fimage.png?alt=media&token=7c6fa014-6804-483b-9d0b-2b3222ea0916" alt=""><figcaption></figcaption></figure>
You can paste and save the copied JSON code in a separate file that can later be imported to the Explorer.
#### Compiled Using Hardhat
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. 
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FCIOcyVXuuMe5zZAI1M7v%2Fimage.png?alt=media&token=53e9025f-2fc6-465c-9fbd-c6824e40819f" alt="" width="333"><figcaption></figcaption></figure>
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://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FufY8vd3kY8YfgZvgOPHI%2Fimage.png?alt=media&token=80c2bef0-a552-4b31-ba40-12f2895d1120" alt="" width="563"><figcaption></figcaption></figure>
#### Compiled Using Foundry
With Foundry, you'll need to run the following command to obtain the input and output information for your contract code.
```bash
forge build --build-info
```
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 in a **new** empty JSON file, which you can then import to the Explorer.
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FUz84HGkU3UIYsz7gwYZI%2Fimage.png?alt=media&token=7ab1fe90-f159-4a1a-bcb2-dd2c4c184718" alt="" width="563"><figcaption></figcaption></figure>
#### Importing JSON File to Explorer
Once you've obtained the standard input JSON values in a file, you can import it to the Explorer, as shown below.
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FifGpVMK490RWMGicanp8%2Fimage.png?alt=media&token=54a0b22b-0f9d-4d92-b0b6-d2fcbba153bb" alt="" width="563"><figcaption></figcaption></figure>
# Debugging Contracts with Foundry
### What is Foundry
Foundry is a Solidity Framework for building, testing, fuzzing, debugging and deploying Solidity smart contracts. It can manage dependencies, compile your project, run tests, deploy contracts, and lets you interact with the chain from the command-line and via Solidity scripts.Foundry comes with the following tools that can be used to debugging:
* Anvil: Used to deploy a local Ethereum node
* Cast: Used to send RPC calls and interact with the chain
* Forge: Used to build, compile, and test smart contracts
\
You can install Foundry by running the following commands:
**Linux/Mac**:
```Bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
```
**Windows**: (Requires Rust, install from <https://rustup.rs/>)
```Bash
cargo install --git https://github.com/foundry-rs/foundry --bins --locked
```
**Docker**:
```SQL
docker pull ghcr.io/foundry-rs/foundry:latest
```
{% hint style="info" %}
More detailed instructions on installation available here:
<https://book.getfoundry.sh/getting-started/installation>
{% endhint %}
#### Anvil
Anvil deploys a local Ethereum node in your environment that can be used to deploy a node. Simply run anvil to deploy one. 
It has a few useful properties. It allows you to fork a network at the latest, or any specific height. You can run the following command to fork the Mantle mainnet environment for debugging your code:
```Bash
export RPC_URL="https://rpc.mantle.xyz"
anvil --fork-url $RPC_URL
```
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2Ff8c2MUY85oJomoBJYgBb%2Fimage.png?alt=media&token=a34abeaa-58e6-4e27-9fd7-c5ed0d620c74" alt=""><figcaption></figcaption></figure>
Call traces are more detailed, i.e., we can see which functions are being executed in remote contracts, i.e., the functions that executed by every contract.
{% hint style="info" %}
Note that it isn't necessary to use an Anvil node to use Foundry. You can use other local nodes like Hardhat, Ganache, etc. as well.
{% endhint %}
#### Cast
You can use the following cast commands to interact with the chain.
{% hint style="info" %}
The examples below are based on Anvil. If you have Anvil up and running, try invoking the following cases in another command line window. This will allow you to debug online contracts using the mantle mainnet.
{% endhint %}
**cast call / cast send**
[cast send](https://book.getfoundry.sh/reference/cast/cast-send) can be used to sign and publish a transaction on the chain.You can use it make an arbitrary contract method call, like so:
> cast send $contract\_address "someFunc(unit256)" 0x... \\--private-key $wallet\_private\_key
[cast call](https://book.getfoundry.sh/reference/cast/cast-call) on the other hand can be used to perform an account call without publishing it to the chain, and that's why you don't need to specify a private key.So for instance, you can make a contract method call, like so:
> cast call $contract\_address "someFunc(uint, args\[])" $number "\[arg\_1, ... , arg\_n]"
```Bash
export WMNT=0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8
cast call $WMNT "balanceOf(address)" 0x1858d52cf57c07A018171D7a1E68DC081F17144f
//0x00000000000000000000000000000000000000000006cfd1854e3ec57a5d636d
cast --to-dec 0x00000000000000000000000000000000000000000006cfd1854e3ec57a5d636d
//8234949754837270014026605
```
\
**cast run**
[cast run](https://book.getfoundry.sh/reference/cast/cast-run) can be used to run a published transaction in a local environment and print the trace. For example:
> cast run $TX\_HASH
```Bash
cast run 0xf500a3c8e08960aa28ccdd557730087b1d9b618a42b9595c7800ceeff53dddaa
```
<figure><img src="https://3701411312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNHXgeqhgrznnRobzznxl%2Fuploads%2FOg12afKjE1n0YXtG33A4%2Fimage.png?alt=media&token=7b8e8da5-8885-4653-a25d-4a160b6c1739" alt=""><figcaption></figcaption></figure>
You can also replay transactions in the debugger using the `--debug` flag, like s프로젝트로 돌아가기