Monad 백서

Monad

원문

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

2. If an EOA is EIP-7702-delegated, when it is called as a smart contract, the `CREATE` and `CREATE2` opcodes are banned. [Discussion](/developer-essentials/eip-7702#delegated-contract-code-cannot-call-create/create2). ## Historical Data Due to Monad's high throughput, full nodes do not provide access to arbitrary historic state, as this would require too much storage. See [Historical Data](/developer-essentials/historical-data) for a fuller discussion. ## RPC See: [RPC Differences](/reference/rpc-differences) # EIP-7702 on Monad: How EOA Delegation Works Source: https://docs.monad.xyz/developer-essentials/eip-7702 EIP-7702 is supported on Monad with the same workflow as Ethereum. Learn how EOA delegation works, the Monad-specific details, and answers to common questions. ## Summary [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) is supported on Monad with the same workflow as in Ethereum: users sign a message authorizing the delegation to a specific account, which they or someone else can submit using transaction type `0x04`. After that occurs, the EOA becomes "delegated", i.e. it can be called like a smart contract account with code equal to the account it has delegated to. EIP-7702-delegated accounts behave the same on Monad as on Ethereum in most cases. The two main nuances are: 1. If an EOA is EIP-7702-delegated, transactions that would reduce its balance to below 10 MON will unconditionally revert. * If the balance is below 10 MON but is unchanged or increased by this transaction, the transaction succeeds * If the delegation is removed, dipping below 10 MON is allowed under certain conditions * [Details](#delegated-eoas-can%E2%80%99t-dip-below-10-mon) 2. When the EOA is treated like a smart contract, that code cannot call `CREATE` or `CREATE2`. [Details](#delegated-contract-code-cannot-call-create%2Fcreate2) ## EIP-7702 Primer [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) allows Externally Owned Accounts (EOAs) to add code to themselves, granting themselves the ability to add new capabilities previously reserved for smart contract accounts, such as transaction batching, gas sponsorship, and alternative authentication. To do this, the EOA signs an authorization designating a specific address as the source of its code. EIP-7702 introduces a new transaction type (type `0x04`) that submits this authorization. The authorization can be submitted by the EOA themselves, or by anyone else. EIP-7702 enables account abstraction directly on an EOA. It is an extension of EIP-4337, which introduced standards for smart contract wallets with flexible validation logic, but which had to assume a separate UserOp mempool and bundler infrastructure for submitting transactions. In Ethereum's account-based model, there are two separate roles - "fee payer" (transaction submitter, i.e. who pays for gas to execute a transaction) and "asset owner" (spend authorizer, i.e. who holds keys with the power to spend from a balance). Originally, EOAs played both roles. Under EIP-4337, the roles were split - a smart contract wallet holds title to assets (and has its own logic for validating that spend was authorized), but fees must still be paid by an EOA, hence the roles are definitively split. With EIP-7702, EOAs are allowed to have code, thus allowing the same account to play both roles again. In essence, EIP-7702 makes it possible for today's EOAs to gain smart wallet-like powers such as multisig, social recovery, session keys, and gas sponsorship **without abandoning their current accounts**, bridging the gap between the old EOA model and the future of full account abstraction. ## EIP-7702 on Monad EIP-7702-delegated accounts behave the same on Monad as on Ethereum in most cases. The two main nuances are: 1. If an EOA is EIP-7702-delegated, transactions that would reduce its balance to below 10 MON will unconditionally revert. * If the balance is below 10 MON but is unchanged or increased by this transaction, the transaction succeeds * If the delegation is removed, dipping below 10 MON is allowed under certain conditions * [Details](#delegated-eoas-can%E2%80%99t-dip-below-10-mon) 2. When the EOA is treated like a smart contract, that code cannot call `CREATE` or `CREATE2`. [Details](#delegated-contract-code-cannot-call-create%2Fcreate2) ### Delegated EOAs can't dip below 10 MON In Monad, the [Reserve Balance](/developer-essentials/reserve-balance) rules carve out a budget of 10 MON for consensus-time balance checks for inflight transactions from each EOA. ("Inflight" means transactions seen by consensus since the delayed view of state, 3 blocks ago.) Execution protects that budget by reverting if the balance of any EOA would *dip below* 10 MON by an amount greater than the transaction's max gas fee. (*"Dip below"* means *"decrement **and** drop below"*; if an EOA's MON balance is unchanged, that doesn't count as dipping.) * An [exception](/developer-essentials/reserve-balance#addressing-the-drawback) is made to this execution-time policy for **undelegated** EOAs where a transaction hasn't been seen from this EOA in several blocks. That exception is what allows undelegated EOAs to submit transactions that would cause their balance to dip below 10 MON by amounts greater than the transaction's max gas fee. * However, this exception can't be made for EIP-7702-delegated accounts. Delegation breaks the invariant that an EOA's balance can only be reduced by transactions signed by that EOA. There is not a reliable way to be sure (at the time of consensus) that another inflight transaction hasn't spent funds from an EIP-7702-delegated account, therefore the exception made for undelegated accounts doesn't apply to **delegated** accounts. An delegated account may be emptied by undelegating first. To be clear, EIP-7702-delegated accounts are **not** required to have a 10 MON balance. For example, a delegated EOA *A* with a balance of 5 MON can still be called by a gas sponsor, and the transaction will succeed as long as *A* ends with 5 MON or more still. This is discussed further [here](/developer-essentials/reserve-balance#eip-7702-delegated-accounts). ### Delegated contract code cannot call `CREATE/CREATE2` There is another difference: when contract code is executing in the context of an EIP‑7702‑delegated EOA (for example, because a contract `CALL`s that EOA’s address), the `CREATE` and `CREATE2` opcodes are not permitted. Any attempt to execute `CREATE` or `CREATE2` in such a frame causes that call frame to revert, and the caller (if any) observes the call as failed (the `CALL`/`DELEGATECALL`/`CALLCODE` returns 0). This prevents delegated code from changing the EOA’s nonce in ways that would make it difficult to statically validate transactions from that account. In contrast, normal contract‑creation transactions sent from a delegated EOA (transactions with no `to` field, where the transaction `data` is treated as init code) are allowed and behave as on Ethereum. Other than those differences, EIP-7702-delegated accounts behave normally with respect to both ordinary transactions sent by the EOA, and transactions that treat the EOA as a smart contract. ## FAQ <Accordion title="What does an EIP-7702 (set code transaction) look like?"> An EIP-7702 transaction is an [EIP-2718](https://eips.ethereum.org/EIPS/eip-2718) transaction with TransactionType `0x04`. This transaction type is only used to set code; once the code is set for an EOA, subsequent transactions will most likely use the default transaction type (TransactionType `0x02`; [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)) transactions to interact with the EOA. Here is an example of an EIP-7702 transaction in a block explorer: <img alt="explorer_example" /> </Accordion> <Accordion title="Is it necessary for the EIP-7702 type transaction to be initiated by the EOA itself?"> No, the EOA can sign an "authorization tuple" which can then be used by the sponsoring entity to send the transaction. This will allow EOAs to behave like smart contracts without any funds for gas! </Accordion> <Accordion title="How can I find out to which address EOA is currently delegating to?"> On successful delegation to a smart contract, the following code is deployed at the EOA's address. `0xef0100` (3 bytes) + `smart_contract_address` (20 bytes) Example: If `0xabc...` (EOA) delegates to `0x493...` (smart contract), then code at `0xabc...` (EOA) is `0xef0100493...` Thus, you may determine the delegated address by inspecting the code. </Accordion> <Accordion title="What if the EOA is pointing to another EOA which is also pointing to a smart contract?"> The chain of delegation is not followed; only the immediate code that the first EOA is pointing to is used. </Accordion> <Accordion title="How do I clear the delegation on an EOA?"> Initiate a `0x04` type transaction from the EOA with `0x000...` (dead address) as the new delegated account. </Accordion> <Accordion title="Does the delegation expire?"> No, the delegation remains valid in perpetuity unless another `0x04` transaction is sent changing the delegation to a different (or null) account. </Accordion> <Accordion title="Is EIP-7702 compatible with ERC-4337?"> Yes; after delegating with EIP-7702, any EOA may behave like an EIP-4337 smart account. Simply initiate a transaction of type `0x04` while pointing to an address containing code for the 4337-compatible smart account. </Accordion> <Accordion title="What happens if the EOA is pointing to a precompile address for code?"> If that's an Ethereum precompile, the `CALL`, `STATICCALL`, `DELEGATECALL`, and `CALLCODE` opcodes, when called with enough gas, proceed with execution as if the EOA has no code. If that's a Monad precompile, calls revert as if the EOA has code starting with an invalid instruction. </Accordion> <Accordion title="Can you give me an example of submitting an EIP-7702 transaction using viem?"> ```ts theme={null} import { createWalletClient, http, parseEther } from 'viem' import { monadTestnet } from 'viem/chains' import { privateKeyToAccount } from 'viem/accounts' const account = privateKeyToAccount('0x...') const walletClient = createWalletClient({ account, chain: monadTestnet, transport: http(), }) const authorization = await walletClient.signAuthorization({ account, contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' }) const hash = await walletClient.sendTransaction({ authorizationList: [authorization], data: '0xdeadbeef', to: walletClient.account.address, }) ``` </Accordion> # Gas Pricing Source: https://docs.monad.xyz/developer-essentials/gas-pricing An estimated USD cost for a typical Monad transaction, plus how gas fees are calculated. ## Summary Monad, like Ethereum, charges for processing transactions based on the complexity of the transaction. Complexity is measured in units of **gas**. This page summarizes how gas is charged, i.e. the conversion between the gas of a transaction and the amount of MON that a user will have to pay. A separate page, [Opcode Pricing](/developer-essentials/opcode-pricing), describes how much each opcode costs in units of gas. | Feature | Detail | | - | - | | **Gas charged** | The gas charged for a transaction is the **gas limit**. [Discussion](#gas-limit-not-gas-used) | | **Price per gas** | EIP-1559-compatible, i.e. price paid per unit of gas is the sum of a system-controlled base fee and a user-specified priority fee. [Discussion](#eip-1559-compatibility) | | **Base fee** | Base fee (aka `base_price_per_gas`) follows a dynamic controller, similar to the EIP-1559 controller but with slower increases and faster decreases. [Details](#base_price_per_gas-controller) | | **Minimum base fee** | 100 MON-gwei (`100 * 10^-9 MON`) | | **Typical cost** | A typical 200,000-gas transaction costs about **\$0.0005** at the minimum base fee (≈ 0.02 MON). [Worked example](#how-much-does-a-transaction-cost) | | **Block gas limit** | 150M gas | | **Transaction gas limit** | 30M gas | | **Opcode pricing** | See [Opcode Pricing](/developer-essentials/opcode-pricing) | | **Transaction ordering** | Default Monad client behavior is to order transactions according to a Priority Gas Auction (descending total gas price). | <Note> These changes are covered formally in the [Monad Initial Spec Proposal](https://category-labs.github.io/category-research/monad-initial-spec-proposal.pdf) </Note> ## How much does a transaction cost? A typical 200,000-gas transaction on Monad costs about **\$0.0005** at the minimum base fee of 100 MON-gwei, and a simple MON transfer (21,000 gas) costs about **\$0.00005**, a fraction of a cent. Fees are paid in MON, so these dollar amounts are approximate; the MON cost is exact. The cost of a transaction is the gas it is charged multiplied by the price paid per unit of gas. At the minimum base fee of 100 MON-gwei and no priority fee, a 200,000-gas transaction costs 0.02 MON, or about \$0.0005: $$ \text{fee} = \text{gas limit} \times \text{price per gas} = 200{,}000 \times \left(100 \times 10^{-9}\ \text{MON}\right) = 0.02\ \text{MON} $$ 200,000 gas is a common reference size for a swap or comparable contract call; a transaction's actual gas depends on what it does. The table below lists representative sizes. | Transaction | Gas limit | Cost (MON) | Cost (USD, approx.) | | - | - | - | - | | Native MON transfer | 21,000 | 0.0021 MON | \~\$0.00005 | | ERC-20 token transfer | \~65,000 | \~0.0065 MON | \~\$0.00016 | | Typical DEX swap | \~200,000 | \~0.02 MON | \~\$0.0005 | <Note> Monad charges the **gas limit** you set, not the gas actually used ([details](#gas-limit-not-gas-used)), so these amounts can't rise after execution the way a gas-used estimate can. They are the most you would pay while the base fee is at its floor. Under sustained network load the base fee can rise above 100 MON-gwei (see [base fee controller](#base_price_per_gas-controller)). A native MON transfer always uses exactly 21,000 gas; gas for token transfers and swaps varies by contract. </Note> ## Gas definitions A common point of confusion among users is the distinction between **gas** of a transaction (units of work) and the **gas price** of a transaction (price in native tokens per unit of work). | Feature | Definition | | - | - | | **Gas** | A unit of work. Gas measures the amount of work the network has to do to process something.<br /><br /> Since the network has multiple kinds of resources (network bandwidth, CPU, SSD bandwidth, and state growth), gas is inherently a projection from many dimensions into a single one. | | **Gas price (price\_per\_gas)** | The **price** (in native tokens) paid **to process one unit** of gas. | | **Gas limit** | The maximum **number of units of gas** that a transaction is allowed to consume. | ## Gas limit, not gas used In Monad, the gas charged for a transaction is the gas limit set in the transaction, rather than the gas used in the course of execution. This is a design decision to support asynchronous execution. Under asynchronous execution, leaders build blocks (and validators vote on block validity) prior to executing. If the protocol charged `gas_used`, a user could submit a transaction with a large `gas_limit` that actually consumes very little gas. This transaction would take up a lot of space toward the block gas limit but wouldn't pay very much for taking up that space, opening up a DOS vector. ``` gas_paid = gas_limit * price_per_gas ``` ## EIP-1559 Compatibility Monad supports EIP-1559. EIP-1559 (type 2) transactions have the parameters `priority_price_per_gas` and `max_price_per_gas`, which, together with `base_price_per_gas` (a system parameter that changes each block), determine the gas bid for the transaction: ``` price_per_gas = min(base_price_per_gas + priority_price_per_gas, max_price_per_gas) ``` Notes: * `base_price_per_gas` is a system parameter that changes each block. Every transaction in the same block will have the same `base_price_per_gas` * Users specify `priority_price_per_gas` and `max_price_per_gas` when signing a transaction * Since everyone in the same block will pay the same `base_price_per_gas`, the `priority_price_per_gas` is a way for users to pay more to prioritize their transactions. * Since users don't determine `base_price_per_gas`, the `max_price_per_gas` is a safeguard that limits the amount they may end up paying. Of course, if that value is set too low, the transaction will not end up being chosen for inclusion. [This](https://www.blocknative.com/blog/eip-1559-fees) article provides another good explanation of EIP-1559 gas pricing. ## `base_price_per_gas` controller Monad uses a different controller for `base_price_per_gas` than Ethereum: $$ \begin{align*} \mathrm{block\_gas}_{k} &= \sum\limits_{\mathrm{tx} \in \mathrm{block}_k}\mathrm{gas\_limit}_\mathrm{tx}\\ \mathrm{base\_price\_per\_gas}_{k+1} &= \max\left\{\text{min\_base\_price\_per\_gas}, \mathrm{base\_price\_per\_gas}_k \cdot \exp \left( \eta_k \cdot \frac{\mathrm{block\_gas}_{k} - \text{target}}{\text{block\_gas\_limit} - \text{target}} \right) \right\} \\ \eta_k &= \frac{\text{max\_step\_size}\cdot\epsilon}{\epsilon+\sqrt{\mathrm{moment}_k - \mathrm{trend}_k^2}} \\ \mathrm{trend}_{k+1} &= \beta\cdot \mathrm{trend}_k + (1-\beta)\cdot \left(\text{target}-\mathrm{block\_gas}_{k} \right) \\ \mathrm{moment}_{k+1} &= \beta\cdot \mathrm{moment}_k + (1-\beta)\cdot \left(\text{target}-\mathrm{block\_gas}_{k} \right)^2 \end{align*} $$ This inductive formula starts with $$ \begin{align*} \mathrm{base\_price\_per\_gas}_{0} &= 0 \\ \mathrm{moment}_{0} &= 0 \\ \mathrm{trend}_{0} &= 0 \end{align*} $$ and with the following parameters: $$ \begin{align*} \mathrm{max\_step\_size} &= 1/28 \\ \mathrm{target} &= 160\text{M}\ \text{(80\% full)}\ \\ \beta &= 0.96 \\ \epsilon &= \mathrm{target} = 160\text{M} \end{align*} $$ And $$ \text{min\_base\_price\_per\_gas} = 100\ \text{MON-gwei}\ (100 \times 10^{-9}\ \text{MON}) $$ Compared to the `base_price_per_gas` controller in Ethereum, this controller increases more slowly and decreases more quickly. This is to avoid underutilization of blockspace due to an overpriced `base_price_per_gas`. For a more comprehensive discussion of Monad controller design considerations and behavior, check out [this](https://www.category.xyz/blogs/redesigning-a-base-fee-for-monad) blog post from Category Labs. ## Recommendations for developers ### Set the gas limit explicitly if it is constant Many on-chain actions have a fixed gas cost. The simplest example is that a transfer of native tokens always costs 21,000 gas, but there are many others. For actions where the gas cost of the transaction is known ahead of time, it is recommended to set it directly prior to handing the transaction off to the wallet. This offers several benefits: * It reduces latency and gives users a better experience, since the wallet doesn't have to call `eth_estimateGas` and wait for the RPC to respond. * It retains greater control over the user experience, avoiding cases where the wallet sets a high gas limit in a corner case as described in the warning below. <Warning> Some wallets, including MetaMask, are known to have the following behavior: when `eth_estimateGas` is called and the contract call reverts, they set the gas limit for this transaction to a very high value. This is the wallet's way of giving up on setting the gas limit and accepting whatever gas usage is at execution time. However, it doesn't make sense on Monad where the full gas limit is charged. Contract call reversion happens whenever the user is trying to do something impossible. For example, a user might be trying to mint an NFT that has minted out. If the gas limit is known ahead of time, setting it explicitly is best practice, since it ensures the wallet won't handle this case unexpect
프로젝트로 돌아가기