Skip to content

Commit

Permalink
Mx get-sc-balance hook
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta authored and Virgil Serbanuta committed Aug 28, 2024
1 parent c98f1ca commit f5f9185
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
27 changes: 27 additions & 0 deletions mx-semantics/main/accounts/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```k
module MX-ACCOUNTS-CONFIGURATION
imports INT-SYNTAX
imports STRING-SYNTAX
configuration
<mx-accounts>
<mx-account multiplicity="*" type="Map">
// TODO: The address should be bytes.
<mx-account-address> "" </mx-account-address>
<mx-esdt-datas>
<mx-esdt-data multiplicity="*" type="Map">
// TODO: The esdt-id should be bytes.
<mx-esdt-id>
<mx-esdt-id-name> "" </mx-esdt-id-name>
<mx-esdt-id-nonce> 0 </mx-esdt-id-nonce>
</mx-esdt-id>
<mx-esdt-balance> 0 </mx-esdt-balance>
</mx-esdt-data>
</mx-esdt-datas>
</mx-account>
</mx-accounts>
endmodule
```
35 changes: 35 additions & 0 deletions mx-semantics/main/accounts/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```k
module MX-ACCOUNTS-HOOKS
imports private COMMON-K-CELL
imports private MX-ACCOUNTS-CONFIGURATION
imports private MX-COMMON-SYNTAX
rule
<k> MX#bigIntGetESDTExternalBalance
( mxStringValue(Owner:String)
, mxStringValue(TokenId:String)
, mxIntValue(Nonce:Int)
, .MxHookArgs
) => MX#bigIntNew(mxIntValue(Balance)) ... </k>
<mx-account-address> Owner </mx-account-address>
<mx-esdt-id>
<mx-esdt-id-name> TokenId </mx-esdt-id-name>
<mx-esdt-id-nonce> Nonce </mx-esdt-id-nonce>
</mx-esdt-id>
<mx-esdt-balance> Balance:Int </mx-esdt-balance>
[priority(50)]
rule
<k> MX#bigIntGetESDTExternalBalance
( mxStringValue(Owner:String)
, mxStringValue(TokenId:String)
, mxIntValue(Nonce:Int)
, .MxHookArgs
) => MX#bigIntNew(mxIntValue(0)) ... </k>
<mx-account-address> Owner </mx-account-address>
[priority(100)]
endmodule
```
3 changes: 3 additions & 0 deletions mx-semantics/main/configuration.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
```k
requires "accounts/configuration.md"
requires "biguint/configuration.md"
requires "calls/configuration.md"
module MX-COMMON-CONFIGURATION
imports MX-ACCOUNTS-CONFIGURATION
imports MX-BIGUINT-CONFIGURATION
imports MX-CALL-CONFIGURATION
configuration
<mx-common>
<mx-call-data/>
<mx-biguint/>
<mx-accounts/>
</mx-common>
endmodule
Expand Down
2 changes: 2 additions & 0 deletions mx-semantics/main/mx-common.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
```k
requires "accounts/hooks.md"
requires "biguint/hooks.md"
requires "calls/hooks.md"
module MX-COMMON
imports private MX-ACCOUNTS-HOOKS
imports private MX-BIGUINT-HOOKS
imports private MX-CALLS-HOOKS
endmodule
Expand Down
59 changes: 59 additions & 0 deletions mx-semantics/test/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module MX-TEST-EXECUTION-PARSING-SYNTAX
| getBigint(Int)
| "check_eq" MxValue
| setCaller(String)
| addAccount(String)
| setBalance(account:String, token:String, nonce:Int, value:Int)
syntax MxTest ::= NeList{TestInstruction, ";"}
Expand All @@ -20,6 +22,7 @@ endmodule
module MX-TEST-EXECUTION
imports private COMMON-K-CELL
imports private INT
imports private MX-ACCOUNTS-TEST
imports private MX-BIGUINT-TEST
imports private MX-CALL-TEST
imports private MX-TEST-CONFIGURATION
Expand Down Expand Up @@ -92,4 +95,60 @@ module MX-CALL-TEST
<mx-caller> _ => S </mx-caller>
endmodule
module MX-ACCOUNTS-TEST
imports private COMMON-K-CELL
imports private MX-ACCOUNTS-CONFIGURATION
imports private MX-TEST-EXECUTION-PARSING-SYNTAX
rule
<k> addAccount(S:String) => .K ... </k>
<mx-accounts>
.Bag
=> <mx-account>
<mx-account-address> S </mx-account-address>
<mx-esdt-datas> .Bag </mx-esdt-datas>
</mx-account>
</mx-accounts>
rule
<k> setBalance
(... account: Account:String
, token: TokenName:String
, nonce: Nonce:Int
, value: Value:Int
) => .K
...
</k>
<mx-account-address> Account </mx-account-address>
<mx-esdt-id>
<mx-esdt-id-name> TokenName </mx-esdt-id-name>
<mx-esdt-id-nonce> Nonce </mx-esdt-id-nonce>
</mx-esdt-id>
<mx-esdt-balance> _ => Value </mx-esdt-balance>
[priority(50)]
rule
<k> setBalance
(... account: Account:String
, token: TokenName:String
, nonce: Nonce:Int
, value: Value:Int
) => .K
...
</k>
<mx-account-address> Account </mx-account-address>
<mx-esdt-datas>
.Bag =>
<mx-esdt-data>
<mx-esdt-id>
<mx-esdt-id-name> TokenName </mx-esdt-id-name>
<mx-esdt-id-nonce> Nonce </mx-esdt-id-nonce>
</mx-esdt-id>
<mx-esdt-balance> Value </mx-esdt-balance>
</mx-esdt-data>
</mx-esdt-datas>
[priority(100)]
endmodule
```
17 changes: 17 additions & 0 deletions tests/mx/blockchain/get-sc-balance.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
addAccount("Owner");

push mxIntValue(0);
push mxStringValue("MyToken");
push mxStringValue("Owner");
call 3 MX#bigIntGetESDTExternalBalance;
get_big_int;
check_eq mxIntValue(0);

setBalance("Owner", "MyToken", 0, 1234);

push mxIntValue(0);
push mxStringValue("MyToken");
push mxStringValue("Owner");
call 3 MX#bigIntGetESDTExternalBalance;
get_big_int;
check_eq mxIntValue(1234)

0 comments on commit f5f9185

Please sign in to comment.