From 5d71ca19369083dcb956b1ea4980e095c71bf100 Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 13 Aug 2019 14:22:53 +0200 Subject: [PATCH 1/2] Fixes #6, add assertion for not payable. --- examples/payable.nim | 13 +++++++++++++ nimplay.nimble | 6 +++--- nimplay/nimplay_macros.nim | 17 +++++++++++++++-- nimplay/utils.nim | 1 + 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 examples/payable.nim diff --git a/examples/payable.nim b/examples/payable.nim new file mode 100644 index 0000000..fc1d222 --- /dev/null +++ b/examples/payable.nim @@ -0,0 +1,13 @@ +import ../nimplay/nimplay_macros +import ../nimplay/types +import ../nimplay/ewasm_eei +import stint + + +contract("PayAssert"): + + proc plus_two*(a: uint256): uint256 {.payable.} = + a + 2 + + proc plus_one*(a: uint256): uint256 = + a + 1 diff --git a/nimplay.nimble b/nimplay.nimble index 8079f60..036c77d 100644 --- a/nimplay.nimble +++ b/nimplay.nimble @@ -18,10 +18,10 @@ proc buildTool(name: string) = task examples, "Build examples": - buildExample("wrc20") - buildExample("wrc202") + # buildExample("wrc20") + # buildExample("wrc202") buildExample("king_of_the_hill") - # buildExample("hello") + # buildExample("payable") # buildExample("hello2") # buildExample("hello3") diff --git a/nimplay/nimplay_macros.nim b/nimplay/nimplay_macros.nim index 93c8b94..9fe8c6e 100644 --- a/nimplay/nimplay_macros.nim +++ b/nimplay/nimplay_macros.nim @@ -210,11 +210,21 @@ proc handle_event_defines(event_def: NimNode, global_ctx: var GlobalContext) = proc get_util_functions(): NimNode = - parseStmt(""" + var stmts = newStmtList() + + stmts.add(parseStmt(""" template copy_into_ba(to_ba: var untyped, offset: int, from_ba: untyped) = for i, x in from_ba: to_ba[offset + i] = x - """) + """)) + stmts.add(parseStmt(""" + proc assertNotPayable() = + var b {.noinit.}: array[16, byte] + getCallValue(addr b) + if Uint128.fromBytesBE(b) > 0.stuint(128): + revert(nil, 0) + """)) + stmts # return proc get_getter_func(var_struct: VariableType): NimNode = @@ -317,6 +327,9 @@ proc handle_contract_interface(in_stmts: NimNode): NimNode = ) var start_offset = 4 + if not func_sig.payable: + call_and_copy_block.add(parseStmt("assertNotPayable()")) + for idx, param in func_sig.inputs: var static_param_size = get_byte_size_of(param.var_type) var tmp_var_name = fmt"{func_sig.name}_param_{idx}" diff --git a/nimplay/utils.nim b/nimplay/utils.nim index 2ae5c4b..e8e6bce 100644 --- a/nimplay/utils.nim +++ b/nimplay/utils.nim @@ -62,6 +62,7 @@ proc get_byte_size_of*(type_str: string): int = let BASE32_TYPES_NAMES: array = [ "uint256", "uint128", + "int128", "address", "bytes32", "wei_value" From 992a84508a437eed5b7c1a575431c25e1672653c Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Tue, 13 Aug 2019 15:30:56 +0200 Subject: [PATCH 2/2] Fixes #8, add nimplay0_1 export file --- examples/king_of_the_hill.nim | 6 +----- nimplay0_1.nim | 9 +++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 nimplay0_1.nim diff --git a/examples/king_of_the_hill.nim b/examples/king_of_the_hill.nim index bbc95d2..89018a7 100644 --- a/examples/king_of_the_hill.nim +++ b/examples/king_of_the_hill.nim @@ -1,9 +1,5 @@ -import ../nimplay/nimplay_macros -import ../nimplay/types -import ../nimplay/ewasm_eei -import stint +import ../nimplay0_1 -# import nimplay0_1 contract("KingOfTheHill"): diff --git a/nimplay0_1.nim b/nimplay0_1.nim new file mode 100644 index 0000000..b67ab56 --- /dev/null +++ b/nimplay0_1.nim @@ -0,0 +1,9 @@ +import nimplay/nimplay_macros +import nimplay/types +import nimplay/ewasm_eei +import stint + +export nimplay_macros +export types +export ewasm_eei +export stint