2016-08-31 18:02:06 +00:00
|
|
|
package ethapi
|
|
|
|
|
|
|
|
import (
|
2017-02-23 00:22:43 +00:00
|
|
|
"errors"
|
|
|
|
"math/big"
|
2016-09-27 13:51:08 +00:00
|
|
|
|
2016-08-31 18:02:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-02-23 00:22:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2016-08-31 18:02:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les/status"
|
2017-05-01 11:09:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-08-31 18:02:06 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
// StatusBackend exposes Ethereum internals to support custom semantics in status-go bindings
|
2016-08-31 18:02:06 +00:00
|
|
|
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
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
am *status.AccountManager
|
2016-08-31 18:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 00:22:43 +00:00
|
|
|
var (
|
|
|
|
ErrStatusBackendNotInited = errors.New("StatusIM backend is not properly inited")
|
|
|
|
)
|
2016-09-27 13:51:08 +00:00
|
|
|
|
2016-08-31 18:02:06 +00:00
|
|
|
// NewStatusBackend creates a new backend using an existing Ethereum object.
|
|
|
|
func NewStatusBackend(apiBackend Backend) *StatusBackend {
|
2017-05-01 11:09:48 +00:00
|
|
|
log.Info("StatusIM: backend service inited")
|
2017-02-23 00:22:43 +00:00
|
|
|
return &StatusBackend{
|
2017-09-04 12:56:58 +00:00
|
|
|
eapi: NewPublicEthereumAPI(apiBackend),
|
|
|
|
bcapi: NewPublicBlockChainAPI(apiBackend),
|
|
|
|
txapi: NewPublicTransactionPoolAPI(apiBackend, new(AddrLocker)),
|
|
|
|
am: status.NewAccountManager(apiBackend.AccountManager()),
|
2017-02-23 00:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 13:51:08 +00:00
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
// SetAccountsFilterHandler sets a callback that is triggered when account list is requested
|
2016-09-27 13:51:08 +00:00
|
|
|
func (b *StatusBackend) SetAccountsFilterHandler(fn status.AccountsFilterHandler) {
|
|
|
|
b.am.SetAccountsFilterHandler(fn)
|
|
|
|
}
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
// AccountManager returns reference to account manager
|
2016-09-27 13:51:08 +00:00
|
|
|
func (b *StatusBackend) AccountManager() *status.AccountManager {
|
|
|
|
return b.am
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:16:22 +00:00
|
|
|
// SendTransaction wraps call to PublicTransactionPoolAPI.SendTransactionWithPassphrase
|
2017-09-04 12:56:58 +00:00
|
|
|
func (b *StatusBackend) SendTransaction(ctx context.Context, args status.SendTxArgs, passphrase string) (common.Hash, error) {
|
2016-08-31 18:02:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:16:22 +00:00
|
|
|
return b.txapi.SendTransactionWithPassphrase(ctx, SendTxArgs(args), passphrase)
|
2016-10-30 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
2016-08-31 18:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 00:22:43 +00:00
|
|
|
var gasPrice hexutil.Big
|
|
|
|
if args.GasPrice != nil {
|
|
|
|
gasPrice = *args.GasPrice
|
2016-08-31 18:02:06 +00:00
|
|
|
}
|
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)
|
2016-08-31 18:02:06 +00:00
|
|
|
}
|