ethclient.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // Contains a wrapper for the Ethereum client.
  17. package geth
  18. import (
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/core/vm"
  22. "github.com/ethereum/go-ethereum/ethclient"
  23. )
  24. // EthereumClient provides access to the Ethereum APIs.
  25. type EthereumClient struct {
  26. client *ethclient.Client
  27. }
  28. // NewEthereumClient connects a client to the given URL.
  29. func NewEthereumClient(rawurl string) (*EthereumClient, error) {
  30. client, err := ethclient.Dial(rawurl)
  31. return &EthereumClient{client}, err
  32. }
  33. // GetBlockByHash returns the given full block.
  34. func (ec *EthereumClient) GetBlockByHash(ctx *Context, hash *Hash) (*Block, error) {
  35. block, err := ec.client.BlockByHash(ctx.context, hash.hash)
  36. return &Block{block}, err
  37. }
  38. // GetBlockByNumber returns a block from the current canonical chain. If number is <0, the
  39. // latest known block is returned.
  40. func (ec *EthereumClient) GetBlockByNumber(ctx *Context, number int64) (*Block, error) {
  41. if number < 0 {
  42. block, err := ec.client.BlockByNumber(ctx.context, nil)
  43. return &Block{block}, err
  44. }
  45. block, err := ec.client.BlockByNumber(ctx.context, big.NewInt(number))
  46. return &Block{block}, err
  47. }
  48. // GetHeaderByHash returns the block header with the given hash.
  49. func (ec *EthereumClient) GetHeaderByHash(ctx *Context, hash *Hash) (*Header, error) {
  50. header, err := ec.client.HeaderByHash(ctx.context, hash.hash)
  51. return &Header{header}, err
  52. }
  53. // GetHeaderByNumber returns a block header from the current canonical chain. If number is <0,
  54. // the latest known header is returned.
  55. func (ec *EthereumClient) GetHeaderByNumber(ctx *Context, number int64) (*Header, error) {
  56. if number < 0 {
  57. header, err := ec.client.HeaderByNumber(ctx.context, nil)
  58. return &Header{header}, err
  59. }
  60. header, err := ec.client.HeaderByNumber(ctx.context, big.NewInt(number))
  61. return &Header{header}, err
  62. }
  63. // GetTransactionByHash returns the transaction with the given hash.
  64. func (ec *EthereumClient) GetTransactionByHash(ctx *Context, hash *Hash) (*Transaction, error) {
  65. tx, err := ec.client.TransactionByHash(ctx.context, hash.hash)
  66. return &Transaction{tx}, err
  67. }
  68. // GetTransactionCount returns the total number of transactions in the given block.
  69. func (ec *EthereumClient) GetTransactionCount(ctx *Context, hash *Hash) (int, error) {
  70. count, err := ec.client.TransactionCount(ctx.context, hash.hash)
  71. return int(count), err
  72. }
  73. // GetTransactionInBlock returns a single transaction at index in the given block.
  74. func (ec *EthereumClient) GetTransactionInBlock(ctx *Context, hash *Hash, index int) (*Transaction, error) {
  75. tx, err := ec.client.TransactionInBlock(ctx.context, hash.hash, uint(index))
  76. return &Transaction{tx}, err
  77. }
  78. // GetTransactionReceipt returns the receipt of a transaction by transaction hash.
  79. // Note that the receipt is not available for pending transactions.
  80. func (ec *EthereumClient) GetTransactionReceipt(ctx *Context, hash *Hash) (*Receipt, error) {
  81. receipt, err := ec.client.TransactionReceipt(ctx.context, hash.hash)
  82. return &Receipt{receipt}, err
  83. }
  84. // SyncProgress retrieves the current progress of the sync algorithm. If there's
  85. // no sync currently running, it returns nil.
  86. func (ec *EthereumClient) SyncProgress(ctx *Context) (*SyncProgress, error) {
  87. progress, err := ec.client.SyncProgress(ctx.context)
  88. if progress == nil {
  89. return nil, err
  90. }
  91. return &SyncProgress{*progress}, err
  92. }
  93. // NewHeadHandler is a client-side subscription callback to invoke on events and
  94. // subscription failure.
  95. type NewHeadHandler interface {
  96. OnNewHead(header *Header)
  97. OnError(failure string)
  98. }
  99. // SubscribeNewHead subscribes to notifications about the current blockchain head
  100. // on the given channel.
  101. func (ec *EthereumClient) SubscribeNewHead(ctx *Context, handler NewHeadHandler, buffer int) (*Subscription, error) {
  102. // Subscribe to the event internally
  103. ch := make(chan *types.Header, buffer)
  104. sub, err := ec.client.SubscribeNewHead(ctx.context, ch)
  105. if err != nil {
  106. return nil, err
  107. }
  108. // Start up a dispatcher to feed into the callback
  109. go func() {
  110. for {
  111. select {
  112. case header := <-ch:
  113. handler.OnNewHead(&Header{header})
  114. case err := <-sub.Err():
  115. handler.OnError(err.Error())
  116. return
  117. }
  118. }
  119. }()
  120. return &Subscription{sub}, nil
  121. }
  122. // State Access
  123. // GetBalanceAt returns the wei balance of the given account.
  124. // The block number can be <0, in which case the balance is taken from the latest known block.
  125. func (ec *EthereumClient) GetBalanceAt(ctx *Context, account *Address, number int64) (*BigInt, error) {
  126. if number < 0 {
  127. balance, err := ec.client.BalanceAt(ctx.context, account.address, nil)
  128. return &BigInt{balance}, err
  129. }
  130. balance, err := ec.client.BalanceAt(ctx.context, account.address, big.NewInt(number))
  131. return &BigInt{balance}, err
  132. }
  133. // GetStorageAt returns the value of key in the contract storage of the given account.
  134. // The block number can be <0, in which case the value is taken from the latest known block.
  135. func (ec *EthereumClient) GetStorageAt(ctx *Context, account *Address, key *Hash, number int64) ([]byte, error) {
  136. if number < 0 {
  137. return ec.client.StorageAt(ctx.context, account.address, key.hash, nil)
  138. }
  139. return ec.client.StorageAt(ctx.context, account.address, key.hash, big.NewInt(number))
  140. }
  141. // GetCodeAt returns the contract code of the given account.
  142. // The block number can be <0, in which case the code is taken from the latest known block.
  143. func (ec *EthereumClient) GetCodeAt(ctx *Context, account *Address, number int64) ([]byte, error) {
  144. if number < 0 {
  145. return ec.client.CodeAt(ctx.context, account.address, nil)
  146. }
  147. return ec.client.CodeAt(ctx.context, account.address, big.NewInt(number))
  148. }
  149. // GetNonceAt returns the account nonce of the given account.
  150. // The block number can be <0, in which case the nonce is taken from the latest known block.
  151. func (ec *EthereumClient) GetNonceAt(ctx *Context, account *Address, number int64) (int64, error) {
  152. if number < 0 {
  153. nonce, err := ec.client.NonceAt(ctx.context, account.address, nil)
  154. return int64(nonce), err
  155. }
  156. nonce, err := ec.client.NonceAt(ctx.context, account.address, big.NewInt(number))
  157. return int64(nonce), err
  158. }
  159. // Filters
  160. // FilterLogs executes a filter query.
  161. func (ec *EthereumClient) FilterLogs(ctx *Context, query *FilterQuery) (*Logs, error) {
  162. logs, err := ec.client.FilterLogs(ctx.context, query.query)
  163. if err != nil {
  164. return nil, err
  165. }
  166. // Temp hack due to vm.Logs being []*vm.Log
  167. res := make(vm.Logs, len(logs))
  168. for i, log := range logs {
  169. res[i] = &log
  170. }
  171. return &Logs{res}, nil
  172. }
  173. // FilterLogsHandler is a client-side subscription callback to invoke on events and
  174. // subscription failure.
  175. type FilterLogsHandler interface {
  176. OnFilterLogs(log *Log)
  177. OnError(failure string)
  178. }
  179. // SubscribeFilterLogs subscribes to the results of a streaming filter query.
  180. func (ec *EthereumClient) SubscribeFilterLogs(ctx *Context, query *FilterQuery, handler FilterLogsHandler, buffer int) (*Subscription, error) {
  181. // Subscribe to the event internally
  182. ch := make(chan vm.Log, buffer)
  183. sub, err := ec.client.SubscribeFilterLogs(ctx.context, query.query, ch)
  184. if err != nil {
  185. return nil, err
  186. }
  187. // Start up a dispatcher to feed into the callback
  188. go func() {
  189. for {
  190. select {
  191. case log := <-ch:
  192. handler.OnFilterLogs(&Log{&log})
  193. case err := <-sub.Err():
  194. handler.OnError(err.Error())
  195. return
  196. }
  197. }
  198. }()
  199. return &Subscription{sub}, nil
  200. }
  201. // Pending State
  202. // GetPendingBalanceAt returns the wei balance of the given account in the pending state.
  203. func (ec *EthereumClient) GetPendingBalanceAt(ctx *Context, account *Address) (*BigInt, error) {
  204. balance, err := ec.client.PendingBalanceAt(ctx.context, account.address)
  205. return &BigInt{balance}, err
  206. }
  207. // GetPendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
  208. func (ec *EthereumClient) GetPendingStorageAt(ctx *Context, account *Address, key *Hash) ([]byte, error) {
  209. return ec.client.PendingStorageAt(ctx.context, account.address, key.hash)
  210. }
  211. // GetPendingCodeAt returns the contract code of the given account in the pending state.
  212. func (ec *EthereumClient) GetPendingCodeAt(ctx *Context, account *Address) ([]byte, error) {
  213. return ec.client.PendingCodeAt(ctx.context, account.address)
  214. }
  215. // GetPendingNonceAt returns the account nonce of the given account in the pending state.
  216. // This is the nonce that should be used for the next transaction.
  217. func (ec *EthereumClient) GetPendingNonceAt(ctx *Context, account *Address) (int64, error) {
  218. nonce, err := ec.client.PendingNonceAt(ctx.context, account.address)
  219. return int64(nonce), err
  220. }
  221. // GetPendingTransactionCount returns the total number of transactions in the pending state.
  222. func (ec *EthereumClient) GetPendingTransactionCount(ctx *Context) (int, error) {
  223. count, err := ec.client.PendingTransactionCount(ctx.context)
  224. return int(count), err
  225. }
  226. // Contract Calling
  227. // CallContract executes a message call transaction, which is directly executed in the VM
  228. // of the node, but never mined into the blockchain.
  229. //
  230. // blockNumber selects the block height at which the call runs. It can be <0, in which
  231. // case the code is taken from the latest known block. Note that state from very old
  232. // blocks might not be available.
  233. func (ec *EthereumClient) CallContract(ctx *Context, msg *CallMsg, number int64) ([]byte, error) {
  234. if number < 0 {
  235. return ec.client.CallContract(ctx.context, msg.msg, nil)
  236. }
  237. return ec.client.CallContract(ctx.context, msg.msg, big.NewInt(number))
  238. }
  239. // PendingCallContract executes a message call transaction using the EVM.
  240. // The state seen by the contract call is the pending state.
  241. func (ec *EthereumClient) PendingCallContract(ctx *Context, msg *CallMsg) ([]byte, error) {
  242. return ec.client.PendingCallContract(ctx.context, msg.msg)
  243. }
  244. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  245. // execution of a transaction.
  246. func (ec *EthereumClient) SuggestGasPrice(ctx *Context) (*BigInt, error) {
  247. price, err := ec.client.SuggestGasPrice(ctx.context)
  248. return &BigInt{price}, err
  249. }
  250. // EstimateGas tries to estimate the gas needed to execute a specific transaction based on
  251. // the current pending state of the backend blockchain. There is no guarantee that this is
  252. // the true gas limit requirement as other transactions may be added or removed by miners,
  253. // but it should provide a basis for setting a reasonable default.
  254. func (ec *EthereumClient) EstimateGas(ctx *Context, msg *CallMsg) (*BigInt, error) {
  255. price, err := ec.client.EstimateGas(ctx.context, msg.msg)
  256. return &BigInt{price}, err
  257. }
  258. // SendTransaction injects a signed transaction into the pending pool for execution.
  259. //
  260. // If the transaction was a contract creation use the TransactionReceipt method to get the
  261. // contract address after the transaction has been mined.
  262. func (ec *EthereumClient) SendTransaction(ctx *Context, tx *Transaction) error {
  263. return ec.client.SendTransaction(ctx.context, tx.tx)
  264. }