Files

72 lines
2.2 KiB
Go

package adapters
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// EVMAdapter implements ChainAdapter for EVM-compatible chains
type EVMAdapter struct {
client *ethclient.Client
chainID int64
}
// NewEVMAdapter creates a new EVM chain adapter
func NewEVMAdapter(client *ethclient.Client, chainID int64) *EVMAdapter {
return &EVMAdapter{
client: client,
chainID: chainID,
}
}
// ChainAdapter defines the interface for chain adapters
type ChainAdapter interface {
GetBlockByNumber(ctx context.Context, number int64) (*types.Block, error)
GetTransaction(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error)
GetTransactionReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error)
GetCode(ctx context.Context, address common.Address) ([]byte, error)
GetBalance(ctx context.Context, address common.Address) (*big.Int, error)
GetGasPrice(ctx context.Context) (*big.Int, error)
ChainID() int64
}
// GetBlockByNumber gets a block by number
func (e *EVMAdapter) GetBlockByNumber(ctx context.Context, number int64) (*types.Block, error) {
return e.client.BlockByNumber(ctx, big.NewInt(number))
}
// GetTransaction gets a transaction by hash
func (e *EVMAdapter) GetTransaction(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error) {
return e.client.TransactionByHash(ctx, hash)
}
// GetTransactionReceipt gets a transaction receipt
func (e *EVMAdapter) GetTransactionReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error) {
return e.client.TransactionReceipt(ctx, hash)
}
// GetCode gets contract code
func (e *EVMAdapter) GetCode(ctx context.Context, address common.Address) ([]byte, error) {
return e.client.CodeAt(ctx, address, nil)
}
// GetBalance gets account balance
func (e *EVMAdapter) GetBalance(ctx context.Context, address common.Address) (*big.Int, error) {
return e.client.BalanceAt(ctx, address, nil)
}
// GetGasPrice gets current gas price
func (e *EVMAdapter) GetGasPrice(ctx context.Context) (*big.Int, error) {
return e.client.SuggestGasPrice(ctx)
}
// ChainID returns the chain ID
func (e *EVMAdapter) ChainID() int64 {
return e.chainID
}