Skip to content

Commit 3324e43

Browse files
committed
refactor: use existing binding to parse bridge events from logs
Signed-off-by: Ji Hwan <[email protected]>
1 parent 285278d commit 3324e43

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

cmd/ulxly/ulxly.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -620,36 +620,17 @@ func logAndReturnJsonError(cmd *cobra.Command, client *ethclient.Client, tx *typ
620620
}
621621

622622
// Function to parse deposit count from bridge transaction logs
623-
func ParseBridgeDepositCount(logs []types.Log) (uint32, error) {
624-
// Bridge event topic: 0x501781209a1f8899323b96b4ef08b168df93e0a90c673d1e4cce39366cb62f9b
625-
bridgeEventTopic := common.HexToHash("0x501781209a1f8899323b96b4ef08b168df93e0a90c673d1e4cce39366cb62f9b")
626-
623+
func ParseBridgeDepositCount(logs []types.Log, bridgeContract *ulxly.Ulxly) (uint32, error) {
627624
for _, log := range logs {
628-
// Check if this log is the bridge event
629-
if len(log.Topics) > 0 && log.Topics[0] == bridgeEventTopic {
630-
// Bridge event data layout:
631-
// [0-32] : leaf type
632-
// [32-64] : origin network
633-
// [64-96] : origin address
634-
// [96-128] : destination network
635-
// [128-160]: destination address
636-
// [160-192]: amount
637-
// [192-224]: force update GER
638-
// [224-256]: deposit count
639-
// [256-288]: metadata length
640-
641-
if len(log.Data) < 192 {
642-
return 0, fmt.Errorf("bridge event data too short")
643-
}
644-
645-
// Extract deposit count from data
646-
// It starts at position 160 (5 * 32) and is 32 bytes long
647-
depositCountBytes := log.Data[224:256]
648-
// uint32 is 4 bytes long, so only parse the last 4 bytes
649-
depositCount := binary.BigEndian.Uint32(depositCountBytes[28:32])
650-
651-
return depositCount, nil
625+
// Try to parse the log as a BridgeEvent using the contract's filterer
626+
bridgeEvent, err := bridgeContract.ParseBridgeEvent(log)
627+
if err != nil {
628+
// This log is not a bridge event, continue to next log
629+
continue
652630
}
631+
632+
// Successfully parsed a bridge event, return the deposit count
633+
return bridgeEvent.DepositCount, nil
653634
}
654635

655636
return 0, fmt.Errorf("bridge event not found in logs")
@@ -738,12 +719,17 @@ func bridgeAsset(cmd *cobra.Command) error {
738719
if err != nil {
739720
return err
740721
}
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+
}
741727
// Convert []*types.Log to []types.Log
742728
logs := make([]types.Log, len(receipt.Logs))
743729
for i, log := range receipt.Logs {
744730
logs[i] = *log
745731
}
746-
depositCount, err := ParseBridgeDepositCount(logs)
732+
depositCount, err := ParseBridgeDepositCount(logs, bridgeV2)
747733
if err != nil {
748734
log.Error().Err(err).Msg("failed to parse deposit count from logs")
749735
return err
@@ -801,12 +787,17 @@ func bridgeMessage(cmd *cobra.Command) error {
801787
if err != nil {
802788
return err
803789
}
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+
}
804795
// Convert []*types.Log to []types.Log
805796
logs := make([]types.Log, len(receipt.Logs))
806797
for i, log := range receipt.Logs {
807798
logs[i] = *log
808799
}
809-
depositCount, err := ParseBridgeDepositCount(logs)
800+
depositCount, err := ParseBridgeDepositCount(logs, bridgeV2)
810801
if err != nil {
811802
log.Error().Err(err).Msg("failed to parse deposit count from logs")
812803
return err
@@ -867,12 +858,17 @@ func bridgeWETHMessage(cmd *cobra.Command) error {
867858
if err != nil {
868859
return err
869860
}
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+
}
870866
// Convert []*types.Log to []types.Log
871867
logs := make([]types.Log, len(receipt.Logs))
872868
for i, log := range receipt.Logs {
873869
logs[i] = *log
874870
}
875-
depositCount, err := ParseBridgeDepositCount(logs)
871+
depositCount, err := ParseBridgeDepositCount(logs, bridgeV2)
876872
if err != nil {
877873
log.Error().Err(err).Msg("failed to parse deposit count from logs")
878874
return err

0 commit comments

Comments
 (0)