Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage either amount fields from thorchain swap API #338

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 20 additions & 10 deletions src/swap/defi/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -977,14 +978,15 @@ const calcSwapTo = 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)

// 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Loading