Skip to content

Commit 2fb0cc1

Browse files
committed
Add DSS test
1 parent edfaac4 commit 2fb0cc1

File tree

2 files changed

+263
-0
lines changed

2 files changed

+263
-0
lines changed

tests/stylus_tests/dss.rs

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
//! This test expects you to have a devnode running:
2+
//! <https://docs.arbitrum.io/run-arbitrum-node/run-nitro-dev-node>
3+
//!
4+
//! It also expects `cargo-stylus` and `cast` to be installed:
5+
//! - <https://github.com/OffchainLabs/cargo-stylus>
6+
//! - <https://book.getfoundry.sh/cast/>
7+
#![warn(clippy::pedantic)]
8+
9+
// smoelius: This test mimics the initial steps of the interaction described here:
10+
// https://github.com/sky-ecosystem/sai?tab=readme-ov-file#sample-interaction-using-sai
11+
12+
use crate::{call, deploy, send, ADDRESS, MUTEX};
13+
use std::path::{Path, PathBuf};
14+
use tempfile::TempDir;
15+
16+
const CHAIN_ID: u64 = 42161;
17+
18+
const GOLD: &str = "0x676f6c6400000000000000000000000000000000000000000000000000000000"; // "gold"
19+
const SPOT: &str = "0x73706f7400000000000000000000000000000000000000000000000000000000"; // "spot"
20+
const LINE_LOWER: &str = "0x6c696e6500000000000000000000000000000000000000000000000000000000"; // "line"
21+
const LINE_UPPER: &str = "0x4c696e6500000000000000000000000000000000000000000000000000000000"; // "Line"
22+
23+
#[test]
24+
fn dss() {
25+
let _lock = MUTEX.lock();
26+
let (dirs, contracts) = deploy_contracts();
27+
let dir = dirs.first().unwrap();
28+
let &[erc20, vat, dai, gem_join, dai_join] =
29+
contracts.iter().by_ref().collect::<Vec<_>>().as_slice()
30+
else {
31+
panic!();
32+
};
33+
34+
configure_contracts(dir.path(), vat, dai, gem_join, dai_join);
35+
36+
// smoelius: Join the system by exchanging some ERC20.
37+
let stdout = send(
38+
dir,
39+
erc20,
40+
["approve(address,uint256)", gem_join, "2200000000000000000"],
41+
)
42+
.unwrap();
43+
println!("{stdout}");
44+
45+
let stdout = send(
46+
dir,
47+
gem_join,
48+
["join(address,uint256)", ADDRESS, "2200000000000000000"],
49+
)
50+
.unwrap();
51+
println!("{stdout}");
52+
53+
// smoelius: Check that our ERC20 balance went down.
54+
let stdout = call(dir, erc20, ["balanceOf(address)(uint256)", ADDRESS]).unwrap();
55+
assert_eq!("999997800000000000000000 [9.999e23]\n", stdout);
56+
57+
// smoelius: Check that the ERC20 is now held by the `Vat`.
58+
let stdout = call(dir, vat, ["gem(bytes32,address)(uint256)", GOLD, ADDRESS]).unwrap();
59+
assert_eq!("2200000000000000000 [2.2e18]\n", stdout);
60+
61+
// smoelius: Lock the ERC20.
62+
let stdout = send(
63+
dir,
64+
vat,
65+
[
66+
"frob(bytes32,address,address,address,int256,int256)",
67+
GOLD,
68+
ADDRESS,
69+
ADDRESS,
70+
ADDRESS,
71+
"1500000000000000000",
72+
"89000000000000000000",
73+
],
74+
)
75+
.unwrap();
76+
println!("{stdout}");
77+
78+
// smoelius: Check our internal DAI balance.
79+
let stdout = call(dir, vat, ["dai(address)(uint256)", ADDRESS]).unwrap();
80+
assert_eq!(
81+
"89000000000000000000000000000000000000000000000 [8.9e46]\n",
82+
stdout
83+
);
84+
85+
// smoelius: Convert the ERC20 to DAI.
86+
let stdout = send(
87+
dir,
88+
dai_join,
89+
["exit(address,uint256)", ADDRESS, "89000000000000000000"],
90+
)
91+
.unwrap();
92+
println!("{stdout}");
93+
94+
// smoelius: Check our external DAI balance.
95+
let stdout = call(dir, dai, ["balanceOf(address)(uint256)", ADDRESS]).unwrap();
96+
assert_eq!("89000000000000000000 [8.9e19]\n", stdout);
97+
}
98+
99+
fn deploy_contracts() -> (Vec<TempDir>, Vec<String>) {
100+
let mut dirs_and_contracts = Vec::new();
101+
102+
dirs_and_contracts.push(deploy_erc20());
103+
104+
let erc20 = dirs_and_contracts.last().unwrap().1.clone();
105+
106+
dirs_and_contracts.push(
107+
deploy(
108+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("integration/stylus/dss/vat.sol"),
109+
"Vat",
110+
true,
111+
)
112+
.unwrap(),
113+
);
114+
115+
let _stdout = send(
116+
&dirs_and_contracts.last().unwrap().0,
117+
&dirs_and_contracts.last().unwrap().1,
118+
["initialize()"],
119+
)
120+
.unwrap();
121+
// println!("{}", stdout);
122+
123+
let vat = dirs_and_contracts.last().unwrap().1.clone();
124+
125+
dirs_and_contracts.push(
126+
deploy(
127+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("integration/stylus/dss/dai.sol"),
128+
"Dai",
129+
true,
130+
)
131+
.unwrap(),
132+
);
133+
134+
let _stdout = send(
135+
&dirs_and_contracts.last().unwrap().0,
136+
&dirs_and_contracts.last().unwrap().1,
137+
["initialize(uint256)", &CHAIN_ID.to_string()],
138+
)
139+
.unwrap();
140+
// println!("{}", stdout);
141+
142+
let dai = dirs_and_contracts.last().unwrap().1.clone();
143+
144+
dirs_and_contracts.push(
145+
deploy(
146+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("integration/stylus/dss/join.sol"),
147+
"GemJoin",
148+
true,
149+
)
150+
.unwrap(),
151+
);
152+
153+
let _stdout = send(
154+
&dirs_and_contracts.last().unwrap().0,
155+
&dirs_and_contracts.last().unwrap().1,
156+
["initialize(address,bytes32,address)", &vat, GOLD, &erc20],
157+
)
158+
.unwrap();
159+
// println!("{}", stdout);
160+
161+
dirs_and_contracts.push(
162+
deploy(
163+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("integration/stylus/dss/join.sol"),
164+
"DaiJoin",
165+
true,
166+
)
167+
.unwrap(),
168+
);
169+
170+
let _stdout = send(
171+
&dirs_and_contracts.last().unwrap().0,
172+
&dirs_and_contracts.last().unwrap().1,
173+
["initialize(address,address)", &vat, &dai],
174+
)
175+
.unwrap();
176+
// println!("{}", stdout);
177+
178+
let contracts = dirs_and_contracts
179+
.iter()
180+
.map(|(_, contract)| contract.clone())
181+
.collect();
182+
let dirs = dirs_and_contracts.into_iter().map(|(dir, _)| dir).collect();
183+
184+
(dirs, contracts)
185+
}
186+
187+
const INITIAL_BALANCE: &str = concat!("1000000", "000000000000000000");
188+
189+
fn deploy_erc20() -> (TempDir, String) {
190+
let (tempdir, address) = deploy(
191+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("integration/stylus/Uniswap/test/ERC20.sol"),
192+
"ERC20",
193+
true,
194+
)
195+
.unwrap();
196+
let dir = &tempdir;
197+
198+
let _stdout = send(dir, &address, ["initialize(uint256)", INITIAL_BALANCE]).unwrap();
199+
// println!("{}", stdout);
200+
201+
let stdout = call(dir, &address, ["totalSupply()(uint256)"]).unwrap();
202+
assert_eq!("1000000000000000000000000 [1e24]\n", stdout);
203+
204+
(tempdir, address)
205+
}
206+
207+
fn configure_contracts(dir: &Path, vat: &str, dai: &str, gem_join: &str, dai_join: &str) {
208+
// smoelius: Allow `GOLD` to be an ilk.
209+
let _stdout = send(dir, vat, ["init(bytes32)", GOLD]).unwrap();
210+
// println!("{stdout}");
211+
212+
// smoelius: Set `GOLD`'s spot price.
213+
let stdout = send(
214+
dir,
215+
vat,
216+
[
217+
"file(bytes32,bytes32,uint256)",
218+
GOLD,
219+
SPOT,
220+
"1000000000000000000000000000000000000000000000",
221+
],
222+
)
223+
.unwrap();
224+
println!("{stdout}");
225+
226+
// smoelius: Set `GOLD`'s debt ceiling.
227+
let _stdout = send(
228+
dir,
229+
vat,
230+
[
231+
"file(bytes32,bytes32,uint256)",
232+
GOLD,
233+
LINE_LOWER,
234+
"100000000000000000000000000000000000000000000000000000",
235+
],
236+
)
237+
.unwrap();
238+
239+
// smoelius: Set the global debt ceiling.
240+
let _stdout = send(
241+
dir,
242+
vat,
243+
[
244+
"file(bytes32,uint256)",
245+
LINE_UPPER,
246+
"100000000000000000000000000000000000000000000000000000",
247+
],
248+
)
249+
.unwrap();
250+
251+
// smoelius: Authorize `gem_join` to call `slip` on the `Vat`.
252+
let _stdout = send(dir, vat, ["rely(address)", gem_join]).unwrap();
253+
// println!("{stdout}");
254+
255+
// smoelius: Authorize `dai_join` to `mint` DAI tokens.
256+
let _stdout = send(dir, dai, ["rely(address)", dai_join]).unwrap();
257+
// println!("{stdout}");
258+
259+
// smoelius: Authorize `dai_join` to call `move` on the `Vat`.
260+
let _stdout = send(dir, vat, ["hope(address)", dai_join]).unwrap();
261+
// println!("{stdout}");
262+
}

tests/stylus_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod api;
22
mod arb_wasm;
33
mod counter;
44
mod create2;
5+
mod dss;
56
mod ecrecover;
67
mod erc20;
78
mod milestone_1;

0 commit comments

Comments
 (0)