Skip to content

Commit ced8d53

Browse files
authored
Ported Migration Script to Rust (#752)
* Made some tweaks to the Hyperdrive wrappers build script in preparation of re-writing the migration script in Rust * Ported the devnet migration script to Rust * Updated the u256 deserialization and the docker file * Finalized the docker updates and fixed the addresses serialization * Tried to fix "intrinsic gas cost too high" * Fixed the rust tests
1 parent 6a5956c commit ced8d53

File tree

17 files changed

+1742
-1431
lines changed

17 files changed

+1742
-1431
lines changed

Cargo.lock

Lines changed: 844 additions & 677 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
### Foundry Image ###
1+
FROM ghcr.io/foundry-rs/foundry:master as builder
22

3-
FROM ghcr.io/foundry-rs/foundry:master
3+
# Install the Rust toolchain.
4+
RUN apk add clang lld curl build-base linux-headers git pkgconfig libressl-dev \
5+
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh \
6+
&& chmod +x ./rustup.sh \
7+
&& ./rustup.sh -y --default-toolchain nightly
48

9+
# Set the working directory to where the source code will live.
510
WORKDIR /src
611

712
# Use the production foundry profile.
813
ENV FOUNDRY_PROFILE="production"
914

10-
# Copy the contract dependencies required to run the migration script.
15+
# Copy the dependencies required to run the migration script.
1116
COPY ./.git/ ./.git/
1217
COPY ./contracts/ ./contracts/
18+
COPY ./crates/ ./crates/
1319
COPY ./lib/ ./lib/
1420
COPY ./script/ ./script/
1521
COPY ./test/ ./test/
22+
COPY ./Cargo.toml ./Cargo.toml
23+
COPY ./Cargo.lock ./Cargo.lock
1624
COPY ./foundry.toml ./foundry.toml
1725

1826
# Copy the script used to run the migrations and set its permissions.
@@ -22,10 +30,11 @@ RUN chmod a+x ./migrate.sh
2230
# Install the dependencies and compile the contracts.
2331
RUN forge install && forge build
2432

33+
# Compile the migration script.
34+
Run source $HOME/.profile && cargo build -Z sparse-registry --bin migrate
35+
2536
# Load the environment variables used in the migration script.
26-
ENV ETH_FROM=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
27-
ENV PRIVATE_KEY=ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
28-
ENV RPC_URL=http://localhost:8545
37+
ENV HYPERDRIVE_ETHEREUM_URL=http://localhost:8545
2938
ARG ADMIN
3039
ARG IS_COMPETITION_MODE
3140
ARG BASE_TOKEN_NAME
@@ -55,9 +64,18 @@ ARG STETH_HYPERDRIVE_TIME_STRETCH_APR
5564
# node and dump the state into the "./data" directory. At runtime, the consumer
5665
# can start anvil with the "--load-state ./data" flag to start up anvil with
5766
# the post-migrations state.
58-
RUN anvil --dump-state ./data --balance 100000 & \
67+
RUN anvil --dump-state ./data & \
5968
ANVIL="$!" && \
6069
sleep 2 && \
61-
./migrate.sh && \
70+
./target/debug/migrate && \
6271
kill $ANVIL && \
6372
sleep 1s # HACK(jalextowle): Ensure that "./data" is written before exiting.
73+
74+
FROM ghcr.io/foundry-rs/foundry:master
75+
76+
# Set the working directory to where the source code will live.
77+
WORKDIR /src
78+
79+
# Copy the data and artifacts from the builder stage.
80+
COPY --from=builder /src/data /src/data
81+
COPY --from=builder /src/artifacts /src/artifacts

crates/hyperdrive-addresses/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ use ethers::types::Address;
77
use serde::{Deserialize, Serialize};
88

99
#[derive(Clone, Default, Debug, Eq, PartialEq, Serialize, Deserialize)]
10+
#[serde(rename_all = "camelCase")]
11+
#[serde(default)]
1012
pub struct Addresses {
1113
#[serde(alias = "baseToken")]
1214
#[serde(alias = "base_token_address")]
13-
pub base: Address,
14-
#[serde(alias = "mockHyperdrive")]
15+
pub base_token: Address,
16+
#[serde(alias = "erc4626Hyperdrive")]
1517
#[serde(alias = "hyperdrive_address")]
16-
pub hyperdrive: Address,
18+
pub erc4626_hyperdrive: Address,
19+
#[serde(alias = "stethHyperdrive")]
20+
pub steth_hyperdrive: Address,
21+
pub factory: Address,
1722
}

crates/hyperdrive-math/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod yield_space;
77
use ethers::types::{Address, I256, U256};
88
use fixed_point::FixedPoint;
99
use fixed_point_macros::{fixed, uint256};
10-
use hyperdrive_wrappers::wrappers::i_hyperdrive::{Fees, PoolConfig, PoolInfo};
10+
use hyperdrive_wrappers::wrappers::ihyperdrive::{Fees, PoolConfig, PoolInfo};
1111
pub use long::*;
1212
use rand::{
1313
distributions::{Distribution, Standard},

crates/hyperdrive-math/src/short/max.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ mod tests {
534534
use ethers::types::U256;
535535
use eyre::Result;
536536
use hyperdrive_wrappers::wrappers::{
537-
i_hyperdrive::Checkpoint, mock_hyperdrive_math::MaxTradeParams,
537+
ihyperdrive::Checkpoint, mock_hyperdrive_math::MaxTradeParams,
538538
};
539539
use rand::{thread_rng, Rng};
540540
use test_utils::{

crates/hyperdrive-math/src/short/open.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,9 @@ mod tests {
8282

8383
use ethers::types::U256;
8484
use eyre::Result;
85-
use hyperdrive_wrappers::wrappers::{
86-
i_hyperdrive::Checkpoint, mock_hyperdrive_math::MaxTradeParams,
87-
};
85+
use hyperdrive_wrappers::wrappers::mock_hyperdrive_math::MaxTradeParams;
8886
use rand::{thread_rng, Rng};
89-
use test_utils::{
90-
agent::Agent,
91-
chain::{Chain, TestChain, TestChainWithMocks},
92-
constants::{FAST_FUZZ_RUNS, FUZZ_RUNS},
93-
};
94-
use tracing_test::traced_test;
87+
use test_utils::{chain::TestChainWithMocks, constants::FAST_FUZZ_RUNS};
9588

9689
use super::*;
9790

crates/hyperdrive-math/tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use eyre::Result;
33
use fixed_point::FixedPoint;
44
use fixed_point_macros::{fixed, uint256};
55
use hyperdrive_math::{calculate_initial_bond_reserves, get_effective_share_reserves};
6-
use hyperdrive_wrappers::wrappers::i_hyperdrive::Checkpoint;
6+
use hyperdrive_wrappers::wrappers::ihyperdrive::Checkpoint;
77
use rand::{thread_rng, Rng, SeedableRng};
88
use rand_chacha::ChaCha8Rng;
99
use test_utils::{

crates/hyperdrive-wrappers/build.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,49 @@ const TARGETS: &[&str] = &[
88
// Interfaces
99
"IERC20",
1010
"IERC4626Hyperdrive",
11+
"IStETHHyperdrive",
1112
"IHyperdrive",
13+
"IHyperdriveFactory",
1214
// Tokens
1315
"ERC20Mintable",
14-
// Hyperdrive
16+
"ERC20ForwarderFactory",
17+
// Hyperdrive Factory
18+
"HyperdriveFactory",
19+
// ERC4626 Hyperdrive
1520
"ERC4626Hyperdrive",
21+
"ERC4626HyperdriveCoreDeployer",
22+
"ERC4626HyperdriveDeployerCoordinator",
1623
"ERC4626Target0",
1724
"ERC4626Target1",
1825
"ERC4626Target2",
1926
"ERC4626Target3",
2027
"ERC4626Target4",
28+
"ERC4626Target0Deployer",
29+
"ERC4626Target1Deployer",
30+
"ERC4626Target2Deployer",
31+
"ERC4626Target3Deployer",
32+
"ERC4626Target4Deployer",
33+
// stETH Hyperdrive
34+
"StETHHyperdrive",
35+
"StETHHyperdriveDeployerCoordinator",
36+
"StETHHyperdriveCoreDeployer",
37+
"StETHTarget0",
38+
"StETHTarget1",
39+
"StETHTarget2",
40+
"StETHTarget3",
41+
"StETHTarget4",
42+
"StETHTarget0Deployer",
43+
"StETHTarget1Deployer",
44+
"StETHTarget2Deployer",
45+
"StETHTarget3Deployer",
46+
"StETHTarget4Deployer",
2147
// Test Contracts
2248
"ERC20Mintable",
2349
"EtchingVault",
2450
"MockERC4626",
2551
"MockFixedPointMath",
2652
"MockHyperdriveMath",
53+
"MockLido",
2754
"MockLPMath",
2855
"MockYieldSpaceMath",
2956
];
@@ -76,12 +103,30 @@ fn main() -> Result<()> {
76103
artifacts.sort_by(|a, b| a.1.cmp(&b.1));
77104
artifacts.dedup_by(|a, b| a.1.eq(&b.1));
78105
for (source, name) in artifacts {
106+
let target = name
107+
// Ensure that `StETH` is converted to `steth` in snake case.
108+
.replace("StETH", "STETH")
109+
// Ensure that `IHyperdrive` is converted to `ihyperdrive` in snake case.
110+
.replace("IHyperdrive", "IHYPERDRIVE")
111+
.to_snake_case();
112+
79113
// Write the generated contract wrapper.
80-
let target = name.to_snake_case();
81114
let target_file = generated.join(format!("{}.rs", target));
82115
Abigen::new(name, source)?
83116
.add_derive("serde::Serialize")?
84117
.add_derive("serde::Deserialize")?
118+
// Alias the `IHyperdriveDeployerCoordinator.deploy()` to
119+
// `deploy_hyperdrive()` to avoid conflicts with the builtin
120+
// `deploy()` in the wrapper used to call the constructor.
121+
.add_method_alias("deploy(bytes32,(address,address,bytes32,uint256,uint256,uint256,uint256,uint256,address,address,(uint256,uint256,uint256,uint256)),bytes,bytes32)", "deploy_hyperdrive")
122+
// Alias the `IHyperdriveCoreDeployer.deploy()` to
123+
// `deploy_hyperdrive()` to avoid conflicts with the builtin
124+
// `deploy()` in the wrapper used to call the constructor.
125+
.add_method_alias("deploy((address,address,bytes32,uint256,uint256,uint256,uint256,uint256,uint256,address,address,(uint256,uint256,uint256,uint256)),bytes,address,address,address,address,address,bytes32)", "deploy_hyperdrive")
126+
// Alias the `IHyperdriveTarget.deploy()` to `deploy_target()`
127+
// to avoid conflicts with the builtin `deploy()` in the wrapper
128+
// used to call the constructor.
129+
.add_method_alias("deploy((address,address,bytes32,uint256,uint256,uint256,uint256,uint256,uint256,address,address,(uint256,uint256,uint256,uint256)),bytes,bytes32)", "deploy_target")
85130
.generate()?
86131
.write_to_file(target_file)?;
87132

crates/test-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ name = "test_chain_load_crash"
2424
# External dependencies.
2525
async-trait = "0.1.73"
2626
dotenvy = "0.15"
27+
envy = "0.4.2"
2728
ethers = "2.0.8"
2829
eyre = "0.6.8"
2930
lazy_static = "1.4.0"

crates/test-utils/src/agent.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use hyperdrive_addresses::Addresses;
1414
use hyperdrive_math::State;
1515
use hyperdrive_wrappers::wrappers::{
1616
erc20_mintable::ERC20Mintable,
17-
i_hyperdrive::{Checkpoint, IHyperdrive, IHyperdriveEvents, Options, PoolConfig},
1817
ierc4626_hyperdrive::IERC4626Hyperdrive,
18+
ihyperdrive::{Checkpoint, IHyperdrive, IHyperdriveEvents, Options, PoolConfig},
1919
mock_erc4626::MockERC4626,
2020
};
2121
use rand::{Rng, SeedableRng};
@@ -165,18 +165,20 @@ impl Agent<ChainClient, ChaCha8Rng> {
165165
maybe_seed: Option<u64>,
166166
) -> Result<Self> {
167167
let seed = maybe_seed.unwrap_or(17);
168-
let vault = IERC4626Hyperdrive::new(addresses.hyperdrive, client.clone())
168+
let vault = IERC4626Hyperdrive::new(addresses.erc4626_hyperdrive, client.clone())
169169
.vault()
170170
.call()
171171
.await?;
172172
let vault = MockERC4626::new(vault, client.clone());
173-
let hyperdrive = IHyperdrive::new(addresses.hyperdrive, client.clone());
173+
// TODO: Eventually, the agent should be able to support several
174+
// different pools simultaneously.
175+
let hyperdrive = IHyperdrive::new(addresses.erc4626_hyperdrive, client.clone());
174176
Ok(Self {
175177
address: client.address(),
176178
provider: client.provider().clone(),
177179
hyperdrive: hyperdrive.clone(),
178180
vault,
179-
base: ERC20Mintable::new(addresses.base, client),
181+
base: ERC20Mintable::new(addresses.base_token, client),
180182
config: hyperdrive.get_pool_config().call().await?,
181183
wallet: Wallet::default(),
182184
rng: ChaCha8Rng::seed_from_u64(seed),

0 commit comments

Comments
 (0)