Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foundry-like tests #1231

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading