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

Contract calls and abigen integration #11

Merged
merged 2 commits into from Dec 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 69 additions & 33 deletions fuels-abigen-macro/tests/harness.rs
Expand Up @@ -42,10 +42,11 @@ async fn compile_bindings_from_contract_file() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("0000000003b568d4000000000000002a000000000000000a", encoded);
assert_eq!("00000000c39ba1e9000000000000002a000000000000000a", encoded);
}

#[tokio::test]
Expand Down Expand Up @@ -88,7 +89,8 @@ async fn compile_bindings_from_inline_contract() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("0000000003b568d4000000000000002a000000000000000a", encoded);
Expand Down Expand Up @@ -130,7 +132,8 @@ async fn compile_bindings_single_param() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("000000009593586c000000000000002a", encoded);
Expand Down Expand Up @@ -170,7 +173,8 @@ async fn compile_bindings_array_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!(
Expand Down Expand Up @@ -213,7 +217,8 @@ async fn compile_bindings_bool_array_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!(
Expand Down Expand Up @@ -255,7 +260,8 @@ async fn compile_bindings_byte_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("00000000a4bd3861000000000000000a", encoded);
Expand Down Expand Up @@ -294,7 +300,8 @@ async fn compile_bindings_string_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!(
Expand Down Expand Up @@ -341,7 +348,8 @@ async fn compile_bindings_b256_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!(
Expand Down Expand Up @@ -398,7 +406,8 @@ async fn compile_bindings_struct_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("00000000f5957fce000000000000000a0000000000000001", encoded);
Expand Down Expand Up @@ -458,7 +467,8 @@ async fn compile_bindings_nested_struct_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("00000000e8a04d9c000000000000000a0000000000000001", encoded);
Expand Down Expand Up @@ -507,7 +517,8 @@ async fn compile_bindings_enum_input() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("000000009542a3c90000000000000000000000000000002a", encoded);
Expand Down Expand Up @@ -565,7 +576,8 @@ async fn create_struct_from_decoded_tokens() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("00000000f5957fce000000000000000a0000000000000001", encoded);
Expand Down Expand Up @@ -634,7 +646,8 @@ async fn create_nested_struct_from_decoded_tokens() {

let encoded = format!(
"{}{}",
contract_call.encoded_selector, contract_call.encoded_params
hex::encode(contract_call.encoded_selector),
hex::encode(contract_call.encoded_args)
);

assert_eq!("00000000e8a04d9c000000000000000a0000000000000001", encoded);
Expand All @@ -653,12 +666,24 @@ async fn example_workflow() {
{
"type": "function",
"inputs": [
{
"name": "gas",
"type": "u64"
},
{
"name": "coin",
"type": "u64"
},
{
"name": "color",
"type": "b256"
},
{
"name": "arg",
"type": "u64"
}
],
"name": "initialize",
"name": "initialize_counter",
"outputs": [
{
"name": "arg",
Expand All @@ -669,12 +694,24 @@ async fn example_workflow() {
{
"type": "function",
"inputs": [
{
"name": "gas",
"type": "u64"
},
{
"name": "coin",
"type": "u64"
},
{
"name": "color",
"type": "b256"
},
{
"name": "arg",
"type": "u64"
}
],
"name": "increment",
"name": "increment_counter",
"outputs": [
{
"name": "arg",
Expand All @@ -693,26 +730,25 @@ async fn example_workflow() {
let compiled =
Contract::compile_sway_contract("tests/test_projects/contract_test", salt).unwrap();

// Launch local network and deploy contract to testnet.
// Note that if `false` was passed to `stop_node`,
// `launch_and_deploy` would return a child process
// and we would be responsible for killing this process once
// we're done with testing.
// This is useful in case of long-lived local tests, spanning
// across different contracts being deployed and interacted with in
// the same session.
let (fuel_client, contract_id) = Contract::launch_and_deploy(&compiled).await.unwrap();
let (client, contract_id) = Contract::launch_and_deploy(&compiled).await.unwrap();

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

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

let result = contract_instance
.initialize_counter(42) // Build the ABI call
.call() // Perform the network call
.await
.unwrap();

assert_eq!(42, result.unwrap());

let contract_call = contract_instance.initialize(42);
let result = contract_instance
.increment_counter(10)
.call()
.await
.unwrap();

// 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().await.unwrap();
println!("res: {:?}\n", res);
assert_eq!(52, result.unwrap());
}
12 changes: 12 additions & 0 deletions fuels-abigen-macro/tests/takes_ints_returns_bool.json
Expand Up @@ -2,6 +2,18 @@
{
"type": "function",
"inputs": [
{
"name": "gas",
"type": "u64"
},
{
"name": "coin",
"type": "u64"
},
{
"name": "color",
"type": "b256"
},
{
"name": "arg",
"type": "u32"
Expand Down
26 changes: 15 additions & 11 deletions fuels-abigen-macro/tests/test_projects/contract_test/src/main.sw
@@ -1,19 +1,23 @@
contract;

use increment_abi::Incrementor;
use std::storage::store_u64;
use std::storage::get_u64;

const key = 0x0000000000000000000000000000000000000000000000000000000000000000;
abi TestContract {
fn initialize_counter(gas_: u64, amount_: u64, coin_: b256, value: u64) -> u64;
fn increment_counter(gas_: u64, amount_: u64, coin_: b256, amount: u64) -> u64;
}

const COUNTER_KEY = 0x0000000000000000000000000000000000000000000000000000000000000000;

impl Incrementor for Contract {
fn initialize(gas: u64, amt: u64, color: b256, initial_value: u64) -> u64 {
store_u64(key, initial_value);
initial_value
impl TestContract for Contract {
fn initialize_counter(gas_: u64, amount_: u64, color_: b256, value: u64) -> u64 {
store_u64(COUNTER_KEY, value);
value
}
fn increment(gas: u64, amt: u64, color: b256, increment_by: u64) -> u64 {
let new_val = get_u64(key) + 1;
store_u64(key, new_val);
new_val
fn increment_counter(gas_: u64, amount_: u64, color_: b256, amount: u64) -> u64 {
let value = get_u64(COUNTER_KEY) + amount;
store_u64(COUNTER_KEY, value);
value
}
}
}
15 changes: 15 additions & 0 deletions fuels-rs/src/code_gen/functions_gen.rs
Expand Up @@ -93,6 +93,21 @@ fn expand_function_arguments(
// 1. The name of the argument;
// 2. The type of the argument;
for (i, param) in fun.inputs.iter().enumerate() {
// This is a (likely) temporary workaround the fact that
// Sway ABI functions require gas, coin amount, and color arguments
// pre-pending the user-defined function arguments.
// Since these values (gas, coin, color) are configured elsewhere when
// creating a contract instance in the SDK, it would be noisy to keep them
// in the signature of the function that we're expanding here.
// It's the difference between being forced to write:
// contract_instance.increment_counter($gas, $coin, $color, 42)
// versus simply writing:
// contract_instance.increment_counter(42)
// Note that _any_ significant change in the way the JSON ABI is generated
// could affect this function expansion.
if param.name == "gas" || param.name == "coin" || param.name == "color" {
continue;
}
Comment on lines +96 to +110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed gas/coin/color from the ABI JSON and hardcoded them in our function selector generator for this. Your solution looks better but we are going to remove these anyways so I'm going to keep my dirty solution 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha, yeah, seems like we both had to come up with dirty solutions for this. It bothers me a little bit, but this (and your approach too) is the simplest one to "revert" once these are removed from the ABI JSON.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you guys file issues to go back and remove the hack once the Sway compiler fixes this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TokenStream representing the name of the argument
let name = expand_input_name(i, &param.name);

Expand Down