Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Dec 18, 2024
1 parent 54a7842 commit 1fb0acb
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 88 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"examples/predicates",
"examples/providers",
"examples/rust_bindings",
"examples/scripts",
"examples/types",
"examples/wallets",
"packages/fuels",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/deploying/configurable-constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ In Sway, you can define `configurable` constants which can be changed during the
Each of the configurable constants will get a dedicated `with` method in the SDK. For example, the constant `STR_4` will get the `with_STR_4` method which accepts the same type as defined in the contract code. Below is an example where we chain several `with` methods and deploy the contract with the new constants.

```rust,ignore
{{#include ../../../e2e/tests/configurables.rs:contract_configurables}}
{{#include ../../../examples/contracts/src/lib.rs:contract_configurables}}
```
2 changes: 1 addition & 1 deletion docs/src/running-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ Same as contracts, you can define `configurable` constants in `scripts` which ca
Each configurable constant will get a dedicated `with` method in the SDK. For example, the constant `STR_4` will get the `with_STR_4` method which accepts the same type defined in sway. Below is an example where we chain several `with` methods and execute the script with the new constants.

```rust,ignore
{{#include ../../e2e/tests/configurables.rs:script_configurables}}
{{#include ../../examples/scripts/src/lib.rs:script_configurables}}
```
69 changes: 69 additions & 0 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,4 +1199,73 @@ mod tests {
// ANCHOR_END: decoding_script_transactions
Ok(())
}

#[tokio::test]
async fn contract_configurables() -> Result<()> {
use fuels::prelude::*;

// ANCHOR: contract_configurables
abigen!(Contract(
name = "MyContract",
abi = "e2e/sway/contracts/configurables/out/release/configurables-abi.json"
));

let wallet = launch_provider_and_get_wallet().await?;

let str_4: fuels::types::SizedAsciiString<4> = "FUEL".try_into()?;
let new_struct = StructWithGeneric {
field_1: 16u8,
field_2: 32,
};
let new_enum = EnumWithGeneric::VariantTwo;

let configurables = MyContractConfigurables::default()
.with_BOOL(false)?
.with_U8(7)?
.with_U16(15)?
.with_U32(31)?
.with_U64(63)?
.with_U256(fuels::types::U256::from(8))?
.with_B256(Bits256([2; 32]))?
.with_STR_4(str_4.clone())?
.with_TUPLE((7, false))?
.with_ARRAY([252, 253, 254])?
.with_STRUCT(new_struct.clone())?
.with_ENUM(new_enum.clone())?;

let contract_id = Contract::load_from(
"../../e2e/sway/contracts/configurables/out/release/configurables.bin",
LoadConfiguration::default().with_configurables(configurables),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;

let contract_instance = MyContract::new(contract_id, wallet.clone());
// ANCHOR_END: contract_configurables

let response = contract_instance
.methods()
.return_configurables()
.call()
.await?;

let expected_value = (
false,
7,
15,
31,
63,
fuels::types::U256::from(8),
Bits256([2; 32]),
str_4,
(7, false),
[252, 253, 254],
new_struct,
new_enum,
);

assert_eq!(response.value, expected_value);

Ok(())
}
}
14 changes: 14 additions & 0 deletions examples/scripts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "fuels-example-scripts"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
publish = false
repository = { workspace = true }
description = "Fuel Rust SDK scripts examples."

[dev-dependencies]
fuels = { workspace = true, features = ["default"] }
tokio = { workspace = true, features = ["full"] }
74 changes: 74 additions & 0 deletions examples/scripts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#[cfg(test)]
mod tests {
use fuels::prelude::Result;

#[tokio::test]
async fn script_configurables() -> Result<()> {
use fuels::{
core::codec::EncoderConfig,
prelude::*,
types::{Bits256, SizedAsciiString, U256},
};

// ANCHOR: script_configurables
abigen!(Script(
name = "MyScript",
abi = "e2e/sway/scripts/script_configurables/out/release/script_configurables-abi.json"
));

let wallet = launch_provider_and_get_wallet().await?;
let bin_path =
"../../e2e/sway/scripts/script_configurables/out/release/script_configurables.bin";
let script_instance = MyScript::new(wallet, bin_path);

let str_4: SizedAsciiString<4> = "FUEL".try_into()?;
let new_struct = StructWithGeneric {
field_1: 16u8,
field_2: 32,
};
let new_enum = EnumWithGeneric::VariantTwo;

let configurables = MyScriptConfigurables::new(EncoderConfig {
max_tokens: 5,
..Default::default()
})
.with_BOOL(false)?
.with_U8(7)?
.with_U16(15)?
.with_U32(31)?
.with_U64(63)?
.with_U256(U256::from(8))?
.with_B256(Bits256([2; 32]))?
.with_STR_4(str_4.clone())?
.with_TUPLE((7, false))?
.with_ARRAY([252, 253, 254])?
.with_STRUCT(new_struct.clone())?
.with_ENUM(new_enum.clone())?;

let response = script_instance
.with_configurables(configurables)
.main()
.call()
.await?;
// ANCHOR_END: script_configurables

let expected_value = (
false,
7,
15,
31,
63,
U256::from(8),
Bits256([2; 32]),
str_4,
(7, false),
[252, 253, 254],
new_struct,
new_enum,
);

assert_eq!(response.value, expected_value);

Ok(())
}
}
Loading

0 comments on commit 1fb0acb

Please sign in to comment.