Skip to content

Commit

Permalink
Refactor out getPool
Browse files Browse the repository at this point in the history
  • Loading branch information
paullinator committed Nov 3, 2023
1 parent 082f709 commit e9b88de
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/swap/defi/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ export const asInboundAddresses = asArray(

export const asPool = asObject({
asset: asString,
status: asString,
// status: asString,
assetPrice: asString,
assetPriceUSD: asString,
assetDepth: asString,
runeDepth: asString
assetPriceUSD: asString
// assetDepth: asString,
// runeDepth: asString
})

export const asAssetSpread = asObject({
Expand Down Expand Up @@ -447,13 +447,12 @@ export function makeThorchainPlugin(
const poolJson = await poolResponse.json()
const pools = asPools(poolJson)

const sourcePool = pools.find(pool => {
const [asset] = pool.asset.split('-')
return asset === `${fromMainnetCode}.${fromCurrencyCode}`
})
if (sourcePool == null) {
throw new SwapCurrencyError(swapInfo, request)
}
const sourcePool = getPool(
request,
fromMainnetCode,
fromCurrencyCode,
pools
)
const [
sourceAsset,
sourceTokenContractAddressAllCaps
Expand All @@ -464,13 +463,7 @@ export function makeThorchainPlugin(
: undefined
log(`sourceAsset: ${sourceAsset}`)

const destPool = pools.find(pool => {
const [asset] = pool.asset.split('-')
return asset === `${toMainnetCode}.${toCurrencyCode}`
})
if (destPool == null) {
throw new SwapCurrencyError(swapInfo, request)
}
const destPool = getPool(request, toMainnetCode, toCurrencyCode, pools)

let calcResponse: CalcSwapResponse
if (quoteFor === 'from') {
Expand Down Expand Up @@ -669,6 +662,22 @@ export function makeThorchainPlugin(
return out
}

const getPool = (
request: EdgeSwapRequestPlugin,
mainnetCode: string,
tokenCode: string,
pools: Pool[]
): Pool => {
const pool = pools.find(pool => {
const [asset] = pool.asset.split('-')
return asset === `${mainnetCode}.${tokenCode}`
})
if (pool == null) {
throw new SwapCurrencyError(swapInfo, request)
}
return pool
}

const calcSwapFrom = async ({
log,
fetch,
Expand Down

0 comments on commit e9b88de

Please sign in to comment.