custody: get data from chain

This commit is contained in:
Philip Monk 2019-10-10 18:26:18 -07:00
parent adba48241a
commit c668a24df2
No known key found for this signature in database
GPG Key ID: B66E1F02604E44EC
4 changed files with 194 additions and 131 deletions

View File

@ -1,5 +1,5 @@
/- spider
/+ threadio
/+ threadio, *azimuth
=, thread=thread:spider
=, able:jael
|%
@ -41,136 +41,6 @@
~
[(turn ~(tap in ships) ,@) ~]
::
++ request-rpc
|= [url=@ta id=(unit @t) req=request:rpc:ethereum]
=/ m (thread ,json)
^- form:m
%+ (retry json) `10
=/ m (thread ,(unit json))
^- form:m
|^
=/ =request:http
:* method=%'POST'
url=url
header-list=['Content-Type'^'application/json' ~]
^= body
%- some %- as-octt:mimes:html
%- en-json:html
(request-to-json:rpc:ethereum id req)
==
;< ~ bind:m (send-request:threadio request)
;< rep=(unit client-response:iris) bind:m
take-maybe-response:threadio
?~ rep
(pure:m ~)
(parse-response u.rep)
::
++ parse-response
|= =client-response:iris
=/ m (thread ,(unit json))
^- 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 ~)
=, dejs-soft:format
=/ array=(unit (list response:rpc:jstd))
((ar parse-one-response) u.jon)
?~ array
=/ res=(unit response:rpc:jstd) (parse-one-response u.jon)
?~ res
(thread-fail:threadio %request-rpc-parse-error >id< ~)
?: ?=(%error -.u.res)
(thread-fail:threadio %request-rpc-error >id< >+.res< ~)
?. ?=(%result -.u.res)
(thread-fail:threadio %request-rpc-fail >u.res< ~)
(pure:m `res.u.res)
(thread-fail:threadio %request-rpc-batch >%not-implemented< ~)
:: (pure:m `[%batch u.array])
::
++ 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 ~) ~)
--
::
++ retry
|* result=mold
|= [crash-after=(unit @ud) computation=_*form:(thread (unit result))]
=/ m (thread ,result)
=| try=@ud
|^
|- ^- form:m
=* loop $
?: =(crash-after `try)
(thread-fail:threadio %retry-too-many ~)
;< ~ bind:m (backoff try ~m1)
;< res=(unit result) bind:m computation
?^ res
(pure:m u.res)
loop(try +(try))
::
++ backoff
|= [try=@ud limit=@dr]
=/ m (thread ,~)
^- form:m
;< eny=@uvJ bind:m get-entropy:threadio
;< now=@da bind:m get-time:threadio
%- wait:threadio
%+ add now
%+ min limit
?: =(0 try) ~s0
%+ add
(mul ~s1 (bex (dec try)))
(mul ~s0..0001 (~(rad og eny) 1.000))
--
::
++ get-latest-block
|= url=@ta
=/ m (thread ,block)
^- 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]
=/ m (thread ,block)
^- form:m
|^
;< =json bind:m
(request-rpc url `'block by number' %eth-get-block-by-number number |)
=/ =block (parse-block json)
?. =(number number.id.block)
(thread-fail:threadio %reorg-detected >number< >block< ~)
(pure:m block)
::
++ parse-block
|= =json
^- block
=< [[&1 &2] |2]
^- [@ @ @]
~| json
%. json
=, dejs:format
%- ot
:~ hash+parse-hex-result:rpc:ethereum
number+parse-hex-result:rpc:ethereum
'parentHash'^parse-hex-result:rpc:ethereum
==
--
::
++ get-logs-by-hash
|= [url=@ta whos=(set ship) =hash:block]
=/ m (thread udiffs:point)

30
pkg/arvo/imp/custody.hoon Normal file
View File

@ -0,0 +1,30 @@
/- spider
/+ *threadio, *azimuth, *custody, *csv
=, thread=thread:libthread
^- imp:spider
|= =bowl:mall
=/ m (thread ,~)
^- form:m
=/ url 'http://eth-mainnet.urbit.org:8545'
=/ key-path /=home/(scot %da now.bowl)/keys/txt
=/ loc-path /=home/(scot %da now.bowl)/locations/txt
=/ vases=(list vase)
%+ select
!, *hoon
(need ship.left)
%^ join
(load:keys key-path)
(load:locations loc-path)
!, *hoon
?& !=(~ ship.left)
=(location.left location.right)
=(%master role.left)
==
=/ ships (turn vases |=(=vase !<(ship vase)))
|- ^- form:m
=* loop $
?~ ships
(pure:m)
;< =point:azimuth bind:m (fetch-point url i.ships)
%- (slog >[i.ships owner.own.point]< ~)
loop(ships t.ships)

159
pkg/arvo/lib/azimuth.hoon Normal file
View File

@ -0,0 +1,159 @@
/+ threadio
=, thread=thread:threadio
=, able:jael
|%
++ tract azimuth:contracts:azimuth
++ fetch-point
|= [url=@ta who=ship]
=/ m (thread ,point:azimuth)
^- form:m
=/ =request:rpc:ethereum
:+ %eth-call
=- [from=~ to=tract gas=~ price=~ value=~ data=-]
(encode-call:rpc:ethereum 'points(uint32)' [%uint `@`who]~)
[%label %latest]
;< jon=json bind:m (request-rpc url `'point' request)
=/ res=cord (so:dejs:format jon)
=/ =point:eth-noun:azimuth
(decode-results:abi:ethereum res point:eth-type:azimuth)
::
=/ =request:rpc:ethereum
:+ %eth-call
=- [from=~ to=tract gas=~ price=~ value=~ data=-]
(encode-call:rpc:ethereum 'rights(uint32)' [%uint `@`who]~)
[%label %latest]
;< jon=json bind:m (request-rpc url `'deed' request)
=/ res=cord (so:dejs:format jon)
=/ =deed:eth-noun:azimuth
(decode-results:abi:ethereum res deed:eth-type:azimuth)
::
(pure:m (point-from-eth:azimuth who point deed))
::
++ request-rpc
|= [url=@ta id=(unit @t) req=request:rpc:ethereum]
=/ m (thread ,json)
^- form:m
%+ (retry json) `10
=/ m (thread ,(unit json))
^- form:m
|^
=/ =request:http
:* method=%'POST'
url=url
header-list=['Content-Type'^'application/json' ~]
^= body
%- some %- as-octt:mimes:html
%- en-json:html
(request-to-json:rpc:ethereum id req)
==
;< ~ bind:m (send-request:threadio request)
;< rep=(unit client-response:iris) bind:m
take-maybe-response:threadio
?~ rep
(pure:m ~)
(parse-response u.rep)
::
++ parse-response
|= =client-response:iris
=/ m (thread ,(unit json))
^- 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 ~)
=, dejs-soft:format
=/ array=(unit (list response:rpc:jstd))
((ar parse-one-response) u.jon)
?~ array
=/ res=(unit response:rpc:jstd) (parse-one-response u.jon)
?~ res
(thread-fail:threadio %request-rpc-parse-error >id< ~)
?: ?=(%error -.u.res)
(thread-fail:threadio %request-rpc-error >id< >+.res< ~)
?. ?=(%result -.u.res)
(thread-fail:threadio %request-rpc-fail >u.res< ~)
(pure:m `res.u.res)
(thread-fail:threadio %request-rpc-batch >%not-implemented< ~)
:: (pure:m `[%batch u.array])
::
++ 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 ~) ~)
--
::
++ retry
|* result=mold
|= [crash-after=(unit @ud) computation=_*form:(thread (unit result))]
=/ m (thread ,result)
=| try=@ud
|^
|- ^- form:m
=* loop $
?: =(crash-after `try)
(thread-fail:threadio %retry-too-many ~)
;< ~ bind:m (backoff try ~m1)
;< res=(unit result) bind:m computation
?^ res
(pure:m u.res)
loop(try +(try))
::
++ backoff
|= [try=@ud limit=@dr]
=/ m (thread ,~)
^- form:m
;< eny=@uvJ bind:m get-entropy:threadio
%- sleep:threadio
%+ min limit
?: =(0 try) ~s0
%+ add
(mul ~s1 (bex (dec try)))
(mul ~s0..0001 (~(rad og eny) 1.000))
--
::
++ get-latest-block
|= url=@ta
=/ m (thread ,block)
^- 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]
=/ m (thread ,block)
^- form:m
|^
;< =json bind:m
(request-rpc url `'block by number' %eth-get-block-by-number number |)
=/ =block (parse-block json)
?. =(number number.id.block)
(thread-fail:threadio %reorg-detected >number< >block< ~)
(pure:m block)
::
++ parse-block
|= =json
^- block
=< [[&1 &2] |2]
^- [@ @ @]
~| json
%. json
=, dejs:format
%- ot
:~ hash+parse-hex-result:rpc:ethereum
number+parse-hex-result:rpc:ethereum
'parentHash'^parse-hex-result:rpc:ethereum
==
--
--

View File

@ -97,4 +97,8 @@
^- (list vase)
(turn (load-forms path) |=(=form !>(form)))
--
::
++ policies !!
++ galaxies !!
++ ships !!
--