diff --git a/forc-plugins/forc-client/test/data/big_contract/big_contract-abi.json b/forc-plugins/forc-client/test/data/big_contract/big_contract-abi.json index c3c892e05b5..0fa70821c2c 100644 --- a/forc-plugins/forc-client/test/data/big_contract/big_contract-abi.json +++ b/forc-plugins/forc-client/test/data/big_contract/big_contract-abi.json @@ -15,12 +15,22 @@ { "type": "[bool; 3]", "concreteTypeId": "4926d35d1a5157936b0a29bc126b8aace6d911209a5c130e9b716b0c73643ea6", - "metadataTypeId": 2 + "metadataTypeId": 5 + }, + { + "type": "[enum Location; 2]", + "concreteTypeId": "fb7957f865b6c8ee87b25381a2861c279107dc2fff0e6bd25586198dc65470d0", + "metadataTypeId": 1 + }, + { + "type": "[struct SimpleStruct; 3]", + "concreteTypeId": "96dffad5a46b9db38f00f4431235e7e9bab7a85e8ee9a514aa81063d429ceb7a", + "metadataTypeId": 7 }, { "type": "[u64; 3]", "concreteTypeId": "776fb5a3824169d6736138565fdc20aad684d9111266a5ff6d5c675280b7e199", - "metadataTypeId": 3 + "metadataTypeId": 6 }, { "type": "b256", @@ -33,7 +43,7 @@ { "type": "enum Location", "concreteTypeId": "6966f221767e2951c5d68ba5e81e897449375a9f51f5e4c9e3b65f1dd7defabb", - "metadataTypeId": 4 + "metadataTypeId": 9 }, { "type": "str[4]", @@ -42,12 +52,12 @@ { "type": "struct Person", "concreteTypeId": "a6b272bcf0f572840e2cc1241bdcf8d42db51eeb1aead750c8e608160f83f926", - "metadataTypeId": 6 + "metadataTypeId": 11 }, { "type": "struct SimpleStruct", "concreteTypeId": "75f7f7a06026cab5d7a70984d1fde56001e83505e3a091ff9722b92d7f56d8be", - "metadataTypeId": 7 + "metadataTypeId": 12 }, { "type": "u16", @@ -88,6 +98,36 @@ { "type": "[_; 2]", "metadataTypeId": 1, + "components": [ + { + "name": "__array_element", + "typeId": 9 + } + ] + }, + { + "type": "[_; 2]", + "metadataTypeId": 2, + "components": [ + { + "name": "__array_element", + "typeId": 8 + } + ] + }, + { + "type": "[_; 2]", + "metadataTypeId": 3, + "components": [ + { + "name": "__array_element", + "typeId": 12 + } + ] + }, + { + "type": "[_; 2]", + "metadataTypeId": 4, "components": [ { "name": "__array_element", @@ -97,7 +137,7 @@ }, { "type": "[_; 3]", - "metadataTypeId": 2, + "metadataTypeId": 5, "components": [ { "name": "__array_element", @@ -107,7 +147,7 @@ }, { "type": "[_; 3]", - "metadataTypeId": 3, + "metadataTypeId": 6, "components": [ { "name": "__array_element", @@ -115,9 +155,33 @@ } ] }, + { + "type": "[_; 3]", + "metadataTypeId": 7, + "components": [ + { + "name": "__array_element", + "typeId": 12 + } + ] + }, + { + "type": "enum Color", + "metadataTypeId": 8, + "components": [ + { + "name": "Red", + "typeId": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d" + }, + { + "name": "Blue", + "typeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0" + } + ] + }, { "type": "enum Location", - "metadataTypeId": 4, + "metadataTypeId": 9, "components": [ { "name": "Earth", @@ -126,20 +190,36 @@ { "name": "Mars", "typeId": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d" + }, + { + "name": "SimpleJupiter", + "typeId": 8 + }, + { + "name": "Jupiter", + "typeId": 2 + }, + { + "name": "SimplePluto", + "typeId": 12 + }, + { + "name": "Pluto", + "typeId": 3 } ] }, { "type": "str", - "metadataTypeId": 5 + "metadataTypeId": 10 }, { "type": "struct Person", - "metadataTypeId": 6, + "metadataTypeId": 11, "components": [ { "name": "name", - "typeId": 5 + "typeId": 10 }, { "name": "age", @@ -151,7 +231,7 @@ }, { "name": "location", - "typeId": 4 + "typeId": 9 }, { "name": "some_tuple", @@ -159,7 +239,7 @@ }, { "name": "some_array", - "typeId": 1 + "typeId": 4 }, { "name": "some_b256", @@ -169,7 +249,7 @@ }, { "type": "struct SimpleStruct", - "metadataTypeId": 7, + "metadataTypeId": 12, "components": [ { "name": "a", @@ -183,6 +263,17 @@ } ], "functions": [ + { + "inputs": [ + { + "name": "aoe", + "concreteTypeId": "fb7957f865b6c8ee87b25381a2861c279107dc2fff0e6bd25586198dc65470d0" + } + ], + "name": "array_of_enum_input_output", + "output": "fb7957f865b6c8ee87b25381a2861c279107dc2fff0e6bd25586198dc65470d0", + "attributes": null + }, { "inputs": [], "name": "assert_configurables", @@ -207,7 +298,43 @@ "concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0" } ], - "name": "get_storage", + "name": "get_storage_location", + "output": "6966f221767e2951c5d68ba5e81e897449375a9f51f5e4c9e3b65f1dd7defabb", + "attributes": [ + { + "name": "storage", + "arguments": [ + "read" + ] + } + ] + }, + { + "inputs": [ + { + "name": "index", + "concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0" + } + ], + "name": "get_storage_simple", + "output": "75f7f7a06026cab5d7a70984d1fde56001e83505e3a091ff9722b92d7f56d8be", + "attributes": [ + { + "name": "storage", + "arguments": [ + "read" + ] + } + ] + }, + { + "inputs": [ + { + "name": "index", + "concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0" + } + ], + "name": "get_storage_u16", "output": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef", "attributes": [ { @@ -224,6 +351,44 @@ "output": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903", "attributes": null }, + { + "inputs": [ + { + "name": "value", + "concreteTypeId": "6966f221767e2951c5d68ba5e81e897449375a9f51f5e4c9e3b65f1dd7defabb" + } + ], + "name": "push_storage_location", + "output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d", + "attributes": [ + { + "name": "storage", + "arguments": [ + "read", + "write" + ] + } + ] + }, + { + "inputs": [ + { + "name": "value", + "concreteTypeId": "75f7f7a06026cab5d7a70984d1fde56001e83505e3a091ff9722b92d7f56d8be" + } + ], + "name": "push_storage_simple", + "output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d", + "attributes": [ + { + "name": "storage", + "arguments": [ + "read", + "write" + ] + } + ] + }, { "inputs": [ { @@ -231,7 +396,7 @@ "concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef" } ], - "name": "push_storage", + "name": "push_storage_u16", "output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d", "attributes": [ { @@ -261,67 +426,77 @@ { "name": "BOOL", "concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903", - "offset": 380112 + "offset": 392688 }, { "name": "U8", "concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b", - "offset": 380232 + "offset": 392832 }, { "name": "U16", "concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef", - "offset": 380176 + "offset": 392776 }, { "name": "U32", "concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc", - "offset": 380216 + "offset": 392816 }, { "name": "U64", "concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc", - "offset": 380224 + "offset": 392824 }, { "name": "U256", "concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e", - "offset": 380184 + "offset": 392784 }, { "name": "B256", "concreteTypeId": "7c5ee1cecf5f8eacd1284feb5f0bf2bdea533a51e2f0c9aabe9236d335989f3b", - "offset": 380080 + "offset": 392656 }, { "name": "CONFIGURABLE_STRUCT", "concreteTypeId": "75f7f7a06026cab5d7a70984d1fde56001e83505e3a091ff9722b92d7f56d8be", - "offset": 380136 + "offset": 392736 }, { "name": "CONFIGURABLE_ENUM", "concreteTypeId": "6966f221767e2951c5d68ba5e81e897449375a9f51f5e4c9e3b65f1dd7defabb", - "offset": 380120 + "offset": 392696 }, { "name": "ARRAY_BOOL", "concreteTypeId": "4926d35d1a5157936b0a29bc126b8aace6d911209a5c130e9b716b0c73643ea6", - "offset": 380048 + "offset": 392512 }, { "name": "ARRAY_U64", "concreteTypeId": "776fb5a3824169d6736138565fdc20aad684d9111266a5ff6d5c675280b7e199", - "offset": 380056 + "offset": 392632 + }, + { + "name": "ARRAY_LOCATION", + "concreteTypeId": "fb7957f865b6c8ee87b25381a2861c279107dc2fff0e6bd25586198dc65470d0", + "offset": 392520 + }, + { + "name": "ARRAY_SIMPLE_STRUCT", + "concreteTypeId": "96dffad5a46b9db38f00f4431235e7e9bab7a85e8ee9a514aa81063d429ceb7a", + "offset": 392600 }, { "name": "TUPLE_BOOL_U64", "concreteTypeId": "c998ca9a5f221fe7b5c66ae70c8a9562b86d964408b00d17f883c906bc1fe4be", - "offset": 380160 + "offset": 392760 }, { "name": "STR_4", "concreteTypeId": "94f0fa95c830be5e4f711963e83259fe7e8bc723278ab6ec34449e791a99b53a", - "offset": 380152 + "offset": 392752 } ] } \ No newline at end of file diff --git a/forc-plugins/forc-client/test/data/big_contract/src/main.sw b/forc-plugins/forc-client/test/data/big_contract/src/main.sw index b62ee995940..83eb21e2d1f 100644 --- a/forc-plugins/forc-client/test/data/big_contract/src/main.sw +++ b/forc-plugins/forc-client/test/data/big_contract/src/main.sw @@ -10,11 +10,25 @@ abi MyContract { fn struct_input_output(person: Person) -> Person; + fn array_of_enum_input_output(aoe: [Location; 2]) -> [Location; 2]; + + #[storage(read, write)] + fn push_storage_u16(value: u16); + + #[storage(read)] + fn get_storage_u16(index: u64) -> u16; + + #[storage(read, write)] + fn push_storage_simple(value: SimpleStruct); + + #[storage(read)] + fn get_storage_simple(index: u64) -> SimpleStruct; + #[storage(read, write)] - fn push_storage(value: u16); + fn push_storage_location(value: Location); #[storage(read)] - fn get_storage(index: u64) -> u16; + fn get_storage_location(index: u64) -> Location; fn assert_configurables() -> bool; } @@ -22,6 +36,15 @@ abi MyContract { enum Location { Earth: u64, Mars: (), + SimpleJupiter: Color, + Jupiter: [Color; 2], + SimplePluto: SimpleStruct, + Pluto: [SimpleStruct; 2], +} + +enum Color { + Red: (), + Blue: u64, } struct Person { @@ -41,6 +64,8 @@ struct SimpleStruct { storage { my_vec: StorageVec = StorageVec {}, + my_simple_vec: StorageVec = StorageVec {}, + my_location_vec: StorageVec = StorageVec {}, } configurable { @@ -55,15 +80,37 @@ configurable { CONFIGURABLE_ENUM: Location = Location::Earth(1), ARRAY_BOOL: [bool; 3] = [true, false, true], ARRAY_U64: [u64; 3] = [9, 8, 7], + ARRAY_LOCATION: [Location; 2] = [Location::Earth(10), Location::Mars], + ARRAY_SIMPLE_STRUCT: [SimpleStruct; 3] = [ SimpleStruct { a: true, b: 5}, SimpleStruct { a: false, b: 0 }, SimpleStruct { a: true, b: u64::max() }], TUPLE_BOOL_U64: (bool, u64) = (true, 11), STR_4: str[4] = __to_str_array("abcd"), } +impl core::ops::Eq for Color { + fn eq(self, other: Color) -> bool { + match (self, other) { + (Color::Red, Color::Red) => true, + (Color::Blue(inner1), Color::Blue(inner2)) => inner1 == inner2, + _ => false, + } + } +} + +impl core::ops::Eq for SimpleStruct { + fn eq(self, other: SimpleStruct) -> bool { + self.a == other.a && self.b == other.b + } +} + impl core::ops::Eq for Location { fn eq(self, other: Location) -> bool { match (self, other) { (Location::Earth(inner1), Location::Earth(inner2)) => inner1 == inner2, (Location::Mars, Location::Mars) => true, + (Location::SimpleJupiter(inner1), Location::SimpleJupiter(inner2)) => inner1 == inner2, + (Location::Jupiter(inner1), Location::Jupiter(inner2)) => (inner1[0] == inner2[0] && inner1[1] == inner2[1]), + (Location::SimplePluto(inner1), Location::SimplePluto(inner2)) => inner1 == inner2, + (Location::Pluto(inner1), Location::Pluto(inner2)) => (inner1[0] == inner2[0] && inner1[1] == inner2[1]), _ => false, } } @@ -85,16 +132,40 @@ impl MyContract for Contract { person } + fn array_of_enum_input_output(aoe: [Location; 2]) -> [Location; 2] { + aoe + } + #[storage(read, write)] - fn push_storage(value: u16) { + fn push_storage_u16(value: u16) { storage.my_vec.push(value); } #[storage(read)] - fn get_storage(index: u64) -> u16 { + fn get_storage_u16(index: u64) -> u16 { storage.my_vec.get(index).unwrap().read() } + #[storage(read, write)] + fn push_storage_simple(value: SimpleStruct) { + storage.my_simple_vec.push(value); + } + + #[storage(read)] + fn get_storage_simple(index: u64) -> SimpleStruct { + storage.my_simple_vec.get(index).unwrap().read() + } + + #[storage(read, write)] + fn push_storage_location(value: Location) { + storage.my_location_vec.push(value); + } + + #[storage(read)] + fn get_storage_location(index: u64) -> Location { + storage.my_location_vec.get(index).unwrap().read() + } + fn assert_configurables() -> bool { assert(BOOL == true); assert(U8 == 1); @@ -112,6 +183,16 @@ impl MyContract for Contract { assert(ARRAY_U64[0] == 9); assert(ARRAY_U64[1] == 8); assert(ARRAY_U64[2] == 7); + assert(ARRAY_LOCATION[0] == Location::Earth(10)); + assert(ARRAY_LOCATION[1] == Location::Mars); + assert(ARRAY_SIMPLE_STRUCT[0].a == true); + assert(ARRAY_SIMPLE_STRUCT[0].b == 5); + assert(ARRAY_SIMPLE_STRUCT[1].a == false); + assert(ARRAY_SIMPLE_STRUCT[1].b == 0); + assert(ARRAY_SIMPLE_STRUCT[2].a == true); + assert(ARRAY_SIMPLE_STRUCT[2].b == u64::max()); + assert(ARRAY_LOCATION[1] == Location::Mars); + assert(ARRAY_LOCATION[1] == Location::Mars); assert(TUPLE_BOOL_U64.0 == true); assert(TUPLE_BOOL_U64.1 == 11); assert(sha256_str_array(STR_4) == sha256("abcd")); diff --git a/forc-plugins/forc-client/tests/deploy.rs b/forc-plugins/forc-client/tests/deploy.rs index faba22f143a..a9c01c8d381 100644 --- a/forc-plugins/forc-client/tests/deploy.rs +++ b/forc-plugins/forc-client/tests/deploy.rs @@ -134,6 +134,68 @@ async fn assert_big_contract_calls(wallet: WalletUnlocked, contract_id: Contract .value; assert_eq!(result, Location::Mars); + // Test enum with "tuple like struct" with simple value. + let result = instance + .methods() + .enum_input_output(Location::Earth(u64::MAX)) + .call() + .await + .unwrap() + .value; + assert_eq!(result, Location::Earth(u64::MAX)); + + // Test enum with "tuple like struct" with enum value. + let result = instance + .methods() + .enum_input_output(Location::SimpleJupiter(Color::Red)) + .call() + .await + .unwrap() + .value; + assert_eq!(result, Location::SimpleJupiter(Color::Red)); + + // Test enum with "tuple like struct" with enum value. + let result = instance + .methods() + .enum_input_output(Location::SimpleJupiter(Color::Blue(u64::MAX))) + .call() + .await + .unwrap() + .value; + assert_eq!(result, Location::SimpleJupiter(Color::Blue(u64::MAX))); + + // Test enum with "tuple like struct" with enum array value. + let result = instance + .methods() + .enum_input_output(Location::Jupiter([Color::Red, Color::Blue(u64::MAX)])) + .call() + .await + .unwrap() + .value; + assert_eq!( + result, + Location::Jupiter([Color::Red, Color::Blue(u64::MAX)]) + ); + + // Test enum with "tuple like struct" with struct array value. + let result = instance + .methods() + .enum_input_output(Location::SimplePluto(SimpleStruct { + a: true, + b: u64::MAX, + })) + .call() + .await + .unwrap() + .value; + assert_eq!( + result, + Location::SimplePluto(SimpleStruct { + a: true, + b: u64::MAX, + }) + ); + let input = Person { name: AsciiString::new("Alice".into()).unwrap(), age: 42, @@ -152,16 +214,93 @@ async fn assert_big_contract_calls(wallet: WalletUnlocked, contract_id: Contract .value; assert_eq!(result, input); - let _ = instance.methods().push_storage(42).call().await.unwrap(); + let _ = instance + .methods() + .push_storage_u16(42) + .call() + .await + .unwrap(); let result = instance .methods() - .get_storage(0) + .get_storage_u16(0) .call() .await .unwrap() .value; assert_eq!(result, 42); + let _ = instance + .methods() + .push_storage_simple(SimpleStruct { + a: true, + b: u64::MAX, + }) + .call() + .await + .unwrap(); + let result = instance + .methods() + .get_storage_simple(0) + .call() + .await + .unwrap() + .value; + assert_eq!( + result, + SimpleStruct { + a: true, + b: u64::MAX, + } + ); + + let _ = instance + .methods() + .push_storage_location(Location::Mars) + .call() + .await + .unwrap(); + let result = instance + .methods() + .get_storage_location(0) + .call() + .await + .unwrap() + .value; + assert_eq!(result, Location::Mars); + + let _ = instance + .methods() + .push_storage_location(Location::Earth(u64::MAX)) + .call() + .await + .unwrap(); + let result = instance + .methods() + .get_storage_location(1) + .call() + .await + .unwrap() + .value; + assert_eq!(result, Location::Earth(u64::MAX)); + + let _ = instance + .methods() + .push_storage_location(Location::Jupiter([Color::Red, Color::Blue(u64::MAX)])) + .call() + .await + .unwrap(); + let result = instance + .methods() + .get_storage_location(2) + .call() + .await + .unwrap() + .value; + assert_eq!( + result, + Location::Jupiter([Color::Red, Color::Blue(u64::MAX)]) + ); + let result = instance .methods() .assert_configurables()