urbit/app/constitution.hoon

194 lines
4.3 KiB
Plaintext
Raw Normal View History

2018-03-23 22:55:30 +03:00
/- constitution, ethereum, json-rpc
/+ ethereum
::TODO =, ethereum / constitution causes bail fail. find minimal repro
=> [. constitution ethereum]
=, eyre
|%
++ state
2018-04-09 17:59:14 +03:00
$: ships=(map @p hull)
block=@ud :: last heard
filter=(unit @ud) :: our filter id
2018-03-23 22:55:30 +03:00
==
::
2018-04-09 17:59:14 +03:00
++ complete-ship
$: state=hull
history=(list diff-hull) :: newest first
keys=(map @ud (pair @ @))
==
::
+= move [bone card] :: [target side-effect]
2018-03-23 22:55:30 +03:00
++ card :: side-effect
$% [%peer wire gill:gall path]
[%hiss wire (unit user:eyre) mark [%hiss hiss]]
==
--
::
|_ {bol=bowl:gall state}
::
++ prep
|= old=(unit *)
:: ?~ old
2018-04-09 17:59:14 +03:00
ta-save:ta-init:ta
2018-03-23 22:55:30 +03:00
:: [~ ..prep(fid u.old)]
::
2018-04-09 17:59:14 +03:00
++ ta
|_ $: moves=(list move) :: side-effects
reqs=(list (pair (unit @t) request)) :: rpc requests
wir=wire :: wire for reqs
==
+* this .
::
++ ta-save
^- (quip move _+>)
=- [[`move`- `(list move)`(flop moves)] ..ta]
^- move
:- `bone`ost.bol
^- card
%+ rpc-request:ca wir
`json`a+(turn (flop reqs) request-to-json)
::
++ ta-move
|= mov=move
%_(+> moves [mov moves])
::
++ ta-card
|= car=card
(ta-move [ost.bol car])
::
++ ta-request
|= [id=(unit @t) req=request]
%_(+> reqs [[id req] reqs])
::
++ ta-read
|= cal=ships:function
=- %+ ta-request `id
:+ %eth-call
[~ ships:contracts ~ ~ ~ (encode-call dat)]
[%label %latest]
::TODO probably turn the below into a lib arm
^- [id=@t dat=call-data]
?- -.cal
%ships
:- (crip "ships({(scow %p who.cal)})")
['ships(uint32)' ~[uint+`@`who.cal]]
==
::
++ ta-read-ships
|= who=(list @p)
?~ who this
$(who t.who, this (ta-read %ships i.who))
2018-04-09 17:59:14 +03:00
::
::
++ ta-init
%- ta-read-ships(wir /init)
(gulf ~zod ~nec) ::TODO ~fes)
2018-04-09 17:59:14 +03:00
::
++ ta-init-results
2018-04-09 17:59:14 +03:00
|= rep=response:json-rpc
^+ this
?> ?=(%batch -.rep)
=. wir /init
|- ^+ +>.^$
?~ bas.rep +>.^$
=. +>.^$ (ta-init-result i.bas.rep)
$(bas.rep t.bas.rep)
::
++ ta-init-result
|= rep=response:json-rpc
?< ?=(%batch -.rep)
~& id.rep
?: ?=(%error -.rep)
~& [%rpc-error message.rep]
::TODO retry or something
+>.$
?> ?=(%s -.res.rep)
2018-04-09 17:59:14 +03:00
=/ hul=hull:eth-noun
(decode-results p.res.rep hull:eth-type)
?. active.hul +>.$
2018-04-09 17:59:14 +03:00
=/ who=@p
%+ rash id.rep
2018-04-09 17:59:14 +03:00
(ifix [(jest 'ships(~') (just ')')] fed:ag)
=. ships
%+ ~(put by ships) who
(hull-from-eth hul)
(ta-read-ships (kids who))
--
::
:: arms for card generation
++ ca
|%
++ rpc-request
|= [w=wire j=json]
^- card
:^ %hiss w ~
:+ %json-rpc-response %hiss
=- (json-request - j)
=+ (need (de-purl:html 'http://localhost:8545'))
-(p.p |)
--
2018-03-23 22:55:30 +03:00
::
2018-04-09 17:59:14 +03:00
++ kids
|= pre=@p
^- (list @p)
=/ wyd=bloq
?+ (clan:title pre) 0
%czar 3
%king 4
%duke 5
==
%+ turn
(gulf 1 (dec (pow 2 (bex wyd))))
?: =(~zod pre)
|=(a=@p (lsh 3 1 a))
|=(a=@p (cat wyd pre a))
2018-03-23 22:55:30 +03:00
::
::TODO there definitely needs to be a helper function of some kind,
:: but is there a way for the type system to be aware of the return
:: type if we ask for ie ~[%address %uint %bool] data as a noun?
++ hull-from-eth
|= hul=hull:eth-noun
2018-03-23 22:55:30 +03:00
^- hull
=, hul
:* owner
spawn-count
2018-03-23 22:55:30 +03:00
::
?> =(32 p.encryption-key)
`@`q.encryption-key
2018-03-23 22:55:30 +03:00
::
?> =(32 p.authentication-key)
`@`q.authentication-key
2018-03-23 22:55:30 +03:00
::
key-revision
`@p`sponsor
2018-03-23 22:55:30 +03:00
::
?. escape-requested ~
``@p`escape-to
2018-03-23 22:55:30 +03:00
::
spawn-proxy
transfer-proxy
2018-03-23 22:55:30 +03:00
==
::
++ poke-noun
|= a/@
2018-04-09 17:59:14 +03:00
?: =(a 0)
~& [%have-ships ~(key by ships)]
[~ +>.$]
2018-03-23 22:55:30 +03:00
[~ +>.$]
::
++ sigh-tang
|= [w=wire t=tang]
~& [%failed-sigh]
~& (turn t (cury wash [0 80]))
[~ +>.$]
::
2018-04-09 17:59:14 +03:00
++ sigh-json-rpc-response-init
|= [w=wire r=response:json-rpc]
~& %got-init-response
ta-save:(ta-init-results:ta r)
2018-04-09 17:59:14 +03:00
::
2018-03-23 22:55:30 +03:00
++ sigh-json-rpc-response
|= [w=wire r=response:json-rpc]
~& [%rpc-resp w r]
[~ +>.$]
--