package evm

import (
	"context"
	"math/big"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/explorer/backend/libs/go-chain-adapters/adapters"
)

type EVMAdapter struct {
	client  *ethclient.Client
	chainID int64
}

func NewEVMAdapter(client *ethclient.Client, chainID int64) *EVMAdapter {
	return &EVMAdapter{client: client, chainID: chainID}
}

func (e *EVMAdapter) GetBlockByNumber(ctx context.Context, number int64) (*types.Block, error) {
	return e.client.BlockByNumber(ctx, big.NewInt(number))
}
func (e *EVMAdapter) GetTransaction(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error) {
	return e.client.TransactionByHash(ctx, hash)
}
func (e *EVMAdapter) GetTransactionReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error) {
	return e.client.TransactionReceipt(ctx, hash)
}
func (e *EVMAdapter) GetCode(ctx context.Context, address common.Address) ([]byte, error) {
	return e.client.CodeAt(ctx, address, nil)
}
func (e *EVMAdapter) GetBalance(ctx context.Context, address common.Address) (*big.Int, error) {
	return e.client.BalanceAt(ctx, address, nil)
}
func (e *EVMAdapter) GetGasPrice(ctx context.Context) (*big.Int, error) {
	return e.client.SuggestGasPrice(ctx)
}
func (e *EVMAdapter) ChainID() int64 { return e.chainID }

var _ adapters.ChainAdapter = (*EVMAdapter)(nil)
