2016-09-05 16:07:57 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Contains a wrapper for the Ethereum client.
|
|
|
|
|
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EthereumClient provides access to the Ethereum APIs.
|
|
|
|
type EthereumClient struct {
|
|
|
|
client *ethclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEthereumClient connects a client to the given URL.
|
2016-12-08 12:09:26 +00:00
|
|
|
func NewEthereumClient(rawurl string) (client *EthereumClient, _ error) {
|
|
|
|
rawClient, err := ethclient.Dial(rawurl)
|
|
|
|
return &EthereumClient{rawClient}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the given full block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetBlockByHash(ctx *Context, hash *Hash) (block *Block, _ error) {
|
|
|
|
rawBlock, err := ec.client.BlockByHash(ctx.context, hash.hash)
|
|
|
|
return &Block{rawBlock}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByNumber returns a block from the current canonical chain. If number is <0, the
|
|
|
|
// latest known block is returned.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetBlockByNumber(ctx *Context, number int64) (block *Block, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
2016-12-08 12:09:26 +00:00
|
|
|
rawBlock, err := ec.client.BlockByNumber(ctx.context, nil)
|
|
|
|
return &Block{rawBlock}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
2016-12-08 12:09:26 +00:00
|
|
|
rawBlock, err := ec.client.BlockByNumber(ctx.context, big.NewInt(number))
|
|
|
|
return &Block{rawBlock}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaderByHash returns the block header with the given hash.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetHeaderByHash(ctx *Context, hash *Hash) (header *Header, _ error) {
|
|
|
|
rawHeader, err := ec.client.HeaderByHash(ctx.context, hash.hash)
|
|
|
|
return &Header{rawHeader}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaderByNumber returns a block header from the current canonical chain. If number is <0,
|
|
|
|
// the latest known header is returned.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetHeaderByNumber(ctx *Context, number int64) (header *Header, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
2016-12-08 12:09:26 +00:00
|
|
|
rawHeader, err := ec.client.HeaderByNumber(ctx.context, nil)
|
|
|
|
return &Header{rawHeader}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
2016-12-08 12:09:26 +00:00
|
|
|
rawHeader, err := ec.client.HeaderByNumber(ctx.context, big.NewInt(number))
|
|
|
|
return &Header{rawHeader}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByHash returns the transaction with the given hash.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetTransactionByHash(ctx *Context, hash *Hash) (tx *Transaction, _ error) {
|
2016-11-29 14:54:06 +00:00
|
|
|
// TODO(karalabe): handle isPending
|
2016-12-08 12:09:26 +00:00
|
|
|
rawTx, _, err := ec.client.TransactionByHash(ctx.context, hash.hash)
|
|
|
|
return &Transaction{rawTx}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 09:03:28 +00:00
|
|
|
// GetTransactionSender returns the sender address of a transaction. The transaction must
|
|
|
|
// be included in blockchain at the given block and index.
|
|
|
|
func (ec *EthereumClient) GetTransactionSender(ctx *Context, tx *Transaction, blockhash *Hash, index int) (sender *Address, _ error) {
|
|
|
|
addr, err := ec.client.TransactionSender(ctx.context, tx.tx, blockhash.hash, uint(index))
|
|
|
|
return &Address{addr}, err
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:07:57 +00:00
|
|
|
// GetTransactionCount returns the total number of transactions in the given block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetTransactionCount(ctx *Context, hash *Hash) (count int, _ error) {
|
|
|
|
rawCount, err := ec.client.TransactionCount(ctx.context, hash.hash)
|
|
|
|
return int(rawCount), err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionInBlock returns a single transaction at index in the given block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetTransactionInBlock(ctx *Context, hash *Hash, index int) (tx *Transaction, _ error) {
|
|
|
|
rawTx, err := ec.client.TransactionInBlock(ctx.context, hash.hash, uint(index))
|
|
|
|
return &Transaction{rawTx}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionReceipt returns the receipt of a transaction by transaction hash.
|
|
|
|
// Note that the receipt is not available for pending transactions.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetTransactionReceipt(ctx *Context, hash *Hash) (receipt *Receipt, _ error) {
|
|
|
|
rawReceipt, err := ec.client.TransactionReceipt(ctx.context, hash.hash)
|
|
|
|
return &Receipt{rawReceipt}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncProgress retrieves the current progress of the sync algorithm. If there's
|
|
|
|
// no sync currently running, it returns nil.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) SyncProgress(ctx *Context) (progress *SyncProgress, _ error) {
|
|
|
|
rawProgress, err := ec.client.SyncProgress(ctx.context)
|
|
|
|
if rawProgress == nil {
|
2016-09-05 16:07:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-08 12:09:26 +00:00
|
|
|
return &SyncProgress{*rawProgress}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHeadHandler is a client-side subscription callback to invoke on events and
|
|
|
|
// subscription failure.
|
|
|
|
type NewHeadHandler interface {
|
|
|
|
OnNewHead(header *Header)
|
|
|
|
OnError(failure string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeNewHead subscribes to notifications about the current blockchain head
|
|
|
|
// on the given channel.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) SubscribeNewHead(ctx *Context, handler NewHeadHandler, buffer int) (sub *Subscription, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
// Subscribe to the event internally
|
|
|
|
ch := make(chan *types.Header, buffer)
|
2016-12-08 12:09:26 +00:00
|
|
|
rawSub, err := ec.client.SubscribeNewHead(ctx.context, ch)
|
2016-09-05 16:07:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Start up a dispatcher to feed into the callback
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case header := <-ch:
|
|
|
|
handler.OnNewHead(&Header{header})
|
|
|
|
|
2016-12-08 12:09:26 +00:00
|
|
|
case err := <-rawSub.Err():
|
2016-09-05 16:07:57 +00:00
|
|
|
handler.OnError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-12-08 12:09:26 +00:00
|
|
|
return &Subscription{rawSub}, nil
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// State Access
|
|
|
|
|
|
|
|
// GetBalanceAt returns the wei balance of the given account.
|
|
|
|
// The block number can be <0, in which case the balance is taken from the latest known block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetBalanceAt(ctx *Context, account *Address, number int64) (balance *BigInt, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
2016-12-08 12:09:26 +00:00
|
|
|
rawBalance, err := ec.client.BalanceAt(ctx.context, account.address, nil)
|
|
|
|
return &BigInt{rawBalance}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
2016-12-08 12:09:26 +00:00
|
|
|
rawBalance, err := ec.client.BalanceAt(ctx.context, account.address, big.NewInt(number))
|
|
|
|
return &BigInt{rawBalance}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorageAt returns the value of key in the contract storage of the given account.
|
|
|
|
// The block number can be <0, in which case the value is taken from the latest known block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetStorageAt(ctx *Context, account *Address, key *Hash, number int64) (storage []byte, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
|
|
|
return ec.client.StorageAt(ctx.context, account.address, key.hash, nil)
|
|
|
|
}
|
|
|
|
return ec.client.StorageAt(ctx.context, account.address, key.hash, big.NewInt(number))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCodeAt returns the contract code of the given account.
|
|
|
|
// The block number can be <0, in which case the code is taken from the latest known block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetCodeAt(ctx *Context, account *Address, number int64) (code []byte, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
|
|
|
return ec.client.CodeAt(ctx.context, account.address, nil)
|
|
|
|
}
|
|
|
|
return ec.client.CodeAt(ctx.context, account.address, big.NewInt(number))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNonceAt returns the account nonce of the given account.
|
|
|
|
// The block number can be <0, in which case the nonce is taken from the latest known block.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetNonceAt(ctx *Context, account *Address, number int64) (nonce int64, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
2016-12-08 12:09:26 +00:00
|
|
|
rawNonce, err := ec.client.NonceAt(ctx.context, account.address, nil)
|
|
|
|
return int64(rawNonce), err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
2016-12-08 12:09:26 +00:00
|
|
|
rawNonce, err := ec.client.NonceAt(ctx.context, account.address, big.NewInt(number))
|
|
|
|
return int64(rawNonce), err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filters
|
|
|
|
|
|
|
|
// FilterLogs executes a filter query.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) FilterLogs(ctx *Context, query *FilterQuery) (logs *Logs, _ error) {
|
|
|
|
rawLogs, err := ec.client.FilterLogs(ctx.context, query.query)
|
2016-09-05 16:07:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Temp hack due to vm.Logs being []*vm.Log
|
2017-01-05 13:03:50 +00:00
|
|
|
res := make([]*types.Log, len(rawLogs))
|
2016-12-08 12:09:26 +00:00
|
|
|
for i, log := range rawLogs {
|
2016-09-05 16:07:57 +00:00
|
|
|
res[i] = &log
|
|
|
|
}
|
|
|
|
return &Logs{res}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterLogsHandler is a client-side subscription callback to invoke on events and
|
|
|
|
// subscription failure.
|
|
|
|
type FilterLogsHandler interface {
|
|
|
|
OnFilterLogs(log *Log)
|
|
|
|
OnError(failure string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeFilterLogs subscribes to the results of a streaming filter query.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) SubscribeFilterLogs(ctx *Context, query *FilterQuery, handler FilterLogsHandler, buffer int) (sub *Subscription, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
// Subscribe to the event internally
|
2017-01-05 13:03:50 +00:00
|
|
|
ch := make(chan types.Log, buffer)
|
2016-12-08 12:09:26 +00:00
|
|
|
rawSub, err := ec.client.SubscribeFilterLogs(ctx.context, query.query, ch)
|
2016-09-05 16:07:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Start up a dispatcher to feed into the callback
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-ch:
|
|
|
|
handler.OnFilterLogs(&Log{&log})
|
|
|
|
|
2016-12-08 12:09:26 +00:00
|
|
|
case err := <-rawSub.Err():
|
2016-09-05 16:07:57 +00:00
|
|
|
handler.OnError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-12-08 12:09:26 +00:00
|
|
|
return &Subscription{rawSub}, nil
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pending State
|
|
|
|
|
|
|
|
// GetPendingBalanceAt returns the wei balance of the given account in the pending state.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetPendingBalanceAt(ctx *Context, account *Address) (balance *BigInt, _ error) {
|
|
|
|
rawBalance, err := ec.client.PendingBalanceAt(ctx.context, account.address)
|
|
|
|
return &BigInt{rawBalance}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetPendingStorageAt(ctx *Context, account *Address, key *Hash) (storage []byte, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
return ec.client.PendingStorageAt(ctx.context, account.address, key.hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPendingCodeAt returns the contract code of the given account in the pending state.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetPendingCodeAt(ctx *Context, account *Address) (code []byte, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
return ec.client.PendingCodeAt(ctx.context, account.address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPendingNonceAt returns the account nonce of the given account in the pending state.
|
|
|
|
// This is the nonce that should be used for the next transaction.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetPendingNonceAt(ctx *Context, account *Address) (nonce int64, _ error) {
|
|
|
|
rawNonce, err := ec.client.PendingNonceAt(ctx.context, account.address)
|
|
|
|
return int64(rawNonce), err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPendingTransactionCount returns the total number of transactions in the pending state.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) GetPendingTransactionCount(ctx *Context) (count int, _ error) {
|
|
|
|
rawCount, err := ec.client.PendingTransactionCount(ctx.context)
|
|
|
|
return int(rawCount), err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contract Calling
|
|
|
|
|
|
|
|
// CallContract executes a message call transaction, which is directly executed in the VM
|
|
|
|
// of the node, but never mined into the blockchain.
|
|
|
|
//
|
|
|
|
// blockNumber selects the block height at which the call runs. It can be <0, in which
|
|
|
|
// case the code is taken from the latest known block. Note that state from very old
|
|
|
|
// blocks might not be available.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) CallContract(ctx *Context, msg *CallMsg, number int64) (output []byte, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
if number < 0 {
|
|
|
|
return ec.client.CallContract(ctx.context, msg.msg, nil)
|
|
|
|
}
|
|
|
|
return ec.client.CallContract(ctx.context, msg.msg, big.NewInt(number))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PendingCallContract executes a message call transaction using the EVM.
|
|
|
|
// The state seen by the contract call is the pending state.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) PendingCallContract(ctx *Context, msg *CallMsg) (output []byte, _ error) {
|
2016-09-05 16:07:57 +00:00
|
|
|
return ec.client.PendingCallContract(ctx.context, msg.msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
|
|
|
|
// execution of a transaction.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) SuggestGasPrice(ctx *Context) (price *BigInt, _ error) {
|
|
|
|
rawPrice, err := ec.client.SuggestGasPrice(ctx.context)
|
|
|
|
return &BigInt{rawPrice}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
|
|
|
|
// the current pending state of the backend blockchain. There is no guarantee that this is
|
|
|
|
// the true gas limit requirement as other transactions may be added or removed by miners,
|
|
|
|
// but it should provide a basis for setting a reasonable default.
|
2016-12-08 12:09:26 +00:00
|
|
|
func (ec *EthereumClient) EstimateGas(ctx *Context, msg *CallMsg) (gas *BigInt, _ error) {
|
|
|
|
rawGas, err := ec.client.EstimateGas(ctx.context, msg.msg)
|
|
|
|
return &BigInt{rawGas}, err
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction injects a signed transaction into the pending pool for execution.
|
|
|
|
//
|
|
|
|
// If the transaction was a contract creation use the TransactionReceipt method to get the
|
|
|
|
// contract address after the transaction has been mined.
|
|
|
|
func (ec *EthereumClient) SendTransaction(ctx *Context, tx *Transaction) error {
|
|
|
|
return ec.client.SendTransaction(ctx.context, tx.tx)
|
|
|
|
}
|