Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 8, 2023
1 parent ceb6179 commit 0dbfea2
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 119 deletions.
13 changes: 6 additions & 7 deletions contracts/foundry/addercaller/src/addercaller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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
// The endpoints `call_adder` and `call_adder_esdt` accepts
// tokens in EGLD and ESDT and performs transfer & execute
// to the adder's `add` endpoint.

Expand All @@ -12,7 +12,6 @@ multiversx_sc::imports!();
/// 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>;

Expand All @@ -23,7 +22,7 @@ pub trait AdderCaller {

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

Expand All @@ -37,13 +36,13 @@ pub trait AdderCaller {

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

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

Expand All @@ -58,9 +57,9 @@ pub trait AdderCaller {

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

//
//
3 changes: 1 addition & 2 deletions contracts/foundry/callee/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub trait CalleeContract {
#[endpoint]
#[payable("EGLD")]
fn fail_if_neg(&self, value: i64) -> ManagedBuffer {

require!( value >= 0 , "negative" );
require!(value >= 0, "negative");

if value == 0 {
ManagedBuffer::from("zero")
Expand Down
42 changes: 24 additions & 18 deletions contracts/foundry/caller/src/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ multiversx_sc::imports!();

#[multiversx_sc::contract]
pub trait CallerContract {


#[init]
fn init(&self) {
}
fn init(&self) {}

#[endpoint]
#[payable("EGLD")]
fn call_other(&self, dest: ManagedAddress, func: ManagedBuffer, value: i64, to_send: BigUint) -> ManagedBuffer {

fn call_other(
&self,
dest: ManagedAddress,
func: ManagedBuffer,
value: i64,
to_send: BigUint,
) -> ManagedBuffer {
let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(value);

let result = self.send_raw().direct_egld_execute(
&dest,
&to_send,
5000000,
&func,
&arg_buffer,
);
let result =
self.send_raw()
.direct_egld_execute(&dest, &to_send, 5000000, &func, &arg_buffer);

match result {
Result::Err(_) => ManagedBuffer::from("failed"),
Result::Ok(_) => ManagedBuffer::from("done")
Result::Ok(_) => ManagedBuffer::from("done"),
}
}

#[endpoint]
#[payable("EGLD")]
fn call_other_exec_on_dest_ctx(&self, dest: ManagedAddress, func: ManagedBuffer, value: i64, to_send: BigUint) -> ManagedBuffer {

fn call_other_exec_on_dest_ctx(
&self,
dest: ManagedAddress,
func: ManagedBuffer,
value: i64,
to_send: BigUint,
) -> ManagedBuffer {
let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(value);

Expand All @@ -45,8 +48,11 @@ pub trait CallerContract {
&func,
&arg_buffer,
);

require!(result.len() == 1, "ExecuteOnDestContext result data is empty");

require!(
result.len() == 1,
"ExecuteOnDestContext result data is empty"
);

ManagedBuffer::from("done")
}
Expand Down
57 changes: 28 additions & 29 deletions contracts/foundry/foundrylike/src/test_adder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

multiversx_sc::imports!();

static INIT_SUM : u32 = 5u32;
static INIT_SUM: u32 = 5u32;
#[multiversx_sc::contract]
pub trait TestAdder {

#[storage_mapper("ownerAddress")]
fn owner_address(&self) -> SingleValueMapper<ManagedAddress>;

Expand All @@ -15,60 +14,60 @@ pub trait TestAdder {
/// Create the owner account and deploy adder
#[init]
fn init(&self, code_path: ManagedBuffer) {

// create the owner account
let owner = ManagedAddress::from(b"owner___________________________");
self.owner_address().set(&owner);

self.test_raw().create_account(&owner, 1, &BigUint::from(0u64));


self.test_raw()
.create_account(&owner, 1, &BigUint::from(0u64));

// register an address for the contract to be deployed
let adder = ManagedAddress::from(b"adder___________________________");
self.test_raw().register_new_address(&owner, 1, &adder, );
self.test_raw().register_new_address(&owner, 1, &adder);

// deploy the adder contract
let mut adder_init_args = ManagedArgBuffer::new();
adder_init_args.push_arg(INIT_SUM); // initial sum

// deploy a contract from `owner`
let adder = self.test_raw().deploy_contract(
&owner,
5000000000000,
&BigUint::zero(),
&code_path,
&adder_init_args,
);
&owner,
5000000000000,
&BigUint::zero(),
&code_path,
&adder_init_args,
);

// save the deployed contract's address
self.adder_address().set(&adder);

// check the initial sum value
let sum_as_bytes = self.test_raw().get_storage(&adder, &ManagedBuffer::from(b"sum"));
let sum_as_bytes = self
.test_raw()
.get_storage(&adder, &ManagedBuffer::from(b"sum"));
let sum = BigUint::from(sum_as_bytes);
self.test_raw().assert( sum == INIT_SUM );

self.test_raw().assert(sum == INIT_SUM);
}

// Make a call from 'owner' to 'adder' and check the sum value
#[endpoint(test_call_add)]
fn test_call_add(&self, value: BigUint) {

self.test_raw().assume(value <= 100u32);

let adder = self.adder_address().get();

self.call_add(&value);

// check the sum value
let sum_as_bytes = self.test_raw().get_storage(&adder, &ManagedBuffer::from(b"sum"));
let sum_as_bytes = self
.test_raw()
.get_storage(&adder, &ManagedBuffer::from(b"sum"));
let sum = BigUint::from(sum_as_bytes);
self.test_raw().assert( sum == (value + INIT_SUM) );

self.test_raw().assert(sum == (value + INIT_SUM));
}

#[endpoint(test_call_add_twice)]
fn test_call_add_twice(&self, value1: BigUint, value2: BigUint) {

self.test_raw().assume(value1 <= 100u32);
self.test_raw().assume(value2 <= 100u32);

Expand All @@ -78,10 +77,11 @@ pub trait TestAdder {
self.call_add(&value2);

// check the sum value
let sum_as_bytes = self.test_raw().get_storage(&adder, &ManagedBuffer::from(b"sum"));
let sum_as_bytes = self
.test_raw()
.get_storage(&adder, &ManagedBuffer::from(b"sum"));
let sum = BigUint::from(sum_as_bytes);
self.test_raw().assert( sum == (value1 + value2 + INIT_SUM) );

self.test_raw().assert(sum == (value1 + value2 + INIT_SUM));
}

fn call_add(&self, value: &BigUint) {
Expand All @@ -94,18 +94,17 @@ pub trait TestAdder {
// start a prank and call 'adder' from 'owner'
self.test_raw().start_prank(&owner);
let res = self.send_raw().direct_egld_execute(
&adder,
&BigUint::from(0u32),
5000000,
&adder,
&BigUint::from(0u32),
5000000,
&ManagedBuffer::from(b"add"),
&adder_init_args,
);
self.test_raw().stop_prank();

match res {
Result::Err(_) => panic!("call failed"),
Result::Ok(_) => ()
Result::Ok(_) => (),
};

}
}
Loading

0 comments on commit 0dbfea2

Please sign in to comment.