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