diff --git a/mx-rust-semantics/main/modules.md b/mx-rust-semantics/main/modules.md index e195a2c..cb20cdc 100644 --- a/mx-rust-semantics/main/modules.md +++ b/mx-rust-semantics/main/modules.md @@ -2,6 +2,7 @@ requires "modules/address.md" requires "modules/biguint.md" +requires "modules/blockchain.md" requires "modules/call-value.md" requires "modules/hooks.md" requires "modules/managed-vec.md" @@ -13,6 +14,7 @@ requires "modules/token-identifier.md" module MX-RUST-MODULES imports private MX-RUST-MODULES-ADDRESS imports private MX-RUST-MODULES-BIGUINT + imports private MX-RUST-MODULES-BLOCKCHAIN imports private MX-RUST-MODULES-CALL-VALUE imports private MX-RUST-MODULES-HOOKS imports private MX-RUST-MODULES-MANAGED-VEC diff --git a/mx-rust-semantics/main/modules/blockchain.md b/mx-rust-semantics/main/modules/blockchain.md new file mode 100644 index 0000000..d74301a --- /dev/null +++ b/mx-rust-semantics/main/modules/blockchain.md @@ -0,0 +1,55 @@ +```k + +module MX-RUST-MODULES-BLOCKCHAIN + imports private COMMON-K-CELL + imports private MX-COMMON-SYNTAX + imports private MX-RUST-REPRESENTATION + imports private MX-RUST-REPRESENTATION-CONVERSIONS + imports private RUST-EXECUTION-CONFIGURATION + imports private RUST-SHARED-SYNTAX + + syntax Identifier ::= "MxRust#Blockchain" [token] + + syntax MxRustStructType ::= "blockchainType" [function, total] + rule blockchainType + => rustStructType + ( MxRust#Blockchain + , .MxRustStructFields + ) + + rule + normalizedMethodCall + ( MxRust#Blockchain + , #token("new", "Identifier"):Identifier + , .PtrList + ) + => mxRustNewStruct + ( blockchainType + , .CallParamsList + ) + + rule + normalizedMethodCall + ( MxRust#Blockchain + , #token("get_caller", "Identifier"):Identifier + , ( ptr(_SelfId:Int) + , .PtrList + ) + ) + => MX#getCaller ( .MxValueList ) + ~> mxValueToRust(#token("ManagedAddress", "Identifier"):Identifier) + + rule + normalizedMethodCall + ( MxRust#Blockchain + , #token("get_block_timestamp", "Identifier"):Identifier + , ( ptr(_SelfId:Int) + , .PtrList + ) + ) + => MX#getBlockTimestamp ( .MxValueList ) + ~> mxValueToRust(u64) + +endmodule + +``` diff --git a/mx-rust-semantics/main/preprocessing/contract.md b/mx-rust-semantics/main/preprocessing/contract.md index 6c343ea..cacf921 100644 --- a/mx-rust-semantics/main/preprocessing/contract.md +++ b/mx-rust-semantics/main/preprocessing/contract.md @@ -7,6 +7,7 @@ module MX-RUST-PREPROCESSING-CONTRACT syntax MxRustInstruction ::= rustMxAddContractSend(TypePath) | rustMxAddContractCallValue(TypePath) + | rustMxAddContractBlockchain(TypePath) | rustMxAddContractGenericMethod ( trait: TypePath , method: Identifier @@ -16,6 +17,7 @@ module MX-RUST-PREPROCESSING-CONTRACT rule rustMxAddContractMethods(Trait:TypePath) => rustMxAddContractSend(Trait:TypePath) ~> rustMxAddContractCallValue(Trait:TypePath) + ~> rustMxAddContractBlockchain(Trait:TypePath) rule rustMxAddContractSend(Trait:TypePath) => rustMxAddContractGenericMethod @@ -31,6 +33,13 @@ module MX-RUST-PREPROCESSING-CONTRACT , struct: #token("MxRust#CallValue", "Identifier") ) + rule rustMxAddContractBlockchain(Trait:TypePath) + => rustMxAddContractGenericMethod + (... trait: Trait + , method: #token("blockchain", "Identifier") + , struct: #token("MxRust#Blockchain", "Identifier") + ) + rule rustMxAddContractGenericMethod diff --git a/tests/mx-rust-contracts/blockchain.caller.args b/tests/mx-rust-contracts/blockchain.caller.args new file mode 100644 index 0000000..e69de29 diff --git a/tests/mx-rust-contracts/blockchain.caller.run b/tests/mx-rust-contracts/blockchain.caller.run new file mode 100644 index 0000000..35c9a9f --- /dev/null +++ b/tests/mx-rust-contracts/blockchain.caller.run @@ -0,0 +1,13 @@ +setCallee("Owner"); + +push mxListValue(); +push mxStringValue("getCaller"); +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("Owner") diff --git a/tests/mx-rust-contracts/blockchain.rs b/tests/mx-rust-contracts/blockchain.rs new file mode 100644 index 0000000..11fc218 --- /dev/null +++ b/tests/mx-rust-contracts/blockchain.rs @@ -0,0 +1,23 @@ +#![no_std] + +#[allow(unused_imports)] +use multiversx_sc::imports::*; + +#[multiversx_sc::contract] +pub trait Blockchain { + #[init] + fn init(&self) {} + + #[upgrade] + fn upgrade(&self) {} + + #[endpoint(getCaller)] + fn get_caller(&self) -> ManagedAddress { + self.blockchain().get_caller() + } + + #[endpoint(getBlockTimestamp)] + fn get_block_timestamp(&self) -> u64 { + self.blockchain().get_block_timestamp() + } +} diff --git a/tests/mx-rust-contracts/blockchain.timestamp.args b/tests/mx-rust-contracts/blockchain.timestamp.args new file mode 100644 index 0000000..e69de29 diff --git a/tests/mx-rust-contracts/blockchain.timestamp.run b/tests/mx-rust-contracts/blockchain.timestamp.run new file mode 100644 index 0000000..8f0110f --- /dev/null +++ b/tests/mx-rust-contracts/blockchain.timestamp.run @@ -0,0 +1,14 @@ +setCallee("Owner"); +setBlockTimestamp(123); + +push mxListValue(); +push mxStringValue("getBlockTimestamp"); +push mxIntValue(0); +push mxTransfersValue(); +push mxIntValue(0); +push mxStringValue("TestContract"); +call 6 MX#managedExecuteOnDestContext; +check_eq mxIntValue(0); + +push_return_value; +check_eq mxIntValue(123)