From 1540fe128d06618ef7e9c51e54d5d924494e2faa Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Fri, 26 Jul 2024 16:03:43 -0700 Subject: [PATCH] Leverage either amount fields from thorchain swap API --- CHANGELOG.md | 2 ++ src/swap/defi/thorchain.ts | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 990d5e27..a91eef81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- fixed: Thorchain swap error caused by failing cleaner + ## 2.7.3 (2024-07-23) - changed: Cap Exolix to 70k USD swaps diff --git a/src/swap/defi/thorchain.ts b/src/swap/defi/thorchain.ts index e66bebce..d6130cee 100644 --- a/src/swap/defi/thorchain.ts +++ b/src/swap/defi/thorchain.ts @@ -222,8 +222,9 @@ export const asExchangeInfo = asObject({ const asPools = asArray(asPool) const asQuoteSwap = asObject({ - // expected_amount_out: asString, // "61409897" - expected_amount_out_streaming: asString, // "62487221" + expected_amount_out: asOptional(asString), // "61409897" + /** @deprecated */ + expected_amount_out_streaming: asOptional(asString), expiry: asNumber, // 1692149478 // fees: asObject({ // affiliate: asString, // "0" @@ -846,13 +847,13 @@ const calcSwapFrom = async ({ ) const { - expected_amount_out_streaming: toThorAmount, inbound_address: thorAddress, memo: preMemo, router, streaming_swap_blocks: streamingSwapBlocks, total_swap_seconds: maxFulfillmentSeconds } = bestQuote + const toThorAmount = getExpectedAmount(bestQuote) const canBePartial = !isEstimate || streamingSwapBlocks > 1 @@ -977,7 +978,6 @@ const calcSwapTo = async ({ ) const { - expected_amount_out_streaming: toThorAmount, inbound_address: thorAddress, memo: preMemo, router, @@ -985,6 +985,8 @@ const calcSwapTo = async ({ total_swap_seconds: maxFulfillmentSeconds } = bestQuote + const toThorAmount = getExpectedAmount(bestQuote) + // If we get a streaming quote, this should be considered a fully executing // transaction since we don't put a slippage limit const canBePartial = !isEstimate || streamingSwapBlocks > 1 @@ -1079,12 +1081,7 @@ const getBestQuote = async ( bestQuote = quote continue } - if ( - gt( - quote.expected_amount_out_streaming, - bestQuote.expected_amount_out_streaming - ) - ) { + if (gt(getExpectedAmount(quote), getExpectedAmount(bestQuote))) { bestQuote = quote } continue @@ -1250,3 +1247,16 @@ export const getVolatilitySpread = ({ return volatilitySpreadFinal.toString() } + +/** + * This will return the expected amount out from the quote maintaining backwards + * compatibility with deprecated `expected_amount_out_streaming` field. + */ +function getExpectedAmount(quote: QuoteSwap): string { + const amount = + quote.expected_amount_out ?? quote.expected_amount_out_streaming + if (amount == null) { + throw new Error('Missing expected amount out from Thorchain API response') + } + return amount +}