-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: parse buy txid from midgard actions (#6813)
- Loading branch information
1 parent
daac3b4
commit 149ac8c
Showing
2 changed files
with
21 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 9 additions & 12 deletions
21
src/lib/swapper/swappers/ThorchainSwapper/utils/parseThorBuyTxHash.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |