Skip to content

Commit 3b85613

Browse files
committed
chore: cleanup reused logic into function
Signed-off-by: Ji Hwan <[email protected]>
1 parent 3324e43 commit 3b85613

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

cmd/ulxly/ulxly.go

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,34 @@ func ParseBridgeDepositCount(logs []types.Log, bridgeContract *ulxly.Ulxly) (uin
636636
return 0, fmt.Errorf("bridge event not found in logs")
637637
}
638638

639+
// parseDepositCountFromTransaction extracts the deposit count from a bridge transaction receipt
640+
func parseDepositCountFromTransaction(ctx context.Context, client *ethclient.Client, txHash common.Hash, bridgeContract *ulxly.Ulxly) (uint32, error) {
641+
receipt, err := client.TransactionReceipt(ctx, txHash)
642+
if err != nil {
643+
return 0, err
644+
}
645+
646+
// Check if the transaction was successful before trying to parse logs
647+
if receipt.Status == 0 {
648+
log.Error().Str("txHash", receipt.TxHash.String()).Msg("Bridge transaction failed")
649+
return 0, fmt.Errorf("bridge transaction failed with hash: %s", receipt.TxHash.String())
650+
}
651+
652+
// Convert []*types.Log to []types.Log
653+
logs := make([]types.Log, len(receipt.Logs))
654+
for i, log := range receipt.Logs {
655+
logs[i] = *log
656+
}
657+
658+
depositCount, err := ParseBridgeDepositCount(logs, bridgeContract)
659+
if err != nil {
660+
log.Error().Err(err).Msg("failed to parse deposit count from logs")
661+
return 0, err
662+
}
663+
664+
return depositCount, nil
665+
}
666+
639667
func bridgeAsset(cmd *cobra.Command) error {
640668
bridgeAddr := *inputUlxlyArgs.bridgeAddress
641669
privateKey := *inputUlxlyArgs.privateKey
@@ -715,25 +743,10 @@ func bridgeAsset(cmd *cobra.Command) error {
715743
if err = WaitMineTransaction(cmd.Context(), client, bridgeTxn, timeoutTxnReceipt); err != nil {
716744
return err
717745
}
718-
receipt, err := client.TransactionReceipt(cmd.Context(), bridgeTxn.Hash())
746+
depositCount, err := parseDepositCountFromTransaction(cmd.Context(), client, bridgeTxn.Hash(), bridgeV2)
719747
if err != nil {
720748
return err
721749
}
722-
// Check if the transaction was successful before trying to parse logs
723-
if receipt.Status == 0 {
724-
log.Error().Str("txHash", receipt.TxHash.String()).Msg("Bridge transaction failed")
725-
return fmt.Errorf("bridge transaction failed with hash: %s", receipt.TxHash.String())
726-
}
727-
// Convert []*types.Log to []types.Log
728-
logs := make([]types.Log, len(receipt.Logs))
729-
for i, log := range receipt.Logs {
730-
logs[i] = *log
731-
}
732-
depositCount, err := ParseBridgeDepositCount(logs, bridgeV2)
733-
if err != nil {
734-
log.Error().Err(err).Msg("failed to parse deposit count from logs")
735-
return err
736-
}
737750

738751
log.Info().Uint32("depositCount", depositCount).Msg("Bridge deposit count parsed from logs")
739752
return nil
@@ -783,25 +796,10 @@ func bridgeMessage(cmd *cobra.Command) error {
783796
if err = WaitMineTransaction(cmd.Context(), client, bridgeTxn, timeoutTxnReceipt); err != nil {
784797
return err
785798
}
786-
receipt, err := client.TransactionReceipt(cmd.Context(), bridgeTxn.Hash())
799+
depositCount, err := parseDepositCountFromTransaction(cmd.Context(), client, bridgeTxn.Hash(), bridgeV2)
787800
if err != nil {
788801
return err
789802
}
790-
// Check if the transaction was successful before trying to parse logs
791-
if receipt.Status == 0 {
792-
log.Error().Str("txHash", receipt.TxHash.String()).Msg("Bridge transaction failed")
793-
return fmt.Errorf("bridge transaction failed with hash: %s", receipt.TxHash.String())
794-
}
795-
// Convert []*types.Log to []types.Log
796-
logs := make([]types.Log, len(receipt.Logs))
797-
for i, log := range receipt.Logs {
798-
logs[i] = *log
799-
}
800-
depositCount, err := ParseBridgeDepositCount(logs, bridgeV2)
801-
if err != nil {
802-
log.Error().Err(err).Msg("failed to parse deposit count from logs")
803-
return err
804-
}
805803

806804
log.Info().Uint32("depositCount", depositCount).Msg("Bridge deposit count parsed from logs")
807805
return nil
@@ -854,23 +852,8 @@ func bridgeWETHMessage(cmd *cobra.Command) error {
854852
if err = WaitMineTransaction(cmd.Context(), client, bridgeTxn, timeoutTxnReceipt); err != nil {
855853
return err
856854
}
857-
receipt, err := client.TransactionReceipt(cmd.Context(), bridgeTxn.Hash())
858-
if err != nil {
859-
return err
860-
}
861-
// Check if the transaction was successful before trying to parse logs
862-
if receipt.Status == 0 {
863-
log.Error().Str("txHash", receipt.TxHash.String()).Msg("Bridge transaction failed")
864-
return fmt.Errorf("bridge transaction failed with hash: %s", receipt.TxHash.String())
865-
}
866-
// Convert []*types.Log to []types.Log
867-
logs := make([]types.Log, len(receipt.Logs))
868-
for i, log := range receipt.Logs {
869-
logs[i] = *log
870-
}
871-
depositCount, err := ParseBridgeDepositCount(logs, bridgeV2)
855+
depositCount, err := parseDepositCountFromTransaction(cmd.Context(), client, bridgeTxn.Hash(), bridgeV2)
872856
if err != nil {
873-
log.Error().Err(err).Msg("failed to parse deposit count from logs")
874857
return err
875858
}
876859

0 commit comments

Comments
 (0)