diff --git a/test/src/sdk-harness/Forc.lock b/test/src/sdk-harness/Forc.lock index b05cb399826..6db035f3459 100644 --- a/test/src/sdk-harness/Forc.lock +++ b/test/src/sdk-harness/Forc.lock @@ -251,11 +251,21 @@ name = "run_external_proxy" source = "member" dependencies = ["std"] +[[package]] +name = "run_external_proxy_with_storage" +source = "member" +dependencies = ["std"] + [[package]] name = "run_external_target" source = "member" dependencies = ["std"] +[[package]] +name = "run_external_target_with_storage" +source = "member" +dependencies = ["std"] + [[package]] name = "script_bytecode" source = "member" diff --git a/test/src/sdk-harness/Forc.toml b/test/src/sdk-harness/Forc.toml index 97a7001d7da..e6b081c63d0 100644 --- a/test/src/sdk-harness/Forc.toml +++ b/test/src/sdk-harness/Forc.toml @@ -29,6 +29,8 @@ members = [ "test_projects/result_option_expect", "test_projects/run_external_proxy", "test_projects/run_external_target", + "test_projects/run_external_proxy_with_storage", + "test_projects/run_external_target_with_storage", "test_projects/script_bytecode", "test_projects/script_data", "test_projects/storage", diff --git a/test/src/sdk-harness/test_projects/harness.rs b/test/src/sdk-harness/test_projects/harness.rs index bc01e3cbc5b..6f297b67ad9 100644 --- a/test/src/sdk-harness/test_projects/harness.rs +++ b/test/src/sdk-harness/test_projects/harness.rs @@ -31,6 +31,7 @@ mod registers; mod result_in_abi; mod result_option_expect; mod run_external_proxy; +mod run_external_proxy_with_storage; mod script_data; mod storage; mod storage_access; diff --git a/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/Forc.lock b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/Forc.lock new file mode 100644 index 00000000000..c0e7ac7bd1a --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = 'core' +source = 'path+from-root-37E4B1588712FA61' + +[[package]] +name = 'std' +source = 'path+from-root-37E4B1588712FA61' +dependencies = ['core'] + +[[package]] +name = 'run_external_proxy' +source = 'member' +dependencies = ['std'] diff --git a/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/Forc.toml b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/Forc.toml new file mode 100644 index 00000000000..77108416623 --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "run_external_proxy_with_storage" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/mod.rs b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/mod.rs new file mode 100644 index 00000000000..39e70a5fcc4 --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/mod.rs @@ -0,0 +1,106 @@ +use fuels::{prelude::*, types::Bits256}; + +abigen!(Contract( + name = "RunExternalProxyContract", + abi = "test_projects/run_external_proxy_with_storage/out/release/run_external_proxy_with_storage-abi.json", +)); + +#[tokio::test] +async fn run_external_can_proxy_call() { + let wallet = launch_provider_and_get_wallet().await.unwrap(); + + let storage_configuration = + StorageConfiguration::default().add_slot_overrides_from_file("test_projects/run_external_target_with_storage/out/release/run_external_target_with_storage-storage_slots.json").unwrap(); + + let target_id = Contract::load_from( + "test_projects/run_external_target_with_storage/out/release/run_external_target_with_storage.bin", + LoadConfiguration::default() + .with_storage_configuration(storage_configuration.clone()), + ) + .unwrap() + .deploy(&wallet, TxPolicies::default()) + .await + .unwrap(); + + let configurables = RunExternalProxyContractConfigurables::default() + .with_TARGET(target_id.clone().into()) + .unwrap(); + let id = Contract::load_from( + "test_projects/run_external_proxy_with_storage/out/release/run_external_proxy_with_storage.bin", + LoadConfiguration::default().with_configurables(configurables).with_storage_configuration(storage_configuration), + ) + .unwrap() + .deploy(&wallet, TxPolicies::default()) + .await + .unwrap(); + let instance = RunExternalProxyContract::new(id.clone(), wallet); + // Call "large_value" + // Will call run_external_proxy::large_value + // that will call run_external_target::large_value + // and return the value doubled. + let result = instance + .methods() + .large_value() + .with_contract_ids(&[target_id.clone().into()]) + .call() + .await + .unwrap(); + for r in result.receipts.iter() { + match r { + Receipt::LogData { data, .. } => { + if let Some(data) = data { + if data.len() > 8 { + if let Ok(s) = std::str::from_utf8(&data[8..]) { + print!("{:?} ", s); + } + } + println!("{:?}", data); + } + } + _ => {} + } + } + let expected_large = + Bits256::from_hex_str("0x00000000000000000000000059F2f1fCfE2474fD5F0b9BA1E73ca90b143Eb8d0") + .unwrap(); + assert_eq!(result.value, expected_large); + // Call "double_value" + // Will call run_external_proxy::double_value + // that will call run_external_target::double_value + // and return the value doubled. + let result = instance + .methods() + .double_value(42) + .with_contract_ids(&[target_id.clone().into()]) + .call() + .await + .unwrap(); + for r in result.receipts.iter() { + match r { + Receipt::LogData { data, .. } => { + if let Some(data) = data { + if data.len() > 8 { + if let Ok(s) = std::str::from_utf8(&data[8..]) { + print!("{:?} ", s); + } + } + println!("{:?}", data); + } + } + _ => {} + } + } + assert_eq!(result.value, 84); + // Call "does_not_exist_in_the_target" + // Will call run_external_proxy::does_not_exist_in_the_target + // it will proxy the call to run_external_target, + // and endup in the fallback, fn that will triple the input value + let result = instance + .methods() + .does_not_exist_in_the_target(42) + .with_contract_ids(&[target_id.into()]) + .call() + .await + .unwrap(); + assert_eq!(result.value, 126); +} diff --git a/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/src/main.sw b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/src/main.sw new file mode 100644 index 00000000000..05dc44f0473 --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_proxy_with_storage/src/main.sw @@ -0,0 +1,29 @@ +contract; + +use std::execution::run_external; + +configurable { + TARGET: ContractId = ContractId::zero(), +} + +abi RunExternalTest { + fn double_value(foo: u64) -> u64; + fn large_value() -> b256; + fn does_not_exist_in_the_target(foo: u64) -> u64; +} +impl RunExternalTest for Contract { + fn double_value(_foo: u64) -> u64 { + __log(1); + run_external(TARGET) + } + + fn large_value() -> b256 { + run_external(TARGET) + } + + // ANCHOR: does_not_exist_in_the_target + fn does_not_exist_in_the_target(_foo: u64) -> u64 { + run_external(TARGET) + } + // ANCHOR_END: does_not_exist_in_the_target +} \ No newline at end of file diff --git a/test/src/sdk-harness/test_projects/run_external_target_with_storage/Forc.lock b/test/src/sdk-harness/test_projects/run_external_target_with_storage/Forc.lock new file mode 100644 index 00000000000..05131a01155 --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_target_with_storage/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = 'core' +source = 'path+from-root-37E4B1588712FA61' + +[[package]] +name = 'std' +source = 'path+from-root-37E4B1588712FA61' +dependencies = ['core'] + +[[package]] +name = 'run_external_target' +source = 'member' +dependencies = ['std'] diff --git a/test/src/sdk-harness/test_projects/run_external_target_with_storage/Forc.toml b/test/src/sdk-harness/test_projects/run_external_target_with_storage/Forc.toml new file mode 100644 index 00000000000..130715dae8c --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_target_with_storage/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "run_external_target_with_storage" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/sdk-harness/test_projects/run_external_target_with_storage/src/main.sw b/test/src/sdk-harness/test_projects/run_external_target_with_storage/src/main.sw new file mode 100644 index 00000000000..ab7d770ccef --- /dev/null +++ b/test/src/sdk-harness/test_projects/run_external_target_with_storage/src/main.sw @@ -0,0 +1,38 @@ +contract; + +use std::constants::ZERO_B256; + +storage { + owner: Identity = Identity::Address(Address::from(ZERO_B256)), + simple_value: u64 = 0, +} + +abi RunExternalTest { + fn double_value(foo: u64) -> u64; + fn large_value() -> b256; +} + +impl RunExternalTest for Contract { + fn double_value(foo: u64) -> u64 { + __log(2); + foo * 2 + } + fn large_value() -> b256 { + 0x00000000000000000000000059F2f1fCfE2474fD5F0b9BA1E73ca90b143Eb8d0 + } +} + +// ANCHOR: fallback +#[fallback, storage(read, write)] +fn fallback() -> u64 { + let iden = storage.owner.read(); + + use std::call_frames::*; + __log(3); + __log(called_method()); + __log("double_value"); + __log(called_method() == "double_value"); + let foo = called_args::(); + foo * 3 +} +// ANCHOR_END: fallback