2021-06-23 06:38:05 +00:00
|
|
|
package eth
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-07-09 09:04:46 +00:00
|
|
|
"encoding/json"
|
2021-05-12 13:08:31 +00:00
|
|
|
"fmt"
|
2021-04-18 16:39:15 +00:00
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-05-12 16:50:26 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2021-05-12 16:50:26 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2021-04-18 16:39:15 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2021-05-12 16:50:26 +00:00
|
|
|
|
2021-06-30 09:35:11 +00:00
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-05-12 13:08:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-07-15 06:01:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2021-06-08 11:11:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/hd"
|
2021-06-23 06:38:05 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/backend"
|
2021-06-22 10:49:18 +00:00
|
|
|
rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types"
|
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
// PublicAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
|
|
|
|
type PublicAPI struct {
|
2021-04-18 16:39:15 +00:00
|
|
|
ctx context.Context
|
|
|
|
clientCtx client.Context
|
2021-05-12 16:50:26 +00:00
|
|
|
queryClient *rpctypes.QueryClient
|
2021-04-18 16:39:15 +00:00
|
|
|
chainIDEpoch *big.Int
|
|
|
|
logger log.Logger
|
2021-06-23 06:38:05 +00:00
|
|
|
backend backend.Backend
|
2021-05-12 16:50:26 +00:00
|
|
|
nonceLock *rpctypes.AddrLocker
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
// NewPublicAPI creates an instance of the public ETH Web3 API.
|
|
|
|
func NewPublicAPI(
|
2021-07-12 18:39:35 +00:00
|
|
|
logger log.Logger,
|
2021-04-18 16:39:15 +00:00
|
|
|
clientCtx client.Context,
|
2021-06-23 06:38:05 +00:00
|
|
|
backend backend.Backend,
|
2021-05-17 10:13:08 +00:00
|
|
|
nonceLock *rpctypes.AddrLocker,
|
2021-06-23 06:38:05 +00:00
|
|
|
) *PublicAPI {
|
2021-04-18 16:39:15 +00:00
|
|
|
epoch, err := ethermint.ParseChainID(clientCtx.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:50:26 +00:00
|
|
|
algos, _ := clientCtx.Keyring.SupportedAlgorithms()
|
|
|
|
|
|
|
|
if !algos.Contains(hd.EthSecp256k1) {
|
|
|
|
kr, err := keyring.New(
|
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
viper.GetString(flags.FlagKeyringBackend),
|
|
|
|
clientCtx.KeyringDir,
|
|
|
|
clientCtx.Input,
|
|
|
|
hd.EthSecp256k1Option(),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCtx = clientCtx.WithKeyring(kr)
|
|
|
|
}
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
api := &PublicAPI{
|
2021-04-18 16:39:15 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
clientCtx: clientCtx,
|
2021-05-17 10:13:08 +00:00
|
|
|
queryClient: rpctypes.NewQueryClient(clientCtx),
|
2021-04-18 16:39:15 +00:00
|
|
|
chainIDEpoch: epoch,
|
2021-07-12 18:39:35 +00:00
|
|
|
logger: logger.With("client", "json-rpc"),
|
2021-04-18 16:39:15 +00:00
|
|
|
backend: backend,
|
|
|
|
nonceLock: nonceLock,
|
|
|
|
}
|
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:50:26 +00:00
|
|
|
// ClientCtx returns client context
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) ClientCtx() client.Context {
|
2021-05-12 16:50:26 +00:00
|
|
|
return e.clientCtx
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// ProtocolVersion returns the supported Ethereum protocol version.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) ProtocolVersion() hexutil.Uint {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_protocolVersion")
|
2021-06-14 14:24:08 +00:00
|
|
|
return hexutil.Uint(ethermint.ProtocolVersion)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChainId returns the chain's identifier in hex format
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) ChainId() (hexutil.Uint, error) { // nolint
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_chainId")
|
2021-04-18 16:39:15 +00:00
|
|
|
return hexutil.Uint(uint(e.chainIDEpoch.Uint64())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Syncing returns whether or not the current node is syncing with other peers. Returns false if not, or a struct
|
|
|
|
// outlining the state of the sync if it is.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) Syncing() (interface{}, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_syncing")
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
status, err := e.clientCtx.Client.Status(e.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !status.SyncInfo.CatchingUp {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
// "startingBlock": nil, // NA
|
|
|
|
"currentBlock": hexutil.Uint64(status.SyncInfo.LatestBlockHeight),
|
|
|
|
// "highestBlock": nil, // NA
|
|
|
|
// "pulledStates": nil, // NA
|
|
|
|
// "knownStates": nil, // NA
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coinbase is the address that staking rewards will be send to (alias for Etherbase).
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) Coinbase() (string, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_coinbase")
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
node, err := e.clientCtx.GetNode()
|
|
|
|
if err != nil {
|
2021-06-22 10:14:40 +00:00
|
|
|
return "", err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status, err := node.Status(e.ctx)
|
|
|
|
if err != nil {
|
2021-06-22 10:14:40 +00:00
|
|
|
return "", err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 10:14:40 +00:00
|
|
|
req := &evmtypes.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: sdk.ConsAddress(status.ValidatorInfo.Address).String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := e.queryClient.ValidatorAccount(e.ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
toAddr, _ := sdk.AccAddressFromBech32(res.AccountAddress)
|
|
|
|
ethAddr := common.BytesToAddress(toAddr.Bytes())
|
|
|
|
return ethAddr.Hex(), nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mining returns whether or not this node is currently mining. Always false.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) Mining() bool {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_mining")
|
2021-04-18 16:39:15 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashrate returns the current node's hashrate. Always 0.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) Hashrate() hexutil.Uint64 {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_hashrate")
|
2021-04-18 16:39:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GasPrice returns the current gas price based on Ethermint's gas price oracle.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GasPrice() *hexutil.Big {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_gasPrice")
|
2021-06-21 08:03:54 +00:00
|
|
|
// TODO: use minimum value defined in config instead of default or implement oracle
|
|
|
|
out := big.NewInt(ethermint.DefaultGasPrice)
|
2021-04-18 16:39:15 +00:00
|
|
|
return (*hexutil.Big)(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accounts returns the list of accounts available to this node.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) Accounts() ([]common.Address, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_accounts")
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-04-19 10:49:55 +00:00
|
|
|
addresses := make([]common.Address, 0) // return [] instead of nil if empty
|
|
|
|
|
|
|
|
infos, err := e.clientCtx.Keyring.List()
|
|
|
|
if err != nil {
|
|
|
|
return addresses, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
addressBytes := info.GetPubKey().Address().Bytes()
|
|
|
|
addresses = append(addresses, common.BytesToAddress(addressBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockNumber returns the current block number.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) BlockNumber() (hexutil.Uint64, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_blockNumber")
|
2021-04-18 16:39:15 +00:00
|
|
|
return e.backend.BlockNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBalance returns the provided account's balance up to the provided block number.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetBalance(address common.Address, blockNum rpctypes.BlockNumber) (*hexutil.Big, error) { // nolint: interfacer
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getBalance", "address", address.String(), "block number", blockNum)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
req := &evmtypes.QueryBalanceRequest{
|
|
|
|
Address: address.String(),
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
res, err := e.queryClient.Balance(rpctypes.ContextWithHeight(blockNum.Int64()), req)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 09:29:47 +00:00
|
|
|
val, ok := sdk.NewIntFromString(res.Balance)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("invalid balance")
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*hexutil.Big)(val.BigInt()), nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorageAt returns the contract storage at the given address, block number, and key.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetStorageAt(address common.Address, key string, blockNum rpctypes.BlockNumber) (hexutil.Bytes, error) { // nolint: interfacer
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getStorageAt", "address", address.Hex(), "key", key, "block number", blockNum)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
req := &evmtypes.QueryStorageRequest{
|
|
|
|
Address: address.String(),
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
res, err := e.queryClient.Storage(rpctypes.ContextWithHeight(blockNum.Int64()), req)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := common.HexToHash(res.Value)
|
|
|
|
return value.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetTransactionCount(address common.Address, blockNum rpctypes.BlockNumber) (*hexutil.Uint64, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getTransactionCount", "address", address.Hex(), "block number", blockNum)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
// Get nonce (sequence) from account
|
|
|
|
from := sdk.AccAddress(address.Bytes())
|
|
|
|
accRet := e.clientCtx.AccountRetriever
|
|
|
|
|
|
|
|
err := accRet.EnsureExists(e.clientCtx, from)
|
|
|
|
if err != nil {
|
|
|
|
// account doesn't exist yet, return 0
|
|
|
|
n := hexutil.Uint64(0)
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
2021-06-30 07:30:01 +00:00
|
|
|
includePending := blockNum == rpctypes.EthPendingBlockNumber
|
2021-07-09 08:34:49 +00:00
|
|
|
nonce, err := e.getAccountNonce(address, includePending, blockNum.Int64(), e.logger)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := hexutil.Uint64(nonce)
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getBlockTransactionCountByHash", "hash", hash.Hex())
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
|
|
|
|
if err != nil {
|
2021-07-16 10:13:15 +00:00
|
|
|
e.logger.Debug("block not found", "hash", hash.Hex(), "error", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock.Block == nil {
|
|
|
|
e.logger.Debug("block not found", "hash", hash.Hex())
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n := hexutil.Uint(len(resBlock.Block.Txs))
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTransactionCountByNumber returns the number of transactions in the block identified by number.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint {
|
2021-07-16 10:13:15 +00:00
|
|
|
e.logger.Debug("eth_getBlockTransactionCountByNumber", "height", blockNum.Int64())
|
2021-04-18 16:39:15 +00:00
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, blockNum.TmHeight())
|
|
|
|
if err != nil {
|
2021-07-16 10:13:15 +00:00
|
|
|
e.logger.Debug("block not found", "height", blockNum.Int64(), "error", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock.Block == nil {
|
|
|
|
e.logger.Debug("block not found", "height", blockNum.Int64())
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n := hexutil.Uint(len(resBlock.Block.Txs))
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockHash returns the number of uncles in the block identified by hash. Always zero.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetUncleCountByBlockHash(hash common.Hash) hexutil.Uint {
|
2021-04-18 16:39:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockNumber returns the number of uncles in the block identified by number. Always zero.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetUncleCountByBlockNumber(blockNum rpctypes.BlockNumber) hexutil.Uint {
|
2021-04-18 16:39:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCode returns the contract code at the given address and block number.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetCode(address common.Address, blockNumber rpctypes.BlockNumber) (hexutil.Bytes, error) { // nolint: interfacer
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getCode", "address", address.Hex(), "block number", blockNumber)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
req := &evmtypes.QueryCodeRequest{
|
|
|
|
Address: address.String(),
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
res, err := e.queryClient.Code(rpctypes.ContextWithHeight(blockNumber.Int64()), req)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Code, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionLogs returns the logs given a transaction hash.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getTransactionLogs", "hash", txHash)
|
2021-04-18 16:39:15 +00:00
|
|
|
return e.backend.GetTransactionLogs(txHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs the provided data using the private key of address via Geth's signature standard.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_sign", "address", address.Hex(), "data", common.Bytes2Hex(data))
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
from := sdk.AccAddress(address.Bytes())
|
|
|
|
|
|
|
|
_, err := e.clientCtx.Keyring.KeyByAddress(from)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to find key in keyring", "address", address.String())
|
2021-05-12 13:08:31 +00:00
|
|
|
return nil, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the requested hash with the wallet
|
|
|
|
signature, _, err := e.clientCtx.Keyring.SignByAddress(from, data)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("keyring.SignByAddress failed", "address", address.Hex())
|
2021-05-12 13:08:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signature[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
|
|
|
|
return signature, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction sends an Ethereum transaction.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_sendTransaction", "args", args.String())
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
// Look up the wallet containing the requested signer
|
|
|
|
_, err := e.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes()))
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to find key in keyring", "address", args.From, "error", err.Error())
|
2021-05-12 13:08:31 +00:00
|
|
|
return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
args, err = e.setTxDefaults(args)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:32:06 +00:00
|
|
|
msg := args.ToTransaction()
|
|
|
|
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("tx failed basic validation", "error", err.Error())
|
2021-06-22 07:32:06 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
// TODO: get from chain config
|
2021-06-22 07:32:06 +00:00
|
|
|
signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt())
|
2021-07-05 16:39:08 +00:00
|
|
|
|
2021-06-22 07:32:06 +00:00
|
|
|
// Sign transaction
|
|
|
|
if err := msg.Sign(signer, e.clientCtx.Keyring); err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("failed to sign tx", "error", err.Error())
|
2021-06-22 07:32:06 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
// Assemble transaction from fields
|
|
|
|
builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder)
|
|
|
|
if !ok {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error())
|
2021-05-12 13:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{})
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error())
|
2021-05-12 13:08:31 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.SetExtensionOptions(option)
|
2021-06-22 07:32:06 +00:00
|
|
|
err = builder.SetMsgs(msg)
|
2021-05-12 13:08:31 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("builder.SetMsgs failed", "error", err.Error())
|
2021-05-12 13:08:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 08:22:36 +00:00
|
|
|
// Query params to use the EVM denomination
|
|
|
|
res, err := e.queryClient.QueryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to query evm params", "error", err.Error())
|
2021-06-24 08:22:36 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(msg.Data)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to unpack tx data", "error", err.Error())
|
2021-07-05 16:39:08 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))}
|
2021-05-12 13:08:31 +00:00
|
|
|
builder.SetFeeAmount(fees)
|
2021-06-22 07:32:06 +00:00
|
|
|
builder.SetGasLimit(msg.GetGas())
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
// Encode transaction by default Tx encoder
|
|
|
|
txEncoder := e.clientCtx.TxConfig.TxEncoder()
|
2021-06-22 07:32:06 +00:00
|
|
|
txBytes, err := txEncoder(builder.GetTx())
|
2021-05-12 13:08:31 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to encode eth tx using default encoder", "error", err.Error())
|
2021-05-12 13:08:31 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:35:11 +00:00
|
|
|
txHash := msg.AsTransaction().Hash()
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
// Broadcast transaction in sync mode (default)
|
|
|
|
// NOTE: If error is encountered on the node, the broadcast will not return an error
|
2021-06-22 07:11:53 +00:00
|
|
|
syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync)
|
|
|
|
rsp, err := syncCtx.BroadcastTx(txBytes)
|
|
|
|
if err != nil || rsp.Code != 0 {
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New(rsp.RawLog)
|
|
|
|
}
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to broadcast tx", "error", err.Error())
|
2021-05-12 13:08:31 +00:00
|
|
|
return txHash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return transaction hash
|
|
|
|
return txHash, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendRawTransaction send a raw Ethereum transaction.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_sendRawTransaction", "length", len(data))
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
// RLP decode raw transaction bytes
|
2021-06-08 11:11:37 +00:00
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(data)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("transaction decoding failed", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
ethereumTx, isEthTx := tx.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !isEthTx {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("invalid transaction type", "type", fmt.Sprintf("%T", tx))
|
2021-06-08 11:11:37 +00:00
|
|
|
return common.Hash{}, fmt.Errorf("invalid transaction type %T", tx)
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
if err := ethereumTx.ValidateBasic(); err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("tx failed basic validation", "error", err.Error())
|
2021-07-05 16:39:08 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder)
|
|
|
|
if !ok {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{})
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.SetExtensionOptions(option)
|
2021-06-08 11:11:37 +00:00
|
|
|
err = builder.SetMsgs(tx.GetMsgs()...)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("builder.SetMsgs failed", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 08:22:36 +00:00
|
|
|
// Query params to use the EVM denomination
|
|
|
|
res, err := e.queryClient.QueryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to query evm params", "error", err.Error())
|
2021-06-24 08:22:36 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(ethereumTx.Data)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to unpack tx data", "error", err.Error())
|
2021-07-05 16:39:08 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))}
|
2021-04-18 16:39:15 +00:00
|
|
|
builder.SetFeeAmount(fees)
|
|
|
|
builder.SetGasLimit(ethereumTx.GetGas())
|
|
|
|
|
|
|
|
// Encode transaction by default Tx encoder
|
|
|
|
txBytes, err := e.clientCtx.TxConfig.TxEncoder()(builder.GetTx())
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to encode eth tx using default encoder", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:35:11 +00:00
|
|
|
txHash := ethereumTx.AsTransaction().Hash()
|
2021-06-08 11:11:37 +00:00
|
|
|
|
2021-06-22 07:11:53 +00:00
|
|
|
syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync)
|
|
|
|
rsp, err := syncCtx.BroadcastTx(txBytes)
|
|
|
|
if err != nil || rsp.Code != 0 {
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New(rsp.RawLog)
|
|
|
|
}
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to broadcast tx", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return txHash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call performs a raw contract call.
|
2021-07-09 09:04:46 +00:00
|
|
|
func (e *PublicAPI) Call(args evmtypes.CallArgs, blockNr rpctypes.BlockNumber, _ *rpctypes.StateOverride) (hexutil.Bytes, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_call", "args", args.String(), "block number", blockNr)
|
2021-07-09 09:04:46 +00:00
|
|
|
data, err := e.doCall(args, blockNr)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
2021-05-12 13:08:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
return (hexutil.Bytes)(data.Ret), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoCall performs a simulated call operation through the evmtypes. It returns the
|
|
|
|
// estimated gas used on the operation or an error if fails.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) doCall(
|
2021-07-09 09:04:46 +00:00
|
|
|
args evmtypes.CallArgs, blockNr rpctypes.BlockNumber,
|
|
|
|
) (*evmtypes.MsgEthereumTxResponse, error) {
|
|
|
|
bz, err := json.Marshal(&args)
|
2021-06-24 08:22:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-09 09:04:46 +00:00
|
|
|
req := evmtypes.EthCallRequest{Args: bz}
|
|
|
|
res, err := e.queryClient.EthCall(rpctypes.ContextWithHeight(blockNr.Int64()), &req)
|
2021-07-05 16:39:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-15 06:01:05 +00:00
|
|
|
if res.Failed() {
|
|
|
|
if res.VmError == vm.ErrExecutionReverted.Error() {
|
|
|
|
return nil, evmtypes.NewExecErrorWithReason(res.Ret)
|
|
|
|
}
|
|
|
|
return nil, errors.New(res.VmError)
|
|
|
|
}
|
|
|
|
|
2021-07-09 09:04:46 +00:00
|
|
|
return res, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:39:35 +00:00
|
|
|
// EstimateGas returns an estimate of gas usage for the given smart contract call.
|
2021-07-09 09:04:46 +00:00
|
|
|
func (e *PublicAPI) EstimateGas(args evmtypes.CallArgs) (hexutil.Uint64, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_estimateGas")
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
// From ContextWithHeight: if the provided height is 0,
|
|
|
|
// it will return an empty context and the gRPC query will use
|
|
|
|
// the latest block height for querying.
|
2021-07-09 09:04:46 +00:00
|
|
|
data, err := e.doCall(args, 0)
|
2021-06-02 08:52:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-06-30 09:31:30 +00:00
|
|
|
return hexutil.Uint64(data.GasUsed), nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the block identified by hash.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getBlockByHash", "hash", hash.Hex(), "full", fullTx)
|
2021-04-18 16:39:15 +00:00
|
|
|
return e.backend.GetBlockByHash(hash, fullTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByNumber returns the block identified by number.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetBlockByNumber(ethBlockNum rpctypes.BlockNumber, fullTx bool) (map[string]interface{}, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getBlockByNumber", "number", ethBlockNum, "full", fullTx)
|
2021-04-18 16:39:15 +00:00
|
|
|
return e.backend.GetBlockByNumber(ethBlockNum, fullTx)
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:35:11 +00:00
|
|
|
// GetTxByEthHash uses `/tx_query` to find transaction by ethereum tx hash
|
|
|
|
// TODO: Don't need to convert once hashing is fixed on Tendermint
|
|
|
|
// https://github.com/tendermint/tendermint/issues/6539
|
|
|
|
func (e *PublicAPI) GetTxByEthHash(hash common.Hash) (*tmrpctypes.ResultTx, error) {
|
|
|
|
query := fmt.Sprintf("%s.%s='%s'", evmtypes.TypeMsgEthereumTx, evmtypes.AttributeKeyEthereumTxHash, hash.Hex())
|
|
|
|
resTxs, err := e.clientCtx.Client.TxSearch(e.ctx, query, false, nil, nil, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(resTxs.Txs) == 0 {
|
|
|
|
return nil, errors.Errorf("ethereum tx not found for hash %s", hash.Hex())
|
|
|
|
}
|
|
|
|
return resTxs.Txs[0], nil
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// GetTransactionByHash returns the transaction identified by hash.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransaction, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getTransactionByHash", "hash", hash.Hex())
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-06-30 09:35:11 +00:00
|
|
|
res, err := e.GetTxByEthHash(hash)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("tx not found", "hash", hash.Hex(), "error", err.Error())
|
2021-07-12 10:34:47 +00:00
|
|
|
|
|
|
|
// try to find tx in mempool
|
|
|
|
txs, err := e.backend.PendingTransactions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tx := range txs {
|
2021-07-12 17:42:53 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(tx)
|
2021-07-12 10:34:47 +00:00
|
|
|
if err != nil {
|
2021-07-12 17:42:53 +00:00
|
|
|
// not ethereum tx
|
|
|
|
continue
|
2021-07-12 10:34:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
rpctx, err := rpctypes.NewTransactionFromMsg(
|
|
|
|
msg,
|
2021-07-12 10:34:47 +00:00
|
|
|
common.Hash{},
|
|
|
|
uint64(0),
|
|
|
|
uint64(0),
|
2021-07-12 17:42:53 +00:00
|
|
|
e.chainIDEpoch,
|
2021-07-12 10:34:47 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rpctx, nil
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("block not found", "height", res.Height, "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(res.Tx)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("decoding failed", "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode tx: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(&tx)
|
2021-07-02 09:34:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("invalid tx", "error", err.Error())
|
2021-07-02 09:34:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
return rpctypes.NewTransactionFromMsg(
|
|
|
|
msg,
|
2021-06-08 11:11:37 +00:00
|
|
|
common.BytesToHash(resBlock.Block.Hash()),
|
|
|
|
uint64(res.Height),
|
|
|
|
uint64(res.Index),
|
2021-07-12 17:42:53 +00:00
|
|
|
e.chainIDEpoch,
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockHashAndIndex returns the transaction identified by hash and index.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getTransactionByBlockHashAndIndex", "hash", hash.Hex(), "index", idx)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("block not found", "hash", hash.Hex(), "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, nil
|
2021-05-31 14:54:59 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:13:15 +00:00
|
|
|
if resBlock.Block == nil {
|
|
|
|
e.logger.Debug("block not found", "hash", hash.Hex())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
i := int(idx)
|
|
|
|
if i >= len(resBlock.Block.Txs) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("block txs index out of bound", "index", i)
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
txBz := resBlock.Block.Txs[i]
|
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
|
2021-05-31 14:54:59 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("decoding failed", "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode tx: %w", err)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(&tx)
|
2021-07-05 16:39:08 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("invalid tx", "error", err.Error())
|
2021-07-12 17:42:53 +00:00
|
|
|
return nil, err
|
2021-07-05 16:39:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
return rpctypes.NewTransactionFromMsg(
|
|
|
|
msg,
|
2021-06-08 11:11:37 +00:00
|
|
|
hash,
|
|
|
|
uint64(resBlock.Block.Height),
|
|
|
|
uint64(idx),
|
2021-07-12 17:42:53 +00:00
|
|
|
e.chainIDEpoch,
|
2021-06-08 11:11:37 +00:00
|
|
|
)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockNumberAndIndex returns the transaction identified by number and index.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getTransactionByBlockNumberAndIndex", "number", blockNum, "index", idx)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, blockNum.TmHeight())
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("block not found", "height", blockNum.Int64(), "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:13:15 +00:00
|
|
|
if resBlock.Block == nil {
|
|
|
|
e.logger.Debug("block not found", "height", blockNum.Int64())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
i := int(idx)
|
|
|
|
if i >= len(resBlock.Block.Txs) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("block txs index out of bound", "index", i)
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
txBz := resBlock.Block.Txs[i]
|
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("decoding failed", "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode tx: %w", err)
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(&tx)
|
2021-07-05 16:39:08 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("invalid tx", "error", err.Error())
|
2021-07-12 17:42:53 +00:00
|
|
|
return nil, err
|
2021-07-05 16:39:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
return rpctypes.NewTransactionFromMsg(
|
|
|
|
msg,
|
2021-06-08 11:11:37 +00:00
|
|
|
common.BytesToHash(resBlock.Block.Hash()),
|
|
|
|
uint64(resBlock.Block.Height),
|
2021-04-18 16:39:15 +00:00
|
|
|
uint64(idx),
|
2021-07-12 17:42:53 +00:00
|
|
|
e.chainIDEpoch,
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionReceipt returns the transaction receipt identified by hash.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getTransactionReceipt", "hash", hash.Hex())
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-06-30 09:35:11 +00:00
|
|
|
res, err := e.GetTxByEthHash(hash)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("tx not found", "hash", hash.Hex(), "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("block not found", "height", res.Height, "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(res.Tx)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("decoding failed", "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode tx: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(&tx)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("invalid tx", "error", err.Error())
|
2021-07-12 17:42:53 +00:00
|
|
|
return nil, err
|
2021-06-08 11:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(msg.Data)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Error("failed to unpack tx data", "error", err.Error())
|
2021-07-05 16:39:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
cumulativeGasUsed := uint64(0)
|
|
|
|
blockRes, err := e.clientCtx.Client.BlockResults(e.ctx, &res.Height)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("failed to retrieve block results", "height", res.Height, "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
return nil, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
for i := 0; i <= int(res.Index) && i < len(blockRes.TxsResults); i++ {
|
|
|
|
cumulativeGasUsed += uint64(blockRes.TxsResults[i].GasUsed)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 08:14:11 +00:00
|
|
|
// Get the transaction result from the log
|
2021-04-18 16:39:15 +00:00
|
|
|
var status hexutil.Uint
|
2021-07-15 06:01:05 +00:00
|
|
|
if strings.Contains(res.TxResult.GetLog(), evmtypes.AttributeKeyEthereumTxFailed) {
|
2021-06-08 11:11:37 +00:00
|
|
|
status = hexutil.Uint(ethtypes.ReceiptStatusFailed)
|
2021-07-08 08:14:11 +00:00
|
|
|
} else {
|
|
|
|
status = hexutil.Uint(ethtypes.ReceiptStatusSuccessful)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:34:15 +00:00
|
|
|
from, err := msg.GetSender(e.chainIDEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-08 11:11:37 +00:00
|
|
|
|
|
|
|
resLogs, err := e.queryClient.TxLogs(e.ctx, &evmtypes.QueryTxLogsRequest{Hash: hash.Hex()})
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("logs not found", "hash", hash.Hex(), "error", err.Error())
|
2021-06-08 11:11:37 +00:00
|
|
|
resLogs = &evmtypes.QueryTxLogsResponse{Logs: []*evmtypes.Log{}}
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
logs := evmtypes.LogsToEthereum(resLogs.Logs)
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
receipt := map[string]interface{}{
|
|
|
|
// Consensus fields: These fields are defined by the Yellow Paper
|
|
|
|
"status": status,
|
|
|
|
"cumulativeGasUsed": hexutil.Uint64(cumulativeGasUsed),
|
2021-06-08 11:11:37 +00:00
|
|
|
"logsBloom": ethtypes.BytesToBloom(ethtypes.LogsBloom(logs)),
|
|
|
|
"logs": logs,
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
// Implementation fields: These fields are added by geth when processing a transaction.
|
|
|
|
// They are stored in the chain database.
|
2021-06-08 11:11:37 +00:00
|
|
|
"transactionHash": hash,
|
|
|
|
"contractAddress": nil,
|
|
|
|
"gasUsed": hexutil.Uint64(res.TxResult.GasUsed),
|
2021-07-05 16:39:08 +00:00
|
|
|
"type": hexutil.Uint(txData.TxType()),
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
// Inclusion information: These fields provide information about the inclusion of the
|
|
|
|
// transaction corresponding to this receipt.
|
2021-06-08 11:11:37 +00:00
|
|
|
"blockHash": common.BytesToHash(resBlock.Block.Header.Hash()).Hex(),
|
|
|
|
"blockNumber": hexutil.Uint64(res.Height),
|
|
|
|
"transactionIndex": hexutil.Uint64(res.Index),
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
// sender and receiver (contract or EOA) addreses
|
2021-06-08 11:11:37 +00:00
|
|
|
"from": from,
|
2021-07-05 16:39:08 +00:00
|
|
|
"to": txData.GetTo(),
|
2021-06-08 11:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if logs == nil {
|
|
|
|
receipt["logs"] = [][]*ethtypes.Log{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
|
2021-07-05 16:39:08 +00:00
|
|
|
if txData.GetTo() == nil {
|
|
|
|
receipt["contractAddress"] = crypto.CreateAddress(from, txData.GetNonce())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return receipt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PendingTransactions returns the transactions that are in the transaction pool
|
|
|
|
// and have a from address that is one of the accounts this node manages.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) PendingTransactions() ([]*rpctypes.RPCTransaction, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getPendingTransactions")
|
2021-07-09 08:34:49 +00:00
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
txs, err := e.backend.PendingTransactions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*rpctypes.RPCTransaction, 0, len(txs))
|
|
|
|
for _, tx := range txs {
|
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(tx)
|
|
|
|
if err != nil {
|
|
|
|
// not valid ethereum tx
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rpctx, err := rpctypes.NewTransactionFromMsg(
|
|
|
|
msg,
|
|
|
|
common.Hash{},
|
|
|
|
uint64(0),
|
|
|
|
uint64(0),
|
|
|
|
e.chainIDEpoch,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, rpctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleByBlockHashAndIndex returns the uncle identified by hash and index. Always returns nil.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetUncleByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) map[string]interface{} {
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleByBlockNumberAndIndex returns the uncle identified by number and index. Always returns nil.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetUncleByBlockNumberAndIndex(number hexutil.Uint, idx hexutil.Uint) map[string]interface{} {
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProof returns an account object with proof and any storage proofs
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) GetProof(address common.Address, storageKeys []string, blockNumber rpctypes.BlockNumber) (*rpctypes.AccountResult, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
height := blockNumber.Int64()
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("eth_getProof", "address", address.Hex(), "keys", storageKeys, "number", height)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
ctx := rpctypes.ContextWithHeight(height)
|
2021-04-18 16:39:15 +00:00
|
|
|
clientCtx := e.clientCtx.WithHeight(height)
|
|
|
|
|
|
|
|
// query storage proofs
|
2021-05-17 10:13:08 +00:00
|
|
|
storageProofs := make([]rpctypes.StorageResult, len(storageKeys))
|
2021-07-05 16:39:08 +00:00
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
for i, key := range storageKeys {
|
|
|
|
hexKey := common.HexToHash(key)
|
|
|
|
valueBz, proof, err := e.queryClient.GetProof(clientCtx, evmtypes.StoreKey, evmtypes.StateKey(address, hexKey.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for proof
|
|
|
|
var proofStr string
|
|
|
|
if proof != nil {
|
|
|
|
proofStr = proof.String()
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
storageProofs[i] = rpctypes.StorageResult{
|
2021-04-18 16:39:15 +00:00
|
|
|
Key: key,
|
|
|
|
Value: (*hexutil.Big)(new(big.Int).SetBytes(valueBz)),
|
|
|
|
Proof: []string{proofStr},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query EVM account
|
|
|
|
req := &evmtypes.QueryAccountRequest{
|
|
|
|
Address: address.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := e.queryClient.Account(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// query account proofs
|
|
|
|
accountKey := authtypes.AddressStoreKey(sdk.AccAddress(address.Bytes()))
|
|
|
|
_, proof, err := e.queryClient.GetProof(clientCtx, authtypes.StoreKey, accountKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for proof
|
|
|
|
var accProofStr string
|
|
|
|
if proof != nil {
|
|
|
|
accProofStr = proof.String()
|
|
|
|
}
|
|
|
|
|
2021-07-02 09:29:47 +00:00
|
|
|
balance, ok := sdk.NewIntFromString(res.Balance)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("invalid balance")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
return &rpctypes.AccountResult{
|
2021-04-18 16:39:15 +00:00
|
|
|
Address: address,
|
|
|
|
AccountProof: []string{accProofStr},
|
2021-07-02 09:29:47 +00:00
|
|
|
Balance: (*hexutil.Big)(balance.BigInt()),
|
2021-06-09 07:19:08 +00:00
|
|
|
CodeHash: common.HexToHash(res.CodeHash),
|
2021-04-18 16:39:15 +00:00
|
|
|
Nonce: hexutil.Uint64(res.Nonce),
|
|
|
|
StorageHash: common.Hash{}, // NOTE: Ethermint doesn't have a storage hash. TODO: implement?
|
|
|
|
StorageProof: storageProofs,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
// setTxDefaults populates tx message with default values in case they are not
|
|
|
|
// provided on the args
|
2021-06-23 06:38:05 +00:00
|
|
|
func (e *PublicAPI) setTxDefaults(args rpctypes.SendTxArgs) (rpctypes.SendTxArgs, error) {
|
2021-05-12 13:08:31 +00:00
|
|
|
|
|
|
|
if args.GasPrice == nil {
|
|
|
|
// TODO: Change to either:
|
|
|
|
// - min gas price from context once available through server/daemon, or
|
|
|
|
// - suggest a gas price based on the previous included txs
|
|
|
|
args.GasPrice = (*hexutil.Big)(big.NewInt(ethermint.DefaultGasPrice))
|
|
|
|
}
|
|
|
|
|
2021-06-21 08:02:05 +00:00
|
|
|
if args.Nonce == nil {
|
2021-05-12 13:08:31 +00:00
|
|
|
// get the nonce from the account retriever
|
|
|
|
// ignore error in case tge account doesn't exist yet
|
2021-07-09 08:34:49 +00:00
|
|
|
nonce, _ := e.getAccountNonce(args.From, true, 0, e.logger)
|
2021-05-12 13:08:31 +00:00
|
|
|
args.Nonce = (*hexutil.Uint64)(&nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) {
|
|
|
|
return args, errors.New("both 'data' and 'input' are set and not equal. Please use 'input' to pass transaction call data")
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.To == nil {
|
|
|
|
// Contract creation
|
|
|
|
var input []byte
|
|
|
|
if args.Data != nil {
|
|
|
|
input = *args.Data
|
|
|
|
} else if args.Input != nil {
|
|
|
|
input = *args.Input
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(input) == 0 {
|
|
|
|
return args, errors.New(`contract creation without any data provided`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Gas == nil {
|
|
|
|
// For backwards-compatibility reason, we try both input and data
|
|
|
|
// but input is preferred.
|
|
|
|
input := args.Input
|
|
|
|
if input == nil {
|
|
|
|
input = args.Data
|
|
|
|
}
|
|
|
|
|
2021-07-09 09:04:46 +00:00
|
|
|
callArgs := evmtypes.CallArgs{
|
2021-05-12 13:08:31 +00:00
|
|
|
From: &args.From, // From shouldn't be nil
|
|
|
|
To: args.To,
|
|
|
|
Gas: args.Gas,
|
|
|
|
GasPrice: args.GasPrice,
|
|
|
|
Value: args.Value,
|
|
|
|
Data: input,
|
|
|
|
AccessList: args.AccessList,
|
|
|
|
}
|
|
|
|
estimated, err := e.EstimateGas(callArgs)
|
|
|
|
if err != nil {
|
|
|
|
return args, err
|
|
|
|
}
|
|
|
|
args.Gas = &estimated
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("estimate gas usage automatically", "gas", args.Gas)
|
2021-05-12 13:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.ChainID == nil {
|
|
|
|
args.ChainID = (*hexutil.Big)(e.chainIDEpoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return args, nil
|
|
|
|
}
|
2021-06-30 07:30:01 +00:00
|
|
|
|
|
|
|
// getAccountNonce returns the account nonce for the given account address.
|
|
|
|
// If the pending value is true, it will iterate over the mempool (pending)
|
|
|
|
// txs in order to compute and return the pending tx sequence.
|
|
|
|
// Todo: include the ability to specify a blockNumber
|
2021-07-09 08:34:49 +00:00
|
|
|
func (e *PublicAPI) getAccountNonce(accAddr common.Address, pending bool, height int64, logger log.Logger) (uint64, error) {
|
2021-07-09 06:19:20 +00:00
|
|
|
queryClient := authtypes.NewQueryClient(e.clientCtx)
|
2021-07-09 08:34:49 +00:00
|
|
|
res, err := queryClient.Account(rpctypes.ContextWithHeight(height), &authtypes.QueryAccountRequest{Address: sdk.AccAddress(accAddr.Bytes()).String()})
|
2021-06-30 07:30:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-07-09 06:19:20 +00:00
|
|
|
var acc authtypes.AccountI
|
|
|
|
if err := e.clientCtx.InterfaceRegistry.UnpackAny(res.Account, &acc); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce := acc.GetSequence()
|
2021-06-30 07:30:01 +00:00
|
|
|
|
|
|
|
if !pending {
|
|
|
|
return nonce, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the account retriever doesn't include the uncommitted transactions on the nonce so we need to
|
|
|
|
// to manually add them.
|
2021-07-09 06:19:20 +00:00
|
|
|
pendingTxs, err := e.backend.PendingTransactions()
|
2021-06-30 07:30:01 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
logger.Error("failed to fetch pending transactions", "error", err.Error())
|
2021-06-30 07:30:01 +00:00
|
|
|
return nonce, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the uncommitted txs to the nonce counter
|
2021-07-09 08:34:49 +00:00
|
|
|
// only supports `MsgEthereumTx` style tx
|
|
|
|
for _, tx := range pendingTxs {
|
2021-07-12 17:42:53 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(tx)
|
|
|
|
if err != nil {
|
|
|
|
// not ethereum tx
|
2021-07-09 08:34:49 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-07-12 17:42:53 +00:00
|
|
|
|
2021-07-09 08:34:49 +00:00
|
|
|
sender, err := msg.GetSender(e.chainIDEpoch)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sender == accAddr {
|
|
|
|
nonce++
|
2021-06-30 07:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonce, nil
|
|
|
|
}
|