Skip to content

Commit 750080a

Browse files
authored
anvil-polkadot: add support for testing smart contracts (paritytech#342)
* anvil-polkadot: add support for testing smart contracts
1 parent 26eda0d commit 750080a

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"abi": [
3+
{
4+
"inputs": [],
5+
"stateMutability": "nonpayable",
6+
"type": "constructor"
7+
},
8+
{
9+
"inputs": [],
10+
"name": "getValue",
11+
"outputs": [
12+
{
13+
"internalType": "uint256",
14+
"name": "",
15+
"type": "uint256"
16+
}
17+
],
18+
"stateMutability": "view",
19+
"type": "function"
20+
},
21+
{
22+
"inputs": [
23+
{
24+
"internalType": "uint256",
25+
"name": "_value",
26+
"type": "uint256"
27+
}
28+
],
29+
"name": "setValue",
30+
"outputs": [],
31+
"stateMutability": "nonpayable",
32+
"type": "function"
33+
},
34+
{
35+
"inputs": [],
36+
"name": "storedValue",
37+
"outputs": [
38+
{
39+
"internalType": "uint256",
40+
"name": "",
41+
"type": "uint256"
42+
}
43+
],
44+
"stateMutability": "view",
45+
"type": "function"
46+
}
47+
],
48+
"bin": "6080604052348015600e575f5ffd5b505f5f81905550610171806100225f395ff3fe608060405234801561000f575f5ffd5b506004361061003f575f3560e01c8063209652551461004357806355241077146100615780636d619daa1461007d575b5f5ffd5b61004b61009b565b60405161005891906100c9565b60405180910390f35b61007b60048036038101906100769190610110565b6100a3565b005b6100856100ac565b60405161009291906100c9565b60405180910390f35b5f5f54905090565b805f8190555050565b5f5481565b5f819050919050565b6100c3816100b1565b82525050565b5f6020820190506100dc5f8301846100ba565b92915050565b5f5ffd5b6100ef816100b1565b81146100f9575f5ffd5b50565b5f8135905061010a816100e6565b92915050565b5f60208284031215610125576101246100e2565b5b5f610132848285016100fc565b9150509291505056fea2646970667358221220c547befd0ae0fa7030ccf4ffe07e8d359864145dafda34ae00e97cf69a78b19f64736f6c634300081e0033",
49+
"bin-runtime": "608060405234801561000f575f5ffd5b506004361061003f575f3560e01c8063209652551461004357806355241077146100615780636d619daa1461007d575b5f5ffd5b61004b61009b565b60405161005891906100c9565b60405180910390f35b61007b60048036038101906100769190610110565b6100a3565b005b6100856100ac565b60405161009291906100c9565b60405180910390f35b5f5f54905090565b805f8190555050565b5f5481565b5f819050919050565b6100c3816100b1565b82525050565b5f6020820190506100dc5f8301846100ba565b92915050565b5f5ffd5b6100ef816100b1565b81146100f9575f5ffd5b50565b5f8135905061010a816100e6565b92915050565b5f60208284031215610125576101246100e2565b5b5f610132848285016100fc565b9150509291505056fea2646970667358221220c547befd0ae0fa7030ccf4ffe07e8d359864145dafda34ae00e97cf69a78b19f64736f6c634300081e0033"
50+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pragma solidity ^0.8.0;
2+
3+
contract SimpleStorage {
4+
// Storage slot 0
5+
uint256 public storedValue;
6+
7+
constructor() {
8+
storedValue = 0;
9+
}
10+
11+
function setValue(uint256 _value) public {
12+
storedValue = _value;
13+
}
14+
15+
function getValue() public view returns (uint256) {
16+
return storedValue;
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use alloy_sol_types::sol;
2+
3+
sol!(
4+
#[derive(Debug)]
5+
SimpleStorage,
6+
"test-data/SimpleStorage.json"
7+
);

crates/anvil-polkadot/tests/it/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod abi;
12
mod impersonation;
23
mod mining;
34
mod standard_rpc;

crates/anvil-polkadot/tests/it/utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,26 @@ where
277277
ResponseResult::Error(err) => Err(err),
278278
}
279279
}
280+
281+
#[allow(unused)]
282+
pub struct ContractCode {
283+
pub init: Vec<u8>,
284+
pub runtime: Option<Vec<u8>>,
285+
}
286+
287+
#[allow(unused)]
288+
pub fn get_contract_code(name: &str) -> ContractCode {
289+
let contract_path =
290+
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("test-data/{name}.json"));
291+
292+
let contract_json: Value = serde_json::from_reader(std::io::BufReader::new(
293+
std::fs::File::open(contract_path).unwrap(),
294+
))
295+
.unwrap();
296+
297+
let init = hex::decode(contract_json.get("bin").unwrap().as_str().unwrap()).unwrap();
298+
let runtime =
299+
contract_json.get("bin-runtime").map(|code| hex::decode(code.as_str().unwrap()).unwrap());
300+
301+
ContractCode { init, runtime }
302+
}

0 commit comments

Comments
 (0)