Skip to content

Commit f060c8d

Browse files
committed
add all functions to adapters to improve testability
1 parent ec729ea commit f060c8d

File tree

6 files changed

+145
-83
lines changed

6 files changed

+145
-83
lines changed

cmd/zetatool/cctx/inbound.go

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import (
88
ethcommon "github.com/ethereum/go-ethereum/common"
99
ethtypes "github.com/ethereum/go-ethereum/core/types"
1010
"github.com/gagliardetto/solana-go"
11-
solrpc "github.com/gagliardetto/solana-go/rpc"
1211
"github.com/rs/zerolog"
13-
"github.com/zeta-chain/protocol-contracts-evm/pkg/erc20custody.sol"
14-
"github.com/zeta-chain/protocol-contracts-evm/pkg/gatewayevm.sol"
15-
"github.com/zeta-chain/protocol-contracts-evm/pkg/zetaconnector.non-eth.sol"
1612

1713
zetatoolclients "github.com/zeta-chain/node/cmd/zetatool/clients"
1814
"github.com/zeta-chain/node/cmd/zetatool/context"
@@ -21,13 +17,9 @@ import (
2117
solanacontracts "github.com/zeta-chain/node/pkg/contracts/solana"
2218
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
2319
observertypes "github.com/zeta-chain/node/x/observer/types"
24-
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/client"
25-
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/common"
2620
btcobserver "github.com/zeta-chain/node/zetaclient/chains/bitcoin/observer"
2721
evmobserver "github.com/zeta-chain/node/zetaclient/chains/evm/observer"
2822
solobserver "github.com/zeta-chain/node/zetaclient/chains/solana/observer"
29-
solrepo "github.com/zeta-chain/node/zetaclient/chains/solana/repo"
30-
zetaclientConfig "github.com/zeta-chain/node/zetaclient/config"
3123
)
3224

3325
// CheckInbound checks the inbound chain,gets the inbound ballot identifier and updates the TrackingDetails
@@ -106,14 +98,7 @@ func (c *TrackingDetails) btcInboundBallotIdentifier(ctx *context.Context) error
10698
return fmt.Errorf("unable to get bitcoin net params from chain id: %w", err)
10799
}
108100

109-
connCfg := zetaclientConfig.BTCConfig{
110-
RPCUsername: cfg.BtcUser,
111-
RPCPassword: cfg.BtcPassword,
112-
RPCHost: cfg.BtcHost,
113-
RPCParams: params.Name,
114-
}
115-
116-
btcClient, err := client.New(connCfg, inboundChain.ChainId, logger)
101+
btcClient, err := zetatoolclients.NewBitcoinClientAdapter(cfg, inboundChain, logger)
117102
if err != nil {
118103
return fmt.Errorf("unable to create rpc client: %w", err)
119104
}
@@ -163,17 +148,15 @@ func (c *TrackingDetails) btcInboundBallotIdentifier(ctx *context.Context) error
163148
return fmt.Errorf("failed to get block: %w", err)
164149
}
165150

