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

chore: update sway's counter example #1601

Merged
merged 4 commits into from
Feb 17, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions e2e/sway/contracts/contract_test/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ abi TestContract {
#[storage(write)]
fn initialize_counter(value: u64) -> u64;
#[storage(read, write)]
fn increment_counter(value: u64) -> u64;
fn increment_counter(amount: u64) -> u64;
#[storage(read)]
fn get_counter() -> u64;
fn read_counter() -> u64;
// ANCHOR: low_level_call
#[storage(write)]
fn set_value_multiple_complex(a: MyStruct, b: str[4]);
Expand All @@ -35,8 +35,6 @@ abi TestContract {
fn get_str_value() -> str[4];
#[storage(read)]
fn get_bool_value() -> bool;
#[storage(read)]
fn get_value() -> u64;
fn get(x: u64, y: u64) -> u64;
fn get_alt(x: MyType) -> MyType;
fn get_single(x: u64) -> u64;
Expand All @@ -48,10 +46,8 @@ abi TestContract {
fn new() -> u64;
}

const COUNTER_KEY = 0x0000000000000000000000000000000000000000000000000000000000000000;

storage {
value: u64 = 0,
counter: u64 = 0,
value_str: str[4] = __to_str_array("none"),
value_bool: bool = false,
}
Expand All @@ -70,27 +66,29 @@ impl TestContract for Contract {
// ANCHOR_END: msg_amount
#[storage(write)]
fn initialize_counter(value: u64) -> u64 {
write(COUNTER_KEY, 0, value);
storage.counter.write(value);

value
}

/// This method will read the counter from storage, increment it
/// and write the incremented value to storage
#[storage(read, write)]
fn increment_counter(value: u64) -> u64 {
let new_value = read::<u64>(COUNTER_KEY, 0).unwrap_or(0) + value;
write(COUNTER_KEY, 0, new_value);
new_value
fn increment_counter(amount: u64) -> u64 {
let incremented = storage.counter.read() + amount;
storage.counter.write(incremented);

incremented
}

#[storage(read)]
fn get_counter() -> u64 {
read::<u64>(COUNTER_KEY, 0).unwrap_or(0)
fn read_counter() -> u64 {
storage.counter.read()
}

#[storage(write)]
fn set_value_multiple_complex(a: MyStruct, b: str[4]) {
storage.value.write(a.b[1]);
storage.counter.write(a.b[1]);
storage.value_str.write(b);
storage.value_bool.write(a.a);
}
Expand All @@ -105,11 +103,6 @@ impl TestContract for Contract {
storage.value_bool.read()
}

#[storage(read)]
fn get_value() -> u64 {
storage.value.read()
}

fn get(x: u64, y: u64) -> u64 {
x + y
}
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ async fn low_level_call() -> Result<()> {

let response = target_contract_instance
.methods()
.get_counter()
.read_counter()
.call()
.await?;
assert_eq!(response.value, 42);
Expand Down Expand Up @@ -1307,7 +1307,7 @@ async fn low_level_call() -> Result<()> {

let result_uint = target_contract_instance
.methods()
.get_counter()
.read_counter()
.call()
.await
.unwrap()
Expand All @@ -1329,7 +1329,7 @@ async fn low_level_call() -> Result<()> {
.unwrap()
.value;

assert_eq!(result_uint, 42);
assert_eq!(result_uint, 2);
assert!(result_bool);
assert_eq!(result_str, "fuel");

Expand Down Expand Up @@ -2284,7 +2284,7 @@ async fn regular_contract_can_be_deployed() -> Result<()> {

let response = contract_instance
.methods()
.get_counter()
.read_counter()
.call()
.await?
.value;
Expand Down
6 changes: 3 additions & 3 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mod tests {
.await?;
// ANCHOR_END: contract_call_cost_estimation

let expected_gas = 2816;
let expected_gas = 2596;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down Expand Up @@ -636,7 +636,7 @@ mod tests {
.await?;
// ANCHOR_END: multi_call_cost_estimation

let expected_gas = 4402;
let expected_gas = 4200;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down Expand Up @@ -769,7 +769,7 @@ mod tests {

let result_uint = target_contract_instance
.methods()
.get_value()
.read_counter()
.call()
.await
.unwrap()
Expand Down
Loading