foundry_test_utils/
script.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::{init_tracing, util::lossy_string, TestCommand};
use alloy_primitives::Address;
use alloy_provider::Provider;
use eyre::Result;
use foundry_common::provider::{get_http_provider, RetryProvider};
use std::{
    collections::BTreeMap,
    fs,
    path::{Path, PathBuf},
    str::FromStr,
};

const BROADCAST_TEST_PATH: &str = "src/Broadcast.t.sol";
const TESTDATA: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../testdata");

fn init_script_cmd(
    cmd: &mut TestCommand,
    project_root: &Path,
    target_contract: &str,
    endpoint: Option<&str>,
) {
    cmd.forge_fuse();
    cmd.set_current_dir(project_root);

    cmd.args([
        "script",
        "-R",
        "ds-test/=lib/",
        "-R",
        "cheats/=cheats/",
        target_contract,
        "--root",
        project_root.to_str().unwrap(),
        "-vvvvv",
    ]);

    if let Some(rpc_url) = endpoint {
        cmd.args(["--fork-url", rpc_url]);
    }
}
/// A helper struct to test forge script scenarios
pub struct ScriptTester {
    pub accounts_pub: Vec<Address>,
    pub accounts_priv: Vec<String>,
    pub provider: Option<RetryProvider>,
    pub nonces: BTreeMap<u32, u64>,
    pub address_nonces: BTreeMap<Address, u64>,
    pub cmd: TestCommand,
    pub project_root: PathBuf,
    pub target_contract: String,
    pub endpoint: Option<String>,
}

impl ScriptTester {
    /// Creates a new instance of a Tester for the given contract
    pub fn new(
        mut cmd: TestCommand,
        endpoint: Option<&str>,
        project_root: &Path,
        target_contract: &str,
    ) -> Self {
        init_tracing();
        Self::copy_testdata(project_root).unwrap();
        init_script_cmd(&mut cmd, project_root, target_contract, endpoint);

        let mut provider = None;
        if let Some(endpoint) = endpoint {
            provider = Some(get_http_provider(endpoint))
        }

        Self {
            accounts_pub: vec![
                Address::from_str("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").unwrap(),
                Address::from_str("0x70997970C51812dc3A010C7d01b50e0d17dc79C8").unwrap(),
                Address::from_str("0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC").unwrap(),
            ],
            accounts_priv: vec![
                "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string(),
                "59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d".to_string(),
                "5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a".to_string(),
            ],
            provider,
            nonces: BTreeMap::default(),
            address_nonces: BTreeMap::default(),
            cmd,
            project_root: project_root.to_path_buf(),
            target_contract: target_contract.to_string(),
            endpoint: endpoint.map(|s| s.to_string()),
        }
    }

    /// Creates a new instance of a Tester for the `broadcast` test at the given `project_root` by
    /// configuring the `TestCommand` with script
    pub fn new_broadcast(cmd: TestCommand, endpoint: &str, project_root: &Path) -> Self {
        let target_contract = project_root.join(BROADCAST_TEST_PATH).to_string_lossy().to_string();

        // copy the broadcast test
        fs::copy(
            Self::testdata_path().join("default/cheats/Broadcast.t.sol"),
            project_root.join(BROADCAST_TEST_PATH),
        )
        .expect("Failed to initialize broadcast contract");

        Self::new(cmd, Some(endpoint), project_root, &target_contract)
    }

    /// Creates a new instance of a Tester for the `broadcast` test at the given `project_root` by
    /// configuring the `TestCommand` with script without an endpoint
    pub fn new_broadcast_without_endpoint(cmd: TestCommand, project_root: &Path) -> Self {
        let target_contract = project_root.join(BROADCAST_TEST_PATH).to_string_lossy().to_string();

        // copy the broadcast test
        let testdata = Self::testdata_path();
        fs::copy(
            testdata.join("default/cheats/Broadcast.t.sol"),
            project_root.join(BROADCAST_TEST_PATH),
        )
        .expect("Failed to initialize broadcast contract");

        Self::new(cmd, None, project_root, &target_contract)
    }

