Skip to content

Commit 3213c9c

Browse files
author
Daisuke Kanda
committed
move client interface to ethereum package
Signed-off-by: Daisuke Kanda <[email protected]>
1 parent 6621802 commit 3213c9c

File tree

5 files changed

+47
-67
lines changed

5 files changed

+47
-67
lines changed

pkg/client/client.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,7 @@ import (
1414
"github.com/ethereum/go-ethereum/core/vm"
1515
"github.com/ethereum/go-ethereum/ethclient"
1616
"github.com/ethereum/go-ethereum/rpc"
17-
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client/txpool"
1817
)
19-
/*
20-
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
21-
type RPCTransaction struct {
22-
BlockHash *common.Hash `json:"blockHash"`
23-
BlockNumber *hexutil.Big `json:"blockNumber"`
24-
From common.Address `json:"from"`
25-
Gas hexutil.Uint64 `json:"gas"`
26-
GasPrice *hexutil.Big `json:"gasPrice"`
27-
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
28-
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
29-
MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
30-
Hash common.Hash `json:"hash"`
31-
Input hexutil.Bytes `json:"input"`
32-
Nonce hexutil.Uint64 `json:"nonce"`
33-
To *common.Address `json:"to"`
34-
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
35-
Value *hexutil.Big `json:"value"`
36-
Type hexutil.Uint64 `json:"type"`
37-
Accesses *gethtypes.AccessList `json:"accessList,omitempty"`
38-
ChainID *hexutil.Big `json:"chainId,omitempty"`
39-
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
40-
V *hexutil.Big `json:"v"`
41-
R *hexutil.Big `json:"r"`
42-
S *hexutil.Big `json:"s"`
43-
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
44-
}
45-
*/
46-
// wrapping interface of ethclient.Client struct
47-
type IETHClient interface {
48-
Inner() *ethclient.Client
49-
SuggestGasPrice(ctx context.Context) (*big.Int, error)
50-
HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error);
51-
FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error)
52-
GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error);
53-
}
5418

