bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 19쪽
elow.
* A terminal with `curl` available (preinstalled on macOS and most Linux distributions).
## Install Foundry
Foundry is a toolkit for Ethereum development.
This tutorial uses two of its command-line tools: `forge` (to compile and deploy) and `cast` (to send transactions and read state).
<Steps>
<Step title="Install foundryup">
```bash theme={null}
curl -L https://foundry.paradigm.xyz | bash
```
This installs `foundryup`, Foundry's version manager.
Follow the on-screen instructions to add it to your `PATH` (you may need to open a new terminal).
</Step>
<Step title="Install the Foundry toolchain">
```bash theme={null}
foundryup
```
</Step>
</Steps>
### Verify the install
Confirm `forge` and `cast` are available:
```bash theme={null}
forge --version
cast --version
```
Each command should print a version string.
If the command isn't found, revisit the `PATH` instructions from `foundryup` and open a new terminal.
## Create a project and contract
<Steps>
<Step title="Initialize a Foundry project">
```bash theme={null}
mkdir first-contract
cd first-contract
forge init
```
`forge init` scaffolds a new project with `src/`, `test/`, and `script/` directories.
</Step>
<Step title="Add the Greeter contract">
Replace the contents of `src/Greeter.sol` with the following.
This is a variation on [Hardhat's Greeter contract](https://github.com/matter-labs/hardhat-zksync/blob/main/examples/upgradable-example/contracts/Greeter.sol): it stores a greeting string, exposes it through `greet()`, and lets anyone update it through `setGreeting()`.
```solidity theme={null}
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Greeter {
string greeting;
event SetGreeting(
address indexed sender, // msg.sender
string greeting
);
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
emit SetGreeting(msg.sender, _greeting);
}
}
```
</Step>
<Step title="Compile the contract">
```bash theme={null}
forge build
```
</Step>
</Steps>
### Verify the build
`forge build` should report a successful compilation and write artifacts to the `out/` directory.
If compilation fails, check that `src/Greeter.sol` matches the code above exactly.
## Configure OP Sepolia and your account
You need two things to deploy: an RPC endpoint for OP Sepolia and a private key to sign the deployment transaction.
<Steps>
<Step title="Create a deployment account">
Create a fresh key for this tutorial rather than reusing a key that holds real funds.
```bash theme={null}
cast wallet new
```
This prints an `Address` and a `Private key`.
Save both somewhere safe.
</Step>
<Step title="Set your environment variables">
Export the OP Sepolia RPC URL and the private key you just created.
These variables are read by the `forge` and `cast` commands in the rest of the tutorial.
```bash theme={null}
export L2_RPC_URL=https://sepolia.optimism.io
export PRIVATE_KEY=0x...your-private-key...
export ACCOUNT_ADDRESS=$(cast wallet address --private-key $PRIVATE_KEY)
```
</Step>
</Steps>
<Info>
`https://sepolia.optimism.io` is a public, rate-limited endpoint suited to development and testing.
For a full list of endpoints and production providers, see the [OP Stack RPC directory](/app-developers/reference/rpc-providers).
For OP Sepolia's chain ID (`11155420`) and other network parameters, see [Connecting to OP Mainnet](/op-mainnet/network-information/connecting-to-op#op-sepolia).
</Info>
## Fund your account
Deploying a contract costs gas, so your account needs testnet ETH on OP Sepolia.
<Steps>
<Step title="Request testnet ETH">
Use the [Superchain Faucet](https://console.optimism.io/faucet?utm_source=op-docs\&utm_medium=docs) to send OP Sepolia ETH to your `ACCOUNT_ADDRESS`.
</Step>
</Steps>
### Verify your balance
Check that the faucet funds have arrived before deploying:
```bash theme={null}
cast balance --ether $ACCOUNT_ADDRESS --rpc-url $L2_RPC_URL
```
The command prints your balance in ETH.
Wait until it's greater than `0` before continuing.
## Deploy the contract
Deploy `Greeter` to OP Sepolia and capture the resulting contract address.
```bash theme={null}
CONTRACT_ADDRESS=$(forge create \
--rpc-url $L2_RPC_URL \
--private-key $PRIVATE_KEY \
Greeter \
--broadcast \
| awk '/Deployed to:/ {print $3}')
echo "Deployed to: $CONTRACT_ADDRESS"
```
The `forge create` command compiles (if needed), signs, and broadcasts the deployment transaction.
Its output includes a `Deployed to:` line; the `awk` command extracts that address into the `CONTRACT_ADDRESS` variable so you can reuse it in the next step.
<Info>
Run `forge create` on its own (without the `awk` pipe) if you want to see the full output — the deployer address, the new contract address, and the transaction hash.
</Info>
### Verify the deployment
Confirm the contract exists on-chain by fetching its bytecode:
```bash theme={null}
cast code $CONTRACT_ADDRESS --rpc-url $L2_RPC_URL
```
A deployed contract returns a long hex string.
If it returns `0x`, the deployment didn't land — re-check your balance and rerun the deploy step.
## Interact with the contract
Now read from and write to your live contract using `cast`.
<Steps>
<Step title="Read the initial greeting">
```bash theme={null}
cast call --rpc-url $L2_RPC_URL $CONTRACT_ADDRESS "greet()" | cast --to-ascii
```
The greeting starts empty, so this returns an empty string.
</Step>
<Step title="Set a new greeting">
This sends a transaction that calls `setGreeting()`:
```bash theme={null}
cast send \
--private-key $PRIVATE_KEY \
--rpc-url $L2_RPC_URL \
$CONTRACT_ADDRESS \
"setGreeting(string)" "Hello from OP Sepolia"
```
</Step>
<Step title="Read the greeting again">
```bash theme={null}
cast call --rpc-url $L2_RPC_URL $CONTRACT_ADDRESS "greet()" | cast --to-ascii
```
This now returns `Hello from OP Sepolia`, confirming your write landed on-chain.
</Step>
</Steps>
## View your contract on the block explorer
Open an [OP Sepolia block explorer](/app-developers/tools-sdks/block-explorers) and search for your `CONTRACT_ADDRESS` to see the deployment transaction and the `setGreeting` call you just sent.
Publishing (verifying) your contract's source code on the explorer is optional but recommended, because it lets anyone read and interact with the contract from the explorer UI.
## Next steps
* Learn the broader conventions in [Building apps on OP Stack chains](/app-developers/guides/building-apps).
* Understand [the differences between Ethereum and OP Stack chains](/op-stack/protocol/differences).
* Try a cross-chain tutorial next, such as [bridging ERC-20 tokens](/app-developers/tutorials/bridging/cross-dom-bridge-erc20).
<Info>
**Running your app in production**
A production application depends on infrastructure your team does not run: RPC endpoints that hold up under real traffic (the public endpoints are rate-limited and not built for production), bridges your users rely on, and a chain whose operator keeps sequencing, upgrades, and incident response going around the clock. These docs cover building and testing. If your application is growing toward dedicated blockspace of its own, [OP Enterprise](https://optimism.io/op-enterprise?utm_source=docs\&utm_medium=docs\&utm_campaign=op-enterprise) offers managed and supported paths to running a chain. These docs stay the reference for what you build either way. OP Enterprise is Optimism's managed offering.
</Info>
# First steps
Source: https://docs.optimism.io/app-developers/tutorials/development/supersim/first-steps
Take your first steps with Supersim.
`supersim` allows testing multichain features **locally**. Previously, testing multichain features required complex docker setups or using a testnet. To see it in practice, this tutorial walks you through sending some ETH from the L1 to the L2.
## Deposit ETH from the L1 into the L2 (L1 to L2 message passing)
<Steps>
<Step title="Check initial balance on the L2 (chain 901)">
Grab the balance of the sender account on L2:
```sh theme={null}
cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9545
```
</Step>
<Step title="Send the Ether">
You can use two different methods to complete this action: `OptimismPortal` or `L1StandardBridge`.
#### First method: `OptimismPortal`
* Send the Ether to `OptimismPortal` contract of the respective L2 (on chain 900)
<Info>
For chain 901, the contract is `0x37a418800d0c812A9dE83Bc80e993A6b76511B57`.
</Info>
* Initiate a bridge transaction on the L1:
```sh theme={null}
cast send 0x37a418800d0c812A9dE83Bc80e993A6b76511B57 --value 0.1ether --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
```
#### Second method: `L1StandardBridge`
* Call `bridgeETH` function on the `L1StandardBridgeProxy` / `L1StandardBridge` contract of the respective L2 on L1 (chain 900)
<Info>
For chain 901, the contract is `0x8d515eb0e5F293B16B6bBCA8275c060bAe0056B0`.
</Info>
* Initiate a bridge transaction on the L1:
```sh theme={null}
cast send 0x8d515eb0e5F293B16B6bBCA8275c060bAe0056B0 "bridgeETH(uint32 _minGasLimit, bytes calldata _extraData)" 50000 0x --value 0.1ether --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
```
</Step>
<Step title="Check the balance on the L2 (chain 901)">
Verify that the ETH balance of the sender has increased on the L2:
```sh theme={null}
cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9545
```
</Step>
</Steps>
## Send an interoperable ERC20 token from chain 901 to 902 (L2 to L2 message passing)
In a typical L2 to L2 cross-chain transfer, two transactions are required:
1. Send transaction on the source chain – This initiates the token transfer on Chain 901.
2. Relay message transaction on the destination chain – This relays the transfer details to Chain 902.
To simplify this process, you can use the `--interop.autorelay` flag. This flag automatically triggers the relay message transaction once the initial send transaction is completed on the source chain, improving the developer experience by removing the need to manually send the relay message.
<Steps>
<Step title="Start `supersim` with the autorelayer enabled">
```sh theme={null}
supersim --interop.autorelay
```
</Step>
<Step title="Mint tokens to transfer on chain 901">
Run the following command to mint 1000 test ERC20 tokens to the recipient address:
```sh theme={null}
cast send 0x420beeF000000000000000000000000000000001 "mint(address _to, uint256 _amount)" 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 1000 --rpc-url http://127.0.0.1:9545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
```
</Step>
<Step title="Initiate the send transaction on chain 901">
Send the tokens from Chain 901 to Chain 902 using the following command:
```sh theme={null}
cast send 0x4200000000000000000000000000000000000028 "sendERC20(address _token, address _to, uint256 _amount, uint256 _chainId)" 0x420beeF000000000000000000000000000000001 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 1000 902 --rpc-url http://127.0.0.1:9545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
```
</Step>
<Step title="Wait for the relayed message to appear on chain 902">
In a few seconds, you should see the RelayedMessage on chain 902:
```sh theme={null}
# example
INFO [08-30|14:30:14.698] SuperchainTokenBridge#RelayERC20 token=0x420beeF000000000000000000000000000000001 from=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 to=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 amount=1000 source=901
```
</Step>
<Step title="Check the balance on chain 902">
Verify that the balance of the test ERC20 on chain 902 has increased:
```sh theme={null}
cast balance --erc20 0x420beeF000000000000000000000000000000001 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9546
```
</Step>
</Steps>
With the steps above, you've now successfully completed both an L1 to L2 ETH bridge and an L2 to L2 interoperable ERC20 token transfer, all done locally using `supersim`. This approach simplifies multichain testing, allowing you to focus on development without the need for complex setups or relying on external testnets.
## Next steps
* Learn how to start Supersim in [vanilla (non-forked) mode](/app-developers/reference/tools/supersim/vanilla) or [forked mode](/app-developers/reference/tools/supersim/fork).
* Explore the Supersim [included contracts](/app-developers/reference/tools/supersim/included-contracts) being used to help replicate the OP Stack environment.
# Install Supersim
Source: https://docs.optimism.io/app-developers/tutorials/development/supersim/installation
Learn how to install Supersim and start it in vanilla mode.
This page provides installation instructions for `supersim`.
<Steps>
<Step title="Prerequisites: foundry">
`supersim` requires `anvil`, which is installed alongside the Foundry toolchain.
Follow the [Foundry toolchain](https://book.getfoundry.sh/getting-started/installation) guide for detailed instructions.
</Step>
<Step title="Install supersim">
Either download precompiled binaries or install using Homebrew:
* Precompiled binaries: Download the executable for your platform from the [GitHub releases page](https://github.com/ethereum-optimism/supersim/releases).
* Homebrew: Install [Homebrew](https://brew.sh/) (OS X, Linux), and then run:
```sh theme={null}
brew tap ethereum-optimism/tap
brew install supersim
```
</Step>
<Step title="Start supersim in vanilla mode">
Start `supersim` in vanilla mode by running:
```sh theme={null}
supersim
```
Vanilla mode will start 3 chains, with the OP Stack contracts already deployed.
* (1) L1 Chain
* Chain 900
* (2) L2 Chains
* Chain 901
* Chain 902
</Step>
</Steps>
## Next steps
* Continue to the [First Steps](/app-developers/tutorials/development/supersim/first-steps) tutorial to try L1 to L2 message passing.
* Explore [Supersim](/app-developers/tools-sdks/supersim) features, particularly in [vanilla mode](/app-developers/reference/tools/supersim/vanilla), which starts 3 chains (L1 and L2).
# Making crosschain contract calls (ping pong)
Source: https://docs.optimism.io/app-developers/tutorials/interoperability/contract-calls
Understand how the CrossChainPingPong example contract uses the L2ToL2CrossDomainMessenger to make crosschain contract calls, and the design choices behind it.
<Info>OP Stack interop is in active development. Some features may be experimental.</Info>
This page explains the design of the `CrossChainPingPong.sol` example contract and how it integrates the `L2ToL2CrossDomainMessenger` to make crosschain contract calls. For more info, view the [source code](https://github.com/ethereum-optimism/supersim/blob/main/contracts/src/pingpong/CrossChainPingPong.sol).
## High level overview
`CrossChainPingPong.sol` implements a cross-chain ping-pong game using the `L2ToL2CrossDomainMessenger`.
* Players hit a **virtual ball** back and forth between allowed L2 chains. The game starts with a serve
* from a designated start chain, and each hit increases the rally count. The contract tracks the last hitter's address, chain ID, and the current rally count.
### Diagram
```mermaid theme={null}
sequenceDiagram
participant Chain1 as Chain 1
participant Chain2 as Chain 2
Note over Chain1: 🚀 Game Starts (starting chain)
Note over Chain1: 🏓 Hit Ball
Chain1->>Chain2: 📤 Send PingPongBall {rallyCount: 1, lastHitter: Chain1}
Chain1-->>Chain1: Emit BallSent event
activate Chain2
Note over Chain2: 📥 Receive Ball
Chain2-->>Chain2: Emit BallReceived event
Note over Chain2: 🏓 Hit Ball
Chain2->>Chain1: 📤 Send PingPongBall {rallyCount: 2, lastHitter: Chain2}
Chain2-->>Chain2: Emit BallSent event
deactivate Chain2
activate Chain1
Note over Chain1: 📥 Receive Ball
Chain1-->>Chain1: Emit BallReceived event
Note over Chain1,Chain2: Game continues...
```
### Flow
<Steps>
<Step title="Contract Deployment">
* Deployed on all participating chains
* Utilizes CREATE2 with the same parameter, `_serverChainId`, resulting in the same address and initial state.
</Step>
<Step title="Hit the Ball (Starting Move)">
* Call `hitBallTo` on the chain with the ball, specifying a destination chain.
* Contract uses `L2ToL2CrossDomainMessenger` to send the ball data to the specified chain.
* The reference to the ball is deleted from the serving chain.
</Step>
<Step title="Receive on Destination Chain">
* `L2ToL2CrossDomainMessenger` on destination chain calls `receiveBall`.
* Contract verifies the message sender and origin.
* Ball data is stored, indicating its presence on this chain.
</Step>
<Step title="Continue Game (Hit)">
* Any user on the chain currently holding the ball calls `hitBallTo` to send it to another chain.
* Contract updates the `PingPongBall` data (increment rally count, update last hitter).
* Process repeats from step 2.
</Step>
</Steps>
## Walkthrough
Here's an explanation of the functions in the contract, with a focus on how it interacts with `L2ToL2CrossChainMessenger`.
### Initialize contract state
#### Constructor Setup
```solidity theme={null}
constructor(uint256 _serverChainId) {
if (block.chainid == _serverChainId) {
ball = PingPongBall(1, block.chainid, msg.sender);
}
}
```
If the starting chain, initialize the ball allowing it to be hittable.
#### Reliance on CREATE2 for cross chain consistency
While not explicitly mentioned in the code, this contract's design implicitly assumes the use of CREATE2 for deployment. Here's why CREATE2 is crucial for this setup:
* **Predictable Addresses**:
CREATE2 enables deployment at the same address on all chains, crucial for cross-chain message verification:
```solidity theme={null}
if (messenger.crossDomainMessageSender() != address(this)) revert InvalidCrossDomainSender();
```
* **Self-referential Messaging**:
The contract sends messages to itself on other chains:
```solidity theme={null}
messenger.sendMessage(_toChainId, address(this), _message);
```
This requires `address(this)` to be consistent across chains.
* **Initialization State Considerations**:
The starting chain id is part of the initcode, meaning a deployment with a differing value would result in a different address via CREATE2. This is a nice feature as there's an implicit agreement on the starting chain from the address.
Without CREATE2, you would need to:
* Manually track contract addresses for each chain.
* Implement a more complex initialization process to register contract addresses across chains.
* Potentially redesign the security model that relies on address matching.
### Hit the ball
`hitBallTo`: This function is used to hit the ball, when present, to another chain
#### Hitting constraints
```solidity theme={null}
function hitBallTo(uint256 _toChainId) public {
if (ball.lastHitterAddress == address(0)) revert BallNotPresent();
if (_toChainId == block.chainid) revert InvalidDestination();
...
}
```
* The `ball` contract variable is populated on the chain, indicating its presence
* The destination must be a different chain
### Define receiving handler
```solidity theme={null}
modifier onlyCrossDomainCallback() {
if (msg.sender != address(messenger)) revert CallerNotL2ToL2CrossDomainMessenger();
if (messenger.crossDomainMessageSender() != address(this)) revert InvalidCrossDomainSender();
_;
}
function receiveBall(PingPongBall memory _ball) onlyCrossDomainCallback() external {
// Hold reference to the ball
ball = _ball;
emit BallReceived(messenger.crossDomainMessageSource(), block.chainid, _ball);
}
```
* The handler simply stores reference to the received ball
* The handler c프로젝트로 돌아가기