status-go/vendor/github.com/ethereum/go-ethereum/internal/ethapi/status_backend.go

89 lines
2.6 KiB
Go
Raw Normal View History

package ethapi
import (
2017-02-23 00:22:43 +00:00
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
2017-02-23 00:22:43 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/les/status"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/net/context"
)
// StatusBackend exposes Ethereum internals to support custom semantics in status-go bindings
type StatusBackend struct {
eapi *PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
bcapi *PublicBlockChainAPI // Wrapper around the blockchain to access chain data
txapi *PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
am *status.AccountManager
}
2017-02-23 00:22:43 +00:00
var (
ErrStatusBackendNotInited = errors.New("StatusIM backend is not properly inited")
)
// NewStatusBackend creates a new backend using an existing Ethereum object.
func NewStatusBackend(apiBackend Backend) *StatusBackend {
log.Info("StatusIM: backend service inited")
2017-02-23 00:22:43 +00:00
return &StatusBackend{
eapi: NewPublicEthereumAPI(apiBackend),
bcapi: NewPublicBlockChainAPI(apiBackend),
txapi: NewPublicTransactionPoolAPI(apiBackend, new(AddrLocker)),
am: status.NewAccountManager(apiBackend.AccountManager()),
2017-02-23 00:22:43 +00:00
}
}
// SetAccountsFilterHandler sets a callback that is triggered when account list is requested
func (b *StatusBackend) SetAccountsFilterHandler(fn status.AccountsFilterHandler) {
b.am.SetAccountsFilterHandler(fn)
}
// AccountManager returns reference to account manager
func (b *StatusBackend) AccountManager() *status.AccountManager {
return b.am
}
// SendTransaction wraps call to PublicTransactionPoolAPI.SendTransactionWithPassphrase
func (b *StatusBackend) SendTransaction(ctx context.Context, args status.SendTxArgs, passphrase string) (common.Hash, error) {
if ctx == nil {
ctx = context.Background()
}
2017-02-23 00:22:43 +00:00
if estimatedGas, err := b.EstimateGas(ctx, args); err == nil {
if estimatedGas.ToInt().Cmp(big.NewInt(defaultGas)) == 1 { // gas > defaultGas
args.Gas = estimatedGas
}
}
return b.txapi.SendTransactionWithPassphrase(ctx, SendTxArgs(args), passphrase)
}
2017-02-23 00:22:43 +00:00
// EstimateGas uses underlying blockchain API to obtain gas for a given tx arguments
func (b *StatusBackend) EstimateGas(ctx context.Context, args status.SendTxArgs) (*hexutil.Big, error) {
if args.Gas != nil {
return args.Gas, nil
}
2017-02-23 00:22:43 +00:00
var gasPrice hexutil.Big
if args.GasPrice != nil {
gasPrice = *args.GasPrice
}
2017-02-23 00:22:43 +00:00
var value hexutil.Big
if args.Value != nil {
value = *args.Value
}
callArgs := CallArgs{
From: args.From,
To: args.To,
GasPrice: gasPrice,
Value: value,
Data: args.Data,
}
return b.bcapi.EstimateGas(ctx, callArgs)
}