166-
// Build inbound event
167-
event, err := btcobserver.GetBtcEventWithWitness(
151+
// Build inbound event using the adapter method
152+
event, err := btcClient.GetBtcEventWithWitness(
168153
goCtx,
169-
btcClient,
170154
*tx,
171155
tssBtcAddress,
172156
uint64(blockVb.Height), // #nosec G115 always positive
173157
feeRateMultiplier,
174158
zerolog.New(zerolog.Nop()),
175159
params,
176-
common.CalcDepositorFee,
177160
)
178161
if err != nil {
179162
return fmt.Errorf("failed to build btc event: %w", err)
@@ -251,31 +234,21 @@ func (c *TrackingDetails) evmInboundBallotIdentifier(ctx *context.Context) error
251234
switch {
252235
case compareAddress(tx.To().Hex(), chainParams.ConnectorContractAddress):
253236
{
254-
addrConnector := ethcommon.HexToAddress(chainParams.ConnectorContractAddress)
255-
connector, err := zetaconnector.NewZetaConnectorNonEth(addrConnector, evmClient.Unwrap())
256-
if err != nil {
257-
return fmt.Errorf("failed to get connector contract: %w", err)
258-
}
259237
for _, log := range receipt.Logs {
260-
event, err := connector.ParseZetaSent(*log)
238+
event, err := evmClient.ParseConnectorZetaSent(*log, chainParams.ConnectorContractAddress)
261239
if err == nil && event != nil {
262240
msg = legacy.ZetaTokenVoteV1(event, inboundChain.ChainId)
263241
}
264242
}
265243
}
266244
case compareAddress(tx.To().Hex(), chainParams.Erc20CustodyContractAddress):
267245
{
268-
addrCustody := ethcommon.HexToAddress(chainParams.Erc20CustodyContractAddress)
269-
custody, err := erc20custody.NewERC20Custody(addrCustody, evmClient.Unwrap())
270-
if err != nil {
271-
return fmt.Errorf("failed to get custody contract: %w", err)
272-
}
273-
sender, err := evmClient.Unwrap().TransactionSender(goCtx, tx, receipt.BlockHash, receipt.TransactionIndex)
246+
sender, err := evmClient.TransactionSender(goCtx, tx, receipt.BlockHash, receipt.TransactionIndex)
274247
if err != nil {
275248
return fmt.Errorf("failed to get tx sender: %w", err)
276249
}
277250
for _, log := range receipt.Logs {
278-
zetaDeposited, err := custody.ParseDeposited(*log)
251+
zetaDeposited, err := evmClient.ParseCustodyDeposited(*log, chainParams.Erc20CustodyContractAddress)
279252
if err == nil && zetaDeposited != nil {
280253
msg = legacy.Erc20VoteV1(zetaDeposited, sender, inboundChain.ChainId, zetaChainID)
281254
}
@@ -286,7 +259,7 @@ func (c *TrackingDetails) evmInboundBallotIdentifier(ctx *context.Context) error
286259
if receipt.Status != ethtypes.ReceiptStatusSuccessful {
287260
return fmt.Errorf("tx failed on chain %d", inboundChain.ChainId)
288261
}
289-
sender, err := evmClient.Unwrap().TransactionSender(goCtx, tx, receipt.BlockHash, receipt.TransactionIndex)
262+
sender, err := evmClient.TransactionSender(goCtx, tx, receipt.BlockHash, receipt.TransactionIndex)
290263
if err != nil {
291264
return fmt.Errorf("failed to get tx sender: %w", err)
292265
}
@@ -295,16 +268,12 @@ func (c *TrackingDetails) evmInboundBallotIdentifier(ctx *context.Context) error
295268
default:
296269
{
297270
gatewayAddr := ethcommon.HexToAddress(chainParams.GatewayAddress)
298-
gateway, err := gatewayevm.NewGatewayEVM(gatewayAddr, evmClient.Unwrap())
299-
if err != nil {
300-
return fmt.Errorf("failed to get gateway contract: %w", err)
301-
}
302271
foundLog := false
303272
for _, log := range receipt.Logs {
304273
if log == nil || log.Address != gatewayAddr {
305274
continue
306275
}
307-
eventDeposit, err := gateway.ParseDeposited(*log)
276+
eventDeposit, err := evmClient.ParseGatewayDeposited(*log, chainParams.GatewayAddress)
308277
if err == nil {
309278
voteMsg := evmobserver.NewDepositInboundVote(
310279
eventDeposit,
@@ -318,7 +287,7 @@ func (c *TrackingDetails) evmInboundBallotIdentifier(ctx *context.Context) error
318287
foundLog = true
319288
break
320289
}
321-
eventDepositAndCall, err := gateway.ParseDepositedAndCalled(*log)
290+
eventDepositAndCall, err := evmClient.ParseGatewayDepositedAndCalled(*log, chainParams.GatewayAddress)
322291
if err == nil {
323292
voteMsg := evmobserver.NewDepositAndCallInboundVote(
324293
eventDepositAndCall,
@@ -331,7 +300,7 @@ func (c *TrackingDetails) evmInboundBallotIdentifier(ctx *context.Context) error
331300
foundLog = true
332301
break
333302
}
334-
eventCall, err := gateway.ParseCalled(*log)
303+
eventCall, err := evmClient.ParseGatewayCalled(*log, chainParams.GatewayAddress)
335304
if err == nil {
336305
voteMsg := evmobserver.NewCallInboundVote(
337306
eventCall,
@@ -365,18 +334,18 @@ func (c *TrackingDetails) solanaInboundBallotIdentifier(ctx *context.Context) er
365334
logger = ctx.GetLogger()
366335
goCtx = ctx.GetContext()
367336
)
368-
solClient := solrpc.New(cfg.SolanaRPC)
369-
if solClient == nil {
370-
return fmt.Errorf("error creating rpc client")
337+
338+
solClient, err := zetatoolclients.NewSolanaClientAdapter(cfg.SolanaRPC)
339+
if err != nil {
340+
return fmt.Errorf("error creating rpc client: %w", err)
371341
}
372-
solRepo := solrepo.New(solClient)
373342

374343
signature, err := solana.SignatureFromBase58(inboundHash)
375344
if err != nil {
376345
return fmt.Errorf("error parsing signature: %w", err)
377346
}
378347

379-
txResult, err := solRepo.GetTransaction(goCtx, signature)
348+
txResult, err := solClient.GetTransaction(goCtx, signature)
380349
if err != nil {
381350
return fmt.Errorf("error getting transaction: %w", err)
382351
}
@@ -391,9 +360,9 @@ func (c *TrackingDetails) solanaInboundBallotIdentifier(ctx *context.Context) er
391360
return fmt.Errorf("cannot parse gateway address: %s, err: %w", chainParams.GatewayAddress, err)
392361
}
393362

394-
resolvedTx := solobserver.ProcessTransactionResultWithAddressLookups(goCtx, txResult, solClient, logger, signature)
363+
resolvedTx := solClient.ProcessTransactionResultWithAddressLookups(goCtx, txResult, logger, signature)
395364

396-
events, err := solobserver.FilterInboundEvents(txResult,
365+
events, err := solClient.FilterInboundEvents(txResult,
397366
gatewayID,
398367
inboundChain.ChainId,
399368
logger,

cmd/zetatool/cctx/outbound.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ import (
55

66
"github.com/btcsuite/btcd/chaincfg/chainhash"
77
"github.com/gagliardetto/solana-go"
8-
solrpc "github.com/gagliardetto/solana-go/rpc"
98

109
zetatoolclients "github.com/zeta-chain/node/cmd/zetatool/clients"
1110
"github.com/zeta-chain/node/cmd/zetatool/context"
12-
"github.com/zeta-chain/node/pkg/chains"
13-
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/client"
14-
solrepo "github.com/zeta-chain/node/zetaclient/chains/solana/repo"
15-
zetaclientConfig "github.com/zeta-chain/node/zetaclient/config"
1611
)
1712

1813
func (c *TrackingDetails) CheckOutbound(ctx *context.Context) error {
@@ -75,16 +70,15 @@ func (c *TrackingDetails) checkSolanaOutboundTx(ctx *context.Context) error {
7570
cfg = ctx.GetConfig()
7671
)
7772

78-
foundConfirmedTx := false
79-
solClient := solrpc.New(cfg.SolanaRPC)
80-
if solClient == nil {
81-
return fmt.Errorf("error creating rpc client")
73+
solClient, err := zetatoolclients.NewSolanaClientAdapter(cfg.SolanaRPC)
74+
if err != nil {
75+
return fmt.Errorf("error creating rpc client: %w", err)
8276
}
83-
solRepo := solrepo.New(solClient)
8477

78+
foundConfirmedTx := false
8579
for _, hash := range txHashList {
8680
signature := solana.MustSignatureFromBase58(hash)
87-
_, err := solRepo.GetTransaction(goCtx, signature)
81+
_, err := solClient.GetTransaction(goCtx, signature)
8882
if err != nil {
8983
continue
9084
}
@@ -113,19 +107,7 @@ func (c *TrackingDetails) checkBitcoinOutboundTx(ctx *context.Context) error {
113107
}
114108
confirmationCount := chainParams.OutboundConfirmationSafe()
115109

116-
params, err := chains.BitcoinNetParamsFromChainID(outboundChain.ChainId)
117-
if err != nil {
118-
return fmt.Errorf("unable to get bitcoin net params from chain id: %w", err)
119-
}
120-
121-
connCfg := zetaclientConfig.BTCConfig{
122-
RPCUsername: cfg.BtcUser,
123-
RPCPassword: cfg.BtcPassword,
124-
RPCHost: cfg.BtcHost,
125-
RPCParams: params.Name,
126-
}
127-
128-
btcClient, err := client.New(connCfg, outboundChain.ChainId, logger)
110+
btcClient, err := zetatoolclients.NewBitcoinClientAdapter(cfg, outboundChain, logger)
129111
if err != nil {
130112
return fmt.Errorf("unable to create rpc client: %w", err)
131113
}

cmd/zetatool/clients/bitcoin.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"time"
99

1010
"github.com/btcsuite/btcd/btcjson"
11+
"github.com/btcsuite/btcd/chaincfg"
1112
"github.com/btcsuite/btcd/chaincfg/chainhash"
1213
"github.com/rs/zerolog"
1314

1415
"github.com/zeta-chain/node/cmd/zetatool/config"
1516
"github.com/zeta-chain/node/pkg/chains"
1617
btcclient "github.com/zeta-chain/node/zetaclient/chains/bitcoin/client"
18+
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/common"
19+
btcobserver "github.com/zeta-chain/node/zetaclient/chains/bitcoin/observer"
1720
zetaclientConfig "github.com/zeta-chain/node/zetaclient/config"
1821
)
1922

@@ -89,9 +92,27 @@ func (b *BitcoinClientAdapter) GetBlockVerbose(ctx context.Context, blockHash *c
8992
return b.client.GetBlockVerbose(ctx, blockHash)
9093
}
9194

92-
// GetRawClient returns the underlying btcclient.Client for advanced operations
93-
func (b *BitcoinClientAdapter) GetRawClient() *btcclient.Client {
94-
return b.client
95+
// GetBtcEventWithWitness builds a BTC inbound event from a transaction
96+
func (b *BitcoinClientAdapter) GetBtcEventWithWitness(
97+
ctx context.Context,
98+
tx btcjson.TxRawResult,
99+
tssAddress string,
100+
blockNumber uint64,
101+
feeRateMultiplier float64,
102+
logger zerolog.Logger,
103+
netParams *chaincfg.Params,
104+
) (*btcobserver.BTCInboundEvent, error) {
105+
return btcobserver.GetBtcEventWithWitness(
106+
ctx,
107+
b.client,
108+
tx,
109+
tssAddress,
110+
blockNumber,
111+
feeRateMultiplier,
112+
logger,
113+
netParams,
114+
common.CalcDepositorFee,
115+
)
95116
}
96117

97118
// GetBTCBalance fetches the BTC balance for a given address using mempool.space API

cmd/zetatool/clients/evm.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/ethereum/go-ethereum/ethclient"
1111
"github.com/ethereum/go-ethereum/params"
1212
ethrpc "github.com/ethereum/go-ethereum/rpc"
13+
"github.com/zeta-chain/protocol-contracts-evm/pkg/erc20custody.sol"
14+
"github.com/zeta-chain/protocol-contracts-evm/pkg/gatewayevm.sol"
15+
"github.com/zeta-chain/protocol-contracts-evm/pkg/zetaconnector.non-eth.sol"
1316

1417
"github.com/zeta-chain/node/cmd/zetatool/config"
1518
"github.com/zeta-chain/node/pkg/chains"
@@ -53,9 +56,54 @@ func (a *EVMClientAdapter) BlockNumber(ctx context.Context) (uint64, error) {
5356
return a.client.BlockNumber(ctx)
5457
}
5558

56-
// Unwrap returns the underlying ethclient.Client for use with contract bindings
57-
func (a *EVMClientAdapter) Unwrap() *ethclient.Client {
58-
return a.client
59+
// TransactionSender returns the sender of a transaction
60+
func (a *EVMClientAdapter) TransactionSender(ctx context.Context, tx *ethtypes.Transaction, blockHash ethcommon.Hash, txIndex uint) (ethcommon.Address, error) {
61+
return a.client.TransactionSender(ctx, tx, blockHash, txIndex)
62+
}
63+
64+
// ParseConnectorZetaSent parses a ZetaSent event from the connector contract
65+
func (a *EVMClientAdapter) ParseConnectorZetaSent(log ethtypes.Log, connectorAddr string) (*zetaconnector.ZetaConnectorNonEthZetaSent, error) {
66+
connector, err := zetaconnector.NewZetaConnectorNonEth(ethcommon.HexToAddress(connectorAddr), a.client)
67+
if err != nil {
68+
return nil, fmt.Errorf("failed to create connector contract: %w", err)
69+
}
70+
return connector.ParseZetaSent(log)
71+
}
72+
73+
// ParseCustodyDeposited parses a Deposited event from the custody contract
74+
func (a *EVMClientAdapter) ParseCustodyDeposited(log ethtypes.Log, custodyAddr string) (*erc20custody.ERC20CustodyDeposited, error) {
75+
custody, err := erc20custody.NewERC20Custody(ethcommon.HexToAddress(custodyAddr), a.client)
76+
if err != nil {
77+
return nil, fmt.Errorf("failed to create custody contract: %w", err)
78+
}
79+
return custody.ParseDeposited(log)
80+
}
81+
82+
// ParseGatewayDeposited parses a Deposited event from the gateway contract
83+
func (a *EVMClientAdapter) ParseGatewayDeposited(log ethtypes.Log, gatewayAddr string) (*gatewayevm.GatewayEVMDeposited, error) {
84+
gateway, err := gatewayevm.NewGatewayEVM(ethcommon.HexToAddress(gatewayAddr), a.client)
85+
if err != nil {
86+
return nil, fmt.Errorf("failed to create gateway contract: %w", err)
87+
}
88+
return gateway.ParseDeposited(log)
89+
}
90+
91+
// ParseGatewayDepositedAndCalled parses a DepositedAndCalled event from the gateway contract
92+
func (a *EVMClientAdapter) ParseGatewayDepositedAndCalled(log ethtypes.Log, gatewayAddr string) (*gatewayevm.GatewayEVMDepositedAndCalled, error) {
93+
gateway, err := gatewayevm.NewGatewayEVM(ethcommon.HexToAddress(gatewayAddr), a.client)
94+
if err != nil {
95+
return nil, fmt.Errorf("failed to create gateway contract: %w", err)
96+
}
97+
return gateway.ParseDepositedAndCalled(log)
98+
}
99+
100+
// ParseGatewayCalled parses a Called event from the gateway contract
101+
func (a *EVMClientAdapter) ParseGatewayCalled(log ethtypes.Log, gatewayAddr string) (*gatewayevm.GatewayEVMCalled, error) {
102+
gateway, err := gatewayevm.NewGatewayEVM(ethcommon.HexToAddress(gatewayAddr), a.client)
103+
if err != nil {
104+
return nil, fmt.Errorf("failed to create gateway contract: %w", err)
105+
}
106+
return gateway.ParseCalled(log)
59107
}
60108

61109
// ResolveEVMRPC returns the RPC URL for a given EVM chain

0 commit comments

Comments
 (0)