Optimism 백서

Optimism

원문

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

arget).call{gas: _minGasLimit}(_message); ``` Except, of course, that you're calling a contract on a completely different network. This is glossing over a lot of the technical details that make this whole thing work under the hood, but this should be enough to get you started. Want to call a contract on OP Mainnet from a contract on Ethereum? It's dead simple: ```solidity theme={null} // Pretend this is on L2 contract MyOptimisticContract { function doSomething(uint256 myFunctionParam) public { // ... some sort of code goes here } } // And pretend this is on L1 contract MyContract { function doTheThing(address myOptimisticContractAddress, uint256 myFunctionParam) public { messenger.sendMessage( myOptimisticContractAddress, abi.encodeCall( MyOptimisticContract.doSomething, ( myFunctionParam ) ), 1000000 // or use whatever gas limit you want ) } } ``` <Info> You can find the addresses of the `L1CrossDomainMessenger` and the `L2CrossDomainMessenger` contracts on OP Mainnet and OP Sepolia on the [Contract Addresses](/op-mainnet/network-information/op-addresses) page. </Info> ## Communication speed Unlike calls between contracts on the same blockchain, calls between Ethereum and OP Mainnet are *not* instantaneous. Transactions sent from L1 to L2 take **approximately 1-3 minutes**, because the Sequencer waits for a certain number of L1 blocks to be created before including L1 to L2 transactions to avoid potentially annoying [reorgs](https://www.alchemy.com/overviews/what-is-a-reorg). Transactions sent from L2 to L1 take **approximately 7 days**: the message must be initiated on L2, proven on L1 against an output root, and finalized on L1 only after the [challenge period](#understanding-the-challenge-period) (7 days on mainnet, shorter on test networks) has elapsed. This waiting period is a core part of the security model of the OP Stack and cannot be circumvented. For the step-by-step mechanics in each direction, see [Deposit flow](/op-stack/bridging/deposit-flow) and [Withdrawal flow](/op-stack/bridging/withdrawal-flow). ## Accessing `msg.sender` Contracts frequently make use of `msg.sender` to make decisions based on the calling address. For example, many contracts will use the [Ownable](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol) pattern to selectively restrict access to certain functions. Because messages are essentially shuttled between L1 and L2 by the messenger contracts, **the `msg.sender` you'll see when receiving one of these messages will be the messenger contract** corresponding to the layer you're on. In order to get around this, you can find a `xDomainMessageSender` function to each messenger: ```solidity theme={null} function xDomainMessageSender() public returns (address); ``` If your contract has been called by one of the messenger contracts, you can use this function to see who's *actually* sending this message. Here's how you might implement an `onlyOwner` modifier on L2: ```solidity theme={null} modifier onlyOwner() { require( msg.sender == address(messenger) && messenger.xDomainMessageSender() == owner ); _; } ``` ## Fees for sending data between L1 and L2 ### For L1 to L2 transactions The majority of the cost of an L1 to L2 transaction comes from the smart contract execution on L1. When sending an L1 to L2 transaction, you send to the [`L1CrossDomainMessenger`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L1/L1CrossDomainMessenger.sol) contract, which then sends a call to the [`OptimismPortal`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L1/OptimismPortal2.sol) contract. This involves some execution on L1, which costs gas. The total cost is ultimately determined by gas prices on Ethereum when you're sending the cross-chain transaction. L1 to L2 execution also triggers contract execution on L2. The `OptimismPortal` contract charges you for this L2 execution by burning a dynamic amount of L1 gas during your L1 to L2 transaction, depending on the gas limit you requested on L2. The amount of L1 gas charged increases when more people are sending L1 to L2 transactions (and decreases when fewer people are sending L1 to L2 transactions). <Info> Since the gas amount charged is dynamic, the gas burn can change from block to block. You should always add a buffer of at least 20% to the gas limit for your L1 to L2 transaction to avoid running out of gas. </Info> ### For L2 to L1 transactions Each message from L2 to L1 requires three transactions: 1. An L2 transaction that *initiates* the transaction, which is priced the same as any other transaction made on OP Mainnet. 2. An L1 transaction that *proves* the transaction. This transaction can only be submitted after the L2 block, including your L2 transaction, is proposed on L1. This transaction is expensive because it includes verifying a [Merkle trie](/connect/resources/glossary#merkle-patricia-trie) inclusion proof on L1. 3. An L1 transaction that *finalizes* the transaction. This transaction can only be submitted after the transaction challenge period (7 days on mainnet) has passed. The total cost of an L2 to L1 transaction is therefore the combined cost of the L2 initialization transaction and the two L1 transactions. The L1 proof and finalization transactions are typically significantly more expensive than the L2 initialization transaction. ## Understanding the challenge period One of the most important things to understand about L1 ⇔ L2 interaction is that **mainnet messages sent from Layer 2 to Layer 1 cannot be relayed for at least 7 days**. This period of time is called the "challenge period" because it is the window during which a published transaction result can be challenged with a [fault proof](/op-stack/fault-proofs/explainer): Optimistic Rollups publish transaction *results* to Ethereum without executing the transactions there, so L1 contracts must give challengers time to prove a published result faulty before acting on it. The practical consequence for app developers is that **you don't want to be making decisions about Layer 2 transaction results from inside a smart contract on Layer 1 until this challenge period has elapsed**, and L2 ⇒ L1 messages sent using the standard messenger contracts cannot be relayed until they've waited out the full challenge period. For how the challenge period fits into the withdrawal process, see [Withdrawal flow](/op-stack/bridging/withdrawal-flow); for why it exists, see the [fault proofs explainer](/op-stack/fault-proofs/explainer). # Using the Standard Bridge Source: https://docs.optimism.io/app-developers/guides/bridging/standard-bridge Learn how the Standard Bridge moves ETH and ERC-20 tokens between Layer 1 and Layer 2. The Standard Bridge is a basic token bridging system available on OP Mainnet and all other standard OP Stack chains. The Standard Bridge allows you to easily move ETH and most ERC-20 tokens between Ethereum and OP Mainnet. Transfers from Ethereum to OP Mainnet via the Standard Bridge are usually completed within 1-3 minutes. Transfers from OP Mainnet to Ethereum are completed in 7 days as a result of the [withdrawal challenge period](./messaging#understanding-the-challenge-period). The Standard Bridge is fully permissionless and supports standard ERC-20 tokens. Other bridging systems also exist that provide different features and security properties. You may wish to explore some of these options to find the bridge that works best for you and your application. <Info> The Standard Bridge **does not** support [**fee on transfer tokens**](https://github.com/d-xo/weird-erc20#fee-on-transfer) or [**rebasing tokens**](https://github.com/d-xo/weird-erc20#balance-modifications-outside-of-transfers-rebasingairdrops) because they can cause bridge accounting errors. </Info> ## Design The Standard Bridge allows users to convert tokens that are native to one chain (like Ethereum) into a representation of those tokens on the other chain (like OP Mainnet). Users can then convert these bridged representations back into their original native tokens at any time. This bridging mechanism functions identically in both directions — tokens native to OP Mainnet can be bridged to Ethereum, just like tokens native to Ethereum can be bridged to OP Mainnet. Here you'll get to understand how the Standard Bridge works when moving tokens from Ethereum to OP Mainnet. Since the bridging mechanism is mirrored on both sides, this will also explain how the bridge works in the opposite direction. ### Architecture The Standard Bridge is composed of two contracts, the [`L1StandardBridge`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L1/L1StandardBridge.sol) (on `Ethereum`) and the [`L2StandardBridge`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2StandardBridge.sol) (on `OP Mainnet`). These two contracts interact with one another via the `CrossDomainMessenger` system for sending messages between Ethereum and OP Mainnet. You can read more about the `CrossDomainMessenger` in the guide on [Sending Data Between L1 and L2](./messaging). ### Bridged tokens The Standard Bridge utilizes bridged representations of tokens that are native to another blockchain. Before a token native to one chain can be bridged to the other chain, a bridged representation of that token must be created on the receiving side. A bridged representation of a token is an ERC-20 token that implements the [`IOptimismMintableERC20`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/interfaces/universal/IOptimismMintableERC20.sol#L10-L18) interface. This interface includes a few functions that the `StandardBridge` contracts use to manage the bridging process. All bridged versions of tokens **must** implement this interface to be used with the `StandardBridge`. Native tokens do not need to implement this interface. A native token may have more than one bridged representation at the same time. Users must always specify which bridged token they wish to use when using the bridge. Different bridged representations of the same native token are considered entirely independent tokens. ### Bridging native tokens The Standard Bridge uses a "lock-and-mint" mechanism to convert native tokens into their bridged representations. This means that **native tokens are locked** into the Standard Bridge on one side, after which **bridged tokens are minted** on the other side. The process for bridging a native token involves a few steps. <Steps> <Step title="User gives the Standard Bridge an allowance"> The Standard Bridge must be able to pull tokens from the user to lock them into the bridge contract. To do this, the user must first give the bridge an [allowance](https://eips.ethereum.org/EIPS/eip-20#approve) to transfer the number of tokens that the user wishes to convert into a bridged representation. </Step> <Step title="User calls the bridging function"> After providing a sufficient allowance, the user calls the [`bridgeERC20To`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L217-L229) function on the `StandardBridge` contract on the chain where the native token lives (e.g., the `L1StandardBridge` contract if the token is native to Ethereum). The user must provide the following parameters to this function call: * `address _localToken`: Address of the native token on the sending side. * `address _remoteToken`: Address of the bridged representation on the receiving side. * `address _to`: Address of the recipient of these tokens, usually the sender's address. * `uint256 _amount`: Number of tokens to transfer. * `uint32 _minGasLimit`: Gas to use to complete the transfer on the receiving side. * `bytes calldata _extraData`: Optional identity extra data. <Info> Users can also trigger the [`bridgeERC20`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L194-L206) function instead of `bridgeERC20To` to avoid needing to specify the `address _to` parameter. Doing so will automatically set the `address _to` parameter to the `msg.sender`. **The `bridgeERC20` function can be potentially dangerous for users with [smart contract wallets](https://web.archive.org/web/20231012141406/https://blockworks.co/news/what-are-smart-contract-wallets) as some smart contract wallets cannot be deployed at the same address on every blockchain.** To help users avoid potentially losing access to tokens by accident, the `bridgeERC20` function will always revert when triggered from a smart contract. Smart contract wallet users and other smart contracts should therefore use the `bridgeERC20To` function instead. </Info> </Step> <Step title="The Standard Bridge locks the transferred tokens"> When the user triggers the `bridgeERC20To` function while transferring a native token, the Standard Bridge will pull the `_amount` of `_localToken` tokens from the user's address and lock them inside of the bridge contract. A record of all locked tokens is stored within a [`deposits` mapping](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L41) that keeps track of the total number of tokens deposited for a given `_localToken` and `_remoteToken` pair. Since a native token may have more than one bridged representation, the `deposits` token must keep track of the deposit pools for each `_localToken`/`_remoteToken` pair independently. To illustrate, suppose that two users deposit 100 units of the same native token, `Token A`, but wish to receive two different bridged tokens, `Token B` and `Token C`. Although the Standard Bridge would now have a total balance of 200 units of `Token A`, the mapping would show that the `Token A`/`Token B` pool and the `Token A`/`Token C` pool both have only 100 units. </Step> <Step title="The Standard Bridge sends a minting message"> After locking the native tokens, the Standard Bridge contract on the sending side will trigger a cross-chain message to the Standard Bridge contract on the receiving side via the [`CrossDomainMessenger`](./messaging) system. This message tells the receiving side to **mint** tokens according to the parameters specified by the user. Specifically, this message is an encoded call to the [`finalizeBridgeERC20`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L272-L299) function on the other Standard Bridge contract. At this point, execution ends on the sending side. </Step> <Step title="The minting message is executed"> Once the minting message is sent, it must be relayed to the receiving side. Message relaying is automatic when sending from Ethereum to OP Mainnet but requires additional user transactions when sending from OP Mainnet to Ethereum. Read more about the message relaying process in the guide to [Sending Data Between L1 and L2](./messaging#communication-speed). When the message is relayed, the `finalizeBridgeERC20` function will be triggered on the receiving Standard Bridge contract. This function will receive the `_minGasLimit` gas defined by the user to execute to completion. </Step> <Step title="The minting message is authenticated"> Upon execution, `finalizeBridgeERC20` verifies a number of things about the incoming request: * [The request must have originated from the Standard Bridge contract on the other blockchain](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L281). * [The Standard Bridge must not be in an emergency paused state](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L283). * [The bridged token must properly implement the `IOptimismMintableERC20` interface](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L284). * [The bridged token must recognize the original native token as its `remoteToken()`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L285-L288). </Step> <Step title="The bridged token is minted"> If the minting message is fully verified, `finalizeBridgeERC20` will [mint tokens to the recipient](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L290) equal to the number of tokens originally deposited on the other blockchain. For this to work properly, the bridged representation of the native token must correctly implement a `mint` function that allows the Standard Bridge to mint tokens arbitrarily. This is part of the [`IOptimismMintableERC20`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/interfaces/universal/IOptimismMintableERC20.sol) interface. This completes the process of bridging native tokens. This process is identical in both the Ethereum to OP Mainnet and OP Mainnet to Ethereum directions. </Step> </Steps> ### Bridging non-native tokens The Standard Bridge uses a "burn-and-unlock" mechanism to convert bridged representations of tokens back into their native tokens. This means that **bridged tokens are burned** on the Standard Bridge on one side, after which **native tokens are unlocked** on the other side. The process for bridging a non-native, bridged representation of a token involves a few steps. <Steps> <Step title="User calls the bridging function"> Unlike when bridging native tokens, users do not need to provide an approval to trigger a transfer of a bridged token because the Standard Bridge should already have the ability to `burn` these tokens. Here, the user calls the [`bridgeERC20To`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L217-L229) function on the `StandardBridge` contract on the chain where the bridged token lives (e.g., the `L2StandardBridge` contract if the token is bridged to OP Mainnet). The user must provide the following parameters to this function call: * `address _localToken`: Address of the bridged token on the sending side. * `address _remoteToken`: Address of the native token on the receiving side. * `address _to`: Address of the recipient of these tokens, usually the sender's address. * `uint256 _amount`: Number of tokens to transfer. * `uint32 _minGasLimit`: Gas to use to complete the transfer on the receiving side. * `bytes calldata _extraData`: Optional identity extra data. </Step> <Step title="The Standard Bridge burns the transferred tokens"> When the user triggers the `bridgeERC20To` function while transferring a bridge token, [the Standard Bridge will burn the corresponding `_amount` of tokens from the sender's address](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L359). </Step> <Step title="The Standard Bridge sends an unlock message"> After burning the bridged tokens, the Standard Bridge contract on the sending side will trigger a cross-chain message to the Standard Bridge contract on the receiving side via the [`CrossDomainMessenger`](./messaging) system. This message tells the receiving side to **unlock** tokens according to the parameters specified by the user. Specifically, this message is an encoded call to the [`finalizeBridgeERC20`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/universal/StandardBridge.sol#L272-L299) function on the other Standard Bridge contract. At this point, execution ends on the sendi
프로젝트로 돌아가기