2022-05-02 06:26:24 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/rpc/types"
|
|
|
|
"github.com/evmos/ethermint/server/config"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2022-05-02 06:26:24 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BackendI implements the Cosmos and EVM backend.
|
2022-08-08 08:17:10 +00:00
|
|
|
type BackendI interface { //nolint: revive
|
2022-05-02 06:26:24 +00:00
|
|
|
CosmosBackend
|
|
|
|
EVMBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
// CosmosBackend implements the functionality shared within cosmos namespaces
|
|
|
|
// as defined by Wallet Connect V2: https://docs.walletconnect.com/2.0/json-rpc/cosmos.
|
|
|
|
// Implemented by Backend.
|
2022-06-05 09:22:33 +00:00
|
|
|
type CosmosBackend interface { // TODO: define
|
2022-05-02 06:26:24 +00:00
|
|
|
// GetAccounts()
|
|
|
|
// SignDirect()
|
|
|
|
// SignAmino()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EVMBackend implements the functionality shared within ethereum namespaces
|
|
|
|
// as defined by EIP-1474: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md
|
|
|
|
// Implemented by Backend.
|
|
|
|
type EVMBackend interface {
|
|
|
|
// General Ethereum API
|
|
|
|
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
|
|
|
|
RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
|
|
|
|
RPCTxFeeCap() float64 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for send-transaction variants. The unit is ether.
|
2022-06-22 10:50:36 +00:00
|
|
|
UnprotectedAllowed() bool
|
2022-05-02 06:26:24 +00:00
|
|
|
|
|
|
|
RPCMinGasPrice() int64
|
|
|
|
SuggestGasTipCap(baseFee *big.Int) (*big.Int, error)
|
|
|
|
|
|
|
|
// Blockchain API
|
|
|
|
BlockNumber() (hexutil.Uint64, error)
|
2022-07-29 10:35:07 +00:00
|
|
|
GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (map[string]interface{}, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
GetTendermintBlockByNumber(blockNum types.BlockNumber) (*tmrpctypes.ResultBlock, error)
|
2022-06-22 11:26:20 +00:00
|
|
|
GetTendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
GetTendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
|
|
|
|
GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error)
|
|
|
|
BlockByNumber(blockNum types.BlockNumber) (*ethtypes.Block, error)
|
|
|
|
BlockByHash(blockHash common.Hash) (*ethtypes.Block, error)
|
|
|
|
CurrentHeader() *ethtypes.Header
|
|
|
|
HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error)
|
|
|
|
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
|
2022-07-11 15:04:45 +00:00
|
|
|
GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
PendingTransactions() ([]*sdk.Tx, error)
|
|
|
|
GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error)
|
|
|
|
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
|
|
|
|
GetCoinbase() (sdk.AccAddress, error)
|
|
|
|
GetTransactionByHash(txHash common.Hash) (*types.RPCTransaction, error)
|
|
|
|
GetTxByEthHash(txHash common.Hash) (*tmrpctypes.ResultTx, error)
|
|
|
|
GetTxByTxIndex(height int64, txIndex uint) (*tmrpctypes.ResultTx, error)
|
|
|
|
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *types.BlockNumber) (hexutil.Uint64, error)
|
2022-06-22 11:26:20 +00:00
|
|
|
BaseFee(blockRes *tmrpctypes.ResultBlockResults) (*big.Int, error)
|
2022-06-03 13:31:57 +00:00
|
|
|
GlobalMinGasPrice() (sdk.Dec, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
|
|
|
|
// Fee API
|
|
|
|
FeeHistory(blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*types.FeeHistoryResult, error)
|
|
|
|
|
|
|
|
// Filter API
|
|
|
|
BloomStatus() (uint64, uint64)
|
|
|
|
GetLogs(hash common.Hash) ([][]*ethtypes.Log, error)
|
|
|
|
GetLogsByHeight(height *int64) ([][]*ethtypes.Log, error)
|
|
|
|
ChainConfig() *params.ChainConfig
|
|
|
|
SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.TransactionArgs, error)
|
|
|
|
GetEthereumMsgsFromTendermintBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evmtypes.MsgEthereumTx
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ BackendI = (*Backend)(nil)
|
|
|
|
|
|
|
|
// Backend implements the BackendI interface
|
|
|
|
type Backend struct {
|
2022-06-22 10:50:36 +00:00
|
|
|
ctx context.Context
|
|
|
|
clientCtx client.Context
|
|
|
|
queryClient *types.QueryClient // gRPC query client
|
|
|
|
logger log.Logger
|
|
|
|
chainID *big.Int
|
|
|
|
cfg config.Config
|
|
|
|
allowUnprotectedTxs bool
|
2022-05-02 06:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackend creates a new Backend instance for cosmos and ethereum namespaces
|
2022-06-22 10:50:36 +00:00
|
|
|
func NewBackend(ctx *server.Context, logger log.Logger, clientCtx client.Context, allowUnprotectedTxs bool) *Backend {
|
2022-05-02 06:26:24 +00:00
|
|
|
chainID, err := ethermint.ParseChainID(clientCtx.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
appConf := config.GetConfig(ctx.Viper)
|
|
|
|
|
|
|
|
return &Backend{
|
2022-06-22 10:50:36 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
clientCtx: clientCtx,
|
|
|
|
queryClient: types.NewQueryClient(clientCtx),
|
|
|
|
logger: logger.With("module", "backend"),
|
|
|
|
chainID: chainID,
|
|
|
|
cfg: appConf,
|
|
|
|
allowUnprotectedTxs: allowUnprotectedTxs,
|
2022-05-02 06:26:24 +00:00
|
|
|
}
|
|
|
|
}
|