mirror of
https://github.com/urbit/shrub.git
synced 2024-12-19 16:51:42 +03:00
working thread example
This commit is contained in:
parent
15d598f080
commit
7af04d45e7
@ -4,6 +4,6 @@
|
||||
::
|
||||
:- %say
|
||||
|= $: [now=@da eny=@uvJ =beak]
|
||||
[[act=action ~] ~]
|
||||
[[act=action ~] ~]
|
||||
==
|
||||
[%btc-wallet-action act]
|
||||
|
93
ted/btc-rpc.hoon
Normal file
93
ted/btc-rpc.hoon
Normal file
@ -0,0 +1,93 @@
|
||||
/- spider, rpc=json-rpc
|
||||
/+ strandio, btc
|
||||
=, strand=strand:spider
|
||||
=>
|
||||
|%
|
||||
++ url1 "http://localhost:50002"
|
||||
++ addr ^-(address:btc [%bech32 'bc1q39wus23jwe7m2j7xmrfr2svhrtejmsn262x3j2'])
|
||||
++ btc-req
|
||||
^- request:http
|
||||
=, enjs:format
|
||||
:* method=%'POST'
|
||||
url=`@ta`(crip (weld url1 "/btc-rpc"))
|
||||
header-list=['Content-Type'^'application/json' ~]
|
||||
^= body
|
||||
%- some
|
||||
%- as-octt:mimes:html
|
||||
%- en-json:html
|
||||
%- pairs
|
||||
:~ jsonrpc+s+'2.0'
|
||||
id+s+'block-info'
|
||||
method+s+'getblockchaininfo'
|
||||
==
|
||||
==
|
||||
++ electrs-req
|
||||
^- request:http
|
||||
=, enjs:format
|
||||
:* method=%'POST'
|
||||
url=`@ta`(crip (weld url1 "/electrs-rpc"))
|
||||
header-list=['Content-Type'^'application/json' ~]
|
||||
^= body
|
||||
%- some
|
||||
%- as-octt:mimes:html
|
||||
%- en-json:html
|
||||
%- pairs
|
||||
:~ jsonrpc+s+'2.0'
|
||||
id+s+'list-unspent'
|
||||
method+s+'blockchain.scripthash.listunspent'
|
||||
params+a+~[[%s '34aae877286aa09828803af27ce2315e72c4888efdf74d7d067c975b7c558789']]
|
||||
==
|
||||
==
|
||||
::
|
||||
:: convert address to Electrs ScriptHash that it uses to index
|
||||
:: big-endian sha256 of the output script
|
||||
::
|
||||
++ electrs-script-hash
|
||||
|= a=address:btc
|
||||
^- hexb:btc
|
||||
%- flip:byt:btc
|
||||
%- sha256:btc
|
||||
(script-pubkey:btc a)
|
||||
::
|
||||
++ parse-json-rpc
|
||||
|= =json
|
||||
^- (unit response:rpc)
|
||||
=/ 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 ~) ~)
|
||||
::
|
||||
++ parse-response
|
||||
|= =client-response:iris
|
||||
=/ m (strand:strandio ,(unit response:rpc))
|
||||
^- 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 ~)
|
||||
(pure:m (parse-json-rpc u.jon))
|
||||
::
|
||||
++ attempt-request
|
||||
|= =request:http
|
||||
=/ m (strand:strandio ,~)
|
||||
^- form:m
|
||||
(send-request:strandio request)
|
||||
--
|
||||
^- thread:spider
|
||||
|= arg=vase
|
||||
=+ !<([~ a=@ud] arg)
|
||||
=/ m (strand ,vase)
|
||||
^- form:m
|
||||
;< ~ bind:m (attempt-request electrs-req)
|
||||
;< rep=client-response:iris bind:m
|
||||
take-client-response:strandio
|
||||
;< rpc-resp=(unit response:rpc) bind:m (parse-response rep)
|
||||
(pure:m !>(rpc-resp))
|
Loading…
Reference in New Issue
Block a user