Skip to content

Commit

Permalink
fix: parse buy txid from midgard actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight committed May 3, 2024
1 parent 3dd73f2 commit 3e889a2
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3e889a2

Please sign in to comment.