11local Hash = require " cryptography.hash"
2- local utils = require " utils"
2+ local eth_ebi = require " utils.eth_ebi "
33
44local function parse_topics (json )
55 local _ , _ , topics = json :find (
66 [==[ "topics":%[([^%]]*)%]]==]
77 )
88
99 local t = {}
10- for k ,_ in string.gmatch (topics , [[ "(0x%x+)"]] ) do
10+ for k , _ in string.gmatch (topics , [[ "(0x%x+)"]] ) do
1111 table.insert (t , k )
1212 end
1313
@@ -19,7 +19,7 @@ local function parse_data(json, sig)
1919 [==[ "data":"(0x%x+)"]==]
2020 )
2121
22- local decoded_data = utils .decode_event_data (sig , data )
22+ local decoded_data = eth_ebi .decode_event_data (sig , data )
2323 return decoded_data
2424end
2525
4848
4949local function parse_logs (logs , data_sig )
5050 local ret = {}
51- for k ,_ in string.gmatch (logs , [[ {[^}]*}]] ) do
51+ for k , _ in string.gmatch (logs , [[ {[^}]*}]] ) do
5252 local emited_topics = parse_topics (k )
5353 local decoded_data = parse_data (k , data_sig )
5454 local meta = parse_meta (k )
55- table.insert (ret , {emited_topics = emited_topics , decoded_data = decoded_data , meta = meta })
55+ table.insert (ret , { emited_topics = emited_topics , decoded_data = decoded_data , meta = meta })
5656 end
5757
5858 return ret
@@ -62,7 +62,7 @@ local function join_tables(...)
6262 local function join (ret , t , ...)
6363 if not t then return ret end
6464
65- for k ,v in ipairs (t ) do
65+ for k , v in ipairs (t ) do
6666 ret [k ] = v
6767 end
6868
@@ -91,40 +91,40 @@ local function sort_and_dedup(t)
9191 end )
9292
9393 local ret = {}
94- for k ,v in ipairs (t ) do
95- local v2 = t [k + 1 ]
94+ for k , v in ipairs (t ) do
95+ local v2 = t [k + 1 ]
9696 if not v2 then
9797 table.insert (ret , v )
9898 else
9999 local m1 , m2 = v .meta , v2 .meta
100100 if not (m1 .block_number == m2 .block_number and m1 .log_index == m2 .log_index ) then
101101 table.insert (ret , v )
102- end
102+ end
103103 end
104104 end
105105
106106 return ret
107107end
108108
109109local function quote_args (args , not_quote )
110- local quoted_args = {}
111- for _ ,v in ipairs (args ) do
110+ local quoted_args = {}
111+ for _ , v in ipairs (args ) do
112112 if type (v ) == " table" then
113113 if v ._tag == " tuple" then
114114 local qa = quote_args (v , true )
115115 local ca = table.concat (qa , " ," )
116116 local sb = " '(" .. ca .. " )'"
117117 table.insert (quoted_args , sb )
118118 else
119- local qa = quote_args (v , true )
119+ local qa = quote_args (v , true )
120120 local ca = table.concat (qa , " ," )
121121 local sb = " '[" .. ca .. " ]'"
122122 table.insert (quoted_args , sb )
123123 end
124124 elseif not_quote then
125125 table.insert (quoted_args , v )
126126 else
127- table.insert (quoted_args , ' "' .. v .. ' "' )
127+ table.insert (quoted_args , ' "' .. v .. ' "' )
128128 end
129129 end
130130
@@ -144,7 +144,6 @@ function Client:new(blockchain)
144144
145145 setmetatable (client , self )
146146 return client
147-
148147end
149148
150149local cast_logs_template = [==[
@@ -153,16 +152,16 @@ cast rpc -r "%s" eth_getLogs \
153152]==]
154153
155154function Client :_read_logs (tournament_address , sig , topics , data_sig )
156- topics = topics or {false , false , false }
157- local encoded_sig = utils .encode_sig (sig )
155+ topics = topics or { false , false , false }
156+ local encoded_sig = eth_ebi .encode_sig (sig )
158157 table.insert (topics , 1 , encoded_sig )
159158 assert (# topics == 4 , " topics doesn't have four elements" )
160159
161160 local topics_strs = {}
162- for _ ,v in ipairs (topics ) do
161+ for _ , v in ipairs (topics ) do
163162 local s
164163 if v then
165- s = ' "' .. v .. ' "'
164+ s = ' "' .. v .. ' "'
166165 else
167166 s = " null"
168167 end
@@ -198,8 +197,8 @@ cast call --rpc-url "%s" "%s" "%s" %s 2>&1
198197
199198function Client :_call (address , sig , args )
200199 local quoted_args = {}
201- for _ ,v in ipairs (args ) do
202- table.insert (quoted_args , ' "' .. v .. ' "' )
200+ for _ , v in ipairs (args ) do
201+ table.insert (quoted_args , ' "' .. v .. ' "' )
203202 end
204203 local args_str = table.concat (quoted_args , " " )
205204
@@ -236,20 +235,20 @@ function Client:read_match_created(tournament_address, commitment_hash)
236235 local sig = " matchCreated(bytes32,bytes32,bytes32)"
237236 local data_sig = " (bytes32)"
238237
239- local logs1 = self :_read_logs (tournament_address , sig , {commitment_hash , false , false }, data_sig )
240- local logs2 = self :_read_logs (tournament_address , sig , {false , commitment_hash , false }, data_sig )
238+ local logs1 = self :_read_logs (tournament_address , sig , { commitment_hash , false , false }, data_sig )
239+ local logs2 = self :_read_logs (tournament_address , sig , { false , commitment_hash , false }, data_sig )
241240
242241 local logs = sort_and_dedup (join_tables (logs1 , logs2 ))
243242
244243 local ret = {}
245- for k ,v in ipairs (logs ) do
244+ for k , v in ipairs (logs ) do
246245 local log = {}
247246 log .tournament_address = tournament_address
248247 log .meta = v .meta
249248
250- log .commitment_one = Hash :from_digest (v .emited_topics [2 ])
251- log .commitment_two = Hash :from_digest (v .emited_topics [3 ])
252- log .left_hash = Hash :from_digest (v .decoded_data [1 ])
249+ log .commitment_one = Hash :from_digest_hex (v .emited_topics [2 ])
250+ log .commitment_two = Hash :from_digest_hex (v .emited_topics [3 ])
251+ log .left_hash = Hash :from_digest_hex (v .decoded_data [1 ])
253252 log .match_id_hash = log .commitment_one :join (log .commitment_two )
254253
255254 ret [k ] = log
261260function Client :read_commitment (tournament_address , commitment_hash )
262261 local sig = " getCommitment(bytes32)((uint64,uint64),bytes32)"
263262
264- local call_ret = self :_call (tournament_address , sig , {commitment_hash })
263+ local call_ret = self :_call (tournament_address , sig , { commitment_hash })
265264 assert (# call_ret == 2 )
266265
267266 local allowance , last_resume = call_ret [1 ]:match " %((%d+),(%d+)%)"
@@ -274,7 +273,7 @@ function Client:read_commitment(tournament_address, commitment_hash)
274273
275274 local ret = {
276275 clock = clock ,
277- final_state = Hash :from_digest (call_ret [2 ])
276+ final_state = Hash :from_digest_hex (call_ret [2 ])
278277 }
279278
280279 return ret
@@ -284,14 +283,14 @@ function Client:read_tournament_created(tournament_address, match_id_hash)
284283 local sig = " newInnerTournament(bytes32,address)"
285284 local data_sig = " (address)"
286285
287- local logs = self :_read_logs (tournament_address , sig , {match_id_hash , false , false }, data_sig )
286+ local logs = self :_read_logs (tournament_address , sig , { match_id_hash , false , false }, data_sig )
288287 assert (# logs <= 1 )
289288
290289 if # logs == 0 then return false end
291290 local log = logs [1 ]
292291
293292 local ret = {
294- parent_match = Hash :from_digest (match_id_hash ),
293+ parent_match = Hash :from_digest_hex (match_id_hash ),
295294 new_tournament = log .decoded_data [1 ],
296295 }
297296
@@ -300,10 +299,10 @@ end
300299
301300function Client :match (address , match_id_hash )
302301 local sig = " getMatch(bytes32)(bytes32,bytes32,bytes32,uint64,uint64,uint64)"
303- local ret = self :_call (address , sig , {match_id_hash })
304- ret [1 ] = Hash :from_digest (ret [1 ])
305- ret [2 ] = Hash :from_digest (ret [2 ])
306- ret [3 ] = Hash :from_digest (ret [3 ])
302+ local ret = self :_call (address , sig , { match_id_hash })
303+ ret [1 ] = Hash :from_digest_hex (ret [1 ])
304+ ret [2 ] = Hash :from_digest_hex (ret [2 ])
305+ ret [3 ] = Hash :from_digest_hex (ret [3 ])
307306
308307 return ret
309308end
@@ -312,26 +311,24 @@ function Client:tournament_winner(address)
312311 local sig = " tournamentWinner()(bytes32)"
313312 local ret = self :_call (address , sig , {})
314313
315- return Hash :from_digest (ret [1 ])
314+ return Hash :from_digest_hex (ret [1 ])
316315end
317316
318317function Client :root_tournament_winner (address )
319318 local sig = " rootTournamentFinalState()(bool,bytes32)"
320319 local ret = self :_call (address , sig , {})
321- ret [2 ] = Hash :from_digest (ret [2 ])
320+ ret [2 ] = Hash :from_digest_hex (ret [2 ])
322321
323322 return ret
324323end
325324
326-
327325function Client :maximum_delay (address )
328326 local sig = " maximumEnforceableDelay()(uint64)"
329327 local ret = self :_call (address , sig , {})
330328
331329 return ret
332330end
333331
334-
335332local cast_send_template = [[
336333cast send --private-key "%s" --rpc-url "%s" "%s" "%s" %s 2>&1
337334]]
363360
364361function Client :tx_join_tournament (tournament_address , final_state , proof , left_child , right_child )
365362 local sig = [[ joinTournament(bytes32,bytes32[],bytes32,bytes32)]]
366- self :_send_tx (tournament_address , sig , {final_state , proof , left_child , right_child })
363+ self :_send_tx (tournament_address , sig , { final_state , proof , left_child , right_child })
367364end
368365
369366function Client :tx_advance_match (
@@ -373,58 +370,56 @@ function Client:tx_advance_match(
373370 self :_send_tx (
374371 tournament_address ,
375372 sig ,
376- {{ commitment_one , commitment_two , _tag = " tuple" }, left , right , new_left , new_right }
373+ { { commitment_one , commitment_two , _tag = " tuple" }, left , right , new_left , new_right }
377374 )
378375end
379376
380-
381377function Client :tx_seal_inner_match (
382378 tournament_address , commitment_one , commitment_two , left , right , initial_hash , proof
383379)
384380 local sig =
385- [[ sealInnerMatchAndCreateInnerTournament((bytes32,bytes32),bytes32,bytes32,bytes32,bytes32[])]]
381+ [[ sealInnerMatchAndCreateInnerTournament((bytes32,bytes32),bytes32,bytes32,bytes32,bytes32[])]]
386382 self :_send_tx (
387383 tournament_address ,
388384 sig ,
389- {{ commitment_one , commitment_two , _tag = " tuple" }, left , right , initial_hash , proof }
385+ { { commitment_one , commitment_two , _tag = " tuple" }, left , right , initial_hash , proof }
390386 )
391387end
392388
393389function Client :tx_win_inner_match (tournament_address , child_tournament_address , left , right )
394390 local sig =
395- [[ winInnerMatch(address,bytes32,bytes32)]]
391+ [[ winInnerMatch(address,bytes32,bytes32)]]
396392 self :_send_tx (
397393 tournament_address ,
398394 sig ,
399- {child_tournament_address , left , right }
395+ { child_tournament_address , left , right }
400396 )
401397end
402398
403399function Client :tx_seal_leaf_match (
404400 tournament_address , commitment_one , commitment_two , left , right , initial_hash , proof
405401)
406402 local sig =
407- [[ sealLeafMatch((bytes32,bytes32),bytes32,bytes32,bytes32,bytes32[])]]
403+ [[ sealLeafMatch((bytes32,bytes32),bytes32,bytes32,bytes32,bytes32[])]]
408404 self :_send_tx (
409405 tournament_address ,
410406 sig ,
411- {{ commitment_one , commitment_two , _tag = " tuple" }, left , right , initial_hash , proof }
407+ { { commitment_one , commitment_two , _tag = " tuple" }, left , right , initial_hash , proof }
412408 )
413409end
414410
415411function Client :tx_win_leaf_match (
416412 tournament_address , commitment_one , commitment_two , left , right
417413)
418414 local sig =
419- [[ winLeafMatch((bytes32,bytes32),bytes32,bytes32)]]
415+ [[ winLeafMatch((bytes32,bytes32),bytes32,bytes32)]]
420416 self :_send_tx (
421417 tournament_address ,
422418 sig ,
423- {{ commitment_one , commitment_two , _tag = " tuple" }, left , right }
419+ { { commitment_one , commitment_two , _tag = " tuple" }, left , right }
424420 )
425421end
426422
427-
428423return Client
429424
430425
0 commit comments