Skip to content

Commit 6bce07d

Browse files
committed
Add better tests for state, code and balance overrides
1 parent 92c102e commit 6bce07d

File tree

2 files changed

+95
-18
lines changed

2 files changed

+95
-18
lines changed

crates/cast/src/cmd/call.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static OVERRIDE_PATTERN: LazyLock<Regex> =
5656
/// --override-nonce 0x123:1 \
5757
/// --override-code 0x123:0x1234 \
5858
/// --override-state 0x123:0x1:0x1234
59+
/// --override-state-diff 0x123:0x1:0x1234
5960
/// ```
6061
#[derive(Debug, Parser)]
6162
pub struct CallArgs {

crates/cast/tests/cli/main.rs

+94-18
Original file line numberDiff line numberDiff line change
@@ -2258,34 +2258,110 @@ forgetest_async!(cast_call_custom_chain_id, |_prj, cmd| {
22582258
});
22592259

22602260
// https://github.com/foundry-rs/foundry/issues/10189
2261-
forgetest_async!(cast_call_custom_override, |_prj, cmd| {
2262-
let (_api, handle) = anvil::spawn(
2263-
NodeConfig::test()
2264-
.with_auto_impersonate(true)
2265-
.with_eth_rpc_url(Some("https://sepolia.base.org")),
2261+
forgetest_async!(cast_call_custom_override, |prj, cmd| {
2262+
let (_, handle) = anvil::spawn(NodeConfig::test()).await;
2263+
2264+
foundry_test_utils::util::initialize(prj.root());
2265+
prj.add_source(
2266+
"Counter",
2267+
r#"
2268+
contract Counter {
2269+
uint256 public number;
2270+
2271+
function getBalance(address target) public returns (uint256) {
2272+
return target.balance;
2273+
}
2274+
}
2275+
"#,
22662276
)
2267-
.await;
2277+
.unwrap();
22682278

2269-
let http_endpoint = handle.http_endpoint();
2279+
// Deploy counter contract.
2280+
cmd.args([
2281+
"script",
2282+
"--private-key",
2283+
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
2284+
"--rpc-url",
2285+
&handle.http_endpoint(),
2286+
"--broadcast",
2287+
"CounterScript",
2288+
])
2289+
.assert_success();
22702290

2291+
// Override state, `number()` should return overridden value.
22712292
cmd.cast_fuse()
22722293
.args([
22732294
"call",
2274-
"5FbDB2315678afecb367f032d93F642f64180aa3",
2295+
"0x5FbDB2315678afecb367f032d93F642f64180aa3",
22752296
"--rpc-url",
2276-
&http_endpoint,
2297+
&handle.http_endpoint(),
2298+
"--override-state",
2299+
"0x5FbDB2315678afecb367f032d93F642f64180aa3:0x0:0x1234",
2300+
"number()(uint256)",
2301+
])
2302+
.assert_success()
2303+
.stdout_eq(str![[r#"
2304+
4660
2305+
2306+
"#]]);
2307+
2308+
// Override balance, `getBalance()` should return overridden value.
2309+
cmd.cast_fuse()
2310+
.args([
2311+
"call",
2312+
"0x5FbDB2315678afecb367f032d93F642f64180aa3",
2313+
"--rpc-url",
2314+
&handle.http_endpoint(),
22772315
"--override-balance",
2278-
"5FbDB2315678afecb367f032d93F642f64180aa3:1234",
2279-
"--override-nonce",
2280-
"5FbDB2315678afecb367f032d93F642f64180aa3:5",
2281-
// "--override-code",
2282-
// "5FbDB2315678afecb367f032d93F642f64180aa3:0x1234",
2316+
"0x5FbDB2315678afecb367f032d93F642f64180aa3:0x1111",
2317+
"getBalance(address)(uint256)",
2318+
"0x5FbDB2315678afecb367f032d93F642f64180aa3",
2319+
])
2320+
.assert_success()
2321+
.stdout_eq(str![[r#"
2322+
4369
2323+
2324+
"#]]);
2325+
2326+
// Override code with
2327+
// contract Counter {
2328+
// uint256 public number1;
2329+
// }
2330+
// Calling `number()` should fail.
2331+
cmd.cast_fuse()
2332+
.args([
2333+
"call",
2334+
"0x5FbDB2315678afecb367f032d93F642f64180aa3",
2335+
"--rpc-url",
2336+
&handle.http_endpoint(),
2337+
"--override-code",
2338+
"0x5FbDB2315678afecb367f032d93F642f64180aa3:0x6080604052348015600e575f5ffd5b50600436106026575f3560e01c8063c223a39e14602a575b5f5ffd5b60306044565b604051603b9190605f565b60405180910390f35b5f5481565b5f819050919050565b6059816049565b82525050565b5f60208201905060705f8301846052565b9291505056fea26469706673582212202a0acfb9083efed3e0e9f27177b090731d4392cf196d58e27e05088f59008d0964736f6c634300081d0033",
2339+
"number()(uint256)",
2340+
])
2341+
.assert_failure()
2342+
.stderr_eq(str![[r#"
2343+
Error: server returned an error response: error code 3: execution reverted, data: "0x"
2344+
2345+
"#]]);
2346+
2347+
// Calling `number1()` with overridden state should return new value.
2348+
cmd.cast_fuse()
2349+
.args([
2350+
"call",
2351+
"0x5FbDB2315678afecb367f032d93F642f64180aa3",
2352+
"--rpc-url",
2353+
&handle.http_endpoint(),
2354+
"--override-code",
2355+
"0x5FbDB2315678afecb367f032d93F642f64180aa3:0x6080604052348015600e575f5ffd5b50600436106026575f3560e01c8063c223a39e14602a575b5f5ffd5b60306044565b604051603b9190605f565b60405180910390f35b5f5481565b5f819050919050565b6059816049565b82525050565b5f60208201905060705f8301846052565b9291505056fea26469706673582212202a0acfb9083efed3e0e9f27177b090731d4392cf196d58e27e05088f59008d0964736f6c634300081d0033",
22832356
"--override-state",
2284-
"5FbDB2315678afecb367f032d93F642f64180aa3:0x1:1234",
2285-
// "--override-state-diff",
2286-
// "5FbDB2315678afecb367f032d93F642f64180aa3:0x2:1234",
2357+
"0x5FbDB2315678afecb367f032d93F642f64180aa3:0x0:0x2222",
2358+
"number1()(uint256)",
22872359
])
2288-
.assert_success();
2360+
.assert_success()
2361+
.stdout_eq(str![[r#"
2362+
8738
2363+
2364+
"#]]);
22892365
});
22902366

22912367
// https://github.com/foundry-rs/foundry/issues/9541

0 commit comments

Comments
 (0)