From d0c01252dfebad51a6cf47c4a4a015c055c9418a Mon Sep 17 00:00:00 2001 From: Virgil Date: Mon, 30 Sep 2024 22:20:20 +0300 Subject: [PATCH] Implement EgldOrEsdtTokenIdentifier --- mx-rust-semantics/main/modules.md | 2 + .../modules/egld-or-esdt-token-identifier.md | 62 +++++++++++++++++++ .../eoe-token-identifier.1.args | 0 .../eoe-token-identifier.1.run | 13 ++++ .../mx-rust-contracts/eoe-token-identifier.rs | 18 ++++++ 5 files changed, 95 insertions(+) create mode 100644 mx-rust-semantics/main/modules/egld-or-esdt-token-identifier.md create mode 100644 tests/mx-rust-contracts/eoe-token-identifier.1.args create mode 100644 tests/mx-rust-contracts/eoe-token-identifier.1.run create mode 100644 tests/mx-rust-contracts/eoe-token-identifier.rs diff --git a/mx-rust-semantics/main/modules.md b/mx-rust-semantics/main/modules.md index a4a15c6..a3fa234 100644 --- a/mx-rust-semantics/main/modules.md +++ b/mx-rust-semantics/main/modules.md @@ -4,6 +4,7 @@ requires "modules/address.md" requires "modules/biguint.md" requires "modules/blockchain.md" requires "modules/call-value.md" +requires "modules/egld-or-esdt-token-identifier.md" requires "modules/hooks.md" requires "modules/managed-vec.md" requires "modules/multi-value-encoded.md" @@ -17,6 +18,7 @@ module MX-RUST-MODULES imports private MX-RUST-MODULES-BIGUINT imports private MX-RUST-MODULES-BLOCKCHAIN imports private MX-RUST-MODULES-CALL-VALUE + imports private MX-RUST-MODULES-EGLD-OR-ESDT-TOKEN-IDENTIFIER imports private MX-RUST-MODULES-HOOKS imports private MX-RUST-MODULES-MANAGED-VEC imports private MX-RUST-MODULES-MULTI-VALUE-ENCODED diff --git a/mx-rust-semantics/main/modules/egld-or-esdt-token-identifier.md b/mx-rust-semantics/main/modules/egld-or-esdt-token-identifier.md new file mode 100644 index 0000000..fbd2ba8 --- /dev/null +++ b/mx-rust-semantics/main/modules/egld-or-esdt-token-identifier.md @@ -0,0 +1,62 @@ +```k + +module MX-RUST-MODULES-EGLD-OR-ESDT-TOKEN-IDENTIFIER + imports private MX-COMMON-SYNTAX + imports private MX-RUST-REPRESENTATION + imports private MX-RUST-REPRESENTATION-CONVERSIONS + imports private RUST-SHARED-SYNTAX + + rule + normalizedMethodCall + ( #token("EgldOrEsdtTokenIdentifier", "Identifier"):Identifier + , #token("from", "Identifier"):Identifier + , ( ptr(ValueId:Int) + , .PtrList + ) + ) + // TODO: Should check that V >= 0 + => ( + let #token("x", "Identifier"):Identifier = newEgldOrEsdtTokenIdentifier(); + #token("x", "Identifier") . #token("mx_token_identifier", "Identifier") + = ptr(ValueId) . #token("mx_token_identifier", "Identifier"); + #token("x", "Identifier"):Expression + ):Statements + + // -------------------------------------- + + syntax MxRustType ::= "egldOrEsdtTokenIdentifierType" [function, total] + rule egldOrEsdtTokenIdentifierType + => rustStructType + ( #token("EgldOrEsdtTokenIdentifier", "Identifier"):Identifier + , ( mxRustStructField + ( #token("mx_token_identifier", "Identifier"):Identifier + , str + ) + , .MxRustStructFields + ) + ) + + rule mxRustEmptyValue(rustType(#token("EgldOrEsdtTokenIdentifier", "Identifier"))) + => mxToRustTyped(egldOrEsdtTokenIdentifierType, mxListValue(mxStringValue(""))) + + rule mxValueToRust(#token("EgldOrEsdtTokenIdentifier", "Identifier"), V:MxValue) + => mxToRustTyped(egldOrEsdtTokenIdentifierType, mxListValue(V)) + + rule rustValueToMx + ( struct + ( #token("EgldOrEsdtTokenIdentifier", "Identifier"):Identifier + , #token("mx_token_identifier", "Identifier"):Identifier |-> TokenValueId:Int + .Map + ) + ) + => ptr(TokenValueId) ~> rustValueToMx + + // -------------------------------------- + + syntax Expression ::= "newEgldOrEsdtTokenIdentifier" + + rule newEgldOrEsdtTokenIdentifier => mxRustEmptyValue(rustType(#token("EgldOrEsdtTokenIdentifier", "Identifier"))) + +endmodule + +``` diff --git a/tests/mx-rust-contracts/eoe-token-identifier.1.args b/tests/mx-rust-contracts/eoe-token-identifier.1.args new file mode 100644 index 0000000..e69de29 diff --git a/tests/mx-rust-contracts/eoe-token-identifier.1.run b/tests/mx-rust-contracts/eoe-token-identifier.1.run new file mode 100644 index 0000000..4d3af87 --- /dev/null +++ b/tests/mx-rust-contracts/eoe-token-identifier.1.run @@ -0,0 +1,13 @@ +setCallee("Owner"); + +push mxListValue(mxStringValue("MyToken")); +push mxStringValue("eoeTokenIdentifier"); +push mxIntValue(0); +push mxTransfersValue(); +push mxIntValue(0); +push mxStringValue("TestContract"); +call 6 MX#managedExecuteOnDestContext; +check_eq mxIntValue(0); + +push_return_value; +check_eq mxStringValue("MyToken") diff --git a/tests/mx-rust-contracts/eoe-token-identifier.rs b/tests/mx-rust-contracts/eoe-token-identifier.rs new file mode 100644 index 0000000..cd8f21d --- /dev/null +++ b/tests/mx-rust-contracts/eoe-token-identifier.rs @@ -0,0 +1,18 @@ +#![no_std] + +#[allow(unused_imports)] +use multiversx_sc::imports::*; + +#[multiversx_sc::contract] +pub trait Contract { + #[init] + fn init(&self) {} + + #[upgrade] + fn upgrade(&self) {} + + #[endpoint(eoeTokenIdentifier)] + fn eoe_token_identifier(&self, t: TokenIdentifier) -> EgldOrEsdtTokenIdentifier { + EgldOrEsdtTokenIdentifier::from(t) + } +}