Skip to content

Commit

Permalink
Require fuel_client for contract setup
Browse files Browse the repository at this point in the history
  • Loading branch information
digorithm committed Nov 24, 2021
1 parent 5f4ba2c commit 5853551
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 61 deletions.
2 changes: 2 additions & 0 deletions fuels-abigen-macro/Cargo.toml
Expand Up @@ -14,6 +14,8 @@ anyhow = "1"
bytes = { version = "1.0.1", features = ["serde"] }
forc = { git = "ssh://git@github.com/FuelLabs/sway.git", features = ["test", "util"], default-features = false }
fuel-asm = { git = "ssh://git@github.com/FuelLabs/fuel-asm.git", features = ["serde-types"] }
fuel-client = { git = "ssh://git@github.com/FuelLabs/fuel-core", default-features = false}
fuel-core = { git = "ssh://git@github.com/FuelLabs/fuel-core", default-features = false}
fuel-types = {git = "ssh://git@github.com/FuelLabs/fuel-types.git"}
fuels-rs = {path = "../fuels-rs"}
fuel-tx = { git = "ssh://git@github.com/FuelLabs/fuel-tx.git" }
Expand Down
115 changes: 74 additions & 41 deletions fuels-abigen-macro/tests/harness.rs
@@ -1,3 +1,5 @@
use fuel_client::client::FuelClient;
use fuel_core::service::{Config, FuelService};
use fuel_tx::Salt;
use fuels_abigen_macro::abigen;
use fuels_rs::contract::Contract;
Expand All @@ -6,17 +8,24 @@ use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use sha2::{Digest, Sha256};