5519
type ETHClient struct {
5620
*ethclient.Client
@@ -101,20 +65,6 @@ func (cl *ETHClient) Raw() *rpc.Client {
10165
return cl.Client.Client()
10266
}
10367

104-
// implements IETHClient
105-
func (cl *ETHClient) Inner() *ethclient.Client {
106-
return cl.Client
107-
}
108-
func (cl *ETHClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
109-
return cl.Client.SuggestGasPrice(ctx)
110-
}
111-
func (cl *ETHClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
112-
return cl.Client.HeaderByNumber(ctx, number)
113-
}
114-
func (cl *ETHClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
115-
return cl.Client.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
116-
}
117-
11868
func (cl *ETHClient) GetTransactionReceipt(ctx context.Context, txHash common.Hash) (rc *Receipt, err error) {
11969
var r *Receipt
12070

pkg/relay/ethereum/chain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Chain struct {
4343
codec codec.ProtoCodecMarshaler
4444
msgEventListener core.MsgEventListener
4545

46-
client *client.ETHClient
46+
client *ChainClient // TODO: use IChainClient after defining all methods in the interface
4747
ibcHandler *ibchandler.Ibchandler
4848
multicall3 *multicall3.Multicall3
4949

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

118118
return &Chain{
119119
config: config,
120-
client: client,
120+
client: &ChainClient{ ETHClient: client },
121121
chainID: id,
122122

123123
ibcHandler: ibcHandler,
@@ -192,7 +192,7 @@ func (c *Chain) Codec() codec.ProtoCodecMarshaler {
192192

193193
// Client returns the RPC client for ethereum
194194
func (c *Chain) Client() *client.ETHClient {
195-
return c.client
195+
return c.client.ETHClient
196196
}
197197

198198
// SetRelayInfo sets source's path and counterparty's info to the chain

pkg/relay/ethereum/client.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import (
44
"context"
55
"math/big"
66

7+
"github.com/ethereum/go-ethereum"
8+
"github.com/ethereum/go-ethereum/common"
9+
gethtypes "github.com/ethereum/go-ethereum/core/types"
710
"github.com/ethereum/go-ethereum/accounts/abi/bind"
11+
12+
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client"
13+
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client/txpool"
814
)
915

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

4147
return txOpts, nil
4248
}
49+
50+
// wrapping interface of client.ETHClient struct
51+
type IChainClient interface {
52+
SuggestGasPrice(ctx context.Context) (*big.Int, error)
53+
HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error);
54+
FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error)
55+
GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error);
56+
}
57+
58+
type ChainClient struct {
59+
*client.ETHClient
60+
}
61+
62+
// implements IETHChainClient
63+
func (cl *ChainClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
64+
return cl.ETHClient.Client.SuggestGasPrice(ctx)
65+
}
66+
func (cl *ChainClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
67+
return cl.ETHClient.Client.HeaderByNumber(ctx, number)
68+
}
69+
func (cl *ChainClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
70+
return cl.ETHClient.Client.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
71+
}
72+
73+

pkg/relay/ethereum/gas.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import (
55
"fmt"
66
"math/big"
77

8-
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client"
98
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/client/txpool"
109
"github.com/ethereum/go-ethereum"
1110
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1211
"github.com/ethereum/go-ethereum/common"
1312
)
1413

1514
type GasFeeCalculator struct {
16-
client client.IETHClient
15+
client IChainClient
1716
config *ChainConfig
1817
}
1918

20-
func NewGasFeeCalculator(client client.IETHClient, config *ChainConfig) *GasFeeCalculator {
19+
func NewGasFeeCalculator(client IChainClient, config *ChainConfig) *GasFeeCalculator {
2120
return &GasFeeCalculator{
2221
client: client,
2322
config: config,

pkg/relay/ethereum/gas_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Test_TxOpts_LegacyTx(t *testing.T) {
2121
}
2222
config := createConfig()
2323
config.TxType = "legacy"
24-
calculator := NewGasFeeCalculator(ethClient, config)
24+
calculator := NewGasFeeCalculator(&ChainClient{ ETHClient: ethClient }, config)
2525
txOpts := &bind.TransactOpts{}
2626
if err = calculator.Apply(context.Background(), txOpts); err != nil {
2727
t.Fatal(err)
@@ -43,7 +43,7 @@ func Test_TxOpts_DynamicTx(t *testing.T) {
4343
t.Fatal(err)
4444
}
4545
config := createConfig()
46-
calculator := NewGasFeeCalculator(ethClient, config)
46+
calculator := NewGasFeeCalculator(&ChainClient{ ETHClient: ethClient }, config)
4747
txOpts := &bind.TransactOpts{}
4848
if err = calculator.Apply(context.Background(), txOpts); err != nil {
4949
t.Fatal(err)
@@ -66,7 +66,7 @@ func Test_TxOpts_AutoTx(t *testing.T) {
6666
}
6767
config := createConfig()
6868
config.TxType = "auto"
69-
calculator := NewGasFeeCalculator(ethClient, config)
69+
calculator := NewGasFeeCalculator(&ChainClient{ ETHClient: ethClient }, config)
7070
txOpts := &bind.TransactOpts{}
7171
if err = calculator.Apply(context.Background(), txOpts); err != nil {
7272
t.Fatal(err)
@@ -135,15 +135,15 @@ func Test_getFeeInfo(t *testing.T) {
135135

136136
}
137137

138-
type MockETHClient struct {
139-
client.IETHClient
138+
type MockChainClient struct {
139+
IChainClient
140140
MockSuggestGasPrice big.Int
141141
MockPendingTransaction *txpool.RPCTransaction
142142
MockLatestHeaderNumber big.Int
143143
MockHistoryGasTipCap big.Int
144144
MockHistoryGasFeeCap big.Int
145145
}
146-
func (cl *MockETHClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
146+
func (cl *MockChainClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
147147
return &cl.MockSuggestGasPrice, nil
148148
}
149149

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

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

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

165-
func (cl *MockETHClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
165+
func (cl *MockChainClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
166166
if number != nil {
167167
return &gethtypes.Header{
168168
Number: big.NewInt(0).Set(number),
@@ -173,7 +173,7 @@ func (cl *MockETHClient) HeaderByNumber(ctx context.Context, number *big.Int) (*
173173
}, nil
174174
}
175175
}
176-
func (cl *MockETHClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
176+
func (cl *MockChainClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
177177
return &ethereum.FeeHistory{
178178
Reward: [][]*big.Int{ // gasTipCap
179179
{ &cl.MockHistoryGasTipCap, },
@@ -185,7 +185,7 @@ func (cl *MockETHClient) FeeHistory(ctx context.Context, blockCount uint64, last
185185
}
186186

187187
func TestPriceBumpLegacy(t *testing.T) {
188-
cli := MockETHClient{}
188+
cli := MockChainClient{}
189189
config := createConfig()
190190
config.TxType = TxTypeLegacy
191191
config.PriceBump = 10
@@ -223,7 +223,7 @@ func TestPriceBumpLegacy(t *testing.T) {
223223
}
224224

225225
func TestPriceBumpDynamic(t *testing.T) {
226-
cli := MockETHClient{}
226+
cli := MockChainClient{}
227227
config := createConfig()
228228
config.TxType = TxTypeDynamic
229229
config.PriceBump = 100 //double

0 commit comments

Comments
 (0)