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 542d272 commit 54a7842
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 70 deletions.
1 change: 1 addition & 0 deletions e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fuels = { workspace = true }
tai64 = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true, features = ["test-util"] }
test-case = { workspace = true }

[build-dependencies]
anyhow = { workspace = true, features = ["std"] }
Expand Down
175 changes: 111 additions & 64 deletions e2e/tests/configurables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@ use fuels::{
prelude::*,
types::{Bits256, SizedAsciiString, U256},
};
use test_case::test_case;

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn contract_default_configurables() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
name = "MyContract",
project = "e2e/sway/contracts/configurables"
)),
Deploy(
name = "contract_instance",
contract = "MyContract",
wallet = "wallet",
random_salt = false,
),
);
async fn contract_default_configurables(is_regular: bool) -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi = "e2e/sway/contracts/configurables/out/release/configurables-abi.json"
));

let wallet = launch_provider_and_get_wallet().await?;

let contract = Contract::load_from(
"sway/contracts/configurables/out/release/configurables.bin",
LoadConfiguration::default(),
)?;

let contract_id = if is_regular {
contract
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?
} else {
contract
.convert_to_loader(124)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?
};

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

let response = contract_instance
.methods()
Expand Down Expand Up @@ -49,25 +63,29 @@ async fn contract_default_configurables() -> Result<()> {
Ok(())
}

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn script_default_configurables() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Script(
name = "MyScript",
project = "e2e/sway/scripts/script_configurables"
)),
LoadScript(
name = "script_instance",
script = "MyScript",
wallet = "wallet"
)
);

