Skip to content

Commit

Permalink
fix: parse buy txid from midgard actions (#6813)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight committed May 6, 2024
1 parent daac3b4 commit 149ac8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
29 changes: 12 additions & 17 deletions src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { isNativeEvmAsset } from '../utils/helpers/helpers'
import { THORCHAIN_OUTBOUND_FEE_RUNE_THOR_UNIT } from './constants'
import type { ThorEvmTradeQuote } from './getThorTradeQuote/getTradeQuote'
import { getThorTradeQuote } from './getThorTradeQuote/getTradeQuote'
import { type MidgardActionsResponse, type ThornodeStatusResponse } from './types'
import type { ThornodeStatusResponse } from './types'
import { checkOutboundTxConfirmations } from './utils/checkOutputTxConfirmations'
import { getLatestThorTxStatusMessage } from './utils/getLatestThorTxStatusMessage'
import { TradeType } from './utils/longTailHelpers'
Expand Down Expand Up @@ -334,31 +334,26 @@ export const thorchainApi: SwapperApi = {
const thorTxHash = txHash.replace(/^0x/, '')

// not using monadic axios, this is intentional for simplicity in this non-monadic context
const [{ data: thorTxData }, { data: thorActionsData }] = await Promise.all([
axios.get<ThornodeStatusResponse>(
`${getConfig().REACT_APP_THORCHAIN_NODE_URL}/lcd/thorchain/tx/status/${thorTxHash}`,
),
axios.get<MidgardActionsResponse>(
`${getConfig().REACT_APP_MIDGARD_URL}/actions?txid=${thorTxHash}`,
),
])

if ('error' in thorTxData) {
const { data } = await axios.get<ThornodeStatusResponse>(
`${getConfig().REACT_APP_THORCHAIN_NODE_URL}/lcd/thorchain/tx/status/${thorTxHash}`,
)

if ('error' in data) {
return {
buyTxHash: undefined,
status: TxStatus.Unknown,
message: undefined,
}
}

const outCoinAsset: string | undefined = thorActionsData.actions[0]?.out[0]?.coins[0]?.asset
const hasOutboundTx = outCoinAsset !== 'THOR.RUNE'
const latestOutTx = data.out_txs?.[data.out_txs.length - 1]
const hasOutboundTx = latestOutTx?.chain !== 'THOR'

const buyTxHash = parseThorBuyTxHash(txHash, thorActionsData)
const buyTxHash = parseThorBuyTxHash(txHash, data)

// if we have a buyTxHash, check if it's been confirmed on-chain
// if we have an outbound transaction (non rune) and associated buyTxHash, check if it's been confirmed on-chain
if (hasOutboundTx && buyTxHash) {
const outboundTxConfirmations = await checkOutboundTxConfirmations(thorTxData, buyTxHash)
const outboundTxConfirmations = await checkOutboundTxConfirmations(data, buyTxHash)

if (outboundTxConfirmations !== undefined && outboundTxConfirmations > 0) {
return {
Expand All @@ -369,7 +364,7 @@ export const thorchainApi: SwapperApi = {
}
}

const { message, status } = getLatestThorTxStatusMessage(thorTxData, hasOutboundTx)
const { message, status } = getLatestThorTxStatusMessage(data, hasOutboundTx)

return {
buyTxHash,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import type { MidgardActionsResponse } from '../types'
import type { ThorNodeStatusResponseSuccess } from '../types'

const THORCHAIN_EVM_CHAINS = ['ETH', 'AVAX', 'BSC'] as const

export const parseThorBuyTxHash = (
sellTxId: string,
thorActionsData: MidgardActionsResponse,
response: ThorNodeStatusResponseSuccess,
): string | undefined => {
const inCoinAsset: string | undefined = thorActionsData.actions[0]?.in[0]?.coins[0]?.asset
const outCoinAsset: string | undefined = thorActionsData.actions[0]?.out[0]?.coins[0]?.asset
const isDoubleSwap = outCoinAsset !== 'THOR.RUNE' && inCoinAsset !== 'THOR.RUNE'
const latestOutTx = response.out_txs?.[response.out_txs.length - 1]

// swaps into rune aren't double swaps so don't have a second tx (buy tx)
if (!isDoubleSwap) return sellTxId
if (!latestOutTx) return

const isEvmCoinAsset = THORCHAIN_EVM_CHAINS.some(
thorEvmChain => outCoinAsset?.startsWith(thorEvmChain),
)
// outbound rune transactions do not have a txid as they are processed internally, use sell txid
if (latestOutTx.chain === 'THOR') return sellTxId

const buyTxId = thorActionsData.actions[0]?.out[0]?.txID
return isEvmCoinAsset && buyTxId ? `0x${buyTxId}` : buyTxId
const isEvmCoinAsset = THORCHAIN_EVM_CHAINS.some(chain => chain === latestOutTx.chain)

return isEvmCoinAsset ? `0x${latestOutTx.id}` : latestOutTx.id
}

0 comments on commit 149ac8c

Please sign in to comment.