Skip to content

Commit

Permalink
feat: improve thorchain deposit transaction parsing (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight authored Jan 31, 2024
1 parent 0883f26 commit 5924711
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
35 changes: 16 additions & 19 deletions go/coinstacks/thorchain/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions go/coinstacks/thorchain/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions go/coinstacks/thorchain/thorchain.go
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
33 changes: 33 additions & 0 deletions go/coinstacks/thorchain/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
}
Expand Down

0 comments on commit 5924711

Please sign in to comment.