#[test]
fn compile_bindings_from_contract_file() {
async fn setup_local_node() -> FuelClient {
let srv = FuelService::new_node(Config::local_node()).await.unwrap();
FuelClient::from(srv.bound_address)
}

#[tokio::test]
async fn compile_bindings_from_contract_file() {
// Generates the bindings from an ABI definition in a JSON file
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
SimpleContract,
"fuels-abigen-macro/tests/takes_ints_returns_bool.json"
);

let fuel_client = setup_local_node().await;

// `SimpleContract` is the name of the contract
let contract_instance = SimpleContract::new(Default::default());
let contract_instance = SimpleContract::new(Default::default(), fuel_client);

// Calls the function defined in the JSON ABI.
// Note that this is type-safe, if the function does exist
Expand All @@ -39,8 +48,8 @@ fn compile_bindings_from_contract_file() {
assert_eq!("0000000003b568d4000000000000002a000000000000000a", encoded);
}

#[test]
fn compile_bindings_from_inline_contract() {
#[tokio::test]
async fn compile_bindings_from_inline_contract() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand Down Expand Up @@ -71,7 +80,9 @@ fn compile_bindings_from_inline_contract() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_ints_returns_bool(42 as u32, 10 as u16);

Expand All @@ -83,8 +94,8 @@ fn compile_bindings_from_inline_contract() {
assert_eq!("0000000003b568d4000000000000002a000000000000000a", encoded);
}

#[test]
fn compile_bindings_single_param() {
#[tokio::test]
async fn compile_bindings_single_param() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand All @@ -111,7 +122,9 @@ fn compile_bindings_single_param() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_ints_returns_bool(42 as u32);

Expand All @@ -123,8 +136,8 @@ fn compile_bindings_single_param() {
assert_eq!("000000009593586c000000000000002a", encoded);
}

#[test]
fn compile_bindings_array_input() {
#[tokio::test]
async fn compile_bindings_array_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand All @@ -148,7 +161,9 @@ fn compile_bindings_array_input() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let input: Vec<u16> = vec![1, 2, 3, 4];
let contract_call = contract_instance.takes_array(input);
Expand All @@ -164,8 +179,8 @@ fn compile_bindings_array_input() {
);
}

#[test]
fn compile_bindings_bool_array_input() {
#[tokio::test]
async fn compile_bindings_bool_array_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand All @@ -189,7 +204,9 @@ fn compile_bindings_bool_array_input() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let input: Vec<bool> = vec![true, false, true];
let contract_call = contract_instance.takes_array(input);
Expand All @@ -205,8 +222,8 @@ fn compile_bindings_bool_array_input() {
);
}

#[test]
fn compile_bindings_byte_input() {
#[tokio::test]
async fn compile_bindings_byte_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand All @@ -230,7 +247,9 @@ fn compile_bindings_byte_input() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_byte(10 as u8);

Expand All @@ -242,8 +261,8 @@ fn compile_bindings_byte_input() {
assert_eq!("00000000a4bd3861000000000000000a", encoded);
}

#[test]
fn compile_bindings_string_input() {
#[tokio::test]
async fn compile_bindings_string_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand All @@ -267,7 +286,9 @@ fn compile_bindings_string_input() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_string("This is a full sentence".into());

Expand All @@ -282,8 +303,8 @@ fn compile_bindings_string_input() {
);
}

#[test]
fn compile_bindings_b256_input() {
#[tokio::test]
async fn compile_bindings_b256_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand All @@ -307,7 +328,9 @@ fn compile_bindings_b256_input() {
"#
);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let mut hasher = Sha256::new();
hasher.update("test string".as_bytes());
Expand All @@ -327,8 +350,8 @@ fn compile_bindings_b256_input() {
);
}

#[test]
fn compile_bindings_struct_input() {
#[tokio::test]
async fn compile_bindings_struct_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand Down Expand Up @@ -360,14 +383,16 @@ fn compile_bindings_struct_input() {
"#
);

let fuel_client = setup_local_node().await;

// Because of the abigen! macro, `MyStruct` is now in scope
// and can be used!
let input = MyStruct {
foo: 10 as u8,
bar: true,
};

let contract_instance = SimpleContract::new(Default::default());
let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_struct(input);

Expand All @@ -379,8 +404,8 @@ fn compile_bindings_struct_input() {
assert_eq!("00000000f5957fce000000000000000a0000000000000001", encoded);
}

#[test]
fn compile_bindings_nested_struct_input() {
#[tokio::test]
async fn compile_bindings_nested_struct_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand Down Expand Up @@ -425,7 +450,9 @@ fn compile_bindings_nested_struct_input() {
inner_struct,
};

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_nested_struct(input);

Expand All @@ -437,8 +464,8 @@ fn compile_bindings_nested_struct_input() {
assert_eq!("00000000e8a04d9c000000000000000a0000000000000001", encoded);
}

#[test]
fn compile_bindings_enum_input() {
#[tokio::test]
async fn compile_bindings_enum_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand Down Expand Up @@ -472,7 +499,9 @@ fn compile_bindings_enum_input() {

let variant = MyEnum::X(42);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_enum(variant);

Expand All @@ -484,8 +513,8 @@ fn compile_bindings_enum_input() {
assert_eq!("000000009542a3c90000000000000000000000000000002a", encoded);
}

#[test]
fn create_struct_from_decoded_tokens() {
#[tokio::test]
async fn create_struct_from_decoded_tokens() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand Down Expand Up @@ -528,7 +557,9 @@ fn create_struct_from_decoded_tokens() {
assert_eq!(10 as u8, struct_from_tokens.foo);
assert_eq!(true, struct_from_tokens.bar);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_struct(struct_from_tokens);

Expand All @@ -540,8 +571,8 @@ fn create_struct_from_decoded_tokens() {
assert_eq!("00000000f5957fce000000000000000a0000000000000001", encoded);
}

#[test]
fn create_nested_struct_from_decoded_tokens() {
#[tokio::test]
async fn create_nested_struct_from_decoded_tokens() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(
Expand Down Expand Up @@ -595,7 +626,9 @@ fn create_nested_struct_from_decoded_tokens() {
assert_eq!(10 as u16, nested_struct_from_tokens.x);
assert_eq!(true, nested_struct_from_tokens.inner_struct.a);

let contract_instance = SimpleContract::new(Default::default());
let fuel_client = setup_local_node().await;

let contract_instance = SimpleContract::new(Default::default(), fuel_client);

let contract_call = contract_instance.takes_nested_struct(nested_struct_from_tokens);

Expand Down Expand Up @@ -672,14 +705,14 @@ async fn example_workflow() {

println!("Contract deployed @ {:x}", contract_id);

let contract_instance = MyContract::new(compiled);
let contract_instance = MyContract::new(compiled, fuel_client);

let contract_call = contract_instance.initialize(42);

// Currently, contract calls are empty script calls.
// Soon it will be able to generate/craft the
// `script_data` on the fly and dynamically call a
// contract's function.
let res = contract_call.call(&fuel_client).await.unwrap();
let res = contract_call.call().await.unwrap();
println!("res: {:?}\n", res);
}
7 changes: 0 additions & 7 deletions fuels-abigen-macro/tests/test_projects/script_test/Forc.toml
Expand Up @@ -8,10 +8,3 @@ entry = "main.sw"
[dependencies]
std = { path = "../stdlib" }
increment_abi = { path = "../library_test" }

[[tx-input]]
type = "Contract"
contract-id = "0xe50103684750e4916cd9825b14cf7e6763ffcc6523a9e0af63de93dbd6e3d736"
utxo-id = "0xeeb578f9e1ebfb5b78f8ff74352370c120bc8cacead1f5e4f9c74aafe0ca6bfd"
balance-root = "0xeeb578f9e1ebfb5b78f8ff74352370c120bc8cacead1f5e4f9c74aafe0ca6bfd"
state-root = "0xeeb578f9e1ebfb5b78f8ff74352370c120bc8cacead1f5e4f9c74aafe0ca6bfd"
2 changes: 1 addition & 1 deletion fuels-rs/Cargo.toml
Expand Up @@ -37,4 +37,4 @@ url = "2.1"
[[test]]
name = "integration_tests"
path = "tests/lib.rs"
harness = true
harness = true
8 changes: 5 additions & 3 deletions fuels-rs/src/code_gen/abigen.rs
Expand Up @@ -92,14 +92,16 @@ impl Abigen {
use fuels_rs::contract::{Contract, ContractCall, CompiledContract};
use fuels_rs::tokens::{Tokenizable, Token};
use fuels_rs::types::{EnumSelector};
use fuel_client::client::FuelClient;

pub struct #name {
compiled: CompiledContract
compiled: CompiledContract,
fuel_client: FuelClient
}

impl #name {
pub fn new(compiled: CompiledContract) -> Self {
Self{ compiled }
pub fn new(compiled: CompiledContract, fuel_client: FuelClient) -> Self {
Self{ compiled, fuel_client }
}

#contract_functions
Expand Down
2 changes: 1 addition & 1 deletion fuels-rs/src/code_gen/functions_gen.rs
Expand Up @@ -49,7 +49,7 @@ pub fn expand_function(
Ok(quote! {
#doc
pub fn #name(&self #input) -> #result {
Contract::method_hash(&self.compiled,
Contract::method_hash(&self.fuel_client, &self.compiled,
#tokenized_signature, #arg).expect("method not found (this should never happen)")
}
})
Expand Down

0 comments on commit 5853551

Please sign in to comment.