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

Conversation

digorithm
Copy link
Member

This PR introduces the integration of contract calls and the previous abigen! functionality.

Meaning that, now, the Rust bindings generated from the JSON ABI can build a script and script_data on the fly and call an ABI method from a deployed contract.

This is part of a larger work documented at FuelLabs/sway#124.

Example:

let salt: [u8; 32] = rng.gen();
let salt = Salt::from(salt);

let compiled =
    Contract::compile_sway_contract("tests/test_projects/contract_test", salt).unwrap();

let (client, contract_id) = Contract::launch_and_deploy(&compiled).await.unwrap();

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

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 result = contract_instance
    .increment_counter(10)
    .call()
    .await
    .unwrap();

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

@digorithm digorithm self-assigned this Nov 26, 2021
@digorithm digorithm added the enhancement New feature or request label Nov 26, 2021
AlicanC
AlicanC previously approved these changes Nov 29, 2021
Comment on lines +96 to +110
// 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;
}
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.

fuels-rs/src/contract.rs Show resolved Hide resolved
fuels-rs/src/contract.rs Show resolved Hide resolved
Copy link
Contributor

@adlerjohn adlerjohn left a comment

Choose a reason for hiding this comment

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

lgtm

@digorithm digorithm merged commit 7b253f8 into master Dec 1, 2021
@digorithm digorithm deleted the rodrigo/contract-calls branch December 1, 2021 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants