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

feat: thorchain use max_streaming_quantity quantity in memos #5734

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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' }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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]
Expand Down
19 changes: 15 additions & 4 deletions src/lib/swapper/swappers/ThorchainSwapper/utils/getL1quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<ThorTradeQuote[], SwapErrorRight>> => {
export const getL1quote = async ({
input,
streamingInterval,
streamingQuantity,
}: {
input: GetTradeQuoteInput
streamingInterval: number
streamingQuantity: number
}): Promise<Result<ThorTradeQuote[], SwapErrorRight>> => {
const {
sellAsset,
buyAsset,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -383,6 +392,8 @@ export const getL1quote = async (
chainId: sellAsset.chainId,
affiliateBps,
streamingInterval,
defaultStreamingQuantity: streamingQuantity,
streamingQuantity: quote.max_streaming_quantity ?? 0,
})

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<ThorTradeQuote[], SwapErrorRight>> => {
export const getLongtailToL1Quote = async ({
input,
streamingInterval,
streamingQuantity,
assetsById,
}: {
input: GetTradeQuoteInput
streamingInterval: number
streamingQuantity: number
assetsById: AssetsById
}): Promise<Result<ThorTradeQuote[], SwapErrorRight>> => {
const chainAdapterManager = getChainAdapterManager()
const sellChainId = input.sellAsset.chainId
const nativeBuyAssetId = chainAdapterManager.get(sellChainId)?.getFeeAssetId()
Expand Down Expand Up @@ -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
}