Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement EgldOrEsdtTokenIdentifier #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-buffer.md"
requires "modules/managed-vec.md"
Expand All @@ -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
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)
}
}
Loading