diff --git a/mx-rust-semantics/main/modules.md b/mx-rust-semantics/main/modules.md index 2c3d66d..c8b37c6 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-buffer.md" requires "modules/managed-vec.md" @@ -18,6 +19,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-BUFFER imports private MX-RUST-MODULES-MANAGED-VEC 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) + } +}