Skip to content

Commit

Permalink
Refactor how src and dest assets are determined
Browse files Browse the repository at this point in the history
Original code was missing logic to handle txs where RUNE or CACAO were the assets being swapped.
  • Loading branch information
paullinator committed Nov 1, 2024
1 parent 15a67ba commit 06c7ce2
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/partners/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const asThorchainPluginParams = asObject({

type Settings = ReturnType<typeof asSettings>
type ThorchainResult = ReturnType<typeof asThorchainResult>
type ThorchainTx = ReturnType<typeof asThorchainTx>

const QUERY_LOOKBACK = 1000 * 60 * 60 * 24 * 5 // 5 days
const LIMIT = 50
Expand Down Expand Up @@ -147,19 +148,43 @@ const makeThorchainPlugin = (info: ThorchainInfo): PartnerPlugin => {
if (txStatus !== 'success') {
continue
}
if (pools.length !== 2) {

// Find the source asset
let srcAsset: string | undefined
let depositAmount: number | undefined
let srcAddress: string | undefined
for (const txIn of txIns) {
for (const coin of txIn.coins) {
if (pools.includes(coin.asset)) {
srcAsset = coin.asset
depositAmount = Number(coin.amount) / THORCHAIN_MULTIPLIER
srcAddress = txIn.address
break
} else {
if (pools.length === 1) {
// Either src or dest is the main asset (RUNE/CACOA)
srcAsset = coin.asset
depositAmount = Number(coin.amount) / THORCHAIN_MULTIPLIER
srcAddress = txIn.address
}
}
}
if (srcAsset != null) {
break
}
}
if (srcAsset == null || depositAmount == null) {
continue
}

const srcAsset = txIns[0].coins[0].asset
const match = txOuts.find(o => {
const match2 = o.coins.find(c => c.asset === srcAsset)
return match2 != null
const match = txOuts.some(o => {
const match2 = o.coins.some(c => c.asset === srcAsset)
return match2
})

// If there is a match between source and dest asset that means a refund was made
// and the transaction failed
if (match != null) {
if (match) {
continue
}

Expand All @@ -172,12 +197,22 @@ const makeThorchainPlugin = (info: ThorchainInfo): PartnerPlugin => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_chain, asset] = chainAsset.split('.')

const depositAmount =
Number(txIns[0].coins[0].amount) / THORCHAIN_MULTIPLIER
let txOut: ThorchainTx['out'][0] | undefined

// Find the first output that does not match the affiliate address
// as this is assumed to be the true destination asset/address
// If we can't find one, then just match the affiliate address as
// this means the affiliate address is the actual destination.
for (const txo of txOuts) {
txOut = txo
if (txo.address !== thorchainAddress) {
break
}
}

const txOut = txOuts.find(o => o.coins[0].asset !== 'THOR.RUNE')
if (txOut == null) {
continue
// Should never happen
throw new Error(`${pluginId}: No output found`)
}

const [destChainAsset] = txOut.coins[0].asset.split('-')
Expand Down

0 comments on commit 06c7ce2

Please sign in to comment.