-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the send() function for contracts (#102)
- Loading branch information
1 parent
ee20e88
commit 5481094
Showing
13 changed files
with
256 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
```k | ||
module MX-RUST-MODULES-SEND | ||
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#Send" [token] | ||
syntax MxRustStructType ::= "sendType" [function, total] | ||
rule sendType | ||
=> rustStructType | ||
( MxRust#Send | ||
, .MxRustStructFields | ||
) | ||
rule | ||
normalizedMethodCall | ||
( MxRust#Send | ||
, #token("new", "Identifier"):Identifier | ||
, .PtrList | ||
) | ||
=> mxRustNewStruct | ||
( sendType | ||
, .CallParamsList | ||
) | ||
rule | ||
<k> | ||
normalizedMethodCall | ||
( MxRust#Send | ||
, #token("direct_esdt", "Identifier"):Identifier | ||
, ( ptr(_SelfId:Int) | ||
, ptr(AddressId:Int) | ||
, ptr(TokenIdId:Int) | ||
, ptr(NonceId:Int) | ||
, ptr(AmountId:Int) | ||
, .PtrList | ||
) | ||
) | ||
=> rustMxDirectEsdt | ||
(... destination: Address | ||
, tokenId: TokenId | ||
, nonce: Nonce | ||
, amount: Amount | ||
) | ||
~> mxRustCheckMxStatus | ||
~> ptrValue(null, ()) | ||
... | ||
</k> | ||
<values> | ||
AddressId |-> Address:Value | ||
TokenIdId |-> TokenId:Value | ||
NonceId |-> Nonce:Value | ||
AmountId |-> Amount:Value | ||
... | ||
</values> | ||
syntax RustMxInstruction ::= rustMxDirectEsdt | ||
( destination: MxOrRustValueOrInstruction // MxOrRustValue | ||
, tokenId: MxOrRustValueOrInstruction // MxOrRustValue | ||
, nonce: MxOrRustValueOrInstruction // MxOrRustValue | ||
, amount: MxOrRustValueOrInstruction // MxOrRustValue | ||
) | ||
context rustMxDirectEsdt | ||
(... destination: HOLE:MxOrRustValue => rustToMx(HOLE) | ||
, tokenId: _:MxOrRustValue | ||
, nonce: _:MxOrRustValue | ||
, amount: _:MxOrRustValue | ||
) | ||
[result(MxValue)] | ||
context rustMxDirectEsdt | ||
(... destination: Destination:MxOrRustValue | ||
, tokenId: HOLE:MxOrRustValue => rustToMx(HOLE) | ||
, nonce: _:MxOrRustValue | ||
, amount: _:MxOrRustValue | ||
) | ||
requires isMxValue(Destination) | ||
[result(MxValue)] | ||
context rustMxDirectEsdt | ||
(... destination: Destination:MxOrRustValue | ||
, tokenId: TokenId:MxOrRustValue | ||
, nonce: HOLE:MxOrRustValue => rustToMx(HOLE) | ||
, amount: _:MxOrRustValue | ||
) | ||
requires isMxValue(Destination) | ||
andBool isMxValue(TokenId) | ||
[result(MxValue)] | ||
context rustMxDirectEsdt | ||
(... destination: Destination:MxOrRustValue | ||
, tokenId: TokenId:MxOrRustValue | ||
, nonce: Nonce:MxOrRustValue | ||
, amount: HOLE:MxOrRustValue => rustToMx(HOLE) | ||
) | ||
requires isMxValue(Destination) | ||
andBool isMxValue(TokenId) | ||
andBool isMxValue(Nonce) | ||
[result(MxValue)] | ||
rule rustMxDirectEsdt | ||
(... destination: mxListValue(Destination:MxValue , .MxValueList) | ||
, tokenId: mxListValue(mxStringValue(TokenId:String) , .MxValueList) | ||
, nonce: mxIntValue(Nonce:Int) | ||
, amount: mxIntValue(Amount:Int) | ||
) | ||
=> MX#managedMultiTransferESDTNFTExecute | ||
( Destination | ||
, mxTransfersValue | ||
( mxTransferValue(... token:TokenId, nonce:Nonce, value: Amount) | ||
, .MxEsdtTransferList | ||
) | ||
, mxIntValue(0) // TODO: Use some gas value | ||
, mxStringValue("") | ||
, mxListValue(.MxValueList) | ||
) | ||
endmodule | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
```k | ||
module MX-RUST-PREPROCESSING-CONTRACT | ||
imports COMMON-K-CELL | ||
imports MX-RUST-REPRESENTATION | ||
imports RUST-PREPROCESSING-CONFIGURATION | ||
syntax MxRustInstruction ::= rustMxAddContractSend(TypePath) | ||
rule rustMxAddContractMethods(Trait:TypePath) | ||
=> rustMxAddContractSend(Trait:TypePath) | ||
rule | ||
<k> | ||
rustMxAddContractSend(Trait:TypePath) | ||
=> error | ||
( "send already exists for trait" | ||
, ListItem(Trait) | ||
) | ||
... | ||
</k> | ||
<trait-path> Trait </trait-path> | ||
<method-name> #token("send", "Identifier") </method-name> | ||
[priority(50)] | ||
rule | ||
<k> | ||
rustMxAddContractSend(Trait:TypePath) | ||
=> .K | ||
... | ||
</k> | ||
<trait-path> Trait </trait-path> | ||
( .Bag | ||
=> <method> | ||
<method-name> #token("send", "Identifier") </method-name> | ||
<method-params> self : $selftype , .NormalizedFunctionParameterList </method-params> | ||
<method-return-type> #token("MxRust#CallReturnType", "Identifier") </method-return-type> | ||
<method-implementation> | ||
block({ | ||
.InnerAttributes | ||
( | ||
( | ||
( #token("MxRust#Send", "Identifier") | ||
:: (#token("new", "Identifier"):PathExprSegment) | ||
:: .PathExprSegments | ||
) | ||
( .CallParamsList ) | ||
):Expression | ||
):Statements | ||
}) | ||
</method-implementation> | ||
<method-outer-attributes> `emptyOuterAttributes`(.KList):OuterAttributes </method-outer-attributes> | ||
</method> | ||
) | ||
[priority(100)] | ||
endmodule | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
addAccount("Caller"); | ||
setBalance("TestContract", "MyToken", 0, 1234); | ||
setCallee("Caller"); | ||
|
||
push mxIntValue(0); | ||
push mxStringValue("MyToken"); | ||
push mxStringValue("Caller"); | ||
call 3 MX#bigIntGetESDTExternalBalance; | ||
get_big_int; | ||
check_eq mxIntValue(0); | ||
|
||
push mxIntValue(0); | ||
push mxStringValue("MyToken"); | ||
push mxStringValue("TestContract"); | ||
call 3 MX#bigIntGetESDTExternalBalance; | ||
get_big_int; | ||
check_eq mxIntValue(1234); | ||
|
||
push mxListValue(mxStringValue("Caller"), mxStringValue("MyToken"), mxIntValue(10)); | ||
push mxStringValue("sendTo"); | ||
push mxIntValue(0); | ||
push mxTransfersValue(); | ||
push mxIntValue(0); | ||
push mxStringValue("TestContract"); | ||
call 6 MX#managedExecuteOnDestContext; | ||
get_big_int; | ||
check_eq mxIntValue(0); | ||
|
||
push mxIntValue(0); | ||
push mxStringValue("MyToken"); | ||
push mxStringValue("Caller"); | ||
call 3 MX#bigIntGetESDTExternalBalance; | ||
get_big_int; | ||
check_eq mxIntValue(10); | ||
|
||
push mxIntValue(0); | ||
push mxStringValue("MyToken"); | ||
push mxStringValue("TestContract"); | ||
call 3 MX#bigIntGetESDTExternalBalance; | ||
get_big_int; | ||
check_eq mxIntValue(1224) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Storage { | ||
#[init] | ||
fn init(&self) {} | ||
|
||
#[upgrade] | ||
fn upgrade(&self) {} | ||
|
||
#[endpoint(sendTo)] | ||
fn send_to(&self, address: ManagedAddress, token: TokenIdentifier, amount: BigUint) { | ||
self.send().direct_esdt(address, token, 0_u64, amount); | ||
} | ||
} |