ethclient.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package ethclient provides a client for the Ethereum RPC API.
  17. package ethclient
  18. import (
  19. "context"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "math/big"
  24. "github.com/ethereum/go-ethereum"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/common/hexutil"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/rpc"
  29. )
  30. // Client defines typed wrappers for the Ethereum RPC API.
  31. type Client struct {
  32. c *rpc.Client
  33. }
  34. // Dial connects a client to the given URL.
  35. func Dial(rawurl string) (*Client, error) {
  36. return DialContext(context.Background(), rawurl)
  37. }
  38. func DialContext(ctx context.Context, rawurl string) (*Client, error) {
  39. c, err := rpc.DialContext(ctx, rawurl)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return NewClient(c), nil
  44. }
  45. // NewClient creates a client that uses the given RPC client.
  46. func NewClient(c *rpc.Client) *Client {
  47. return &Client{c}
  48. }
  49. func (ec *Client) Close() {
  50. ec.c.Close()
  51. }
  52. // Blockchain Access
  53. // ChainID retrieves the current chain ID for transaction replay protection.
  54. func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) {
  55. var result hexutil.Big
  56. err := ec.c.CallContext(ctx, &result, "eth_chainId")
  57. if err != nil {
  58. return nil, err
  59. }
  60. return (*big.Int)(&result), err
  61. }
  62. // BlockByHash returns the given full block.
  63. //
  64. // Note that loading full blocks requires two requests. Use HeaderByHash
  65. // if you don't need all transactions or uncle headers.
  66. func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  67. return ec.getBlock(ctx, "eth_getBlockByHash", hash, true)
  68. }
  69. // BlockByNumber returns a block from the current canonical chain. If number is nil, the
  70. // latest known block is returned.
  71. //
  72. // Note that loading full blocks requires two requests. Use HeaderByNumber
  73. // if you don't need all transactions or uncle headers.
  74. func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
  75. return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
  76. }
  77. // BlockNumber returns the most recent block number
  78. func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) {
  79. var result hexutil.Uint64
  80. err := ec.c.CallContext(ctx, &result, "eth_blockNumber")
  81. return uint64(result), err
  82. }
  83. // PeerCount returns the number of p2p peers as reported by the net_peerCount method.
  84. func (ec *Client) PeerCount(ctx context.Context) (uint64, error) {
  85. var result hexutil.Uint64
  86. err := ec.c.CallContext(ctx, &result, "net_peerCount")
  87. return uint64(result), err
  88. }
  89. type rpcBlock struct {
  90. Hash common.Hash `json:"hash"`
  91. Transactions []rpcTransaction `json:"transactions"`
  92. UncleHashes []common.Hash `json:"uncles"`
  93. }
  94. func (ec *Client) getBlock(ctx context.Context, method string, args ...interface{}) (*types.Block, error) {
  95. var raw json.RawMessage
  96. err := ec.c.CallContext(ctx, &raw, method, args...)
  97. if err != nil {
  98. return nil, err
  99. } else if len(raw) == 0 {
  100. return nil, ethereum.NotFound
  101. }
  102. // Decode header and transactions.
  103. var head *types.Header
  104. var body rpcBlock
  105. if err := json.Unmarshal(raw, &head); err != nil {
  106. return nil, err
  107. }
  108. if err := json.Unmarshal(raw, &body); err != nil {
  109. return nil, err
  110. }
  111. // Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
  112. if head.UncleHash == types.EmptyUncleHash && len(body.UncleHashes) > 0 {
  113. return nil, fmt.Errorf("server returned non-empty uncle list but block header indicates no uncles")
  114. }
  115. if head.UncleHash != types.EmptyUncleHash && len(body.UncleHashes) == 0 {
  116. return nil, fmt.Errorf("server returned empty uncle list but block header indicates uncles")
  117. }
  118. if head.TxHash == types.EmptyRootHash && len(body.Transactions) > 0 {
  119. return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
  120. }
  121. if head.TxHash != types.EmptyRootHash && len(body.Transactions) == 0 {
  122. return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
  123. }
  124. // Load uncles because they are not included in the block response.
  125. var uncles []*types.Header
  126. if len(body.UncleHashes) > 0 {
  127. uncles = make([]*types.Header, len(body.UncleHashes))
  128. reqs := make([]rpc.BatchElem, len(body.UncleHashes))
  129. for i := range reqs {
  130. reqs[i] = rpc.BatchElem{
  131. Method: "eth_getUncleByBlockHashAndIndex",
  132. Args: []interface{}{body.Hash, hexutil.EncodeUint64(uint64(i))},
  133. Result: &uncles[i],
  134. }
  135. }
  136. if err := ec.c.BatchCallContext(ctx, reqs); err != nil {
  137. return nil, err
  138. }
  139. for i := range reqs {
  140. if reqs[i].Error != nil {
  141. return nil, reqs[i].Error
  142. }
  143. if uncles[i] == nil {
  144. return nil, fmt.Errorf("got null header for uncle %d of block %x", i, body.Hash[:])
  145. }
  146. }
  147. }
  148. // Fill the sender cache of transactions in the block.
  149. txs := make([]*types.Transaction, len(body.Transactions))
  150. for i, tx := range body.Transactions {
  151. if tx.From != nil {
  152. setSenderFromServer(tx.tx, *tx.From, body.Hash)
  153. }
  154. txs[i] = tx.tx
  155. }
  156. return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil
  157. }
  158. // HeaderByHash returns the block header with the given hash.
  159. func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  160. var head *types.Header
  161. err := ec.c.CallContext(ctx, &head, "eth_getBlockByHash", hash, false)
  162. if err == nil && head == nil {
  163. err = ethereum.NotFound
  164. }
  165. return head, err
  166. }
  167. // HeaderByNumber returns a block header from the current canonical chain. If number is
  168. // nil, the latest known header is returned.
  169. func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  170. var head *types.Header
  171. err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)
  172. if err == nil && head == nil {
  173. err = ethereum.NotFound
  174. }
  175. return head, err
  176. }
  177. type rpcTransaction struct {
  178. tx *types.Transaction
  179. txExtraInfo
  180. }
  181. type txExtraInfo struct {
  182. BlockNumber *string `json:"blockNumber,omitempty"`
  183. BlockHash *common.Hash `json:"blockHash,omitempty"`
  184. From *common.Address `json:"from,omitempty"`
  185. }
  186. func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
  187. if err := json.Unmarshal(msg, &tx.tx); err != nil {
  188. return err
  189. }
  190. return json.Unmarshal(msg, &tx.txExtraInfo)
  191. }
  192. // TransactionByHash returns the transaction with the given hash.
  193. func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
  194. var json *rpcTransaction
  195. err = ec.c.CallContext(ctx, &json, "eth_getTransactionByHash", hash)
  196. if err != nil {
  197. return nil, false, err
  198. } else if json == nil {
  199. return nil, false, ethereum.NotFound
  200. } else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
  201. return nil, false, fmt.Errorf("server returned transaction without signature")
  202. }
  203. if json.From != nil && json.BlockHash != nil {
  204. setSenderFromServer(json.tx, *json.From, *json.BlockHash)
  205. }
  206. return json.tx, json.BlockNumber == nil, nil
  207. }
  208. // TransactionSender returns the sender address of the given transaction. The transaction
  209. // must be known to the remote node and included in the blockchain at the given block and
  210. // index. The sender is the one derived by the protocol at the time of inclusion.
  211. //
  212. // There is a fast-path for transactions retrieved by TransactionByHash and
  213. // TransactionInBlock. Getting their sender address can be done without an RPC interaction.
  214. func (ec *Client) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error) {
  215. // Try to load the address from the cache.
  216. sender, err := types.Sender(&senderFromServer{blockhash: block}, tx)
  217. if err == nil {
  218. return sender, nil
  219. }
  220. // It was not found in cache, ask the server.
  221. var meta struct {
  222. Hash common.Hash
  223. From common.Address
  224. }
  225. if err = ec.c.CallContext(ctx, &meta, "eth_getTransactionByBlockHashAndIndex", block, hexutil.Uint64(index)); err != nil {
  226. return common.Address{}, err
  227. }
  228. if meta.Hash == (common.Hash{}) || meta.Hash != tx.Hash() {
  229. return common.Address{}, errors.New("wrong inclusion block/index")
  230. }
  231. return meta.From, nil
  232. }
  233. // TransactionCount returns the total number of transactions in the given block.
  234. func (ec *Client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
  235. var num hexutil.Uint
  236. err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByHash", blockHash)
  237. return uint(num), err
  238. }
  239. // TransactionInBlock returns a single transaction at index in the given block.
  240. func (ec *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) {
  241. var json *rpcTransaction
  242. err := ec.c.CallContext(ctx, &json, "eth_getTransactionByBlockHashAndIndex", blockHash, hexutil.Uint64(index))
  243. if err != nil {
  244. return nil, err
  245. }
  246. if json == nil {
  247. return nil, ethereum.NotFound
  248. } else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
  249. return nil, fmt.Errorf("server returned transaction without signature")
  250. }
  251. if json.From != nil && json.BlockHash != nil {
  252. setSenderFromServer(json.tx, *json.From, *json.BlockHash)
  253. }
  254. return json.tx, err
  255. }
  256. // TransactionReceipt returns the receipt of a transaction by transaction hash.
  257. // Note that the receipt is not available for pending transactions.
  258. func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
  259. var r *types.Receipt
  260. err := ec.c.CallContext(ctx, &r, "eth_getTransactionReceipt", txHash)
  261. if err == nil {
  262. if r == nil {
  263. return nil, ethereum.NotFound
  264. }
  265. }
  266. return r, err
  267. }
  268. // SyncProgress retrieves the current progress of the sync algorithm. If there's
  269. // no sync currently running, it returns nil.
  270. func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
  271. var raw json.RawMessage
  272. if err := ec.c.CallContext(ctx, &raw, "eth_syncing"); err != nil {
  273. return nil, err
  274. }
  275. // Handle the possible response types
  276. var syncing bool
  277. if err := json.Unmarshal(raw, &syncing); err == nil {
  278. return nil, nil // Not syncing (always false)
  279. }
  280. var p *rpcProgress
  281. if err := json.Unmarshal(raw, &p); err != nil {
  282. return nil, err
  283. }
  284. return p.toSyncProgress(), nil
  285. }
  286. // SubscribeNewHead subscribes to notifications about the current blockchain head
  287. // on the given channel.
  288. func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
  289. return ec.c.EthSubscribe(ctx, ch, "newHeads")
  290. }
  291. // State Access
  292. // NetworkID returns the network ID (also known as the chain ID) for this chain.
  293. func (ec *Client) NetworkID(ctx context.Context) (*big.Int, error) {
  294. version := new(big.Int)
  295. var ver string
  296. if err := ec.c.CallContext(ctx, &ver, "net_version"); err != nil {
  297. return nil, err
  298. }
  299. if _, ok := version.SetString(ver, 10); !ok {
  300. return nil, fmt.Errorf("invalid net_version result %q", ver)
  301. }
  302. return version, nil
  303. }
  304. // BalanceAt returns the wei balance of the given account.
  305. // The block number can be nil, in which case the balance is taken from the latest known block.
  306. func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
  307. var result hexutil.Big
  308. err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, toBlockNumArg(blockNumber))
  309. return (*big.Int)(&result), err
  310. }
  311. // StorageAt returns the value of key in the contract storage of the given account.
  312. // The block number can be nil, in which case the value is taken from the latest known block.
  313. func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
  314. var result hexutil.Bytes
  315. err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, toBlockNumArg(blockNumber))
  316. return result, err
  317. }
  318. // CodeAt returns the contract code of the given account.
  319. // The block number can be nil, in which case the code is taken from the latest known block.
  320. func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
  321. var result hexutil.Bytes
  322. err := ec.c.CallContext(ctx, &result, "eth_getCode", account, toBlockNumArg(blockNumber))
  323. return result, err
  324. }
  325. // NonceAt returns the account nonce of the given account.
  326. // The block number can be nil, in which case the nonce is taken from the latest known block.
  327. func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
  328. var result hexutil.Uint64
  329. err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, toBlockNumArg(blockNumber))
  330. return uint64(result), err
  331. }
  332. // Filters
  333. // FilterLogs executes a filter query.
  334. func (ec *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
  335. var result []types.Log
  336. arg, err := toFilterArg(q)
  337. if err != nil {
  338. return nil, err
  339. }
  340. err = ec.c.CallContext(ctx, &result, "eth_getLogs", arg)
  341. return result, err
  342. }
  343. // SubscribeFilterLogs subscribes to the results of a streaming filter query.
  344. func (ec *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
  345. arg, err := toFilterArg(q)
  346. if err != nil {
  347. return nil, err
  348. }
  349. return ec.c.EthSubscribe(ctx, ch, "logs", arg)
  350. }
  351. func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {
  352. arg := map[string]interface{}{
  353. "address": q.Addresses,
  354. "topics": q.Topics,
  355. }
  356. if q.BlockHash != nil {
  357. arg["blockHash"] = *q.BlockHash
  358. if q.FromBlock != nil || q.ToBlock != nil {
  359. return nil, fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
  360. }
  361. } else {
  362. if q.FromBlock == nil {
  363. arg["fromBlock"] = "0x0"
  364. } else {
  365. arg["fromBlock"] = toBlockNumArg(q.FromBlock)
  366. }
  367. arg["toBlock"] = toBlockNumArg(q.ToBlock)
  368. }
  369. return arg, nil
  370. }
  371. // Pending State
  372. // PendingBalanceAt returns the wei balance of the given account in the pending state.
  373. func (ec *Client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
  374. var result hexutil.Big
  375. err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, "pending")
  376. return (*big.Int)(&result), err
  377. }
  378. // PendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
  379. func (ec *Client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) {
  380. var result hexutil.Bytes
  381. err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, "pending")
  382. return result, err
  383. }
  384. // PendingCodeAt returns the contract code of the given account in the pending state.
  385. func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
  386. var result hexutil.Bytes
  387. err := ec.c.CallContext(ctx, &result, "eth_getCode", account, "pending")
  388. return result, err
  389. }
  390. // PendingNonceAt returns the account nonce of the given account in the pending state.
  391. // This is the nonce that should be used for the next transaction.
  392. func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
  393. var result hexutil.Uint64
  394. err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending")
  395. return uint64(result), err
  396. }
  397. // PendingTransactionCount returns the total number of transactions in the pending state.
  398. func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
  399. var num hexutil.Uint
  400. err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByNumber", "pending")
  401. return uint(num), err
  402. }
  403. // Contract Calling
  404. // CallContract executes a message call transaction, which is directly executed in the VM
  405. // of the node, but never mined into the blockchain.
  406. //
  407. // blockNumber selects the block height at which the call runs. It can be nil, in which
  408. // case the code is taken from the latest known block. Note that state from very old
  409. // blocks might not be available.
  410. func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
  411. var hex hexutil.Bytes
  412. err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), toBlockNumArg(blockNumber))
  413. if err != nil {
  414. return nil, err
  415. }
  416. return hex, nil
  417. }
  418. // CallContractAtHash is almost the same as CallContract except that it selects
  419. // the block by block hash instead of block height.
  420. func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error) {
  421. var hex hexutil.Bytes
  422. err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), rpc.BlockNumberOrHashWithHash(blockHash, false))
  423. if err != nil {
  424. return nil, err
  425. }
  426. return hex, nil
  427. }
  428. // PendingCallContract executes a message call transaction using the EVM.
  429. // The state seen by the contract call is the pending state.
  430. func (ec *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
  431. var hex hexutil.Bytes
  432. err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), "pending")
  433. if err != nil {
  434. return nil, err
  435. }
  436. return hex, nil
  437. }
  438. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  439. // execution of a transaction.
  440. func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
  441. var hex hexutil.Big
  442. if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
  443. return nil, err
  444. }
  445. return (*big.Int)(&hex), nil
  446. }
  447. // SuggestGasTipCap retrieves the currently suggested gas tip cap after 1559 to
  448. // allow a timely execution of a transaction.
  449. func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
  450. var hex hexutil.Big
  451. if err := ec.c.CallContext(ctx, &hex, "eth_maxPriorityFeePerGas"); err != nil {
  452. return nil, err
  453. }
  454. return (*big.Int)(&hex), nil
  455. }
  456. type feeHistoryResultMarshaling struct {
  457. OldestBlock *hexutil.Big `json:"oldestBlock"`
  458. Reward [][]*hexutil.Big `json:"reward,omitempty"`
  459. BaseFee []*hexutil.Big `json:"baseFeePerGas,omitempty"`
  460. GasUsedRatio []float64 `json:"gasUsedRatio"`
  461. }
  462. // FeeHistory retrieves the fee market history.
  463. func (ec *Client) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
  464. var res feeHistoryResultMarshaling
  465. if err := ec.c.CallContext(ctx, &res, "eth_feeHistory", hexutil.Uint(blockCount), toBlockNumArg(lastBlock), rewardPercentiles); err != nil {
  466. return nil, err
  467. }
  468. reward := make([][]*big.Int, len(res.Reward))
  469. for i, r := range res.Reward {
  470. reward[i] = make([]*big.Int, len(r))
  471. for j, r := range r {
  472. reward[i][j] = (*big.Int)(r)
  473. }
  474. }
  475. baseFee := make([]*big.Int, len(res.BaseFee))
  476. for i, b := range res.BaseFee {
  477. baseFee[i] = (*big.Int)(b)
  478. }
  479. return &ethereum.FeeHistory{
  480. OldestBlock: (*big.Int)(res.OldestBlock),
  481. Reward: reward,
  482. BaseFee: baseFee,
  483. GasUsedRatio: res.GasUsedRatio,
  484. }, nil
  485. }
  486. // EstimateGas tries to estimate the gas needed to execute a specific transaction based on
  487. // the current pending state of the backend blockchain. There is no guarantee that this is
  488. // the true gas limit requirement as other transactions may be added or removed by miners,
  489. // but it should provide a basis for setting a reasonable default.
  490. func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
  491. var hex hexutil.Uint64
  492. err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
  493. if err != nil {
  494. return 0, err
  495. }
  496. return uint64(hex), nil
  497. }
  498. // SendTransaction injects a signed transaction into the pending pool for execution.
  499. //
  500. // If the transaction was a contract creation use the TransactionReceipt method to get the
  501. // contract address after the transaction has been mined.
  502. func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) error {
  503. data, err := tx.MarshalBinary()
  504. if err != nil {
  505. return err
  506. }
  507. return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
  508. }
  509. func toBlockNumArg(number *big.Int) string {
  510. if number == nil {
  511. return "latest"
  512. }
  513. pending := big.NewInt(-1)
  514. if number.Cmp(pending) == 0 {
  515. return "pending"
  516. }
  517. return hexutil.EncodeBig(number)
  518. }
  519. func toCallArg(msg ethereum.CallMsg) interface{} {
  520. arg := map[string]interface{}{
  521. "from": msg.From,
  522. "to": msg.To,
  523. }
  524. if len(msg.Data) > 0 {
  525. arg["data"] = hexutil.Bytes(msg.Data)
  526. }
  527. if msg.Value != nil {
  528. arg["value"] = (*hexutil.Big)(msg.Value)
  529. }
  530. if msg.Gas != 0 {
  531. arg["gas"] = hexutil.Uint64(msg.Gas)
  532. }
  533. if msg.GasPrice != nil {
  534. arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
  535. }
  536. return arg
  537. }
  538. // rpcProgress is a copy of SyncProgress with hex-encoded fields.
  539. type rpcProgress struct {
  540. StartingBlock hexutil.Uint64
  541. CurrentBlock hexutil.Uint64
  542. HighestBlock hexutil.Uint64
  543. PulledStates hexutil.Uint64
  544. KnownStates hexutil.Uint64
  545. SyncedAccounts hexutil.Uint64
  546. SyncedAccountBytes hexutil.Uint64
  547. SyncedBytecodes hexutil.Uint64
  548. SyncedBytecodeBytes hexutil.Uint64
  549. SyncedStorage hexutil.Uint64
  550. SyncedStorageBytes hexutil.Uint64
  551. HealedTrienodes hexutil.Uint64
  552. HealedTrienodeBytes hexutil.Uint64
  553. HealedBytecodes hexutil.Uint64
  554. HealedBytecodeBytes hexutil.Uint64
  555. HealingTrienodes hexutil.Uint64
  556. HealingBytecode hexutil.Uint64
  557. }
  558. func (p *rpcProgress) toSyncProgress() *ethereum.SyncProgress {
  559. if p == nil {
  560. return nil
  561. }
  562. return &ethereum.SyncProgress{
  563. StartingBlock: uint64(p.StartingBlock),
  564. CurrentBlock: uint64(p.CurrentBlock),
  565. HighestBlock: uint64(p.HighestBlock),
  566. PulledStates: uint64(p.PulledStates),
  567. KnownStates: uint64(p.KnownStates),
  568. SyncedAccounts: uint64(p.SyncedAccounts),
  569. SyncedAccountBytes: uint64(p.SyncedAccountBytes),
  570. SyncedBytecodes: uint64(p.SyncedBytecodes),
  571. SyncedBytecodeBytes: uint64(p.SyncedBytecodeBytes),
  572. SyncedStorage: uint64(p.SyncedStorage),
  573. SyncedStorageBytes: uint64(p.SyncedStorageBytes),
  574. HealedTrienodes: uint64(p.HealedTrienodes),
  575. HealedTrienodeBytes: uint64(p.HealedTrienodeBytes),
  576. HealedBytecodes: uint64(p.HealedBytecodes),
  577. HealedBytecodeBytes: uint64(p.HealedBytecodeBytes),
  578. HealingTrienodes: uint64(p.HealingTrienodes),
  579. HealingBytecode: uint64(p.HealingBytecode),
  580. }
  581. }