From 3e889a2a4fbef703fffc399f9cc5d181ed744fd0 Mon Sep 17 00:00:00 2001 From: kaladinlight <35275952+kaladinlight@users.noreply.github.com> Date: Fri, 3 May 2024 11:55:26 -0600 Subject: [PATCH 1/3] fix: parse buy txid from midgard actions --- .../utils/parseThorBuyTxHash.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts b/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts index cfd9993b6ef..4fdffcd1aa3 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts @@ -6,17 +6,19 @@ export const parseThorBuyTxHash = ( sellTxId: string, thorActionsData: MidgardActionsResponse, ): 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 outTxs = thorActionsData.actions[0]?.out - // swaps into rune aren't double swaps so don't have a second tx (buy tx) - if (!isDoubleSwap) return sellTxId + if (!outTxs?.length) return + + const latestOutTx = outTxs[outTxs.length - 1] + const latestTxId = latestOutTx.txID + + // outbound rune transactions do not have a txid as they are processed internally + if (!latestTxId) return sellTxId const isEvmCoinAsset = THORCHAIN_EVM_CHAINS.some( - thorEvmChain => outCoinAsset?.startsWith(thorEvmChain), + thorEvmChain => latestOutTx.coins[0].asset?.startsWith(thorEvmChain), ) - const buyTxId = thorActionsData.actions[0]?.out[0]?.txID - return isEvmCoinAsset && buyTxId ? `0x${buyTxId}` : buyTxId + return isEvmCoinAsset ? `0x${latestTxId}` : latestTxId } From 2e86c1c0dedfc2a2aa1516c99432ecd0cf8ee0a1 Mon Sep 17 00:00:00 2001 From: kaladinlight <35275952+kaladinlight@users.noreply.github.com> Date: Mon, 6 May 2024 13:00:03 -0600 Subject: [PATCH 2/3] use status endpoint only and always check latest out tx --- .../swappers/ThorchainSwapper/endpoints.ts | 27 ++++++++----------- .../utils/parseThorBuyTxHash.ts | 21 ++++++--------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts b/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts index b4a5e59a43c..229077ac1c2 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts @@ -334,16 +334,11 @@ 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( - `${getConfig().REACT_APP_THORCHAIN_NODE_URL}/lcd/thorchain/tx/status/${thorTxHash}`, - ), - axios.get( - `${getConfig().REACT_APP_MIDGARD_URL}/actions?txid=${thorTxHash}`, - ), - ]) - - if ('error' in thorTxData) { + const { data } = await axios.get( + `${getConfig().REACT_APP_THORCHAIN_NODE_URL}/lcd/thorchain/tx/status/${thorTxHash}`, + ) + + if ('error' in data) { return { buyTxHash: undefined, status: TxStatus.Unknown, @@ -351,14 +346,14 @@ export const thorchainApi: SwapperApi = { } } - 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 { @@ -369,7 +364,7 @@ export const thorchainApi: SwapperApi = { } } - const { message, status } = getLatestThorTxStatusMessage(thorTxData, hasOutboundTx) + const { message, status } = getLatestThorTxStatusMessage(data, hasOutboundTx) return { buyTxHash, diff --git a/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts b/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts index 4fdffcd1aa3..e107f8f72c7 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts @@ -1,24 +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 outTxs = thorActionsData.actions[0]?.out + const latestOutTx = response.out_txs?.[response.out_txs.length - 1] - if (!outTxs?.length) return + if (!latestOutTx) return - const latestOutTx = outTxs[outTxs.length - 1] - const latestTxId = latestOutTx.txID + // outbound rune transactions do not have a txid as they are processed internally, use sell txid + if (latestOutTx.chain === 'THOR') return sellTxId - // outbound rune transactions do not have a txid as they are processed internally - if (!latestTxId) return sellTxId + const isEvmCoinAsset = THORCHAIN_EVM_CHAINS.some(chain => chain === latestOutTx.chain) - const isEvmCoinAsset = THORCHAIN_EVM_CHAINS.some( - thorEvmChain => latestOutTx.coins[0].asset?.startsWith(thorEvmChain), - ) - - return isEvmCoinAsset ? `0x${latestTxId}` : latestTxId + return isEvmCoinAsset ? `0x${latestOutTx.id}` : latestOutTx.id } From 28e06d241fd71173070f1294d3e37947caf26d6c Mon Sep 17 00:00:00 2001 From: kaladinlight <35275952+kaladinlight@users.noreply.github.com> Date: Mon, 6 May 2024 13:06:25 -0600 Subject: [PATCH 3/3] fix lint --- src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts b/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts index 229077ac1c2..0b28ff61e62 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts @@ -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'