2021-08-22 19:23:30 +03:00
|
|
|
|
:: roller/send: send rollup tx
|
2021-05-13 18:21:35 +03:00
|
|
|
|
::
|
2021-08-04 16:53:57 +03:00
|
|
|
|
/- rpc=json-rpc, *dice
|
2021-05-13 18:21:35 +03:00
|
|
|
|
/+ naive, ethereum, ethio, strandio
|
|
|
|
|
::
|
|
|
|
|
::
|
|
|
|
|
|= args=vase
|
2021-05-21 16:07:08 +03:00
|
|
|
|
=+ !<(rpc-send-roll args)
|
2021-05-13 18:21:35 +03:00
|
|
|
|
=/ m (strand:strandio ,vase)
|
|
|
|
|
|^
|
|
|
|
|
^- form:m
|
|
|
|
|
::
|
2021-05-21 13:16:57 +03:00
|
|
|
|
=/ =address:ethereum (address-from-prv:key:ethereum pk)
|
2021-05-13 18:21:35 +03:00
|
|
|
|
;< expected-nonce=@ud bind:m
|
|
|
|
|
(get-next-nonce:ethio endpoint address)
|
2022-03-20 16:07:27 +03:00
|
|
|
|
:: Infura enforces a max calldata size (32, 64, 128 Kb?) so we calculate how
|
|
|
|
|
:: many txs are included in a batch of that size, and only send those
|
|
|
|
|
::
|
|
|
|
|
=/ max-calldata=@ud 128.000
|
|
|
|
|
=/ [n-txs=@ud batch-data=octs]
|
|
|
|
|
=| n-txs=@ud
|
|
|
|
|
=| size=@ud
|
|
|
|
|
=| out=(list octs)
|
|
|
|
|
|- ^- [@ud octs]
|
|
|
|
|
?~ txs
|
|
|
|
|
[n-txs (cad:naive 3 (flop out))]
|
|
|
|
|
=* raw-tx i.txs
|
|
|
|
|
=. size :(add 65 p.raw.raw-tx size)
|
|
|
|
|
?: (gth size max-calldata)
|
|
|
|
|
[n-txs (cad:naive 3 (flop out))]
|
|
|
|
|
%_ $
|
|
|
|
|
n-txs +(n-txs)
|
|
|
|
|
txs t.txs
|
|
|
|
|
out [raw.raw-tx 65^sig.raw-tx out]
|
|
|
|
|
==
|
2021-12-14 17:24:25 +03:00
|
|
|
|
:: if the batch is malformed, emit error to kick it out of sending
|
|
|
|
|
::
|
|
|
|
|
?~ (parse-roll:naive q.batch-data)
|
|
|
|
|
(pure:m !>(%.n^[%not-sent %batch-parse-error]))
|
2021-05-13 18:21:35 +03:00
|
|
|
|
:: if chain expects a different nonce, don't send this transaction
|
|
|
|
|
::
|
|
|
|
|
?. =(nonce expected-nonce)
|
2021-12-10 13:36:45 +03:00
|
|
|
|
~& >>> [%unexpected-nonce nonce expected+expected-nonce]
|
|
|
|
|
%- pure:m
|
|
|
|
|
!> ^- [%.n @tas @t]
|
|
|
|
|
:+ %.n
|
|
|
|
|
%not-sent
|
|
|
|
|
?: (lth expected-nonce nonce)
|
|
|
|
|
:: if ahead, it will use the same next-gas-price when resending
|
|
|
|
|
::
|
|
|
|
|
%ahead-nonce
|
|
|
|
|
:: if behind, start out-of-sync flow
|
|
|
|
|
::
|
|
|
|
|
%behind-nonce
|
2021-05-13 18:21:35 +03:00
|
|
|
|
:: if a gas-price of 0 was specified, fetch the recommended one
|
|
|
|
|
::
|
|
|
|
|
;< use-gas-price=@ud bind:m
|
|
|
|
|
?: =(0 next-gas-price) fetch-gas-price
|
|
|
|
|
(pure:(strand:strandio @ud) next-gas-price)
|
2021-06-14 18:21:57 +03:00
|
|
|
|
::
|
2021-06-15 08:22:55 +03:00
|
|
|
|
:: each l2 signature is 65 bytes + XX bytes for the raw data
|
2021-06-14 18:21:57 +03:00
|
|
|
|
:: from the ethereum yellow paper:
|
2021-06-15 08:22:55 +03:00
|
|
|
|
:: gasLimit = G_transaction + G_txdatanonzero × dataByteLength
|
2021-06-14 18:21:57 +03:00
|
|
|
|
:: where
|
2021-06-15 08:22:55 +03:00
|
|
|
|
:: G_transaction = 21000 gas (base fee)
|
2022-03-20 16:07:27 +03:00
|
|
|
|
:: + G_txdatanonzero = 16 gas (previously 68; see EIP-2028)
|
2021-06-15 08:22:55 +03:00
|
|
|
|
:: * dataByteLength = (65 + raw) * (lent txs) bytes
|
2021-06-14 18:21:57 +03:00
|
|
|
|
::
|
2022-03-20 16:07:27 +03:00
|
|
|
|
:: 1.000 gas are added to the base fee as extra, for emitting the log
|
2021-06-14 18:21:57 +03:00
|
|
|
|
::
|
2022-03-20 16:07:27 +03:00
|
|
|
|
=/ gas-limit=@ud (add 22.000 (mul 16 p.batch-data))
|
|
|
|
|
=/ max-cost=@ud (mul gas-limit use-gas-price)
|
2021-05-29 15:22:33 +03:00
|
|
|
|
;< balance=@ud bind:m
|
|
|
|
|
(get-balance:ethio endpoint address)
|
|
|
|
|
?: (gth max-cost balance)
|
2022-03-20 16:07:27 +03:00
|
|
|
|
:: if we cannot pay for the transaction, don't bother sending it out
|
|
|
|
|
::
|
2021-10-02 09:44:37 +03:00
|
|
|
|
(pure:m !>(%.n^[%not-sent %insufficient-roller-balance]))
|
2021-05-13 18:21:35 +03:00
|
|
|
|
::
|
2021-05-29 15:57:57 +03:00
|
|
|
|
::NOTE this fails the thread if sending fails, which in the app gives us
|
|
|
|
|
:: the "retry with same gas price" behavior we want
|
2022-03-20 16:07:27 +03:00
|
|
|
|
::
|
2021-05-29 15:57:57 +03:00
|
|
|
|
;< =response:rpc bind:m
|
|
|
|
|
%+ send-batch endpoint
|
2021-05-13 18:21:35 +03:00
|
|
|
|
=; tx=transaction:rpc:ethereum
|
|
|
|
|
(sign-transaction:key:ethereum tx pk)
|
|
|
|
|
:* nonce
|
|
|
|
|
use-gas-price
|
2021-06-14 18:21:57 +03:00
|
|
|
|
gas-limit
|
2021-05-13 18:21:35 +03:00
|
|
|
|
contract
|
|
|
|
|
0
|
2021-06-15 08:22:55 +03:00
|
|
|
|
q.batch-data
|
2021-05-13 18:21:35 +03:00
|
|
|
|
chain-id
|
|
|
|
|
==
|
2021-11-07 12:01:15 +03:00
|
|
|
|
:: log batch tx-hash to getTransactionReceipt(tx-hash)
|
|
|
|
|
::
|
|
|
|
|
~? &(?=(%result -.response) ?=(%s -.res.response))
|
2022-03-20 16:07:27 +03:00
|
|
|
|
^- [nonce=@ud batch-hash=@t gas=@ud sent-txs=@ud bytes=@ud]
|
|
|
|
|
:* nonce
|
|
|
|
|
(so:dejs:format res.response)
|
|
|
|
|
use-gas-price
|
|
|
|
|
n-txs
|
|
|
|
|
p.batch-data
|
|
|
|
|
==
|
2021-05-22 09:10:30 +03:00
|
|
|
|
%- pure:m
|
2022-03-20 16:07:27 +03:00
|
|
|
|
!> ^- (each [@ud @ud] [term @t])
|
2021-09-01 12:01:39 +03:00
|
|
|
|
:: TODO: capture if the tx fails (e.g. Runtime Error: revert)
|
2021-11-02 17:08:18 +03:00
|
|
|
|
:: check that tx-hash in +.response is non-zero?
|
2021-09-01 12:01:39 +03:00
|
|
|
|
::
|
2021-10-02 09:44:37 +03:00
|
|
|
|
?+ -.response %.n^[%error 'unexpected rpc response']
|
|
|
|
|
%error %.n^[%error message.response]
|
2021-05-22 09:10:30 +03:00
|
|
|
|
:: add five gwei to gas price of next attempt
|
|
|
|
|
::
|
2022-03-20 16:07:27 +03:00
|
|
|
|
%result %.y^[n-txs (add use-gas-price 5.000.000.000)]
|
2021-05-22 09:10:30 +03:00
|
|
|
|
==
|
2021-05-13 18:21:35 +03:00
|
|
|
|
::
|
|
|
|
|
::TODO should be distilled further, partially added to strandio?
|
|
|
|
|
++ fetch-gas-price
|
|
|
|
|
=/ m (strand:strandio @ud) ::NOTE return in wei
|
|
|
|
|
^- form:m
|
|
|
|
|
=/ =request:http
|
|
|
|
|
:* method=%'GET'
|
|
|
|
|
url='https://api.etherscan.io/api?module=gastracker&action=gasoracle'
|
|
|
|
|
header-list=~
|
|
|
|
|
~
|
|
|
|
|
==
|
|
|
|
|
;< ~ bind:m
|
|
|
|
|
(send-request:strandio request)
|
|
|
|
|
;< rep=(unit client-response:iris) bind:m
|
|
|
|
|
take-maybe-response:strandio
|
|
|
|
|
=* fallback
|
2021-11-28 18:37:06 +03:00
|
|
|
|
~& >> %fallback-gas-price
|
2021-12-10 13:36:45 +03:00
|
|
|
|
(pure:m fallback-gas-price)
|
2021-05-13 18:21:35 +03:00
|
|
|
|
?. ?& ?=([~ %finished *] rep)
|
|
|
|
|
?=(^ full-file.u.rep)
|
2021-12-14 17:44:43 +03:00
|
|
|
|
:: get suggested price only for mainnet txs
|
|
|
|
|
::
|
|
|
|
|
=(chain-id 1)
|
2021-05-13 18:21:35 +03:00
|
|
|
|
==
|
|
|
|
|
fallback
|
|
|
|
|
?~ jon=(de-json:html q.data.u.full-file.u.rep)
|
|
|
|
|
fallback
|
|
|
|
|
=; res=(unit @ud)
|
|
|
|
|
?~ res fallback
|
|
|
|
|
%- pure:m
|
|
|
|
|
(mul 1.000.000.000 u.res) ::NOTE gwei to wei
|
|
|
|
|
%. u.jon
|
|
|
|
|
=, dejs-soft:format
|
2021-12-13 21:16:47 +03:00
|
|
|
|
(ot 'result'^(ot 'FastGasPrice'^(su dem) ~) ~)
|
2021-05-22 09:10:30 +03:00
|
|
|
|
::
|
|
|
|
|
++ send-batch
|
|
|
|
|
|= [endpoint=@ta batch=@ux]
|
|
|
|
|
=/ m (strand:strandio ,response:rpc)
|
|
|
|
|
^- form:m
|
|
|
|
|
=/ req=[(unit @t) request:rpc:ethereum]
|
|
|
|
|
[`'sendRawTransaction' %eth-send-raw-transaction batch]
|
|
|
|
|
;< res=(list response:rpc) bind:m
|
|
|
|
|
(request-batch-rpc-loose:ethio endpoint [req]~)
|
|
|
|
|
?: ?=([* ~] res)
|
|
|
|
|
(pure:m i.res)
|
|
|
|
|
%+ strand-fail:strandio
|
|
|
|
|
%unexpected-multiple-results
|
|
|
|
|
[>(lent res)< ~]
|
|
|
|
|
::
|
2021-05-13 18:21:35 +03:00
|
|
|
|
--
|