forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump to fuels
v0.37
and add a test and docs for configurable
bloc…
…ks (FuelLabs#4255) ## Description Closes FuelLabs#4206 Three main changes: * Add new configuration-time constants tests to `sdk-harness`. This is important to make sure there are no regression in the backend. * Add a new example for `configurable` * Document `configurable` * Had to bump to `fuels 0.37` to be able to update `configurable` variables from the SDK. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
1 parent
ba5ffe9
commit b33463d
Showing
23 changed files
with
483 additions
and
188 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
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 |
---|---|---|
@@ -1,13 +1,37 @@ | ||
script; | ||
contract; | ||
|
||
struct S { | ||
x: u64, | ||
enum EnumWithGeneric<D> { | ||
VariantOne: D, | ||
VariantTwo: (), | ||
} | ||
|
||
fn main() -> u64 { | ||
let addr = some_contract_addr; | ||
struct StructWithGeneric<D> { | ||
field_1: D, | ||
field_2: u64, | ||
} | ||
|
||
// ANCHOR: configurable_block | ||
configurable { | ||
U8: u8 = 8u8, | ||
BOOL: bool = true, | ||
ARRAY: [u32; 3] = [253u32, 254u32, 255u32], | ||
STR_4: str[4] = "fuel", | ||
STRUCT: StructWithGeneric<u8> = StructWithGeneric { | ||
field_1: 8u8, | ||
field_2: 16, | ||
}, | ||
ENUM: EnumWithGeneric<bool> = EnumWithGeneric::VariantOne(true), | ||
} | ||
// ANCHOR_END: configurable_block | ||
|
||
let string = some_string; | ||
abi TestContract { | ||
fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>); | ||
} | ||
|
||
return if true_bool { some_num } else { 0 }; | ||
impl TestContract for Contract { | ||
// ANCHOR: using_configurables | ||
fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>) { | ||
(U8, BOOL, ARRAY, STR_4, STRUCT) | ||
} | ||
// ANCHOR_END: using_configurables | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
2 changes: 2 additions & 0 deletions
2
test/src/sdk-harness/test_projects/configurables_in_contract/.gitignore
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,2 @@ | ||
out | ||
target |
13 changes: 13 additions & 0 deletions
13
test/src/sdk-harness/test_projects/configurables_in_contract/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'configurables_in_contract' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-3A783B9109EEADE0' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-3A783B9109EEADE0' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
test/src/sdk-harness/test_projects/configurables_in_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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "configurables_in_contract" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../sway-lib-std" } |
98 changes: 98 additions & 0 deletions
98
test/src/sdk-harness/test_projects/configurables_in_contract/mod.rs
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,98 @@ | ||
use fuels::{prelude::*, types::SizedAsciiString}; | ||
|
||
#[tokio::test] | ||
async fn contract_uses_default_configurables() -> Result<()> { | ||
abigen!(Contract( | ||
name = "MyContract", | ||
abi = | ||
"test_projects/configurables_in_contract/out/debug/configurables_in_contract-abi.json" | ||
)); | ||
|
||
let wallet = launch_provider_and_get_wallet().await; | ||
|
||
let contract_id = Contract::deploy( | ||
"test_projects/configurables_in_contract/out/debug/configurables_in_contract.bin", | ||
&wallet, | ||
TxParameters::default(), | ||
StorageConfiguration::default(), | ||
) | ||
.await?; | ||
|
||
let contract_instance = MyContract::new(contract_id, wallet.clone()); | ||
|
||
let response = contract_instance | ||
.methods() | ||
.return_configurables() | ||
.call() | ||
.await?; | ||
|
||
let expected_value = ( | ||
8u8, | ||
true, | ||
[253u32, 254u32, 255u32], | ||
"fuel".try_into()?, | ||
StructWithGeneric { | ||
field_1: 8u8, | ||
field_2: 16, | ||
}, | ||
EnumWithGeneric::VariantOne(true), | ||
); | ||
|
||
assert_eq!(response.value, expected_value); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn contract_configurables() -> Result<()> { | ||
abigen!(Contract( | ||
name = "MyContract", | ||
abi = | ||
"test_projects/configurables_in_contract/out/debug/configurables_in_contract-abi.json" | ||
)); | ||
|
||
let wallet = launch_provider_and_get_wallet().await; | ||
|
||
let new_str: SizedAsciiString<4> = "FUEL".try_into()?; | ||
let new_struct = StructWithGeneric { | ||
field_1: 16u8, | ||
field_2: 32, | ||
}; | ||
let new_enum = EnumWithGeneric::VariantTwo; | ||
|
||
let configurables = MyContractConfigurables::new() | ||
.set_STR_4(new_str.clone()) | ||
.set_STRUCT(new_struct.clone()) | ||
.set_ENUM(new_enum.clone()); | ||
|
||
let contract_id = Contract::deploy_with_parameters( | ||
"test_projects/configurables_in_contract/out/debug/configurables_in_contract.bin", | ||
&wallet, | ||
TxParameters::default(), | ||
StorageConfiguration::default(), | ||
configurables.into(), | ||
Salt::default(), | ||
) | ||
.await?; | ||
|
||
let contract_instance = MyContract::new(contract_id, wallet.clone()); | ||
|
||
let response = contract_instance | ||
.methods() | ||
.return_configurables() | ||
.call() | ||
.await?; | ||
|
||
let expected_value = ( | ||
8u8, | ||
true, | ||
[253u32, 254u32, 255u32], | ||
new_str, | ||
new_struct, | ||
new_enum, | ||
); | ||
|
||
assert_eq!(response.value, expected_value); | ||
|
||
Ok(()) | ||
} |
33 changes: 33 additions & 0 deletions
33
test/src/sdk-harness/test_projects/configurables_in_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,33 @@ | ||
contract; | ||
|
||
enum EnumWithGeneric<D> { | ||
VariantOne: D, | ||
VariantTwo: (), | ||
} | ||
|
||
struct StructWithGeneric<D> { | ||
field_1: D, | ||
field_2: u64, | ||
} | ||
|
||
configurable { | ||
U8: u8 = 8u8, | ||
BOOL: bool = true, | ||
ARRAY: [u32; 3] = [253u32, 254u32, 255u32], | ||
STR_4: str[4] = "fuel", | ||
STRUCT: StructWithGeneric<u8> = StructWithGeneric { | ||
field_1: 8u8, | ||
field_2: 16, | ||
}, | ||
ENUM: EnumWithGeneric<bool> = EnumWithGeneric::VariantOne(true), | ||
} | ||
|
||
abi TestContract { | ||
fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>, EnumWithGeneric<bool>); | ||
} | ||
|
||
impl TestContract for Contract { | ||
fn return_configurables( ) -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>, EnumWithGeneric<bool>) { | ||
(U8, BOOL, ARRAY, STR_4, STRUCT, ENUM) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
test/src/sdk-harness/test_projects/configurables_in_script/.gitignore
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,2 @@ | ||
out | ||
target |
13 changes: 13 additions & 0 deletions
13
test/src/sdk-harness/test_projects/configurables_in_script/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'configurables_in_script' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-6F15923604F9775D' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-6F15923604F9775D' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
test/src/sdk-harness/test_projects/configurables_in_script/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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "configurables_in_script" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../sway-lib-std" } |
Oops, something went wrong.