let mut script_instance = script_instance;
script_instance.convert_into_loader().await?;
async fn script_default_configurables(is_regular: bool) -> Result<()> {
abigen!(Script(
name = "MyScript",
abi = "e2e/sway/scripts/script_configurables/out/release/script_configurables-abi.json"
));

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

let response = if is_regular {
script_instance.main().call().await?
} else {
script_instance
.convert_into_loader()
.await?
.main()
.call()
.await?
};

let expected_value = (
true,
Expand All @@ -92,9 +110,10 @@ async fn script_default_configurables() -> Result<()> {
Ok(())
}

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn contract_configurables() -> Result<()> {
// ANCHOR: contract_configurables
async fn contract_configurables(is_regular: bool) -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi = "e2e/sway/contracts/configurables/out/release/configurables-abi.json"
Expand Down Expand Up @@ -123,15 +142,23 @@ async fn contract_configurables() -> Result<()> {
.with_STRUCT(new_struct.clone())?
.with_ENUM(new_enum.clone())?;

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

let contract_id = if is_regular {
contract
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?
} else {
contract
.convert_to_loader(124)?
.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()
Expand Down Expand Up @@ -159,8 +186,10 @@ async fn contract_configurables() -> Result<()> {
Ok(())
}

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn contract_dyn_configurables() -> Result<()> {
async fn contract_dyn_configurables(is_regular: bool) -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi = "e2e/sway/contracts/dyn_configurables/out/release/dyn_configurables-abi.json"
Expand All @@ -175,15 +204,21 @@ async fn contract_dyn_configurables() -> Result<()> {
.with_STR_3("fuel-fuel".try_into()?)?
.with_LAST_U8(12)?;

dbg!(&configurables);

let contract_id = Contract::load_from(
let contract = Contract::load_from(
"sway/contracts/dyn_configurables/out/release/dyn_configurables.bin",
LoadConfiguration::default().with_configurables(configurables),
)?
.convert_to_loader(100)? //TODO: @hal3e add test-case for convert to loader with or without
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
)?;

let contract_id = if is_regular {
contract
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?
} else {
contract
.convert_to_loader(124)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?
};

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

Expand All @@ -207,9 +242,10 @@ async fn contract_dyn_configurables() -> Result<()> {
Ok(())
}

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn script_configurables() -> Result<()> {
// ANCHOR: script_configurables
async fn script_configurables(is_regular: bool) -> Result<()> {
abigen!(Script(
name = "MyScript",
abi = "e2e/sway/scripts/script_configurables/out/release/script_configurables-abi.json"
Expand Down Expand Up @@ -243,12 +279,18 @@ async fn script_configurables() -> Result<()> {
.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 mut script_instance = script_instance.with_configurables(configurables);

let response = if is_regular {
script_instance.main().call().await?
} else {
script_instance
.convert_into_loader()
.await?
.main()
.call()
.await?
};

let expected_value = (
false,
Expand All @@ -270,8 +312,10 @@ async fn script_configurables() -> Result<()> {
Ok(())
}

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn script_dyn_configurables() -> Result<()> {
async fn script_dyn_configurables(is_regular: bool) -> Result<()> {
abigen!(Script(
name = "MyScript",
abi = "e2e/sway/scripts/script_dyn_configurables/out/release/script_dyn_configurables-abi.json"
Expand All @@ -288,15 +332,18 @@ async fn script_dyn_configurables() -> Result<()> {
.with_STR_3("fuel-fuel".try_into()?)?
.with_LAST_U8(12)?;

dbg!(&configurables);

let response = script_instance
.with_configurables(configurables)
.convert_into_loader() //TODO: @hal3e add test-case for this
.await?
.main()
.call()
.await?;
let mut script_instance = script_instance.with_configurables(configurables);

let response = if is_regular {
script_instance.main().call().await?
} else {
script_instance
.convert_into_loader()
.await?
.main()
.call()
.await?
};

let expected_value = (
false,
Expand Down
27 changes: 22 additions & 5 deletions e2e/tests/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use fuels::{
programs::executable::Executable,
types::{coin::Coin, coin_type::CoinType, input::Input, message::Message, output::Output},
};
use test_case::test_case;

async fn assert_address_balance(
address: &Bech32Address,
Expand Down Expand Up @@ -776,8 +777,10 @@ async fn predicate_configurables() -> Result<()> {
Ok(())
}

#[test_case(true ; "regular")]
#[test_case(false ; "use loader")]
#[tokio::test]
async fn predicate_dyn_configurables() -> Result<()> {
async fn predicate_dyn_configurables(is_regular: bool) -> Result<()> {
abigen!(Predicate(
name = "MyPredicate",
abi = "e2e/sway/predicates/predicate_dyn_configurables/out/release/predicate_dyn_configurables-abi.json"
Expand All @@ -799,18 +802,32 @@ async fn predicate_dyn_configurables() -> Result<()> {
12,
)?;

let mut predicate: Predicate = Predicate::load_from(
let executable = Executable::load_from(
"sway/predicates/predicate_dyn_configurables/out/release/predicate_dyn_configurables.bin",
)?
.with_data(predicate_data)
.with_configurables(configurables)?;
.with_configurables(configurables);

let (maybe_loader, code) = if is_regular {
(None, executable.code()?)
} else {
let loader = executable.convert_to_loader()?;
let code = loader.code()?;

(Some(loader), code)
};

let mut predicate: Predicate = Predicate::from_code(code).with_data(predicate_data);

let num_coins = 4;
let num_messages = 8;
let amount = 16;
let (provider, predicate_balance, receiver, receiver_balance, asset_id, _) =
let (provider, predicate_balance, receiver, receiver_balance, asset_id, extra_wallet) =
setup_predicate_test(predicate.address(), num_coins, num_messages, amount).await?;

if let Some(loader) = maybe_loader {
loader.upload_blob(extra_wallet).await?;
}

predicate.set_provider(provider.clone());

// TODO: https://github.com/FuelLabs/fuels-rs/issues/1394
Expand Down
1 change: 0 additions & 1 deletion e2e/tests/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ async fn test_call_param_gas_errors() -> Result<()> {
.expect_err("should error");

let expected = "transaction reverted: OutOfGas";
dbg!(&response.to_string());
assert!(response.to_string().starts_with(expected));

// Call params gas_forwarded exceeds transaction limit
Expand Down

0 comments on commit 54a7842

Please sign in to comment.