Skip to content

Commit 1cf6656

Browse files
committed
fix: remove duplicate code
1 parent 0b0866e commit 1cf6656

File tree

12 files changed

+97
-231
lines changed

12 files changed

+97
-231
lines changed

cmd/ulxly/bridge/asset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func bridgeAsset(cmd *cobra.Command, _ []string) error {
8686

8787
// Approve the bridge contract to spend the tokens on behalf of the user
8888
approveTxn, iErr := tokenContract.Approve(auth, bridgeAddress, value)
89-
if iErr = logAndReturnJSONError(cmd.Context(), client, approveTxn, auth, iErr); iErr != nil {
89+
if iErr = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, approveTxn, auth, iErr); iErr != nil {
9090
return iErr
9191
}
9292
log.Info().Msg("approveTxn: " + approveTxn.Hash().String())
@@ -97,7 +97,7 @@ func bridgeAsset(cmd *cobra.Command, _ []string) error {
9797
}
9898

9999
bridgeTxn, err := bridgeV2.BridgeAsset(auth, destinationNetwork, toAddress, value, tokenAddress, isForced, callData)
100-
if err = logAndReturnJSONError(cmd.Context(), client, bridgeTxn, auth, err); err != nil {
100+
if err = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, bridgeTxn, auth, err); err != nil {
101101
log.Info().Err(err).Str("calldata", callDataString).Msg("Bridge transaction failed")
102102
return err
103103
}

cmd/ulxly/bridge/message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func bridgeMessage(cmd *cobra.Command, _ []string) error {
6262
}
6363

6464
bridgeTxn, err := bridgeV2.BridgeMessage(auth, destinationNetwork, toAddress, isForced, callData)
65-
if err = logAndReturnJSONError(cmd.Context(), client, bridgeTxn, auth, err); err != nil {
65+
if err = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, bridgeTxn, auth, err); err != nil {
6666
log.Info().Err(err).Str("calldata", callDataString).Msg("Bridge transaction failed")
6767
return err
6868
}

cmd/ulxly/bridge/shared.go

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,15 @@ package bridge
22

33
import (
44
"context"
5-
"encoding/json"
6-
"errors"
75
"fmt"
86

97
"github.com/0xPolygon/polygon-cli/bindings/ulxly"
10-
ulxlycommon "github.com/0xPolygon/polygon-cli/cmd/ulxly/common"
11-
smcerror "github.com/0xPolygon/polygon-cli/errors"
12-
"github.com/ethereum/go-ethereum"
13-
"github.com/ethereum/go-ethereum/accounts/abi/bind"
148
"github.com/ethereum/go-ethereum/common"
159
"github.com/ethereum/go-ethereum/core/types"
1610
"github.com/ethereum/go-ethereum/ethclient"
1711
"github.com/rs/zerolog/log"
1812
)
1913

20-
// logAndReturnJSONError logs and returns a JSON-RPC error with additional context.
21-
func logAndReturnJSONError(ctx context.Context, client *ethclient.Client, tx *types.Transaction, opts *bind.TransactOpts, err error) error {
22-
var callErr error
23-
if tx != nil {
24-
// in case the error came down to gas estimation, we can sometimes get more information by doing a call
25-
_, callErr = client.CallContract(ctx, ethereum.CallMsg{
26-
From: opts.From,
27-
To: tx.To(),
28-
Gas: tx.Gas(),
29-
GasPrice: tx.GasPrice(),
30-
GasFeeCap: tx.GasFeeCap(),
31-
GasTipCap: tx.GasTipCap(),
32-
Value: tx.Value(),
33-
Data: tx.Data(),
34-
AccessList: tx.AccessList(),
35-
BlobGasFeeCap: tx.BlobGasFeeCap(),
36-
BlobHashes: tx.BlobHashes(),
37-
}, nil)
38-
39-
if ulxlycommon.InputArgs.DryRun {
40-
castCmd := "cast call"
41-
castCmd += fmt.Sprintf(" --rpc-url %s", ulxlycommon.InputArgs.RPCURL)
42-
castCmd += fmt.Sprintf(" --from %s", opts.From.String())
43-
castCmd += fmt.Sprintf(" --gas-limit %d", tx.Gas())
44-
if tx.Type() == types.LegacyTxType {
45-
castCmd += fmt.Sprintf(" --gas-price %s", tx.GasPrice().String())
46-
} else {
47-
castCmd += fmt.Sprintf(" --gas-price %s", tx.GasFeeCap().String())
48-
castCmd += fmt.Sprintf(" --priority-gas-price %s", tx.GasTipCap().String())
49-
}
50-
castCmd += fmt.Sprintf(" --value %s", tx.Value().String())
51-
castCmd += fmt.Sprintf(" %s", tx.To().String())
52-
castCmd += fmt.Sprintf(" %s", common.Bytes2Hex(tx.Data()))
53-
log.Info().Str("cmd", castCmd).Msg("use this command to replicate the call")
54-
}
55-
}
56-
57-
if err == nil {
58-
return nil
59-
}
60-
61-
var jsonError ulxlycommon.JSONError
62-
jsonErrorBytes, jsErr := json.Marshal(err)
63-
if jsErr != nil {
64-
log.Error().Err(err).Msg("Unable to interact with the bridge contract")
65-
return err
66-
}
67-
68-
jsErr = json.Unmarshal(jsonErrorBytes, &jsonError)
69-
if jsErr != nil {
70-
log.Error().Err(err).Msg("Unable to interact with the bridge contract")
71-
return err
72-
}
73-
74-
reason, decodeErr := smcerror.DecodeSmcErrorCode(jsonError.Data)
75-
if decodeErr != nil {
76-
log.Error().Err(err).Msg("unable to decode smart contract error")
77-
return err
78-
}
79-
errLog := log.Error().
80-
Err(err).
81-
Str("message", jsonError.Message).
82-
Int("code", jsonError.Code).
83-
Interface("data", jsonError.Data).
84-
Str("reason", reason)
85-
86-
if callErr != nil {
87-
errLog = errLog.Err(callErr)
88-
}
89-
90-
customErr := errors.New(err.Error() + ": " + reason)
91-
if errCode, isValid := jsonError.Data.(string); isValid && errCode == "0x646cf558" {
92-
// I don't want to bother with the additional error logging for previously claimed deposits
93-
return customErr
94-
}
95-
96-
errLog.Msg("Unable to interact with bridge contract")
97-
return customErr
98-
}
99-
10014
// parseDepositCountFromTransaction extracts the deposit count from a bridge transaction receipt.
10115
func parseDepositCountFromTransaction(ctx context.Context, client *ethclient.Client, txHash common.Hash, bridgeContract *ulxly.Ulxly) (uint32, error) {
10216
receipt, err := client.TransactionReceipt(ctx, txHash)

cmd/ulxly/bridge/weth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func bridgeWETHMessage(cmd *cobra.Command, _ []string) error {
6868
callData := common.Hex2Bytes(strings.TrimPrefix(callDataString, "0x"))
6969

7070
bridgeTxn, err := bridgeV2.BridgeMessageWETH(auth, destinationNetwork, toAddress, value, isForced, callData)
71-
if err = logAndReturnJSONError(cmd.Context(), client, bridgeTxn, auth, err); err != nil {
71+
if err = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, bridgeTxn, auth, err); err != nil {
7272
log.Info().Err(err).Str("calldata", callDataString).Msg("Bridge transaction failed")
7373
return err
7474
}

cmd/ulxly/claim/asset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func claimAsset(cmd *cobra.Command) error {
7979
}
8080

8181
claimTxn, err := bridgeV2.ClaimAsset(auth, bridge_service.HashSliceToBytesArray(proof.MerkleProof), bridge_service.HashSliceToBytesArray(proof.RollupMerkleProof), deposit.GlobalIndex, *proof.MainExitRoot, *proof.RollupExitRoot, deposit.OrigNet, deposit.OrigAddr, deposit.DestNet, deposit.DestAddr, deposit.Amount, deposit.Metadata)
82-
if err = logAndReturnJSONError(cmd.Context(), client, claimTxn, auth, err); err != nil {
82+
if err = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, claimTxn, auth, err); err != nil {
8383
return err
8484
}
8585
log.Info().Msg("claimTxn: " + claimTxn.Hash().String())

cmd/ulxly/claim/everything.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func claimSingleDeposit(cmd *cobra.Command, client *ethclient.Client, bridgeCont
251251
claimTx, err = bridgeContract.ClaimMessage(opts, bridge_service.HashSliceToBytesArray(proof.MerkleProof), bridge_service.HashSliceToBytesArray(proof.RollupMerkleProof), deposit.GlobalIndex, *proof.MainExitRoot, *proof.RollupExitRoot, deposit.OrigNet, deposit.OrigAddr, deposit.DestNet, deposit.DestAddr, deposit.Amount, deposit.Metadata)
252252
}
253253

254-
if err = logAndReturnJSONError(cmd.Context(), client, claimTx, opts, err); err != nil {
254+
if err = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, claimTx, opts, err); err != nil {
255255
log.Warn().
256256
Uint32("DepositCnt", deposit.DepositCnt).
257257
Uint32("OrigNet", deposit.OrigNet).

cmd/ulxly/claim/message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func claimMessage(cmd *cobra.Command) error {
7575
}
7676

7777
claimTxn, err := bridgeV2.ClaimMessage(auth, bridge_service.HashSliceToBytesArray(proof.MerkleProof), bridge_service.HashSliceToBytesArray(proof.RollupMerkleProof), deposit.GlobalIndex, *proof.MainExitRoot, *proof.RollupExitRoot, deposit.OrigNet, deposit.OrigAddr, deposit.DestNet, deposit.DestAddr, deposit.Amount, deposit.Metadata)
78-
if err = logAndReturnJSONError(cmd.Context(), client, claimTxn, auth, err); err != nil {
78+
if err = ulxlycommon.LogAndReturnJSONError(cmd.Context(), client, claimTxn, auth, err); err != nil {
7979
return err
8080
}
8181
log.Info().Msg("claimTxn: " + claimTxn.Hash().String())

cmd/ulxly/claim/shared.go

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,10 @@
11
package claim
22

33
import (
4-
"context"
5-
"encoding/json"
64
"errors"
7-
"fmt"
8-
9-
ulxlycommon "github.com/0xPolygon/polygon-cli/cmd/ulxly/common"
10-
smcerror "github.com/0xPolygon/polygon-cli/errors"
11-
"github.com/ethereum/go-ethereum"
12-
"github.com/ethereum/go-ethereum/accounts/abi/bind"
13-
"github.com/ethereum/go-ethereum/common"
14-
"github.com/ethereum/go-ethereum/core/types"
15-
ethclient "github.com/ethereum/go-ethereum/ethclient"
16-
"github.com/rs/zerolog/log"
175
)
186

197
var (
208
ErrNotReadyForClaim = errors.New("the claim transaction is not yet ready to be claimed, try again in a few blocks")
219
ErrDepositAlreadyClaimed = errors.New("the claim transaction has already been claimed")
2210
)
23-
24-
func logAndReturnJSONError(ctx context.Context, client *ethclient.Client, tx *types.Transaction, opts *bind.TransactOpts, err error) error {
25-
26-
var callErr error
27-
if tx != nil {
28-
// in case the error came down to gas estimation, we can sometimes get more information by doing a call
29-
_, callErr = client.CallContract(ctx, ethereum.CallMsg{
30-
From: opts.From,
31-
To: tx.To(),
32-
Gas: tx.Gas(),
33-
GasPrice: tx.GasPrice(),
34-
GasFeeCap: tx.GasFeeCap(),
35-
GasTipCap: tx.GasTipCap(),
36-
Value: tx.Value(),
37-
Data: tx.Data(),
38-
AccessList: tx.AccessList(),
39-
BlobGasFeeCap: tx.BlobGasFeeCap(),
40-
BlobHashes: tx.BlobHashes(),
41-
}, nil)
42-
43-
if ulxlycommon.InputArgs.DryRun {
44-
castCmd := "cast call"
45-
castCmd += fmt.Sprintf(" --rpc-url %s", ulxlycommon.InputArgs.RPCURL)
46-
castCmd += fmt.Sprintf(" --from %s", opts.From.String())
47-
castCmd += fmt.Sprintf(" --gas-limit %d", tx.Gas())
48-
if tx.Type() == types.LegacyTxType {
49-
castCmd += fmt.Sprintf(" --gas-price %s", tx.GasPrice().String())
50-
} else {
51-
castCmd += fmt.Sprintf(" --gas-price %s", tx.GasFeeCap().String())
52-
castCmd += fmt.Sprintf(" --priority-gas-price %s", tx.GasTipCap().String())
53-
}
54-
castCmd += fmt.Sprintf(" --value %s", tx.Value().String())
55-
castCmd += fmt.Sprintf(" %s", tx.To().String())
56-
castCmd += fmt.Sprintf(" %s", common.Bytes2Hex(tx.Data()))
57-
log.Info().Str("cmd", castCmd).Msg("use this command to replicate the call")
58-
}
59-
}
60-
61-
if err == nil {
62-
return nil
63-
}
64-
65-
var jsonError ulxlycommon.JSONError
66-
jsonErrorBytes, jsErr := json.Marshal(err)
67-
if jsErr != nil {
68-
log.Error().Err(err).Msg("Unable to interact with the bridge contract")
69-
return err
70-
}
71-
72-
jsErr = json.Unmarshal(jsonErrorBytes, &jsonError)
73-
if jsErr != nil {
74-
log.Error().Err(err).Msg("Unable to interact with the bridge contract")
75-
return err
76-
}
77-
78-
reason, decodeErr := smcerror.DecodeSmcErrorCode(jsonError.Data)
79-
if decodeErr != nil {
80-
log.Error().Err(err).Msg("unable to decode smart contract error")
81-
return err
82-
}
83-
errLog := log.Error().
84-
Err(err).
85-
Str("message", jsonError.Message).
86-
Int("code", jsonError.Code).
87-
Interface("data", jsonError.Data).
88-
Str("reason", reason)
89-
90-
if callErr != nil {
91-
errLog = errLog.Err(callErr)
92-
}
93-
94-
customErr := errors.New(err.Error() + ": " + reason)
95-
if errCode, isValid := jsonError.Data.(string); isValid && errCode == "0x646cf558" {
96-
// I don't want to bother with the additional error logging for previously claimed deposits
97-
return customErr
98-
}
99-
100-
errLog.Msg("Unable to interact with bridge contract")
101-
return customErr
102-
}

cmd/ulxly/common/common.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"crypto/ecdsa"
66
"crypto/tls"
7+
"encoding/json"
8+
"errors"
79
"fmt"
810
"math/big"
911
"net/http"
@@ -15,7 +17,9 @@ import (
1517
"github.com/0xPolygon/polygon-cli/bindings/ulxly"
1618
"github.com/0xPolygon/polygon-cli/cmd/ulxly/bridge_service"
1719
bridge_service_factory "github.com/0xPolygon/polygon-cli/cmd/ulxly/bridge_service/factory"
20+
smcerror "github.com/0xPolygon/polygon-cli/errors"
1821
"github.com/0xPolygon/polygon-cli/flag"
22+
"github.com/ethereum/go-ethereum"
1923
"github.com/ethereum/go-ethereum/accounts/abi/bind"
2024
ethcommon "github.com/ethereum/go-ethereum/common"
2125
"github.com/ethereum/go-ethereum/core/types"
@@ -417,3 +421,83 @@ func AddTransactionFlags(cmd *cobra.Command) {
417421
f.BoolVar(&InputArgs.Legacy, ArgLegacy, true, "force usage of legacy bridge service")
418422
flag.MarkPersistentFlagsRequired(cmd, ArgBridgeAddress)
419423
}
424+
425+
// LogAndReturnJSONError logs and returns a JSON-RPC error with additional context.
426+
func LogAndReturnJSONError(ctx context.Context, client *ethclient.Client, tx *types.Transaction, opts *bind.TransactOpts, err error) error {
427+
var callErr error
428+
if tx != nil {
429+
// in case the error came down to gas estimation, we can sometimes get more information by doing a call
430+
_, callErr = client.CallContract(ctx, ethereum.CallMsg{
431+
From: opts.From,
432+
To: tx.To(),
433+
Gas: tx.Gas(),
434+
GasPrice: tx.GasPrice(),
435+
GasFeeCap: tx.GasFeeCap(),
436+
GasTipCap: tx.GasTipCap(),
437+
Value: tx.Value(),
438+
Data: tx.Data(),
439+
AccessList: tx.AccessList(),
440+
BlobGasFeeCap: tx.BlobGasFeeCap(),
441+
BlobHashes: tx.BlobHashes(),
442+
}, nil)
443+
444+
if InputArgs.DryRun {
445+
castCmd := "cast call"
446+
castCmd += fmt.Sprintf(" --rpc-url %s", InputArgs.RPCURL)
447+
castCmd += fmt.Sprintf(" --from %s", opts.From.String())
448+
castCmd += fmt.Sprintf(" --gas-limit %d", tx.Gas())
449+
if tx.Type() == types.LegacyTxType {
450+
castCmd += fmt.Sprintf(" --gas-price %s", tx.GasPrice().String())
451+
} else {
452+
castCmd += fmt.Sprintf(" --gas-price %s", tx.GasFeeCap().String())
453+
castCmd += fmt.Sprintf(" --priority-gas-price %s", tx.GasTipCap().String())
454+
}
455+
castCmd += fmt.Sprintf(" --value %s", tx.Value().String())
456+
castCmd += fmt.Sprintf(" %s", tx.To().String())
457+
castCmd += fmt.Sprintf(" %s", ethcommon.Bytes2Hex(tx.Data()))
458+
log.Info().Str("cmd", castCmd).Msg("use this command to replicate the call")
459+
}
460+
}
461+
462+
if err == nil {
463+
return nil
464+
}
465+
466+
var jsonError JSONError
467+
jsonErrorBytes, jsErr := json.Marshal(err)
468+
if jsErr != nil {
469+
log.Error().Err(err).Msg("Unable to interact with the bridge contract")
470+
return err
471+
}
472+
473+
jsErr = json.Unmarshal(jsonErrorBytes, &jsonError)
474+
if jsErr != nil {
475+
log.Error().Err(err).Msg("Unable to interact with the bridge contract")
476+
return err
477+
}
478+
479+
reason, decodeErr := smcerror.DecodeSmcErrorCode(jsonError.Data)
480+
if decodeErr != nil {
481+
log.Error().Err(err).Msg("unable to decode smart contract error")
482+
return err
483+
}
484+
errLog := log.Error().
485+
Err(err).
486+
Str("message", jsonError.Message).
487+
Int("code", jsonError.Code).
488+
Interface("data", jsonError.Data).
489+
Str("reason", reason)
490+
491+
if callErr != nil {
492+
errLog = errLog.Err(callErr)
493+
}
494+
495+
customErr := errors.New(err.Error() + ": " + reason)
496+
if errCode, isValid := jsonError.Data.(string); isValid && errCode == "0x646cf558" {
497+
// I don't want to bother with the additional error logging for previously claimed deposits
498+
return customErr
499+
}
500+
501+
errLog.Msg("Unable to interact with bridge contract")
502+
return customErr
503+
}

0 commit comments

Comments
 (0)