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

Commit

Permalink
Add LibraryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Apr 29, 2020
1 parent 54c9812 commit 99f9306
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ethabi = "12.0.0"
ethabi-derive = "12.0.0"
ethabi-contract = "11.0.0"
solaris = { path = "../solaris", version = "0.1" }
solc = { path = "../solc", version = "0.1" }
ethereum-types = "0.9.0"
rustc-hex = "1.0"

Expand Down
12 changes: 12 additions & 0 deletions tests/contracts/test.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
pragma solidity ^0.5.17;

library TestLibrary {
function getValue() public returns(uint) {
return 300;
}
}

contract GetSenderTest {
function getSender() public returns(address) {
return msg.sender;
Expand Down Expand Up @@ -37,3 +43,9 @@ contract EventLogTest {
emit Bar(value);
}
}

contract LibraryTest {
function getValueFromLibrary() public returns(uint) {
return TestLibrary.getValue();
}
}
47 changes: 47 additions & 0 deletions tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ extern crate ethabi_derive;
extern crate ethereum_types;
extern crate rustc_hex;
extern crate solaris;
extern crate solc;

use std::fs;
use rustc_hex::FromHex;
use ethereum_types::{Address, U256};

Expand All @@ -40,6 +42,11 @@ use_contract!(
"contracts/ConstructorTest.abi"
);

use_contract!(
library_test,
"contracts/LibraryTest.abi"
);

#[test]
fn msg_sender_should_match_value_passed_into_with_sender() {
let mut evm = solaris::evm();
Expand Down Expand Up @@ -187,3 +194,43 @@ fn value_should_match_value_passed_into_constructor() {

assert_eq!(output, U256::from(100));
}

#[test]
fn deploy_contract_with_linking_library_should_succeed() {
let mut evm = solaris::evm();

let contract_owner_address: Address = Address::from_low_u64_be(3);

// deploy TestLibrary
let code_hex = include_str!("../contracts/TestLibrary.bin");
let code_bytes = code_hex.from_hex().unwrap();
let library_address = evm.with_sender(contract_owner_address)
.deploy(&code_bytes)
.expect("library deployment should succeed");

// link to deployed library
solc::link(
vec![format!("test.sol:TestLibrary:{:x}", library_address)],
"LibraryTest.bin".into(),
concat!(env!("CARGO_MANIFEST_DIR"), "/contracts/"));

// deploy LibraryTest
// can't use include_str because the bytecode is updated linking to library
let code_hex = fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/contracts/LibraryTest.bin")).unwrap();
let code_bytes = code_hex.from_hex().unwrap();
let _contract_address = evm.with_sender(contract_owner_address)
.deploy(&code_bytes)
.expect("contract deployment should succeed");

use library_test::functions;

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

let output: U256 = functions::get_value_from_library::decode_output(&result_data)
.unwrap();

assert_eq!(output, U256::from(300));
}

0 comments on commit 99f9306

Please sign in to comment.