-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for contract calls with struct as inputs (#36)
* Add support for contract calls with struct as inputs Before the changes introduced here, using the SDK to call contracts that takes structs as input would cause weird bugs -- it didn't really cause a panic on the VM, but it made the access to fields within a struct retrieve garbage data. That was happening because in the `script_data` we sent in the tx we weren't including a `call_data_offset` that's computed as: ```rust let call_data_offset = script_data_offset as usize + ContractId::LEN + 2 * WORD_SIZE; ``` Once that was included, struct field access worked perfectly. Curiously, this `call_data_offset` isn't needed if the input is a primitive type (u8, bool, ...). So a conditional check that adds the `call_data_offset` to the `script_data` iff the ABI method called takes a struct is now in place. This enables users to successfully perform the following: ```Rust // ... let contract_instance = MyContract::new(compiled, client); let counter_config = CounterConfig { dummy: true, initial_value: 42, }; let result = contract_instance .initialize_counter(counter_config) .call() .await .unwrap(); assert_eq!(42, result); ``` * Tweak calldata offset documentation
- Loading branch information
Showing
5 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
fuels-abigen-macro/tests/test_projects/complex_types_contract/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[project] | ||
author = "Rodrigo Araujo" | ||
license = "MIT" | ||
name = "contract_test" | ||
entry = "main.sw" | ||
|
||
[dependencies] | ||
std = { path = "../lib-std" } | ||
core = { path = "../lib-core" } |
31 changes: 31 additions & 0 deletions
31
fuels-abigen-macro/tests/test_projects/complex_types_contract/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
contract; | ||
|
||
use std::storage::store; | ||
use std::storage::get; | ||
use std::chain::log_u64; | ||
use std::chain::log_u8; | ||
|
||
struct CounterConfig { | ||
dummy: bool, | ||
initial_value: u64, | ||
} | ||
|
||
abi TestContract { | ||
fn initialize_counter(gas_: u64, amount_: u64, color_: b256, config: CounterConfig) -> u64; | ||
fn increment_counter(gas_: u64, amount_: u64, color_: b256, amount: u64) -> u64; | ||
} | ||
|
||
const COUNTER_KEY = 0x0000000000000000000000000000000000000000000000000000000000000000; | ||
|
||
impl TestContract for Contract { | ||
fn initialize_counter(gas_: u64, amount_: u64, color_: b256, config: CounterConfig) -> u64 { | ||
let value = config.initial_value; | ||
store(COUNTER_KEY, value); | ||
value | ||
} | ||
fn increment_counter(gas_: u64, amount_: u64, color_: b256, amount: u64) -> u64 { | ||
let value = get::<u64>(COUNTER_KEY) + amount; | ||
store(COUNTER_KEY, value); | ||
value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ async fn contract_call() { | |
gas_price, | ||
gas_limit, | ||
maturity, | ||
false, | ||
) | ||
.await | ||
.unwrap(); | ||
|