forked from Evalir/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevert.rs
225 lines (188 loc) · 7.18 KB
/
revert.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
use crate::abi::VENDING_MACHINE_CONTRACT;
use anvil::{spawn, NodeConfig};
use ethers::{
contract::{ContractFactory, ContractInstance},
middleware::SignerMiddleware,
types::U256,
utils::WEI_IN_ETHER,
};
use ethers_solc::{project_util::TempProject, Artifact};
use std::sync::Arc;
#[tokio::test(flavor = "multi_thread")]
async fn test_deploy_reverting() {
let prj = TempProject::dapptools().unwrap();
prj.add_source(
"Contract",
r#"
pragma solidity 0.8.13;
contract Contract {
constructor() {
require(false, "");
}
}
"#,
)
.unwrap();
let mut compiled = prj.compile().unwrap();
assert!(!compiled.has_compiler_errors());
let contract = compiled.remove_first("Contract").unwrap();
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
let factory = ContractFactory::new(abi.unwrap(), bytecode.unwrap(), client);
let contract = factory.deploy(()).unwrap().send().await;
assert!(contract.is_err());
// should catch the revert during estimation which results in an err
let err = contract.unwrap_err();
assert!(err.to_string().contains("execution reverted"));
}
#[tokio::test(flavor = "multi_thread")]
async fn test_revert_messages() {
let prj = TempProject::dapptools().unwrap();
prj.add_source(
"Contract",
r#"
pragma solidity 0.8.13;
contract Contract {
address owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "!authorized");
_;
}
function getSecret() public onlyOwner view returns(uint256 secret) {
return 123;
}
}
"#,
)
.unwrap();
let mut compiled = prj.compile().unwrap();
assert!(!compiled.has_compiler_errors());
let contract = compiled.remove_first("Contract").unwrap();
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));
// deploy successfully
let factory = ContractFactory::new(abi.clone().unwrap(), bytecode.unwrap(), client);
let contract = factory.deploy(()).unwrap().send().await.unwrap();
let contract = ContractInstance::new(
contract.address(),
abi.unwrap(),
SignerMiddleware::new(handle.http_provider(), wallets[1].clone()),
);
let resp = contract.method::<_, U256>("getSecret", ()).unwrap().call().await;
let err = resp.unwrap_err();
let msg = err.to_string();
assert!(msg.contains("execution reverted: !authorized"));
}
#[tokio::test(flavor = "multi_thread")]
async fn test_solc_revert_example() {
let prj = TempProject::dapptools().unwrap();
prj.add_source("VendingMachine", VENDING_MACHINE_CONTRACT).unwrap();
let mut compiled = prj.compile().unwrap();
assert!(!compiled.has_compiler_errors());
let contract = compiled.remove_first("VendingMachine").unwrap();
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));
// deploy successfully
let factory = ContractFactory::new(abi.clone().unwrap(), bytecode.unwrap(), client);
let contract = factory.deploy(()).unwrap().send().await.unwrap();
let contract = ContractInstance::new(
contract.address(),
abi.unwrap(),
SignerMiddleware::new(handle.http_provider(), wallets[1].clone()),
);
for fun in ["buyRevert", "buyRequire"] {
let resp = contract.method::<_, ()>(fun, U256::zero()).unwrap().call().await;
resp.unwrap();
let ten = WEI_IN_ETHER.saturating_mul(10u64.into());
let call = contract.method::<_, ()>(fun, ten).unwrap().value(ten);
let resp = call.clone().call().await;
let err = resp.unwrap_err().to_string();
assert!(err.contains("execution reverted: Not enough Ether provided."));
assert!(err.contains("code: 3"));
}
}
// <https://github.com/foundry-rs/foundry/issues/1871>
#[tokio::test(flavor = "multi_thread")]
async fn test_another_revert_message() {
let prj = TempProject::dapptools().unwrap();
prj.add_source(
"Contract",
r#"
pragma solidity 0.8.13;
contract Contract {
uint256 public number;
function setNumber(uint256 num) public {
require(num != 0, "RevertStringFooBar");
number = num;
}
}
"#,
)
.unwrap();
let mut compiled = prj.compile().unwrap();
assert!(!compiled.has_compiler_errors());
let contract = compiled.remove_first("Contract").unwrap();
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));
// deploy successfully
let factory = ContractFactory::new(abi.clone().unwrap(), bytecode.unwrap(), client);
let contract = factory.deploy(()).unwrap().send().await.unwrap();
let contract = ContractInstance::new(
contract.address(),
abi.unwrap(),
SignerMiddleware::new(handle.http_provider(), wallets[1].clone()),
);
let call = contract.method::<_, ()>("setNumber", U256::zero()).unwrap();
let resp = call.send().await;
let err = resp.unwrap_err();
let msg = err.to_string();
assert!(msg.contains("execution reverted: RevertStringFooBar"));
}
#[tokio::test(flavor = "multi_thread")]
async fn test_solc_revert_custom_errors() {
let prj = TempProject::dapptools().unwrap();
prj.add_source(
"Contract",
r#"
pragma solidity 0.8.13;
contract Contract {
uint256 public number;
error AddressRevert(address);
function revertAddress() public {
revert AddressRevert(address(1));
}
}
"#,
)
.unwrap();
let mut compiled = prj.compile().unwrap();
assert!(!compiled.has_compiler_errors());
let contract = compiled.remove_first("Contract").unwrap();
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));
// deploy successfully
let factory =
ContractFactory::new(abi.clone().unwrap(), bytecode.unwrap(), Arc::clone(&client));
let contract = factory.deploy(()).unwrap().send().await.unwrap();
let call = contract.method::<_, ()>("revertAddress", ()).unwrap().gas(150000);
let resp = call.call().await;
let _ = resp.unwrap_err();
}