Skip to content

Commit

Permalink
Implement EgldOrEsdtTokenIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Sep 30, 2024
1 parent a0c040b commit d0c0125
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mx-rust-semantics/main/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
62 changes: 62 additions & 0 deletions mx-rust-semantics/main/modules/egld-or-esdt-token-identifier.md
Original file line number Diff line number Diff line change
@@ -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
```
Empty file.
13 changes: 13 additions & 0 deletions tests/mx-rust-contracts/eoe-token-identifier.1.run
Original file line number Diff line number Diff line change
@@ -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")
18 changes: 18 additions & 0 deletions tests/mx-rust-contracts/eoe-token-identifier.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d0c0125

Please sign in to comment.