-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(subxt-tests): integration test for substrate using subxt
Signed-off-by: Raymond Yeh <extraymond@gmail.com>
- Loading branch information
1 parent
12e9b97
commit e8109b6
Showing
29 changed files
with
4,810 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ Cargo.lock | |
**/*.rs.bk | ||
bundle.ll | ||
|
||
.helix/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rust-analyzer.linkedProjects": [ | ||
"./integration/subxt-tests/Cargo.toml" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pragma solidity ^0.8.0; | ||
import "./UniswapV2Pair.sol"; | ||
|
||
contract Creator { | ||
address public pair; | ||
|
||
constructor() public { | ||
pair = address(new UniswapV2Pair()); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
contracts/ | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
edition = "2021" | ||
name = "subxt-tests" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
anyhow = "1.0.71" | ||
async-trait = "0.1.68" | ||
sp-core = "20.0.0" | ||
sp-runtime = "23.0.0" | ||
sp-weights = "19.0.0" | ||
pallet-contracts-primitives = "23.0.0" | ||
hex = "0.4.3" | ||
num-bigint = "0.4.3" | ||
once_cell = "1.17.2" | ||
parity-scale-codec = { version = "3.5.0", features = ["derive"] } | ||
rand = "0.8.5" | ||
serde_json = "1.0.96" | ||
sp-keyring = "23.0.0" | ||
subxt = "0.28.0" | ||
tokio = {version = "1.28.2", features = ["rt-multi-thread", "macros", "time"]} | ||
contract-metadata = "3.0.1" | ||
contract-transcode = "3.0.1" | ||
|
||
[workspace] | ||
members = [] | ||
|
Binary file not shown.
135 changes: 135 additions & 0 deletions
135
integration/subxt-tests/src/cases/array_struct_mapping_storage.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
use contract_transcode::ContractMessageTranscoder; | ||
use parity_scale_codec::{Decode, Encode}; | ||
use sp_core::{hexdisplay::AsBytesRef, U256}; | ||
|
||
use crate::{Contract, DeployContract, Execution, ReadContract, WriteContract, API}; | ||
|
||
#[tokio::test] | ||
async fn case() -> anyhow::Result<()> { | ||
let api = API::new().await?; | ||
|
||
let mut contract = Contract::new("./contracts/array_struct_mapping_storage.contract")?; | ||
|
||
contract | ||
.deploy( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| t.encode::<_, String>("new", []).unwrap(), | ||
) | ||
.await?; | ||
|
||
contract | ||
.call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| t.encode::<_, _>("setNumber", ["2147483647"]).unwrap(), | ||
) | ||
.await?; | ||
|
||
let b_push = |t: &ContractMessageTranscoder| t.encode::<_, String>("push", []).unwrap(); | ||
|
||
contract | ||
.call(&api, sp_keyring::AccountKeyring::Alice, 0, &b_push) | ||
.await?; | ||
|
||
contract | ||
.call(&api, sp_keyring::AccountKeyring::Alice, 0, &b_push) | ||
.await?; | ||
|
||
for array_no in 0..2 { | ||
for i in 0..10 { | ||
contract | ||
.call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode::<_, _>( | ||
"set", | ||
[ | ||
format!("{}", array_no), | ||
format!("{}", 102 + i + array_no * 500), | ||
format!("{}", 300331 + i), | ||
], | ||
) | ||
.unwrap() | ||
}, | ||
) | ||
.await?; | ||
} | ||
} | ||
|
||
for array_no in 0..2 { | ||
for i in 0..10 { | ||
let rs = contract | ||
.try_call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode::<_, _>( | ||
"get", | ||
[ | ||
format!("{}", array_no), | ||
format!("{}", 102 + i + array_no * 500), | ||
], | ||
) | ||
.unwrap() | ||
}, | ||
) | ||
.await | ||
.and_then(|v| <U256>::decode(&mut v.as_bytes_ref()).map_err(Into::into))?; | ||
|
||
assert_eq!(rs, U256::from(300331_u128 + i)); | ||
} | ||
} | ||
|
||
contract | ||
.call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode::<_, _>("rm", [format!("{}", 0), format!("{}", 104)]) | ||
.unwrap() | ||
}, | ||
) | ||
.await?; | ||
|
||
for i in 0..10 { | ||
let rs = contract | ||
.try_call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode::<_, _>("get", [format!("{}", 0), format!("{}", 102 + i)]) | ||
.unwrap() | ||
}, | ||
) | ||
.await | ||
.and_then(|v| <U256>::decode(&mut v.as_bytes_ref()).map_err(Into::into))?; | ||
|
||
if i != 2 { | ||
assert_eq!(rs, U256::from(300331_u128 + i)); | ||
} else { | ||
assert_eq!(rs, U256::zero()); | ||
} | ||
} | ||
|
||
let rs = contract | ||
.try_call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| t.encode::<_, String>("number", []).unwrap(), | ||
) | ||
.await | ||
.and_then(|v| <i64>::decode(&mut v.as_bytes_ref()).map_err(Into::into))?; | ||
|
||
assert_eq!(rs, 2147483647); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use crate::{Contract, DeployContract, Execution, ReadContract, WriteContract, API}; | ||
use contract_transcode::{ContractMessageTranscoder, Value}; | ||
use hex::FromHex; | ||
|
||
use parity_scale_codec::{Decode, Encode}; | ||
use rand::{seq::SliceRandom, thread_rng, Rng}; | ||
use sp_core::{crypto::AccountId32, hexdisplay::AsBytesRef}; | ||
|
||
#[ignore] | ||
#[tokio::test] | ||
async fn case() -> anyhow::Result<()> { | ||
let api = API::new().await?; | ||
|
||
let mut contract = Contract::new("./contracts/arrays.contract")?; | ||
|
||
contract | ||
.deploy( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| t.encode::<_, String>("new", []).unwrap(), | ||
) | ||
.await?; | ||
|
||
let mut users = Vec::new(); | ||
|
||
for i in 0..3 { | ||
let rnd_addr = rand::random::<[u8; 32]>(); | ||
|
||
let name = format!("name{i}"); | ||
|
||
let id = u32::from_be_bytes(rand::random::<[u8; 4]>()); | ||
let mut perms = Vec::<String>::new(); | ||
|
||
let mut j: f64 = 0.0; | ||
while j < rand::thread_rng().gen_range(0.0..=3.0) { | ||
j += 1.0; | ||
|
||
let p = rand::thread_rng().gen_range(0..8); | ||
perms.push(format!("Perm{}", p + 1)); | ||
} | ||
|
||
contract | ||
.call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode( | ||
"addUser", | ||
[ | ||
id.to_string(), | ||
format!("0x{}", hex::encode(rnd_addr)), | ||
format!("\"{}\"", name.clone()), | ||
format!("[{}]", perms.join(",")), | ||
], | ||
) | ||
.unwrap() | ||
}, | ||
) | ||
.await?; | ||
|
||
users.push((name, rnd_addr, id, perms)); | ||
} | ||
|
||
let (name, addr, id, perms) = users.choose(&mut thread_rng()).unwrap(); | ||
|
||
let output = contract | ||
.try_call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode("getUserById", [format!("\"{id}\"")]).unwrap() | ||
}, | ||
) | ||
.await?; | ||
|
||
let (name, addr, id, perms) = | ||
<(String, AccountId32, u64, Vec<u8>)>::decode(&mut output.as_bytes_ref())?; | ||
|
||
if !perms.is_empty() { | ||
let p = perms.choose(&mut thread_rng()).unwrap(); | ||
|
||
let output = contract | ||
.try_call( | ||
&api, | ||
sp_keyring::AccountKeyring::Alice, | ||
0, | ||
&|t: &ContractMessageTranscoder| { | ||
t.encode( | ||
"hasPermission", | ||
[format!("\"{id}\""), format!("Perm{}", p + 1)], | ||
) | ||
.unwrap() | ||
}, | ||
) | ||
.await?; | ||
|
||
let has_permission = <bool>::decode(&mut output.as_bytes_ref())?; | ||
assert!(has_permission); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.