Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
Support call/transact to multiple contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Apr 17, 2020
1 parent 0701721 commit bcfa14f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
13 changes: 7 additions & 6 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ fn badge_reg_test_fee() {

use badgereg::functions;

let result_data = evm.call(functions::fee::encode_input()).unwrap();
let result_data = evm.call(functions::fee::encode_input(), None).unwrap();
// Initial fee is 1 ETH
assert_eq!(
functions::fee::decode_output(&result_data).unwrap(),
wei::from_ether(1)
);

// The owner should be able to set the fee
evm.transact(functions::set_fee::encode_input(wei::from_gwei(10))).unwrap();
evm.transact(functions::set_fee::encode_input(wei::from_gwei(10)), None).unwrap();

let result_data = evm.call(functions::fee::encode_input()).unwrap();
let result_data = evm.call(functions::fee::encode_input(), None).unwrap();
// Fee should be updated
assert_eq!(
functions::fee::decode_output(&result_data).unwrap(),
Expand All @@ -74,10 +74,10 @@ fn badge_reg_test_fee() {

// Other address should not be allowed to change the fee
evm.with_sender(Address::from_low_u64_be(10))
.transact(functions::set_fee::encode_input(wei::from_gwei(15)))
.transact(functions::set_fee::encode_input(wei::from_gwei(15)), None)
.unwrap();

let result_data = evm.call(functions::fee::encode_input()).unwrap();
let result_data = evm.call(functions::fee::encode_input(), None).unwrap();
// Fee should not be updated
assert_eq!(
functions::fee::decode_output(&result_data).unwrap(),
Expand All @@ -96,6 +96,7 @@ fn anyone_should_be_able_to_register_a_badge() {
.ensure_funds()
.transact(
functions::register::encode_input(Address::from_low_u64_be(10), convert::bytes32("test")),
None
)
.unwrap();

Expand All @@ -113,7 +114,7 @@ fn anyone_should_be_able_to_register_a_badge() {

// TODO [ToDr] Perhaps `with_` should not be persistent?
let result_data = evm.with_value(0.into())
.call(functions::from_name::encode_input(convert::bytes32("test")))
.call(functions::from_name::encode_input(convert::bytes32("test")), None)
.unwrap();

// Test that it was registered correctly
Expand Down
19 changes: 13 additions & 6 deletions solaris/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ impl Evm {
func(self).expect("Unexpected error occured.");
}

pub fn call(&mut self, encoded_input: ethabi::Bytes) -> error::Result<vm::ReturnData> {
let contract_address = self.contract_address
.expect("Contract address is not set. Did you forget to deploy the contract?");
pub fn call(&mut self, encoded_input: ethabi::Bytes, address: Option<Address>) -> error::Result<vm::ReturnData> {
let contract_address = match address {
Some(s) => s,
None => self.contract_address
.expect("Contract address is not set. Did you forget to deploy the contract?")
};

let mut params = vm::ActionParams::default();
params.sender = self.sender;
params.origin = self.sender;
Expand Down Expand Up @@ -215,9 +219,12 @@ impl Evm {
Ok(transact_success.into())
}

pub fn transact(&mut self, encoded_input: ethabi::Bytes) -> error::Result<TransactionOutput> {
let contract_address = self.contract_address
.expect("Contract address is not set. Did you forget to deploy the contract?");
pub fn transact(&mut self, encoded_input: ethabi::Bytes, address: Option<Address>) -> error::Result<TransactionOutput> {
let contract_address = match address {
Some(s) => s,
None => self.contract_address
.expect("Contract address is not set. Did you forget to deploy the contract?")
};
let env_info = self.env_info();
let nonce = self.evm.state().nonce(&self.sender).expect(STATE);
let transaction = Transaction {
Expand Down
20 changes: 10 additions & 10 deletions tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn msg_sender_should_match_value_passed_into_with_sender() {
let sender = Address::from_low_u64_be(5);

let result_data = evm.with_sender(sender)
.call(functions::get_sender::encode_input())
.call(functions::get_sender::encode_input(), None)
.unwrap();

let output: Address = functions::get_sender::decode_output(&result_data)
Expand Down Expand Up @@ -96,7 +96,7 @@ fn msg_value_should_match_value_passed_into_with_value() {

let result_data = evm.with_value(value)
.ensure_funds()
.call(functions::get_value::encode_input())
.call(functions::get_value::encode_input(), None)
.unwrap();

let output: U256 = functions::get_value::decode_output(&result_data)
Expand All @@ -122,20 +122,20 @@ fn logs_should_get_collected_and_retrieved_correctly() {

let first_sender_address = Address::from_low_u64_be(10);
evm.with_sender(first_sender_address)
.transact(functions::emit_foo::encode_input())
.transact(functions::emit_foo::encode_input(), None)
.unwrap();

let second_sender_address = Address::from_low_u64_be(11);
evm.with_sender(second_sender_address)
.transact(functions::emit_foo::encode_input())
.transact(functions::emit_foo::encode_input(), None)
.unwrap();

evm.transact(functions::emit_bar::encode_input(100)).unwrap();
evm.transact(functions::emit_bar::encode_input(101)).unwrap();
evm.transact(functions::emit_bar::encode_input(102)).unwrap();
evm.transact(functions::emit_bar::encode_input(100), None).unwrap();
evm.transact(functions::emit_bar::encode_input(101), None).unwrap();
evm.transact(functions::emit_bar::encode_input(102), None).unwrap();

// call should not show up in logs
evm.call(functions::emit_foo::encode_input())
evm.call(functions::emit_foo::encode_input(), None)
.unwrap();

assert_eq!(evm.raw_logs().len(), 5);
Expand Down Expand Up @@ -186,7 +186,7 @@ fn value_should_match_value_passed_into_constructor() {

let result_data = evm
.ensure_funds()
.call(functions::get_value::encode_input())
.call(functions::get_value::encode_input(), None)
.unwrap();

let output: U256 = functions::get_value::decode_output(&result_data)
Expand Down Expand Up @@ -226,7 +226,7 @@ fn deploy_contract_with_linking_library_should_succeed() {

let result_data = evm
.ensure_funds()
.call(functions::get_value_from_library::encode_input())
.call(functions::get_value_from_library::encode_input(), None)
.unwrap();

let output: U256 = functions::get_value_from_library::decode_output(&result_data)
Expand Down

0 comments on commit bcfa14f

Please sign in to comment.