2019-10-31 17:54:56 +03:00
|
|
|
:: ethio: Asynchronous Ethereum input/output functions.
|
|
|
|
::.
|
2019-11-16 14:25:30 +03:00
|
|
|
/+ strandio
|
2019-11-04 20:59:30 +03:00
|
|
|
=, ethereum-types
|
|
|
|
=, able:jael
|
2019-10-31 17:54:56 +03:00
|
|
|
::
|
|
|
|
=> |%
|
2019-11-04 20:59:30 +03:00
|
|
|
+$ topics (list ?(@ux (list @ux)))
|
2019-10-31 17:54:56 +03:00
|
|
|
--
|
|
|
|
|%
|
|
|
|
:: +request-rpc: send rpc request, with retry
|
|
|
|
::
|
|
|
|
++ request-rpc
|
|
|
|
|= [url=@ta id=(unit @t) req=request:rpc:ethereum]
|
2019-11-16 14:25:30 +03:00
|
|
|
=/ m (strand:strandio ,json)
|
2019-10-31 17:54:56 +03:00
|
|
|
^- form:m
|
2019-11-28 01:50:37 +03:00
|
|
|
;< res=(list [id=@t =json]) bind:m
|
|
|
|
(request-batch-rpc-strict url [id req]~)
|
|
|
|
?: ?=([* ~] res)
|
|
|
|
(pure:m json.i.res)
|
2019-12-11 17:04:54 +03:00
|
|
|
%+ strand-fail:strandio
|
|
|
|
%unexpected-multiple-results
|
|
|
|
[>(lent res)< ~]
|
2019-11-28 01:50:37 +03:00
|
|
|
:: +request-batch-rpc-strict: send rpc request, with retry
|
|
|
|
::
|
2019-12-11 17:04:54 +03:00
|
|
|
:: sends a batch request. produces results for all requests in the batch,
|
2019-11-28 01:50:37 +03:00
|
|
|
:: but only if all of them are successful.
|
|
|
|
::
|
|
|
|
++ request-batch-rpc-strict
|
|
|
|
|= [url=@ta reqs=(list [id=(unit @t) req=request:rpc:ethereum])]
|
|
|
|
|^ %+ (retry:strandio results)
|
|
|
|
`10
|
|
|
|
attempt-request
|
|
|
|
::
|
|
|
|
+$ result [id=@t =json]
|
|
|
|
+$ results (list result)
|
2019-10-31 17:54:56 +03:00
|
|
|
::
|
2019-11-28 01:50:37 +03:00
|
|
|
++ attempt-request
|
|
|
|
=/ m (strand:strandio ,(unit results))
|
|
|
|
^- form:m
|
|
|
|
=/ =request:http
|
|
|
|
:* method=%'POST'
|
|
|
|
url=url
|
|
|
|
header-list=['Content-Type'^'application/json' ~]
|
|
|
|
::
|
|
|
|
^= body
|
|
|
|
%- some %- as-octt:mimes:html
|
|
|
|
%- en-json:html
|
|
|
|
a+(turn reqs request-to-json:rpc:ethereum)
|
|
|
|
==
|
|
|
|
;< ~ bind:m
|
|
|
|
(send-request:strandio request)
|
|
|
|
;< rep=(unit client-response:iris) bind:m
|
|
|
|
take-maybe-response:strandio
|
|
|
|
?~ rep
|
|
|
|
(pure:m ~)
|
|
|
|
(parse-responses u.rep)
|
|
|
|
::
|
|
|
|
++ parse-responses
|
2019-10-31 17:54:56 +03:00
|
|
|
|= =client-response:iris
|
2019-11-28 01:50:37 +03:00
|
|
|
=/ m (strand:strandio ,(unit results))
|
2019-10-31 17:54:56 +03:00
|
|
|
^- form:m
|
|
|
|
?> ?=(%finished -.client-response)
|
|
|
|
?~ full-file.client-response
|
|
|
|
(pure:m ~)
|
|
|
|
=/ body=@t q.data.u.full-file.client-response
|
|
|
|
=/ jon=(unit json) (de-json:html body)
|
|
|
|
?~ jon
|
|
|
|
(pure:m ~)
|
|
|
|
=/ array=(unit (list response:rpc:jstd))
|
2019-11-28 01:50:37 +03:00
|
|
|
((ar:dejs-soft:format parse-one-response) u.jon)
|
2019-10-31 17:54:56 +03:00
|
|
|
?~ array
|
2019-11-28 01:50:37 +03:00
|
|
|
(strand-fail:strandio %rpc-result-incomplete-batch >u.jon< ~)
|
2019-12-04 10:53:42 +03:00
|
|
|
=- ?~ err
|
|
|
|
(pure:m `res)
|
2019-11-28 01:50:37 +03:00
|
|
|
(pure:m ~)
|
|
|
|
%+ roll u.array
|
|
|
|
|= $: rpc=response:rpc:jstd
|
|
|
|
[res=results err=(list [id=@t code=@t message=@t])]
|
|
|
|
==
|
|
|
|
?: ?=(%error -.rpc)
|
|
|
|
[res [+.rpc err]]
|
|
|
|
?. ?=(%result -.rpc)
|
|
|
|
[res [['' 'ethio-rpc-fail' (crip <rpc>)] err]]
|
|
|
|
[[+.rpc res] err]
|
2019-10-31 17:54:56 +03:00
|
|
|
::
|
|
|
|
++ parse-one-response
|
|
|
|
|= =json
|
|
|
|
^- (unit response:rpc:jstd)
|
|
|
|
=/ res=(unit [@t ^json])
|
|
|
|
%. json
|
|
|
|
=, dejs-soft:format
|
|
|
|
(ot id+so result+some ~)
|
|
|
|
?^ res `[%result u.res]
|
|
|
|
~| parse-one-response=json
|
|
|
|
:+ ~ %error %- need
|
|
|
|
%. json
|
|
|
|
=, dejs-soft:format
|
|
|
|
(ot id+so error+(ot code+no message+so ~) ~)
|
|
|
|
--
|
2019-10-31 21:25:57 +03:00
|
|
|
:: +read-contract: calls a read function on a contract, produces result hex
|
|
|
|
::
|
|
|
|
++ read-contract
|
2019-12-11 17:06:33 +03:00
|
|
|
|= [url=@t req=proto-read-request:rpc:ethereum]
|
2019-11-16 14:25:30 +03:00
|
|
|
=/ m (strand:strandio ,@t)
|
2019-12-11 17:06:33 +03:00
|
|
|
;< res=(list [id=@t res=@t]) bind:m
|
|
|
|
(batch-read-contract-strict url [req]~)
|
|
|
|
?: ?=([* ~] res)
|
|
|
|
(pure:m res.i.res)
|
|
|
|
%+ strand-fail:strandio
|
|
|
|
%unexpected-multiple-results
|
|
|
|
[>(lent res)< ~]
|
|
|
|
:: +batch-read-contract-strict: calls read functions on contracts
|
|
|
|
::
|
|
|
|
:: sends a batch request. produces results for all requests in the batch,
|
|
|
|
:: but only if all of them are successful.
|
|
|
|
::
|
|
|
|
++ batch-read-contract-strict
|
|
|
|
|= [url=@t reqs=(list proto-read-request:rpc:ethereum)]
|
|
|
|
|^ =/ m (strand:strandio ,results)
|
|
|
|
^- form:m
|
|
|
|
;< res=(list [id=@t =json]) bind:m
|
|
|
|
%+ request-batch-rpc-strict url
|
|
|
|
(turn reqs proto-to-rpc)
|
|
|
|
=+ ^- [=results =failures]
|
|
|
|
(roll res response-to-result)
|
|
|
|
?~ failures (pure:m results)
|
|
|
|
(strand-fail:strandio %batch-read-failed-for >failures< ~)
|
|
|
|
::
|
|
|
|
+$ results (list [id=@t res=@t])
|
|
|
|
+$ failures (list [id=@t =json])
|
|
|
|
::
|
|
|
|
++ proto-to-rpc
|
|
|
|
|= proto-read-request:rpc:ethereum
|
|
|
|
^- [(unit @t) request:rpc:ethereum]
|
|
|
|
:- id
|
2019-10-31 21:25:57 +03:00
|
|
|
:+ %eth-call
|
|
|
|
^- call:rpc:ethereum
|
|
|
|
[~ to ~ ~ ~ `tape`(encode-call:rpc:ethereum function arguments)]
|
|
|
|
[%label %latest]
|
2019-12-11 17:06:33 +03:00
|
|
|
::
|
|
|
|
++ response-to-result
|
|
|
|
|= [[id=@t =json] =results =failures]
|
|
|
|
^+ [results failures]
|
|
|
|
?: ?=(%s -.json)
|
|
|
|
[[id^p.json results] failures]
|
|
|
|
[results [id^json failures]]
|
|
|
|
--
|
|
|
|
::
|
2019-11-04 20:59:30 +03:00
|
|
|
::
|
|
|
|
++ get-latest-block
|
|
|
|
|= url=@ta
|
2019-11-16 14:25:30 +03:00
|
|
|
=/ m (strand:strandio ,block)
|
2019-11-04 20:59:30 +03:00
|
|
|
^- form:m
|
|
|
|
;< =json bind:m
|
|
|
|
(request-rpc url `'block number' %eth-block-number ~)
|
|
|
|
(get-block-by-number url (parse-eth-block-number:rpc:ethereum json))
|
|
|
|
::
|
|
|
|
++ get-block-by-number
|
|
|
|
|= [url=@ta =number:block]
|
2019-11-16 14:25:30 +03:00
|
|
|
=/ m (strand:strandio ,block)
|
2019-11-04 20:59:30 +03:00
|
|
|
^- form:m
|
|
|
|
|^
|
2019-12-05 04:58:00 +03:00
|
|
|
%+ (retry:strandio ,block) `10
|
|
|
|
=/ m (strand:strandio ,(unit block))
|
|
|
|
^- form:m
|
2019-11-04 20:59:30 +03:00
|
|
|
;< =json bind:m
|
|
|
|
%+ request-rpc url
|
|
|
|
:- `'block by number'
|
|
|
|
[%eth-get-block-by-number number |]
|
2019-12-05 04:58:00 +03:00
|
|
|
(pure:m (parse-block json))
|
2019-11-04 20:59:30 +03:00
|
|
|
::
|
|
|
|
++ parse-block
|
|
|
|
|= =json
|
2019-12-05 04:58:00 +03:00
|
|
|
^- (unit block)
|
|
|
|
=< ?~(. ~ `[[&1 &2] |2]:u)
|
|
|
|
^- (unit [@ @ @])
|
2019-11-04 20:59:30 +03:00
|
|
|
~| json
|
|
|
|
%. json
|
2019-12-05 04:58:00 +03:00
|
|
|
=, dejs-soft:format
|
2019-11-04 20:59:30 +03:00
|
|
|
%- ot
|
2019-12-05 04:58:00 +03:00
|
|
|
:~ hash+parse-hex
|
|
|
|
number+parse-hex
|
|
|
|
'parentHash'^parse-hex
|
2019-11-04 20:59:30 +03:00
|
|
|
==
|
2019-12-05 04:58:00 +03:00
|
|
|
::
|
|
|
|
++ parse-hex |=(=json `(unit @)`(some (parse-hex-result:rpc:ethereum json)))
|
2019-11-04 20:59:30 +03:00
|
|
|
--
|
|
|
|
::
|
|
|
|
++ get-logs-by-hash
|
|
|
|
|= [url=@ta =hash:block contracts=(list address) =topics]
|
2019-11-16 14:25:30 +03:00
|
|
|
=/ m (strand:strandio (list event-log:rpc:ethereum))
|
2019-11-04 20:59:30 +03:00
|
|
|
^- form:m
|
|
|
|
;< =json bind:m
|
|
|
|
%+ request-rpc url
|
|
|
|
:* `'logs by hash'
|
|
|
|
%eth-get-logs-by-hash
|
|
|
|
hash
|
|
|
|
contracts
|
|
|
|
topics
|
|
|
|
==
|
|
|
|
%- pure:m
|
|
|
|
(parse-event-logs:rpc:ethereum json)
|
|
|
|
::
|
|
|
|
++ get-logs-by-range
|
|
|
|
|= $: url=@ta
|
|
|
|
contracts=(list address)
|
|
|
|
=topics
|
|
|
|
=from=number:block
|
|
|
|
=to=number:block
|
|
|
|
==
|
2019-11-16 14:25:30 +03:00
|
|
|
=/ m (strand:strandio (list event-log:rpc:ethereum))
|
2019-11-04 20:59:30 +03:00
|
|
|
^- form:m
|
|
|
|
;< =json bind:m
|
|
|
|
%+ request-rpc url
|
|
|
|
:* `'logs by range'
|
|
|
|
%eth-get-logs
|
|
|
|
`number+from-number
|
|
|
|
`number+to-number
|
|
|
|
contracts
|
|
|
|
topics
|
|
|
|
==
|
|
|
|
%- pure:m
|
|
|
|
(parse-event-logs:rpc:ethereum json)
|
2019-12-11 18:28:17 +03:00
|
|
|
::
|
|
|
|
++ get-next-nonce
|
|
|
|
|= [url=@ta =address]
|
|
|
|
=/ m (strand:strandio ,@ud)
|
|
|
|
^- form:m
|
|
|
|
;< =json bind:m
|
|
|
|
%^ request-rpc url `'nonce'
|
|
|
|
[%eth-get-transaction-count address [%label %latest]]
|
|
|
|
%- pure:m
|
|
|
|
(parse-eth-get-transaction-count:rpc:ethereum json)
|
2019-11-04 20:59:30 +03:00
|
|
|
--
|