Skip to content

Commit

Permalink
introduce ethereum method schema
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda authored and jaromil committed Jun 20, 2023
1 parent 859fa3c commit 88acb42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
34 changes: 30 additions & 4 deletions src/lua/zencode_ethereum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ local function export_signature_f(obj)
return "0x"..O.to_hex(obj)
end

local function import_method_f(obj)
local res = {}
res.name = ZEN.get(obj, 'name', nil, O.from_string)
local input = ZEN.get(obj, 'input', nil, O.from_string)
if type(input) ~= "table" then
error("invalid input type: "..type(obj.output).."should be a string array")
end
res.input = input
local output = ZEN.get(obj, 'output', nil, O.from_string)
if type(input) ~= "table" then
error("invalid output type: "..type(obj.output).."should be a string array")
end
res.output = output
return res
end

local function export_method_f(obj)
res = {}
res.name = obj.name:octet():string()
res.input = deepmap(function(o) return o:octet():string() end, obj.input)
res.output = deepmap(function(o) return o:octet():string() end, obj.output)
return res
end

ZEN.add_schema(
{
ethereum_public_key = { import = O.from_hex,
Expand Down Expand Up @@ -144,7 +168,9 @@ ZEN.add_schema(
wei_value = { import = str_wei_to_big_wei,
export = big_wei_to_str_wei },
ethereum_signature = { import = import_signature_f,
export = export_signature_f}
export = export_signature_f},
ethereum_method = { import = import_method_f,
export = export_method_f},
})

When('create the ethereum key', function()
Expand Down Expand Up @@ -391,11 +417,11 @@ end)

When("use the ethereum transaction to run '' using ''", function(m, p)

local method = have(m)
local method, codec = have(m)
local params = have(p)
ZEN.assert(
method.name and method.input and type(method.input) == "table",
'method must contain the keys name (string) and input (string array)'
codec.schema == 'ethereum_method',
'method must be a `ethereum method`'
)
local input = deepmap(function(o)
return o:octet():string() end, method.input)
Expand Down
5 changes: 3 additions & 2 deletions test/zencode/ethereum.bats
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ EOF
cat <<EOF | zexe call_sc.zen call_sc.data
Scenario 'ethereum': call solidity
Given I have a 'string dictionary' named 'myMethod'
Given I have a 'ethereum method' named 'myMethod'
Given I have a 'ethereum address' named 'solidity address'
Given I have a 'gas price'
Expand All @@ -769,8 +769,9 @@ When I move 'parameter3' in 'myParams'
When I use the ethereum transaction to run 'myMethod' using 'myParams'
Then print the 'ethereum transaction'
Then print 'myMethod'
EOF
save_output import_ethsig_out.json
assert_output '{"ethereum_transaction":{"data":"483041db000000000000000000000000e54c7b475644fbd918cfedc57b1c9179939921e60000000000000000000000000000000000000000000000000000011f71fb09220000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000004568656c6c6f20776f726c642c2074686973206973206120737472696e672070617373656420617320706172616d6574657220746f206120736d61727420636f6e7472616374000000000000000000000000000000000000000000000000000000","gas_limit":"300000","gas_price":"100000000000","nonce":"0","to":"0xE54c7b475644fBd918cfeDC57b1C9179939921E6","value":"0"}}'
assert_output '{"ethereum_transaction":{"data":"483041db000000000000000000000000e54c7b475644fbd918cfedc57b1c9179939921e60000000000000000000000000000000000000000000000000000011f71fb09220000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000004568656c6c6f20776f726c642c2074686973206973206120737472696e672070617373656420617320706172616d6574657220746f206120736d61727420636f6e7472616374000000000000000000000000000000000000000000000000000000","gas_limit":"300000","gas_price":"100000000000","nonce":"0","to":"0xE54c7b475644fBd918cfeDC57b1C9179939921E6","value":"0"},"myMethod":{"input":["address","uint256","string"],"name":"myFantasticMethod","output":"bool"}}'
}

0 comments on commit 88acb42

Please sign in to comment.