diff --git a/src/lib/swapper/swappers/ThorchainSwapper/getThorTradeQuote/getTradeQuote.ts b/src/lib/swapper/swappers/ThorchainSwapper/getThorTradeQuote/getTradeQuote.ts index 3ef29353b24..937f5d4f832 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/getThorTradeQuote/getTradeQuote.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/getThorTradeQuote/getTradeQuote.ts @@ -11,6 +11,7 @@ import { assertUnreachable } from 'lib/utils' import type { AssetsById } from 'state/slices/assetsSlice/assetsSlice' import type { ThornodePoolResponse } from '../types' +import { DEFAULT_STREAMING_NUM_SWAPS } from '../utils/constants' import { getL1quote } from '../utils/getL1quote' import { getLongtailToL1Quote } from '../utils/getLongtailQuote' import { getTradeType, TradeType } from '../utils/longTailHelpers' @@ -95,12 +96,20 @@ export const getThorTradeQuote = async ( })() : // TODO: One of the pools is RUNE - use the as-is 10 until we work out how best to handle this 10 + // Let the network decide the internal number of swaps + // Note, this gets overriden in addSlippageToMemo and uses the max_max_streaming_quantity instead + const defaultStreamingQuantity = DEFAULT_STREAMING_NUM_SWAPS switch (tradeType) { case TradeType.L1ToL1: - return getL1quote(input, streamingInterval) + return getL1quote({ input, streamingInterval, streamingQuantity: defaultStreamingQuantity }) case TradeType.LongTailToL1: - return getLongtailToL1Quote(input, streamingInterval, assetsById) + return getLongtailToL1Quote({ + input, + streamingInterval, + assetsById, + streamingQuantity: defaultStreamingQuantity, + }) case TradeType.LongTailToLongTail: case TradeType.L1ToLongTail: return Err(makeSwapErrorRight({ message: 'Not implemented yet' })) diff --git a/src/lib/swapper/swappers/ThorchainSwapper/utils/addSlippageToMemo.ts b/src/lib/swapper/swappers/ThorchainSwapper/utils/addSlippageToMemo.ts index 6cc111131a0..3042d841cb6 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/utils/addSlippageToMemo.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/utils/addSlippageToMemo.ts @@ -2,7 +2,7 @@ import type { ChainId } from '@shapeshiftoss/caip' import { BigNumber, bn } from 'lib/bignumber/bignumber' import { subtractBasisPointAmount } from 'state/slices/tradeQuoteSlice/utils' -import { DEFAULT_STREAMING_NUM_SWAPS, LIMIT_PART_DELIMITER, MEMO_PART_DELIMITER } from './constants' +import { LIMIT_PART_DELIMITER, MEMO_PART_DELIMITER } from './constants' import { assertIsValidMemo } from './makeSwapMemo/assertIsValidMemo' export const addSlippageToMemo = ({ @@ -14,6 +14,9 @@ export const addSlippageToMemo = ({ chainId, affiliateBps, streamingInterval, + // Our own default streaming quantity, currently sitting at 0 (i.e let the network decide) + defaultStreamingQuantity, + streamingQuantity, }: { expectedAmountOutThorBaseUnit: string affiliateFeesThorBaseUnit: string @@ -23,9 +26,16 @@ export const addSlippageToMemo = ({ affiliateBps: string isStreaming: boolean streamingInterval: number + defaultStreamingQuantity: number + streamingQuantity: number }) => { if (!quotedMemo) throw new Error('no memo provided') + // If the network returns 0 as a streaming quantity, use that - else, use the optimized max_streaming_quantity + const optimizedStreamingQuantity = bn(streamingQuantity).gt(defaultStreamingQuantity) + ? streamingQuantity + : defaultStreamingQuantity + // the missing element is the original limit with (optional, missing) streaming parameters const [prefix, pool, address, , affiliate, memoAffiliateBps] = quotedMemo.split(MEMO_PART_DELIMITER) @@ -39,7 +49,7 @@ export const addSlippageToMemo = ({ ) const updatedLimitComponent = isStreaming - ? [limitWithManualSlippage, streamingInterval, DEFAULT_STREAMING_NUM_SWAPS].join( + ? [limitWithManualSlippage, streamingInterval, optimizedStreamingQuantity].join( LIMIT_PART_DELIMITER, ) : [limitWithManualSlippage] diff --git a/src/lib/swapper/swappers/ThorchainSwapper/utils/getL1quote.ts b/src/lib/swapper/swappers/ThorchainSwapper/utils/getL1quote.ts index 9cc999e46ad..c33b53dec90 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/utils/getL1quote.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/utils/getL1quote.ts @@ -36,10 +36,15 @@ import { getQuote } from './getQuote/getQuote' import { getEvmTxFees } from './txFeeHelpers/evmTxFees/getEvmTxFees' import { getUtxoTxFees } from './txFeeHelpers/utxoTxFees/getUtxoTxFees' -export const getL1quote = async ( - input: GetTradeQuoteInput, - streamingInterval: number, -): Promise> => { +export const getL1quote = async ({ + input, + streamingInterval, + streamingQuantity, +}: { + input: GetTradeQuoteInput + streamingInterval: number + streamingQuantity: number +}): Promise> => { const { sellAsset, buyAsset, @@ -200,6 +205,8 @@ export const getL1quote = async ( affiliateBps, isStreaming, streamingInterval, + defaultStreamingQuantity: streamingQuantity, + streamingQuantity: quote.max_streaming_quantity ?? 0, }) const { data, router } = await getEvmThorTxInfo({ sellAsset, @@ -285,6 +292,8 @@ export const getL1quote = async ( chainId: sellAsset.chainId, affiliateBps, streamingInterval, + defaultStreamingQuantity: streamingQuantity, + streamingQuantity: quote.max_streaming_quantity ?? 0, }) const { vault, opReturnData, pubkey } = await getUtxoThorTxInfo({ sellAsset, @@ -383,6 +392,8 @@ export const getL1quote = async ( chainId: sellAsset.chainId, affiliateBps, streamingInterval, + defaultStreamingQuantity: streamingQuantity, + streamingQuantity: quote.max_streaming_quantity ?? 0, }) return { diff --git a/src/lib/swapper/swappers/ThorchainSwapper/utils/getLongtailQuote.ts b/src/lib/swapper/swappers/ThorchainSwapper/utils/getLongtailQuote.ts index bcdddb8dfc3..991bd230485 100644 --- a/src/lib/swapper/swappers/ThorchainSwapper/utils/getLongtailQuote.ts +++ b/src/lib/swapper/swappers/ThorchainSwapper/utils/getLongtailQuote.ts @@ -17,11 +17,17 @@ import type { ThorTradeQuote } from '../getThorTradeQuote/getTradeQuote' import { getL1quote } from './getL1quote' import { getTokenFromAsset, getWrappedToken } from './longTailHelpers' -export const getLongtailToL1Quote = async ( - input: GetTradeQuoteInput, - streamingInterval: number, - assetsById: AssetsById, -): Promise> => { +export const getLongtailToL1Quote = async ({ + input, + streamingInterval, + streamingQuantity, + assetsById, +}: { + input: GetTradeQuoteInput + streamingInterval: number + streamingQuantity: number + assetsById: AssetsById +}): Promise> => { const chainAdapterManager = getChainAdapterManager() const sellChainId = input.sellAsset.chainId const nativeBuyAssetId = chainAdapterManager.get(sellChainId)?.getFeeAssetId() @@ -87,6 +93,10 @@ export const getLongtailToL1Quote = async ( sellAmountIncludingProtocolFeesCryptoBaseUnit: quotedAmountOut.toString(), } - const thorchainQuote = await getL1quote(l1Tol1QuoteInput, streamingInterval) + const thorchainQuote = await getL1quote({ + input: l1Tol1QuoteInput, + streamingInterval, + streamingQuantity, + }) return thorchainQuote }