    /// Returns the path to the dir that contains testdata
    fn testdata_path() -> &'static Path {
        Path::new(TESTDATA)
    }

    /// Initialises the test contracts by copying them into the workspace
    fn copy_testdata(current_dir: &Path) -> Result<()> {
        let testdata = Self::testdata_path();
        fs::create_dir_all(current_dir.join("cheats"))?;
        fs::copy(testdata.join("cheats/Vm.sol"), current_dir.join("cheats/Vm.sol"))?;
        fs::copy(testdata.join("lib/ds-test/src/test.sol"), current_dir.join("lib/test.sol"))?;
        Ok(())
    }

    pub async fn load_private_keys(&mut self, keys_indexes: &[u32]) -> &mut Self {
        for &index in keys_indexes {
            self.cmd.args(["--private-keys", &self.accounts_priv[index as usize]]);

            if let Some(provider) = &self.provider {
                let nonce = provider
                    .get_transaction_count(self.accounts_pub[index as usize])
                    .await
                    .unwrap();
                self.nonces.insert(index, nonce);
            }
        }
        self
    }

    pub async fn load_addresses(&mut self, addresses: &[Address]) -> &mut Self {
        for &address in addresses {
            let nonce =
                self.provider.as_ref().unwrap().get_transaction_count(address).await.unwrap();
            self.address_nonces.insert(address, nonce);
        }
        self
    }

    pub fn add_deployer(&mut self, index: u32) -> &mut Self {
        self.sender(self.accounts_pub[index as usize])
    }

    /// Adds given address as sender
    pub fn sender(&mut self, addr: Address) -> &mut Self {
        self.args(&["--sender", addr.to_string().as_str()])
    }

    pub fn add_sig(&mut self, contract_name: &str, sig: &str) -> &mut Self {
        self.args(&["--tc", contract_name, "--sig", sig])
    }

    pub fn add_create2_deployer(&mut self, create2_deployer: Address) -> &mut Self {
        self.args(&["--create2-deployer", create2_deployer.to_string().as_str()])
    }

    /// Adds the `--unlocked` flag
    pub fn unlocked(&mut self) -> &mut Self {
        self.arg("--unlocked")
    }

    pub fn simulate(&mut self, expected: ScriptOutcome) -> &mut Self {
        self.run(expected)
    }

    pub fn broadcast(&mut self, expected: ScriptOutcome) -> &mut Self {
        self.arg("--broadcast").run(expected)
    }

    pub fn resume(&mut self, expected: ScriptOutcome) -> &mut Self {
        self.arg("--resume").run(expected)
    }

    /// `[(private_key_slot, expected increment)]`
    pub async fn assert_nonce_increment(&mut self, keys_indexes: &[(u32, u32)]) -> &mut Self {
        for &(private_key_slot, expected_increment) in keys_indexes {
            let addr = self.accounts_pub[private_key_slot as usize];
            let nonce = self.provider.as_ref().unwrap().get_transaction_count(addr).await.unwrap();
            let prev_nonce = self.nonces.get(&private_key_slot).unwrap();

            assert_eq!(
                nonce,
                (*prev_nonce + expected_increment as u64),
                "nonce not incremented correctly for {addr}: \
                 {prev_nonce} + {expected_increment} != {nonce}"
            );
        }
        self
    }

    /// In Vec<(address, expected increment)>
    pub async fn assert_nonce_increment_addresses(
        &mut self,
        address_indexes: &[(Address, u32)],
    ) -> &mut Self {
        for (address, expected_increment) in address_indexes {
            let nonce =
                self.provider.as_ref().unwrap().get_transaction_count(*address).await.unwrap();
            let prev_nonce = self.address_nonces.get(address).unwrap();

            assert_eq!(nonce, *prev_nonce + *expected_increment as u64);
        }
        self
    }

    pub fn run(&mut self, expected: ScriptOutcome) -> &mut Self {
        let out = self.cmd.execute();
        let (stdout, stderr) = (lossy_string(&out.stdout), lossy_string(&out.stderr));

        trace!(target: "tests", "STDOUT\n{stdout}\n\nSTDERR\n{stderr}");

        if !stdout.contains(expected.as_str()) && !stderr.contains(expected.as_str()) {
            panic!(
                "--STDOUT--\n{stdout}\n\n--STDERR--\n{stderr}\n\n--EXPECTED--\n{:?} not found in stdout or stderr",
                expected.as_str()
            );
        }

        self
    }

    pub fn slow(&mut self) -> &mut Self {
        self.arg("--slow")
    }

    pub fn arg(&mut self, arg: &str) -> &mut Self {
        self.cmd.arg(arg);
        self
    }

    pub fn args(&mut self, args: &[&str]) -> &mut Self {
        self.cmd.args(args);
        self
    }

    pub fn clear(&mut self) {
        init_script_cmd(
            &mut self.cmd,
            &self.project_root,
            &self.target_contract,
            self.endpoint.as_deref(),
        );
        self.nonces.clear();
        self.address_nonces.clear();
    }
}

/// Various `forge` script results
#[derive(Debug)]
pub enum ScriptOutcome {
    OkNoEndpoint,
    OkSimulation,
    OkBroadcast,
    WarnSpecifyDeployer,
    MissingSender,
    MissingWallet,
    StaticCallNotAllowed,
    ScriptFailed,
    UnsupportedLibraries,
    ErrorSelectForkOnBroadcast,
    OkRun,
}

impl ScriptOutcome {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::OkNoEndpoint => "If you wish to simulate on-chain transactions pass a RPC URL.",
            Self::OkSimulation => "SIMULATION COMPLETE. To broadcast these",
            Self::OkBroadcast => "ONCHAIN EXECUTION COMPLETE & SUCCESSFUL",
            Self::WarnSpecifyDeployer => "Warning: You have more than one deployer who could predeploy libraries. Using `--sender` instead.",
            Self::MissingSender => "You seem to be using Foundry's default sender. Be sure to set your own --sender",
            Self::MissingWallet => "No associated wallet",
            Self::StaticCallNotAllowed => "staticcall`s are not allowed after `broadcast`; use `startBroadcast` instead",
            Self::ScriptFailed => "script failed: ",
            Self::UnsupportedLibraries => "Multi chain deployment does not support library linking at the moment.",
            Self::ErrorSelectForkOnBroadcast => "cannot select forks during a broadcast",
            Self::OkRun => "Script ran successfully",
        }
    }

    pub fn is_err(&self) -> bool {
        match self {
            Self::OkNoEndpoint |
            Self::OkSimulation |
            Self::OkBroadcast |
            Self::WarnSpecifyDeployer |
            Self::OkRun => false,
            Self::MissingSender |
            Self::MissingWallet |
            Self::StaticCallNotAllowed |
            Self::UnsupportedLibraries |
            Self::ErrorSelectForkOnBroadcast |
            Self::ScriptFailed => true,
        }
    }
}