Skip to content

Commit

Permalink
Merge pull request #1231 from multiversx/foundry
Browse files Browse the repository at this point in the history
Foundry-like tests
  • Loading branch information
andrei-marinica authored Oct 10, 2023
2 parents d9c77f6 + e335988 commit 298a08c
Show file tree
Hide file tree
Showing 83 changed files with 4,560 additions and 419 deletions.
1,172 changes: 759 additions & 413 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,17 @@ members = [
"contracts/feature-tests/rust-testing-framework-tester/meta",
"contracts/feature-tests/use-module",
"contracts/feature-tests/use-module/meta",

"contracts/foundry/addercaller",
"contracts/foundry/addercaller/meta",
"contracts/foundry/callee",
"contracts/foundry/callee/meta",
"contracts/foundry/caller",
"contracts/foundry/caller/meta",
"contracts/foundry/foundrylike",
"contracts/foundry/foundrylike/meta",
"contracts/foundry/test_multisig",
"contracts/foundry/test_multisig/meta",
"contracts/foundry/test_testapi",
"contracts/foundry/test_testapi/meta",
]
7 changes: 7 additions & 0 deletions contracts/foundry/addercaller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The mxpy output
/output*/
18 changes: 18 additions & 0 deletions contracts/foundry/addercaller/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "addercaller"
version = "0.0.0"
authors = [ "you",]
edition = "2018"
publish = false

[lib]
path = "src/addercaller.rs"

[dev-dependencies]
num-bigint = "0.4.2"

[dependencies.multiversx-sc]
version = "0.41.1"

[dev-dependencies.multiversx-sc-scenario]
version = "0.41.1"
14 changes: 14 additions & 0 deletions contracts/foundry/addercaller/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "addercaller-meta"
version = "0.0.0"
edition = "2018"
publish = false
authors = [ "you",]

[dev-dependencies]

[dependencies.addercaller]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.41.1"
3 changes: 3 additions & 0 deletions contracts/foundry/addercaller/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta::cli_main::<addercaller::AbiProvider>();
}
3 changes: 3 additions & 0 deletions contracts/foundry/addercaller/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
178 changes: 178 additions & 0 deletions contracts/foundry/addercaller/scenarios/addercaller.scen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"name": "addercaller",
"comment": "add then check",
"gasSchedule": "v3",
"steps": [
{
"step": "setState",
"accounts": {
"address:owner": {
"nonce": "1",
"balance": "100000",
"esdt": {
"str:MYESDT": "200"
}
}
},
"newAddresses": [
{
"creatorAddress": "address:owner",
"creatorNonce": "1",
"newAddress": "sc:adder"
},
{
"creatorAddress": "address:owner",
"creatorNonce": "2",
"newAddress": "sc:addercaller"
}
]
},
{
"step": "scDeploy",
"id": "deploy adder",
"tx": {
"from": "address:owner",
"contractCode": "file:../../myadder/output/adder.wasm",
"arguments": [
"5"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scDeploy",
"id": "deploy addercaller",
"tx": {
"from": "address:owner",
"contractCode": "file:../output/addercaller.wasm",
"arguments": [
"sc:adder"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "call adder",
"tx": {
"from": "address:owner",
"to": "sc:adder",
"function": "add",
"value": "1000",
"arguments": [
"3"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
},
"expect": {
"out": ["8"],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "call addercaller egld",
"tx": {
"from": "address:owner",
"to": "sc:addercaller",
"function": "call_adder",
"value": "1000",
"arguments": [
"4"
],
"gasLimit": "15,000,000",
"gasPrice": "0"
},
"expect": {
"out": ["12", "str:added"],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "call addercaller esdt",
"tx": {
"from": "address:owner",
"to": "sc:addercaller",
"function": "call_adder_esdt",
"esdtValue": [
{
"tokenIdentifier": "str:MYESDT",
"value": "30"
}
],
"arguments": [
"5"
],
"gasLimit": "15,000,000",
"gasPrice": "0"
},
"expect": {
"out": ["17", "str:added-esdt"],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "checkState",
"accounts": {
"address:owner": {
"nonce": "*",
"balance": "98000",
"storage": {},
"code": "",
"esdt": {
"str:MYESDT": "170"
}
},
"sc:adder": {
"nonce": "0",
"balance": "1030",
"storage": {
"str:sum": "17"
},
"code": "file:../../myadder/output/adder.wasm",
"esdt": {
"str:MYESDT": "20"
}
},
"sc:addercaller": {
"nonce": "0",
"balance": "970",
"storage": {
"str:dest": "sc:adder"
},
"code": "file:../output/addercaller.wasm",
"esdt": {
"str:MYESDT": "10"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "addercaller",
"comment": "add then check",
"gasSchedule": "v3",
"steps": [
{
"step": "setState",
"accounts": {
"address:owner": {
"nonce": "1",
"balance": "100000",
"esdt": {
"str:MYESDT": "200"
}
}
},
"newAddresses": [
{
"creatorAddress": "address:owner",
"creatorNonce": "1",
"newAddress": "sc:adder"
}
]
},
{
"step": "scDeploy",
"id": "deploy adder",
"tx": {
"from": "address:owner",
"contractCode": "file:../../myadder/output/adder.wasm",
"arguments": [
"5"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "call adder: insufficient EGLD balance",
"tx": {
"from": "address:owner",
"to": "sc:adder",
"function": "add",
"value": "1000000",
"arguments": [
"3"
],
"gasLimit": "5,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "7",
"message": "*",
"logs": "*",
"gas": "*",
"refund": "*"
}
}
]
}
65 changes: 65 additions & 0 deletions contracts/foundry/addercaller/src/addercaller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// A smart contract to test transfer & execute functions
// Initialize the contract with the address of the adder
// The endpoints `call_adder` and `call_adder_esdt` accepts
// tokens in EGLD and ESDT and performs transfer & execute
// to the adder's `add` endpoint.

#![no_std]

multiversx_sc::imports!();

/// One of the simplest smart contracts possible,
/// it holds a single variable in storage, which anyone can increment.
#[multiversx_sc::contract]
pub trait AdderCaller {
#[storage_mapper("dest")]
fn dest(&self) -> SingleValueMapper<ManagedAddress>;

#[init]
fn init(&self, dest: &ManagedAddress) {
self.dest().set(dest);
}

#[endpoint]
#[payable("EGLD")]
fn call_adder(&self, value: BigUint) -> ManagedBuffer {
let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(value);

let result = self.send_raw().direct_egld_execute(
&self.dest().get(),
&BigUint::from(30u32),
5000000,
&ManagedBuffer::from(b"add"),
&arg_buffer,
);

match result {
Result::Err(e) => sc_panic!(e),
Result::Ok(_) => ManagedBuffer::from("added"),
}
}

#[endpoint]
#[payable("MYESDT")]
fn call_adder_esdt(&self, value: BigUint) -> ManagedBuffer {
let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(value);

let result = self.send_raw().transfer_esdt_execute(
&self.dest().get(),
&TokenIdentifier::from_esdt_bytes(b"MYESDT"),
&BigUint::from(20u32),
5000000,
&ManagedBuffer::from(b"add"),
&arg_buffer,
);

match result {
Result::Err(e) => sc_panic!(e),
Result::Ok(_) => ManagedBuffer::from("added-esdt"),
}
}
}

//
Loading

0 comments on commit 298a08c

Please sign in to comment.