Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Oct 25, 2024
1 parent b09f443 commit 089e54a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ This section features documentation specifically designed for advanced users, ou
- How to [run a debugger](docs/running-a-debugger/running-a-debugger.org).
- How to work with the [timelock](docs/timelock.org).
- How to [trigger a reorg](docs/trigger-a-reorg/trigger-a-reorg.md).
- How to [deploy contracts with the deterministic deployment proxy](docs/deterministic-deployment-proxy.md).

## Contact

Expand Down
113 changes: 113 additions & 0 deletions docs/deterministic-deployment-proxy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Deploy Contracts with the Deterministic Deployment Proxy

A guide to deploy contracts using the [deterministic deployment proxy](https://github.com/Arachnid/deterministic-deployment-proxy).

## Introduction

You can deploy contracts to both L1 and L2 using the deterministic deployment
proxy. Deploying the same contract to multiple chains will result in the same
contract address.

There are two ways to go about deploying contracts:

1. Manually in your local environment. This requires you to have [`foundry/cast`](https://github.com/foundry-rs/foundry) installed.
2. Using the [`run-l2-contract-setup.sh`](templates/contract-deploy/run-l2-contract-setup.sh) script. This ensures that the contracts will be deployed with every `kurtosis run`.

## Deploying Contracts Locally

For this example, we will be deploying this contract:

```solidity
pragma solidity 0.5.8;
contract Apple {
function banana() external pure returns (uint8) {
return 42;
}
}
```

To determine the contract address of the above contract, compile the bytecode
and run:

```bash
cast create2 --salt $salt --init-code $bytecode
```

To deploy a contract, send the salt and bytecode to the deterministic deployment
proxy deployer address which should be `0x4e59b44847b379578588920ca78fbf26c0b4956c`.

### Deploy Contracts on L1

The accounts using the `code code code code code code code code code code code quality`
mnemonic are pre-funded on L1, so you can use those accounts to for the contract
deployments.

```bash
polycli wallet inspect --mnemonic "code code code code code code code code code code code quality" | jq -r ".Addresses[0].HexPrivateKey"
```

```bash
cast send \
--legacy \
--rpc-url "http://$(kurtosis port print cdk el-1-geth-lighthouse rpc)" \
--private-key "0x42b6e34dc21598a807dc19d7784c71b2a7a01f6480dc6f58258f78e539f1a1fa" \
"0x4e59b44847b379578588920ca78fbf26c0b4956c" \
"$salt$bytecode"
```

### Deploy Contracts on L2

This is similar to L1 the major differences is the pre-funded account on L2 is the
`zkevm_l2_admin_private_key`.

```bash
cast send \
--legacy \
--rpc-url "$(kurtosis port print cdk cdk-erigon-node-001 rpc)" \
--private-key "0x12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625" \
"0x4e59b44847b379578588920ca78fbf26c0b4956c" \
"$salt$bytecode"
```

## Deploying Contracts using `run-l2-contract-setup.sh`

Most of the same concepts from above apply, you just now have access to some
different variables provided through the kurtosis such as `l1_rpc_url` and
`l2_rpc_url`.

Here's a complete example that could be appended to the `run-l2-contract-setup.sh`:

```bash
contract_method_signature="banana()(uint8)"
expected="42"
salt="0x0000000000000000000000000000000000000000000000000000000000000000"
# contract: pragma solidity 0.5.8; contract Apple {function banana() external pure returns (uint8) {return 42;}}
bytecode="6080604052348015600f57600080fd5b5060848061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c3cafc6f14602d575b600080fd5b6033604f565b604051808260ff1660ff16815260200191505060405180910390f35b6000602a90509056fea165627a7a72305820ab7651cb86b8c1487590004c2444f26ae30077a6b96c6bc62dda37f1328539250029"
contract_address=$(cast create2 --salt $salt --init-code $bytecode)

echo_ts "Testing deterministic deployment proxy on l1"
cast send \
--legacy \
--rpc-url "{{.l1_rpc_url}}" \
--private-key "$l1_private_key" \
"$deployer_address" \
"$salt$bytecode"
l1_actual=$(cast call --rpc-url "{{.l1_rpc_url}}" "$contract_address" "$contract_method_signature")
if [[ "$expected" != "$l1_actual" ]]; then
echo_ts "Failed to deploy deterministic deployment proxy on l1 (expected: $expected, actual $l1_actual)"
exit 1
fi

echo_ts "Testing deterministic deployment proxy on l2"
cast send \
--legacy \
--rpc-url "{{.l2_rpc_url}}" \
--private-key "{{.zkevm_l2_admin_private_key}}" \
"$deployer_address" \
"$salt$bytecode"
l2_actual=$(cast call --rpc-url "{{.l2_rpc_url}}" "$contract_address" "$contract_method_signature")
if [[ "$expected" != "$l2_actual" ]]; then
echo_ts "Failed to deploy deterministic deployment proxy on l2 (expected: $expected, actual $l2_actual)"
exit 1
fi
```
46 changes: 8 additions & 38 deletions templates/contract-deploy/run-l2-contract-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ signer_address="0x3fab184622dc19b6109349b94811493bf2a45362"
gas_cost="0.01ether"
transaction="0xf8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222"
deployer_address="0x4e59b44847b379578588920ca78fbf26c0b4956c"
l1_private_key=$(
polycli wallet inspect \
--mnemonic "{{.l1_preallocated_mnemonic}}" \
--addresses 1 \
| jq -r ".Addresses[].HexPrivateKey"
)
l1_private_key=$(polycli wallet inspect --mnemonic "{{.l1_preallocated_mnemonic}}" | jq -r ".Addresses[0].HexPrivateKey")

echo_ts "Deploying deterministic deployment proxy on l1"
cast send \
Expand All @@ -84,6 +79,10 @@ cast send \
--value "$gas_cost" \
"$signer_address"
cast publish --rpc-url "{{.l1_rpc_url}}" "$transaction"
if [[ $(cast code --rpc-url "{{.l1_rpc_url}}" $deployer_address) == "0x" ]]; then
echo_ts "No code at expected l1 address: $deployer_address"
exit 1;
fi

echo_ts "Deploying deterministic deployment proxy on l2"
cast send \
Expand All @@ -93,38 +92,9 @@ cast send \
--value "$gas_cost" \
"$signer_address"
cast publish --rpc-url "{{.l2_rpc_url}}" "$transaction"

contract_method_signature="banana()(uint8)"
expected="42"
salt="0x0000000000000000000000000000000000000000000000000000000000000000"
# contract: pragma solidity 0.5.8; contract Apple {function banana() external pure returns (uint8) {return 42;}}
bytecode="6080604052348015600f57600080fd5b5060848061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c3cafc6f14602d575b600080fd5b6033604f565b604051808260ff1660ff16815260200191505060405180910390f35b6000602a90509056fea165627a7a72305820ab7651cb86b8c1487590004c2444f26ae30077a6b96c6bc62dda37f1328539250029"
contract_address=$(cast create2 --salt $salt --init-code $bytecode)

echo_ts "Testing deterministic deployment proxy on l1"
cast send \
--legacy \
--rpc-url "{{.l1_rpc_url}}" \
--private-key "$l1_private_key" \
"$deployer_address" \
"$salt$bytecode"
l1_actual=$(cast call --rpc-url "{{.l1_rpc_url}}" "$contract_address" "$contract_method_signature")
if [[ "$expected" != "$l1_actual" ]]; then
echo_ts "Failed to deploy deterministic deployment proxy on l1 (expected: $expected, actual $l1_actual)"
exit 1
fi

echo_ts "Testing deterministic deployment proxy on l2"
cast send \
--legacy \
--rpc-url "{{.l2_rpc_url}}" \
--private-key "{{.zkevm_l2_admin_private_key}}" \
"$deployer_address" \
"$salt$bytecode"
l2_actual=$(cast call --rpc-url "{{.l2_rpc_url}}" "$contract_address" "$contract_method_signature")
if [[ "$expected" != "$l2_actual" ]]; then
echo_ts "Failed to deploy deterministic deployment proxy on l2 (expected: $expected, actual $l2_actual)"
exit 1
if [[ $(cast code --rpc-url "{{.l2_rpc_url}}" $deployer_address) == "0x" ]]; then
echo_ts "No code at expected l2 address: $deployer_address"
exit 1;
fi

# The contract setup is done!
Expand Down

0 comments on commit 089e54a

Please sign in to comment.