From 592471129c41b4c1bb15e95bdf5a6dadc87aaf69 Mon Sep 17 00:00:00 2001 From: kevin <35275952+kaladinlight@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:05:16 -0700 Subject: [PATCH] feat: improve thorchain deposit transaction parsing (#934) --- go/coinstacks/thorchain/api/handler.go | 35 ++++++++++++-------------- go/coinstacks/thorchain/api/types.go | 23 +++++++++-------- go/coinstacks/thorchain/thorchain.go | 4 +++ go/coinstacks/thorchain/tx.go | 33 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 30 deletions(-) diff --git a/go/coinstacks/thorchain/api/handler.go b/go/coinstacks/thorchain/api/handler.go index dc8d46bbc..59bade033 100644 --- a/go/coinstacks/thorchain/api/handler.go +++ b/go/coinstacks/thorchain/api/handler.go @@ -194,47 +194,44 @@ func (h *Handler) getTxFromEndBlockEvents(eventCache map[string]interface{}, blo typedEvent := typedEvents[eventIndex] tx := &ResultTx{ - Hash: blockHeader.Hash().String(), - Height: blockHeader.Height, - Timestamp: int(blockHeader.Time.Unix()), - Events: cosmos.EventsByMsgIndex{"0": events[strconv.Itoa(eventIndex)]}, - Messages: thorchain.TypedEventsToMessages([]thorchain.TypedEvent{typedEvent}), - TypedEvent: typedEvent, - formatTx: h.formatTx, + BlockHash: blockHeader.Hash().String(), + BlockHeight: blockHeader.Height, + Timestamp: int(blockHeader.Time.Unix()), + Index: -1, // synthetic transactions don't have a real tx index + Events: cosmos.EventsByMsgIndex{"0": events[strconv.Itoa(eventIndex)]}, + Messages: thorchain.TypedEventsToMessages([]thorchain.TypedEvent{typedEvent}), + TypedEvent: typedEvent, + formatTx: h.formatTx, } switch v := typedEvent.(type) { case *thorchain.EventOutbound: tx.TxID = v.InTxID + tx.Memo = v.Memo tx.Fee = matchFee(v.InTxID, typedEvents) + return tx, nil default: return nil, nil } - - return tx, nil } // formatTx creates a synthetic transaction from a BlockEndEvent func (h *Handler) formatTx(tx *ResultTx) (*cosmos.Tx, error) { t := &cosmos.Tx{ BaseTx: api.BaseTx{ - BlockHash: &tx.Hash, - BlockHeight: int(tx.Height), + TxID: tx.TxID, + BlockHash: &tx.BlockHash, + BlockHeight: int(tx.BlockHeight), Timestamp: tx.Timestamp, }, - Index: -1, // synthetic transactions don't have a real tx index + Index: tx.Index, Fee: tx.Fee, - Confirmations: h.BlockService.Latest.Height - int(tx.Height) + 1, + Confirmations: h.BlockService.Latest.Height - int(tx.BlockHeight) + 1, Events: tx.Events, GasWanted: "0", GasUsed: "0", Messages: tx.Messages, - } - - switch v := tx.TypedEvent.(type) { - case *thorchain.EventOutbound: - t.BaseTx.TxID = v.InTxID - t.Memo = v.Memo + Memo: tx.Memo, } return t, nil diff --git a/go/coinstacks/thorchain/api/types.go b/go/coinstacks/thorchain/api/types.go index 66fdf82d1..b8a247194 100644 --- a/go/coinstacks/thorchain/api/types.go +++ b/go/coinstacks/thorchain/api/types.go @@ -7,20 +7,21 @@ import ( // ResultTx represents a tx_search ResultTx created from block_result block events type ResultTx struct { - Hash string - Height int64 - Timestamp int - Index int - TxID string - Fee cosmos.Value - Events cosmos.EventsByMsgIndex - Messages []cosmos.Message - TypedEvent thorchain.TypedEvent - formatTx func(tx *ResultTx) (*cosmos.Tx, error) + BlockHash string + BlockHeight int64 + Timestamp int + Index int + TxID string + Memo string + Fee cosmos.Value + Events cosmos.EventsByMsgIndex + Messages []cosmos.Message + TypedEvent thorchain.TypedEvent + formatTx func(tx *ResultTx) (*cosmos.Tx, error) } func (r *ResultTx) GetHeight() int64 { - return r.Height + return r.BlockHeight } func (r *ResultTx) GetIndex() int { diff --git a/go/coinstacks/thorchain/thorchain.go b/go/coinstacks/thorchain/thorchain.go index 9f75ceac4..8cc89b768 100644 --- a/go/coinstacks/thorchain/thorchain.go +++ b/go/coinstacks/thorchain/thorchain.go @@ -1,5 +1,9 @@ package thorchain +import "github.com/shapeshift/unchained/internal/log" + +var logger = log.WithoutFields() + // map thorchain assets to native tendermint denoms var assetToDenom = map[string]string{ "THOR.RUNE": "rune", diff --git a/go/coinstacks/thorchain/tx.go b/go/coinstacks/thorchain/tx.go index c9189f8dc..58b718189 100644 --- a/go/coinstacks/thorchain/tx.go +++ b/go/coinstacks/thorchain/tx.go @@ -47,6 +47,18 @@ func ParseMessages(msgs []sdk.Msg, events cosmos.EventsByMsgIndex) []cosmos.Mess to := events[strconv.Itoa(i)]["transfer"]["recipient"] events[strconv.Itoa(i)]["message"]["memo"] = v.Memo // add memo value from message to events + // detect withdraw event as a result of the deposit and use this to address instead + withdraw := events[strconv.Itoa(i)]["withdraw"] + if withdraw != nil { + to = withdraw["to"] + } + + // detect refund event as a result of the deposit and use this to address instead + refund := events[strconv.Itoa(i)]["refund"] + if refund != nil { + to = refund["to"] + } + message := cosmos.Message{ Addresses: []string{v.Signer.String(), to}, Index: strconv.Itoa(i), @@ -57,6 +69,27 @@ func ParseMessages(msgs []sdk.Msg, events cosmos.EventsByMsgIndex) []cosmos.Mess Value: coinToValue(v.Coins[0]), } messages = append(messages, message) + + // detect outbound event as a result of the deposit and create a synthetic message for it + outbound := events[strconv.Itoa(i)]["outbound"] + if outbound != nil { + coin, err := common.ParseCoin(outbound["coin"]) + if err != nil && outbound["coin"] != "" { + logger.Error(err) + } + + message := cosmos.Message{ + Addresses: []string{outbound["from"], outbound["to"]}, + Index: strconv.Itoa(i), + Origin: outbound["from"], + From: outbound["from"], + To: outbound["to"], + Type: "outbound", + Value: coinToValue(coin), + } + messages = append(messages, message) + } + default: unhandledMsgs = append(unhandledMsgs, msg) }