Skip to content

Commit

Permalink
move client interface to ethereum package
Browse files Browse the repository at this point in the history
Signed-off-by: Daisuke Kanda <[email protected]>
  • Loading branch information
Daisuke Kanda committed Feb 4, 2025
1 parent 6621802 commit 3213c9c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 67 deletions.
50 changes: 0 additions & 50 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client/txpool"
)
/*
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct {
BlockHash *common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"`
From common.Address `json:"from"`
Gas hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"`
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
Hash common.Hash `json:"hash"`
Input hexutil.Bytes `json:"input"`
Nonce hexutil.Uint64 `json:"nonce"`
To *common.Address `json:"to"`
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
Value *hexutil.Big `json:"value"`
Type hexutil.Uint64 `json:"type"`
Accesses *gethtypes.AccessList `json:"accessList,omitempty"`
ChainID *hexutil.Big `json:"chainId,omitempty"`
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
V *hexutil.Big `json:"v"`
R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"`
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
}
*/
// wrapping interface of ethclient.Client struct
type IETHClient interface {
Inner() *ethclient.Client
SuggestGasPrice(ctx context.Context) (*big.Int, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error);
FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error)
GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error);
}

type ETHClient struct {
*ethclient.Client
Expand Down Expand Up @@ -101,20 +65,6 @@ func (cl *ETHClient) Raw() *rpc.Client {
return cl.Client.Client()
}

// implements IETHClient
func (cl *ETHClient) Inner() *ethclient.Client {
return cl.Client
}
func (cl *ETHClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return cl.Client.SuggestGasPrice(ctx)
}
func (cl *ETHClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
return cl.Client.HeaderByNumber(ctx, number)
}
func (cl *ETHClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
return cl.Client.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}

func (cl *ETHClient) GetTransactionReceipt(ctx context.Context, txHash common.Hash) (rc *Receipt, err error) {
var r *Receipt

Expand Down
6 changes: 3 additions & 3 deletions pkg/relay/ethereum/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Chain struct {
codec codec.ProtoCodecMarshaler
msgEventListener core.MsgEventListener

client *client.ETHClient
client *ChainClient // TODO: use IChainClient after defining all methods in the interface
ibcHandler *ibchandler.Ibchandler
multicall3 *multicall3.Multicall3

Expand Down Expand Up @@ -117,7 +117,7 @@ func NewChain(config ChainConfig) (*Chain, error) {

return &Chain{
config: config,
client: client,
client: &ChainClient{ ETHClient: client },
chainID: id,

ibcHandler: ibcHandler,
Expand Down Expand Up @@ -192,7 +192,7 @@ func (c *Chain) Codec() codec.ProtoCodecMarshaler {

// Client returns the RPC client for ethereum
func (c *Chain) Client() *client.ETHClient {
return c.client
return c.client.ETHClient
}

// SetRelayInfo sets source's path and counterparty's info to the chain
Expand Down
31 changes: 31 additions & 0 deletions pkg/relay/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import (
"context"
"math/big"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"

"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client"
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client/txpool"
)

func (chain *Chain) CallOpts(ctx context.Context, height int64) *bind.CallOpts {
Expand Down Expand Up @@ -40,3 +46,28 @@ func (chain *Chain) TxOpts(ctx context.Context, useLatestNonce bool) (*bind.Tran

return txOpts, nil
}

// wrapping interface of client.ETHClient struct
type IChainClient interface {
SuggestGasPrice(ctx context.Context) (*big.Int, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error);
FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error)
GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error);
}

type ChainClient struct {
*client.ETHClient
}

// implements IETHChainClient
func (cl *ChainClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return cl.ETHClient.Client.SuggestGasPrice(ctx)
}
func (cl *ChainClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
return cl.ETHClient.Client.HeaderByNumber(ctx, number)
}
func (cl *ChainClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
return cl.ETHClient.Client.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}


5 changes: 2 additions & 3 deletions pkg/relay/ethereum/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"fmt"
"math/big"

"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client"
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client/txpool"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)

type GasFeeCalculator struct {
client client.IETHClient
client IChainClient
config *ChainConfig
}

func NewGasFeeCalculator(client client.IETHClient, config *ChainConfig) *GasFeeCalculator {
func NewGasFeeCalculator(client IChainClient, config *ChainConfig) *GasFeeCalculator {
return &GasFeeCalculator{
client: client,
config: config,
Expand Down
22 changes: 11 additions & 11 deletions pkg/relay/ethereum/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_TxOpts_LegacyTx(t *testing.T) {
}
config := createConfig()
config.TxType = "legacy"
calculator := NewGasFeeCalculator(ethClient, config)
calculator := NewGasFeeCalculator(&ChainClient{ ETHClient: ethClient }, config)
txOpts := &bind.TransactOpts{}
if err = calculator.Apply(context.Background(), txOpts); err != nil {
t.Fatal(err)
Expand All @@ -43,7 +43,7 @@ func Test_TxOpts_DynamicTx(t *testing.T) {
t.Fatal(err)
}
config := createConfig()
calculator := NewGasFeeCalculator(ethClient, config)
calculator := NewGasFeeCalculator(&ChainClient{ ETHClient: ethClient }, config)
txOpts := &bind.TransactOpts{}
if err = calculator.Apply(context.Background(), txOpts); err != nil {
t.Fatal(err)
Expand All @@ -66,7 +66,7 @@ func Test_TxOpts_AutoTx(t *testing.T) {
}
config := createConfig()
config.TxType = "auto"
calculator := NewGasFeeCalculator(ethClient, config)
calculator := NewGasFeeCalculator(&ChainClient{ ETHClient: ethClient }, config)
txOpts := &bind.TransactOpts{}
if err = calculator.Apply(context.Background(), txOpts); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -135,15 +135,15 @@ func Test_getFeeInfo(t *testing.T) {

}

type MockETHClient struct {
client.IETHClient
type MockChainClient struct {
IChainClient
MockSuggestGasPrice big.Int
MockPendingTransaction *txpool.RPCTransaction
MockLatestHeaderNumber big.Int
MockHistoryGasTipCap big.Int
MockHistoryGasFeeCap big.Int
}
func (cl *MockETHClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
func (cl *MockChainClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return &cl.MockSuggestGasPrice, nil
}

Expand All @@ -152,7 +152,7 @@ func inclByPercent(n *big.Int, percent uint64) {
n.Div(n, big.NewInt(100))
}

func (cl *MockETHClient) GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error) {
func (cl *MockChainClient) GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error) {
gasFeeCap := new(big.Int).Set(cl.MockPendingTransaction.GasFeeCap.ToInt())
gasTipCap := new(big.Int).Set(cl.MockPendingTransaction.GasTipCap.ToInt())

Expand All @@ -162,7 +162,7 @@ func (cl *MockETHClient) GetMinimumRequiredFee(ctx context.Context, address comm
return cl.MockPendingTransaction, gasFeeCap, gasTipCap, nil
}

func (cl *MockETHClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
func (cl *MockChainClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
if number != nil {
return &gethtypes.Header{
Number: big.NewInt(0).Set(number),
Expand All @@ -173,7 +173,7 @@ func (cl *MockETHClient) HeaderByNumber(ctx context.Context, number *big.Int) (*
}, nil
}
}
func (cl *MockETHClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
func (cl *MockChainClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
return &ethereum.FeeHistory{
Reward: [][]*big.Int{ // gasTipCap
{ &cl.MockHistoryGasTipCap, },
Expand All @@ -185,7 +185,7 @@ func (cl *MockETHClient) FeeHistory(ctx context.Context, blockCount uint64, last
}

func TestPriceBumpLegacy(t *testing.T) {
cli := MockETHClient{}
cli := MockChainClient{}
config := createConfig()
config.TxType = TxTypeLegacy
config.PriceBump = 10
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestPriceBumpLegacy(t *testing.T) {
}

func TestPriceBumpDynamic(t *testing.T) {
cli := MockETHClient{}
cli := MockChainClient{}
config := createConfig()
config.TxType = TxTypeDynamic
config.PriceBump = 100 //double
Expand Down

0 comments on commit 3213c9c

Please sign in to comment.