From 8eb93165c404eabf5530cb0bdadf26eba04e922f Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 16 Sep 2024 16:47:17 +0100 Subject: [PATCH] chore: consolidate all ABI Sway programs (#3171) * chore: consolidated all ABI Sway programs * chore: added missing `abi-library` * Update packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/Forc.toml --- .../forc-projects/abi-contract/Forc.toml | 3 + .../forc-projects/abi-contract/src/main.sw | 501 +++++++++++++++++- .../forc-projects/abi-library/Forc.toml | 7 + .../forc-projects/abi-library/src/lib.sw | 11 + .../forc-projects/abi-predicate/src/main.sw | 33 +- .../forc-projects/abi-script/src/main.sw | 33 +- 6 files changed, 580 insertions(+), 8 deletions(-) create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/abi-library/Forc.toml create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects/abi-library/src/lib.sw diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/Forc.toml index d1ae3bcb75a..7e941f84b42 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/Forc.toml @@ -2,3 +2,6 @@ authors = ["Fuel Labs "] license = "Apache-2.0" name = "abi-contract" + +[dependencies] +abi-library = { path = "../abi-library" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/src/main.sw index 6abaa813e43..4edc95f5c14 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/abi-contract/src/main.sw @@ -1,11 +1,504 @@ contract; +use abi_library::ExternalStruct; +use abi_library::ExternalEnum; +use std::vm::evm::evm_address::EvmAddress; +use std::b512::B512; +use std::string::String; +use std::bytes::Bytes; -abi AbiContract { - fn types_u8() -> u8; +enum EnumWithNative { + Checked: (), + Pending: (), } -impl AbiContract for Contract { - fn types_u8() -> u8 { +enum EnumWithVector { + num: u8, + vec: Vec, +} + +enum EnumWithBuiltinType { + a: bool, + b: u64, +} + +enum EnumDoubleGeneric { + a: T1, + b: T2, +} + +enum EnumWithStructs { + a: EnumWithNative, + b: StructSimple, + c: EnumDoubleGeneric, +} + +struct StructSimple { + a: bool, + b: u32, +} + +struct StructWithEnumArray { + a: [EnumWithNative; 3], +} + +struct StructWithMultiOption { + multiple: [Option; 5], +} + +struct StructWithSingleOption { + single: Option, +} + +struct StructWithVector { + num: u8, + vec: Vec, +} + +struct StructSingleGeneric { + a: T, +} + +struct StructDoubleGeneric { + a: T1, + b: T2, +} + +struct StructGenericWithEnum { + a: T1, + b: EnumDoubleGeneric, +} + +struct StructWithImplicitGenerics { + arr: [E; 3], + tuple: (E, F), + string: str[5], + array: [u8; 2], +} + +struct StructWithGenericArray { + a: [StructDoubleGeneric; 3], +} + +struct StructWithNestedArray { + a: [StructDoubleGeneric, str[1]>; 2], +} + +struct StructWithNestedTuple { + a: (u8, StructSingleGeneric>, str[3]), +} + +struct StructWithNestedStruct { + a: StructDoubleGeneric, u16>, +} + +struct StructA { + propA1: u8, +} + +struct StructB { + propB1: StructA, + propB2: u16, +} + +struct StructC { + propC1: StructA, + propC2: Vec, + propC3: StructD>, + propC4: Vec>>, + propC5: Vec>>>, +} + +struct StructD { + propD1: Vec>, + propD2: U, + propD3: V, +} + +struct StructE { + propE1: StructA, + propE2: StructB, + propE3: T, +} + +struct StructF { + propF1: u64, + propF2: T, +} + +struct StructG { + propG1: u8, +} + +enum MyContractError { + DivisionByZero: (), +} + +type TupleWithNativeAssets = (AssetId, AssetId, bool); + +fn divide(numerator: u64, denominator: u64) -> Result { + if (denominator == 0) { + return Err(MyContractError::DivisionByZero); + } else { + Ok(numerator / denominator) + } +} + +struct Configurables { + U8_VALUE: u8, + BOOL_VALUE: bool, + B256_VALUE: b256, + OPTION_U8_VALUE: Option, + GENERIC_STRUCT_VALUE: StructDoubleGeneric, u32>, +} + +configurable { + U8_VALUE: u8 = 10, + BOOL_VALUE: bool = true, + B256_VALUE: b256 = 0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96, + OPTION_U8_VALUE: Option = Option::None, + GENERIC_STRUCT_VALUE: StructDoubleGeneric, u32> = StructDoubleGeneric { + a: StructDoubleGeneric { a: 4, b: 257 }, + b: 57000, + }, +} + +abi MyContract { + fn configurables() -> Configurables; + + fn types_u8(x: u8) -> u8; + fn types_u16(x: u16) -> u16; + fn types_u32(x: u32) -> u32; + fn types_u64(x: u64) -> u64; + fn types_u256(x: u256) -> u256; + fn types_bool(x: bool) -> bool; + fn types_b256(x: b256) -> b256; + fn types_b512(x: B512) -> B512; + fn types_bytes(x: Bytes) -> Bytes; + + fn types_str(x: str[5]) -> str[5]; + fn types_str_slice(x: str) -> str; + fn types_raw_slice(x: raw_slice) -> raw_slice; + fn types_std_string(x: String) -> String; + + fn types_array(x: [u8; 4]) -> [u8; 4]; + fn types_array_struct(x: [StructSimple; 3]) -> [StructSimple; 3]; + fn types_array_with_generic_struct( + x: [StructDoubleGeneric, str[1]>; 2], + ) -> [StructDoubleGeneric, str[1]>; 2]; + + fn types_struct_simple(x: StructSimple) -> StructSimple; + fn types_struct_generic(x: StructSingleGeneric) -> StructSingleGeneric; + fn types_struct_with_tuple( + x: StructSingleGeneric<(bool, u64)>, + ) -> StructSingleGeneric<(bool, u64)>; + fn types_struct_double_generic( + x: StructGenericWithEnum, + ) -> StructGenericWithEnum; + fn type_struct_external(x: ExternalStruct) -> ExternalStruct; + fn types_struct_with_implicit_generics( + x: StructWithImplicitGenerics, + ) -> StructWithImplicitGenerics; + fn types_struct_with_array(x: StructWithGenericArray) -> StructWithGenericArray; + fn types_struct_with_vector(x: StructWithVector) -> StructWithVector; + fn types_struct_with_array_of_enums(x: StructWithEnumArray) -> StructWithEnumArray; + fn types_struct_with_nested_array(x: StructWithNestedArray) -> StructWithNestedArray; + fn types_struct_with_nested_tuple(x: StructWithNestedTuple) -> StructWithNestedTuple; + fn types_struct_with_nested_struct(x: StructWithNestedStruct) -> StructWithNestedStruct; + fn types_struct_with_multiple_struct_params(x: StructA, y: StructB, z: StructC) -> bool; + fn types_struct_with_complex_nested_struct(x: StructD>>) -> bool; + fn types_struct_with_single_option(x: StructWithSingleOption) -> StructWithSingleOption; + + fn types_tuple(x: (u8, u8, u8)) -> (u8, u8, u8); + fn types_tuple_complex( + x: (u8, StructSingleGeneric>, str[3]), + ) -> (u8, StructSingleGeneric>, str[3]); + fn types_tuple_with_native_types(x: (AssetId, AssetId, bool)) -> (AssetId, AssetId, bool); + fn types_alias_tuple_with_native_types(x: TupleWithNativeAssets) -> TupleWithNativeAssets; + + fn types_enum(x: EnumWithNative) -> EnumWithNative; + fn types_enum_with_builtin_type(x: EnumWithBuiltinType) -> EnumWithBuiltinType; + fn types_enum_with_vector(x: EnumWithVector) -> EnumWithVector; + fn types_generic_enum(x: EnumDoubleGeneric) -> EnumDoubleGeneric; + fn types_enum_external(x: ExternalEnum) -> ExternalEnum; + fn types_enum_with_structs(x: EnumWithStructs) -> EnumWithStructs; + + fn types_vector_u8(x: Vec) -> Vec; + fn types_vector_boolean(x: Vec) -> Vec; + fn types_vector_geo(x: Vec) -> Vec; + fn types_vector_inside_vector(x: Vec>) -> Vec>; + fn types_vector_inside_array(x: [Vec; 1]) -> [Vec; 1]; + fn types_vector_option(x: Vec) -> Vec; + + fn types_option(x: Option) -> Option; + fn types_option_geo(x: Option) -> Option; + + fn type_identity(x: Identity) -> Identity; + fn type_address(x: Address) -> Address; + fn type_contract_id(x: ContractId) -> ContractId; + fn types_asset_id(x: AssetId) -> AssetId; + fn types_evm_address(x: EvmAddress) -> EvmAddress; + fn types_result(x: Result) -> Result; + + fn types_void(x: ()) -> (); + fn types_void_then_value(x: (), y: u8) -> (); + fn types_value_then_void(x: u8, y: ()) -> (); + fn types_value_then_void_then_value(x: u8, y: (), z: u8) -> (); + fn types_value_then_value_then_void_then_void(x: u8, y: u8, z: (), a: ()) -> (); + + fn multi_arg_u64_u64(x: u64, y: u64) -> u64; + fn multi_arg_b256_bool(x: b256, y: bool) -> (b256, bool); + fn multi_arg_vector_vector(x: Vec, y: Vec) -> (Vec, Vec); + fn multi_arg_vector_b256(x: Vec, y: b256) -> (Vec, b256); + fn multi_arg_struct_vector(x: StructSimple, y: Vec) -> (StructSimple, Vec); + fn multi_arg_u64_struct(x: u64, y: StructSimple) -> (u64, StructSimple); + fn multi_arg_str_str(x: str[5], y: str[5]) -> (str[5], str[5]); + fn multi_arg_u32_vector_vector(x: u32, y: Vec, z: Vec) -> (u32, Vec, Vec); + fn multi_arg_complex( + x: StructDoubleGeneric<[b256; 3], u8>, + y: [StructDoubleGeneric; 4], + z: (str[5], bool), + a: StructSimple, + ); +} + +impl MyContract for Contract { + fn configurables() -> Configurables { + Configurables { + U8_VALUE: U8_VALUE, + BOOL_VALUE: BOOL_VALUE, + B256_VALUE: B256_VALUE, + OPTION_U8_VALUE: OPTION_U8_VALUE, + GENERIC_STRUCT_VALUE: GENERIC_STRUCT_VALUE, + } + } + fn types_u8(x: u8) -> u8 { + 255 + } + fn types_u16(x: u16) -> u16 { + 65535 + } + fn types_u32(x: u32) -> u32 { + 4294967295 + } + fn types_u64(x: u64) -> u64 { + 4294967295000 + } + fn types_u256(x: u256) -> u256 { + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256 + } + fn types_str(x: str[5]) -> str[5] { + __to_str_array("Hello") + } + fn types_bool(x: bool) -> bool { + true + } + fn types_asset_id(x: AssetId) -> AssetId { + x + } + fn types_b256(x: b256) -> b256 { + 0x0000000000000000000000000000000000000000000000000000000000000000 + } + fn types_b512(x: B512) -> B512 { + x + } + fn types_array(x: [u8; 4]) -> [u8; 4] { + x + } + fn types_array_struct(x: [StructSimple; 3]) -> [StructSimple; 3] { + x + } + fn types_array_with_generic_struct( + x: [StructDoubleGeneric, str[1]>; 2], + ) -> [StructDoubleGeneric, str[1]>; 2] { + x + } + fn types_tuple(x: (u8, u8, u8)) -> (u8, u8, u8) { + (x.0, x.1, x.2) + } + fn types_tuple_complex( + x: (u8, StructSingleGeneric>, str[3]), + ) -> (u8, StructSingleGeneric>, str[3]) { + (x.0, x.1, x.2) + } + fn types_tuple_with_native_types(x: (AssetId, AssetId, bool)) -> (AssetId, AssetId, bool) { + (x.0, x.1, x.2) + } + fn types_alias_tuple_with_native_types(x: TupleWithNativeAssets) -> TupleWithNativeAssets { + (x.0, x.1, x.2) + } + fn types_struct_simple(x: StructSimple) -> StructSimple { + x + } + fn types_struct_generic(x: StructSingleGeneric) -> StructSingleGeneric { + x + } + fn types_struct_with_tuple( + x: StructSingleGeneric<(bool, u64)>, + ) -> StructSingleGeneric<(bool, u64)> { + x + } + fn types_struct_with_nested_array(x: StructWithNestedArray) -> StructWithNestedArray { + x + } + fn types_struct_with_nested_tuple(x: StructWithNestedTuple) -> StructWithNestedTuple { + x + } + fn types_struct_with_nested_struct(x: StructWithNestedStruct) -> StructWithNestedStruct { + x + } + fn types_struct_with_multiple_struct_params(x: StructA, y: StructB, z: StructC) -> bool { + true + } + fn types_struct_with_complex_nested_struct(x: StructD>>) -> bool { + false + } + fn types_struct_with_single_option(x: StructWithSingleOption) -> StructWithSingleOption { + x + } + fn types_enum(x: EnumWithNative) -> EnumWithNative { + x + } + fn types_enum_with_builtin_type(x: EnumWithBuiltinType) -> EnumWithBuiltinType { + x + } + fn types_enum_with_vector(x: EnumWithVector) -> EnumWithVector { + x + } + fn types_vector_u8(x: Vec) -> Vec { + x + } + fn types_vector_boolean(x: Vec) -> Vec { + x + } + fn types_vector_inside_vector(x: Vec>) -> Vec> { + x + } + fn types_vector_inside_array(x: [Vec; 1]) -> [Vec; 1] { + x + } + fn types_vector_geo(x: Vec) -> Vec { + x + } + fn types_vector_option(x: Vec) -> Vec { + x + } + fn types_option(x: Option) -> Option { + x + } + fn types_option_geo(x: Option) -> Option { + x + } + fn types_evm_address(x: EvmAddress) -> EvmAddress { + EvmAddress::from(0x0606060606060606060606060606060606060606060606060606060606060606) + } + fn types_bytes(x: Bytes) -> Bytes { + x + } + fn types_raw_slice(x: raw_slice) -> raw_slice { + x + } + fn types_str_slice(x: str) -> str { + x + } + fn types_std_string(x: String) -> String { + x + } + fn types_result(x: Result) -> Result { + if (x.is_err()) { + return Err(__to_str_array("InputError")); + } + + let result = divide(20, x.unwrap()); + match result { + Ok(value) => Ok(value), + Err(MyContractError::DivisionByZero) => Err(__to_str_array("DivisError")), + } + } + fn type_address(x: Address) -> Address { + x + } + fn type_contract_id(x: ContractId) -> ContractId { + x + } + fn type_identity(x: Identity) -> Identity { + x + } + fn types_enum_external(x: ExternalEnum) -> ExternalEnum { + x + } + fn types_enum_with_structs(x: EnumWithStructs) -> EnumWithStructs { + x + } + fn type_struct_external(x: ExternalStruct) -> ExternalStruct { + x + } + fn types_struct_with_implicit_generics( + x: StructWithImplicitGenerics, + ) -> StructWithImplicitGenerics { + x + } + fn types_struct_with_array(x: StructWithGenericArray) -> StructWithGenericArray { + x + } + fn types_struct_with_vector(x: StructWithVector) -> StructWithVector { + x + } + fn types_struct_with_array_of_enums(x: StructWithEnumArray) -> StructWithEnumArray { + x + } + fn types_generic_enum(x: EnumDoubleGeneric) -> EnumDoubleGeneric { + x + } + fn types_struct_double_generic( + x: StructGenericWithEnum, + ) -> StructGenericWithEnum { + x + } + fn types_void(x: ()) -> () { + x + } + fn types_void_then_value(x: (), y: u8) -> () { + () + } + fn types_value_then_void(x: u8, y: ()) -> () { + () + } + fn types_value_then_void_then_value(x: u8, y: (), z: u8) -> () { + () + } + fn types_value_then_value_then_void_then_void(x: u8, y: u8, z: (), a: ()) -> () { + () + } + fn multi_arg_u64_u64(x: u64, y: u64) -> u64 { 0 } + fn multi_arg_b256_bool(x: b256, y: bool) -> (b256, bool) { + (x, y) + } + fn multi_arg_vector_vector(x: Vec, y: Vec) -> (Vec, Vec) { + (x, y) + } + fn multi_arg_vector_b256(x: Vec, y: b256) -> (Vec, b256) { + (x, y) + } + fn multi_arg_struct_vector(x: StructSimple, y: Vec) -> (StructSimple, Vec) { + (x, y) + } + fn multi_arg_u64_struct(x: u64, y: StructSimple) -> (u64, StructSimple) { + (x, y) + } + fn multi_arg_str_str(x: str[5], y: str[5]) -> (str[5], str[5]) { + (x, y) + } + fn multi_arg_u32_vector_vector(x: u32, y: Vec, z: Vec) -> (u32, Vec, Vec) { + (x, y, z) + } + fn multi_arg_complex( + x: StructDoubleGeneric<[b256; 3], u8>, + y: [StructDoubleGeneric; 4], + z: (str[5], bool), + a: StructSimple, + ) { + () + } } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/abi-library/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/abi-library/Forc.toml new file mode 100644 index 00000000000..d28ab3240d9 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/abi-library/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "lib.sw" +license = "Apache-2.0" +name = "abi-library" + +[dependencies] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/abi-library/src/lib.sw b/packages/fuel-gauge/test/fixtures/forc-projects/abi-library/src/lib.sw new file mode 100644 index 00000000000..dd28f6c5470 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects/abi-library/src/lib.sw @@ -0,0 +1,11 @@ +library; + +// anything `pub` here will be exported as a part of this library's API +pub struct ExternalStruct { + pub value: u64, +} + +pub enum ExternalEnum { + A: (), + B: (), +} diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/abi-predicate/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/abi-predicate/src/main.sw index d529f4e1893..1759925ec32 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/abi-predicate/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/abi-predicate/src/main.sw @@ -1,5 +1,34 @@ predicate; -fn main() -> bool { - false +struct Validation { + has_account: bool, + total_complete: u64, +} + +enum MyGenericEnum { + a: T, +} + +struct MyGenericStruct { + a: T, +} + +struct Configurables { + U8_VALUE: u8, + B256_VALUE: b256, +} + +configurable { + U8_VALUE: u8 = 10, + B256_VALUE: b256 = 0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96, +} + +fn main( + configurables: Configurables, + vec: Vec, + enm: MyGenericEnum, + opt: Option, + res: Result, u64>, +) -> bool { + U8_VALUE == configurables.U8_VALUE && B256_VALUE == configurables.B256_VALUE } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/abi-script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/abi-script/src/main.sw index 097ee2e7de5..e57711aa736 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/abi-script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/abi-script/src/main.sw @@ -1,5 +1,34 @@ script; -fn main() -> bool { - false +struct Validation { + has_account: bool, + total_complete: u64, +} + +enum MyGenericEnum { + a: T, +} + +struct MyGenericStruct { + a: T, +} + +struct Configurables { + U8_VALUE: u8, + B256_VALUE: b256, +} + +configurable { + U8_VALUE: u8 = 10, + B256_VALUE: b256 = 0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96, +} + +fn main( + configurables: Configurables, + vec: Vec, + enm: MyGenericEnum, + opt: Option, + res: Result, u64>, +) -> bool { + U8_VALUE == configurables.U8_VALUE && B256_VALUE == configurables.B256_VALUE }