Monad 백서

Monad

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 28쪽

stage. The function below simulates each stage before broadcasting it and stops if a command fails: ```sh theme={null} ( set -eu run_stage() { local stage="$1" local zero_for_one="${2:-true}" export STAGE="$stage" ZERO_FOR_ONE="$zero_for_one" FOUNDRY_PROFILE=monad_testnet forge script \ script/monad/PoolLifecycle.s.sol:PoolLifecycle \ --network monad --rpc-url "$MONAD_TESTNET_RPC_URL" \ --account monad-hooks-testnet \ --sender "$DEPLOYER" FOUNDRY_PROFILE=monad_testnet forge script \ script/monad/PoolLifecycle.s.sol:PoolLifecycle \ --network monad --rpc-url "$MONAD_TESTNET_RPC_URL" \ --account monad-hooks-testnet \ --sender "$DEPLOYER" \ --broadcast --slow } run_stage initialize run_stage fundAndApprove run_stage addLiquidity # Run this immediately after addLiquidity. The next forge run overwrites run-latest.json. python3 tools/record_position.py \ --broadcast broadcast/PoolLifecycle.s.sol/10143/run-latest.json \ --position-manager "$POSITION_MANAGER" \ --deployer "$DEPLOYER" \ --output deployments/position.json export TOKEN_ID="$(jq -er .tokenId deployments/position.json)" cast call "$POSITION_MANAGER" "ownerOf(uint256)(address)" "$TOKEN_ID" \ --rpc-url "$MONAD_TESTNET_RPC_URL" FOUNDRY_PROFILE=monad_testnet forge script \ script/monad/ReadState.s.sol:ReadState \ --network monad --rpc-url "$MONAD_TESTNET_RPC_URL" \ --sender "$DEPLOYER" run_stage swapExactInput true run_stage swapExactInput false run_stage swapExactOutput true run_stage swapExactOutput false run_stage removeLiquidity run_stage revokeApprovals ) ``` During `addLiquidity`, `PositionManager` issues a position NFT to your account. [`record_position.py`](https://github.com/monad-developers/uniswap-v4-hooks-example/blob/c3cc78a88f10e5ba141cd6fb632f31db83c0daf8/tools/record_position.py) reads your NFT’s `Transfer` event from the mint receipt and saves its token ID. Run it immediately after `addLiquidity`, before the next broadcast overwrites `run-latest.json`. PositionManager is used by multiple accounts, so the token ID must come from your receipt. `fundAndApprove` mints mock tokens and approves Permit2 to transfer them. For each token, it grants PositionManager `4e18` units and Universal Router `1e16` units through Permit2, with a one-day expiry. The ERC-20 allowance to Permit2 covers their combined total. The script uses these pool settings: | Setting | Value | | - | - | | Currency order | Token addresses sorted numerically in ascending order. | | Hook and pool | `HOOK_ADDRESS` in the `PoolKey`. | | Fee and tick spacing | `3000` (0.3%) and `60`. | | Initial price | `sqrtPriceX96 = 2**96`, which is price 1 for equal-decimal tokens. | | Position range | Ticks `-600` to `600`. | | Funding | `1000e18` of each mock token minted to `DEPLOYER`. | | Liquidity | `100e18` liquidity units through PositionManager. | | Swap input/output | `1e15` token units (`0.001` tokens) per swap. | | Swap protection | `99%` minimum quoted output for exact input; `101%` maximum quoted input for exact output. | The four swap stages cover exact input and exact output in both directions. Universal Router executes the swaps using Permit2 for token transfers. Each swap uses a fresh quote with a one-percent slippage bound. This example routes swaps through the Counter pool you created. Universal Router executes the supplied route; it does not certify the hook’s safety. Applications that select among pools must evaluate their hooks as well as their quoted prices. ## Check the results [`ReadState.s.sol`](https://github.com/monad-developers/uniswap-v4-hooks-example/blob/c3cc78a88f10e5ba141cd6fb632f31db83c0daf8/script/monad/ReadState.s.sol) reports pool state, hook counters, position liquidity, token balances, and allowances. Run it after the lifecycle completes: ```sh theme={null} FOUNDRY_PROFILE=monad_testnet forge script \ script/monad/ReadState.s.sol:ReadState \ --network monad --rpc-url "$MONAD_TESTNET_RPC_URL" \ --sender "$DEPLOYER" ``` For a newly deployed hook, the completed sequence produces these results: | Value | Expected result | | - | - | | `beforeAddLiquidityCount` | `1` | | `beforeRemoveLiquidityCount` | `1` | | `beforeSwapCount` and `afterSwapCount` | `4` each | | Position liquidity | `0`; `removeLiquidity` burns the position NFT. | | Token approvals | `0` for both ERC-20 allowances to Permit2 and all four Permit2 allowances. | ## Verify the deployed hook and mock tokens Follow the [Foundry verification guide](/guides/verify-smart-contract/foundry) to publish your contract source on an explorer. For Counter, supply the PoolManager address as its constructor argument: ```sh theme={null} FOUNDRY_PROFILE=monad_testnet forge verify-contract \ "$HOOK_ADDRESS" "src/Counter.sol:Counter" \ --chain 10143 \ --constructor-args "$(cast abi-encode 'f(address)' "$POOL_MANAGER")" \ --verifier etherscan \ --etherscan-api-key "$MONADSCAN_API_KEY" \ --watch ``` The compiler settings are configured in [`foundry.toml`](https://github.com/monad-developers/uniswap-v4-hooks-example/blob/c3cc78a88f10e5ba141cd6fb632f31db83c0daf8/foundry.toml). For the mock tokens, [`DeployMockTokens.s.sol`](https://github.com/monad-developers/uniswap-v4-hooks-example/blob/c3cc78a88f10e5ba141cd6fb632f31db83c0daf8/script/monad/DeployMockTokens.s.sol) specifies the constructor arguments: `("Monad Hooks Test A", "MHTA", 18)` and `("Monad Hooks Test B", "MHTB", 18)`. ## Next step Continue to [Customize and publish](/guides/uniswap-v4-hooks/customize-and-publish) to change the hook's behavior and learn about mainnet infrastructure and registry submission. # Set up the hook project Source: https://docs.monad.xyz/guides/uniswap-v4-hooks/setup On this page, you will install the pinned source, run local tests, and prepare a Monad Testnet account. ## Install the toolchain and pin the source Install [Foundry v1.8.0](https://book.getfoundry.sh/getting-started/installation), Git, Python 3, Node.js with npm, and `jq`. Node and npm are required because the Universal Router dependency tree includes npm packages. Check the versions before creating the checkout: ```sh theme={null} forge --version node --version npm --version python3 --version jq --version ``` Clone the [`uniswap-v4-hooks-example` repository](https://github.com/monad-developers/uniswap-v4-hooks-example) into a folder named `monad-hook`. The final argument in the `git clone` command sets this local folder name. Then enter the folder and check out the tested revision before initializing its recursive dependencies: ```sh theme={null} git clone https://github.com/monad-developers/uniswap-v4-hooks-example.git monad-hook cd monad-hook git checkout c3cc78a88f10e5ba141cd6fb632f31db83c0daf8 git submodule update --init --recursive ``` The checkout includes the scripts and dependencies used below. Use this `monad-hook` folder throughout the guide. Create the manifest directory and install the locked npm dependencies. `npm ci --ignore-scripts` keeps package installation from running third-party lifecycle scripts: ```sh theme={null} mkdir -p deployments npm ci --ignore-scripts ``` ## Build and test locally [`foundry.toml`](https://github.com/monad-developers/uniswap-v4-hooks-example/blob/c3cc78a88f10e5ba141cd6fb632f31db83c0daf8/foundry.toml) configures Solidity `0.8.26`, the Cancun EVM, and the compiler settings for the example's dependencies. Build the project and run its tests: ```sh theme={null} FOUNDRY_PROFILE=monad_testnet forge build FOUNDRY_PROFILE=monad_testnet forge test -vv python3 -m unittest discover -s tools -p 'test_*.py' -v ``` The tests cover hook permissions, liquidity positions, swaps, and parsing the position NFT’s mint receipt. ## Prepare a testnet account Import a testnet account into Foundry’s encrypted keystore: ```sh theme={null} cast wallet import monad-hooks-testnet --interactive export DEPLOYER="$(cast wallet address --account monad-hooks-testnet)" ``` Fund the address using the [Monad Testnet faucet](/developer-essentials/testnet). This account will deploy your contracts and own the liquidity position. ```sh theme={null} export MONAD_TESTNET_RPC_URL="https://testnet-rpc.monad.xyz" export EXPECTED_CHAIN_ID=10143 cast chain-id --rpc-url "$MONAD_TESTNET_RPC_URL" cast balance "$DEPLOYER" --rpc-url "$MONAD_TESTNET_RPC_URL" ``` The chain ID should be `10143`. ## Load the contract addresses [`shared-infrastructure.json`](https://github.com/monad-developers/uniswap-v4-hooks-example/blob/c3cc78a88f10e5ba141cd6fb632f31db83c0daf8/shared-infrastructure.json) is included at the root of the `monad-hook` directory you just cloned. It lists the existing testnet contracts. Load those addresses into your shell: ```sh theme={null} export SHARED_INFRA="shared-infrastructure.json" export POOL_MANAGER="$(jq -er .poolManager "$SHARED_INFRA")" export POSITION_MANAGER="$(jq -er .positionManager "$SHARED_INFRA")" export UNIVERSAL_ROUTER="$(jq -er .universalRouter "$SHARED_INFRA")" export STATE_VIEW="$(jq -er .stateView "$SHARED_INFRA")" export QUOTER="$(jq -er .quoter "$SHARED_INFRA")" export POSITION_DESCRIPTOR="$(jq -er .positionDescriptor "$SHARED_INFRA")" export PERMIT2="$(jq -er .permit2 "$SHARED_INFRA")" export WMON="$(jq -er .wrappedNative "$SHARED_INFRA")" printf 'PoolManager: %s\nPositionManager: %s\nUniversalRouter: %s\n' \ "$POOL_MANAGER" "$POSITION_MANAGER" "$UNIVERSAL_ROUTER" ``` ## Next step Continue to [Deploy](/guides/uniswap-v4-hooks/deploy) from this directory and shell. # Verify a smart contract on Monad using Foundry Source: https://docs.monad.xyz/guides/verify-smart-contract/foundry Once your contract is deployed to a live network, the next step is to verify its source code on the block explorer. Verifying a contract means uploading its source code, along with the settings used to compile the code, to a repository (typically maintained by a block explorer). This allows anyone to compile it and compare the generated bytecode with what is deployed on chain. Doing this is extremely important in an open platform like Monad. In this guide we'll explain how to do this using [Foundry](/tooling-and-infra/toolkits/foundry) v1.8 or later with the Monad execution environment enabled. <Tip> This guide assumes you have Foundry v1.8.0 or later installed and `network = "monad"` configured in `foundry.toml`. See the [installation and configuration instructions](/tooling-and-infra/toolkits/foundry#installation) if you haven't set it up yet. </Tip> <Tabs> <Tab title="Mainnet"> <Tabs> <Tab title="Foundry Monad template (Recommended)"> <Note> The [`foundry-monad`](https://github.com/monad-developers/foundry-monad) template is configured for testnet by default. To use mainnet, update your `foundry.toml` file: * Change `eth-rpc-url="https://testnet-rpc.monad.xyz"` to your mainnet RPC URL * Change `chain_id = 10143` to `143` </Note> If you are using [`foundry-monad`](https://github.com/monad-developers/foundry-monad) template, you can use the commands below based on your preferred block explorer: <Tabs> <Tab title="MonadVision"> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` On successful verification of smart contract, you should get a similar output in your terminal: ```sh theme={null} Start verifying contract `0x8fEc29BdEd7A618ab6E3CD945456A79163995769` deployed on monad-mainnet Attempting to verify on Sourcify. Pass the --etherscan-api-key <API_KEY> to verify on Etherscan, or use the --verifier flag to verify on another provider. Submitting verification for [Counter] "0x8fEc29BdEd7A618ab6E3CD945456A79163995769". Contract successfully verified ``` Now check the contract on MonadVision. </Tab> <Tab title="Monadscan"> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 143 \ --verifier etherscan \ --etherscan-api-key YourApiKeyToken \ --watch ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 143 \ --verifier etherscan \ --etherscan-api-key YourApiKeyToken \ --watch ``` On successful verification of smart contract, you should get a similar output in your terminal: ```sh theme={null} Start verifying contract `0x8fEc29BdEd7A618ab6E3CD945456A79163995769` deployed on monad-mainnet Submitting verification for [src/Counter.sol:Counter] 0x8fEc29BdEd7A618ab6E3CD945456A79163995769. Submitted contract for verification: Response: `OK` GUID: `fhxxx4wsub68jce24ejvhe68fqabgtpmpzheqpdqvencgph1za` URL: https://monadscan.com/address/0x8fec29bded7a618ab6e3cd945456a79163995769 Contract verification status: Response: `NOTOK` Details: `Pending in queue` Warning: Verification is still pending...; waiting 15 seconds before trying again (7 tries remaining) Contract verification status: Response: `OK` Details: `Pass - Verified` Contract successfully verified ``` Now check the contract on Monadscan. </Tab> </Tabs> </Tab> <Tab title="Default Foundry Project"> ## 1. Update `foundry.toml` with Monad Configuration ```toml lines title="foundry.toml" theme={null} [profile.default] src = "src" out = "out" libs = ["lib"] metadata = true metadata_hash = "none" # disable ipfs use_literal_content = true # use source code # Monad Configuration eth-rpc-url="https://rpc.monad.xyz" chain_id = 143 ``` ## 2. Verify the contract using one of the following block explorers: <Tabs> <Tab title="MonadVision"> <Note> If you are using MonadVision, you can use [this guide](https://docs.blockvision.org/reference/verify-smart-contract-on-monad-explorer#/). In particular, the [Verify Contract](https://monadvision.com/verify-contract) page provides a convenient way to verify your contract. </Note> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` On successful verification of smart contract, you should get a similar output in your terminal: ```sh theme={null} Start verifying contract `0x8fEc29BdEd7A618ab6E3CD945456A79163995769` deployed on monad-mainnet Attempting to verify on Sourcify. Pass the --etherscan-api-key <API_KEY> to verify on Etherscan, or use the --verifier flag to verify on another provider. Submitting verification for [Counter] "0x8fEc29BdEd7A618ab6E3CD945456A79163995769". Contract successfully verified ``` Now check the contract on MonadVision. </Tab> <Tab title="Monadscan"> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 143 \ --verifier etherscan \ --etherscan-api-key YourApiKeyToken \ --watch ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 143 \ --verifier etherscan \ --etherscan-api-key YourApiKeyToken \ --watch ``` On successful verification of smart contract, you should get a similar output in your terminal: ```sh theme={null} Start verifying contract `0x8fEc29BdEd7A618ab6E3CD945456A79163995769` deployed on monad-mainnet Submitting verification for [src/Counter.sol:Counter] 0x8fEc29BdEd7A618ab6E3CD945456A79163995769. Submitted contract for verification: Response: `OK` GUID: `fhxxx4wsub68jce24ejvhe68fqabgtpmpzheqpdqvencgph1za` URL: https://monadscan.com/address/0x8fec29bded7a618ab6e3cd945456a79163995769 Contract verification status: Response: `NOTOK` Details: `Pending in queue` Warning: Verification is still pending...; waiting 15 seconds before trying again (7 tries remaining) Contract verification status: Response: `OK` Details: `Pass - Verified` Contract successfully verified ``` Now check the contract on Monadscan. </Tab> </Tabs> </Tab> </Tabs> </Tab> <Tab title="Testnet"> <Tabs> <Tab title="Foundry Monad template (Recommended)"> If you are using [`foundry-monad`](https://github.com/monad-developers/foundry-monad) template, you can use the commands below based on your preferred block explorer: <Tabs> <Tab title="MonadVision"> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 10143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 10143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` On successful verification of smart contract, you should get a similar output in your terminal: ```sh theme={null} Start verifying contract `0x8fEc29BdEd7A618ab6E3CD945456A79163995769` deployed on monad-testnet Attempting to verify on Sourcify. Pass the --etherscan-api-key <API_KEY> to verify on Etherscan, or use the --verifier flag to verify on another provider. Submitting verification for [Counter] "0x8fEc29BdEd7A618ab6E3CD945456A79163995769". Contract successfully verified ``` Now check the contract on [MonadVision](https://testnet.monadvision.com/). </Tab> <Tab title="Monadscan"> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 10143 \ --verifier etherscan \ --etherscan-api-key YourApiKeyToken \ --watch ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 10143 \ --verifier etherscan \ --etherscan-api-key YourApiKeyToken \ --watch ``` On successful verification of smart contract, you should get a similar output in your terminal: ```sh theme={null} Start verifying contract `0x8fEc29BdEd7A618ab6E3CD945456A79163995769` deployed on monad-testnet Submitting verification for [src/Counter.sol:Counter] 0x8fEc29BdEd7A618ab6E3CD945456A79163995769. Submitted contract for verification: Response: `OK` GUID: `fhxxx4wsub68jce24ejvhe68fqabgtpmpzheqpdqvencgph1za` URL: https://testnet.monadscan.com/address/0x8fec29bded7a618ab6e3cd945456a79163995769 Contract verification status: Response: `NOTOK` Details: `Pending in queue` Warning: Verification is still pending...; waiting 15 seconds before trying again (7 tries remaining) Contract verification status: Response: `OK` Details: `Pass - Verified` Contract successfully verified ``` Now check the contract on [Monadscan](https://testnet.monadscan.com/). </Tab> </Tabs> </Tab> <Tab title="Default Foundry Project"> <Tip> If you use [`foundry-monad`](https://github.com/monad-developers/foundry-monad) you can skip the configuration step </Tip> ## 1. Update `foundry.toml` with Monad Configuration ```toml lines title="foundry.toml" theme={null} [profile.default] src = "src" out = "out" libs = ["lib"] metadata = true metadata_hash = "none" # disable ipfs use_literal_content = true # use source code # Monad Configuration eth-rpc-url="https://testnet-rpc.monad.xyz" chain_id = 10143 ``` ## 2. Verify the contract using one of the following block explorers: <Tabs> <Tab title="MonadVision"> ```sh theme={null} forge verify-contract \ <contract_address> \ <contract_name> \ --chain 10143 \ --verifier sourcify \ --verifier-url https://sourcify-api-monad.blockvision.org/ ``` **Example:** ```sh theme={null} forge verify-contract \ 0x8fEc29BdEd7A618ab6E3CD945456A79163995769 \ Counter \ --chain 10143 \ --verifier sourcify \ --verifier-url https://sou
프로젝트로 돌아가기