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

fix: remove addSlippageToMemo in favor of addLimitToMemo #7138

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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

This file was deleted.

This file was deleted.

62 changes: 46 additions & 16 deletions src/lib/swapper/swappers/ThorchainSwapper/utils/getL1quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { assertUnreachable, isFulfilled, isRejected } from 'lib/utils'
import { assertGetCosmosSdkChainAdapter } from 'lib/utils/cosmosSdk'
import { assertGetEvmChainAdapter } from 'lib/utils/evm'
import { THOR_PRECISION } from 'lib/utils/thorchain/constants'
import { addLimitToMemo } from 'lib/utils/thorchain/memo/addLimitToMemo'
import { assertGetUtxoChainAdapter } from 'lib/utils/utxo'
import { convertDecimalPercentageToBasisPoints } from 'state/slices/tradeQuoteSlice/utils'

Expand All @@ -39,8 +40,8 @@ import type {
ThorTradeUtxoOrCosmosQuote,
} from '../getThorTradeQuote/getTradeQuote'
import type { ThornodeQuoteResponseSuccess } from '../types'
import { addSlippageToMemo } from './addSlippageToMemo'
import { THORCHAIN_FIXED_PRECISION } from './constants'
import { getLimitWithManualSlippage } from './getLimitWithManualSlippage'
import { getQuote } from './getQuote/getQuote'
import { TradeType } from './longTailHelpers'
import { getEvmTxFees } from './txFeeHelpers/evmTxFees/getEvmTxFees'
Expand Down Expand Up @@ -221,14 +222,24 @@ export const getL1quote = async (
const buyAmountBeforeFeesCryptoBaseUnit =
getRouteBuyAmountBeforeFeesCryptoBaseUnit(quote)

const updatedMemo = addSlippageToMemo({
const limitWithManualSlippage = getLimitWithManualSlippage({
expectedAmountOutThorBaseUnit,
quotedMemo: quote.memo,
slippageBps,
chainId: sellAsset.chainId,
affiliateBps,
isStreaming,
})

if (!quote.memo) throw new Error('no memo provided')
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved

let updatedMemo = quote.memo

// always use TC auto stream quote (0 limit = 5bps - 50bps, sometimes up to 100bps)
// see: https://discord.com/channels/838986635756044328/1166265575941619742/1166500062101250100
if (!isStreaming) {
updatedMemo = addLimitToMemo({
memo: quote.memo,
limit: limitWithManualSlippage,
})
}

gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
const { data, router, vault } = await getEvmThorTxInfo({
sellAsset,
sellAmountCryptoBaseUnit,
Expand Down Expand Up @@ -314,14 +325,24 @@ export const getL1quote = async (
const buyAmountBeforeFeesCryptoBaseUnit =
getRouteBuyAmountBeforeFeesCryptoBaseUnit(quote)

const updatedMemo = addSlippageToMemo({
const limitWithManualSlippage = getLimitWithManualSlippage({
expectedAmountOutThorBaseUnit,
quotedMemo: quote.memo,
slippageBps,
isStreaming,
chainId: sellAsset.chainId,
affiliateBps,
})

if (!quote.memo) throw new Error('no memo provided')

let updatedMemo = quote.memo

// always use TC auto stream quote (0 limit = 5bps - 50bps, sometimes up to 100bps)
// see: https://discord.com/channels/838986635756044328/1166265575941619742/1166500062101250100
if (!isStreaming) {
updatedMemo = addLimitToMemo({
memo: quote.memo,
limit: limitWithManualSlippage,
})
}

const { vault, opReturnData, pubkey } = await getUtxoThorTxInfo({
sellAsset,
xpub: (input as GetUtxoTradeQuoteInput).xpub,
Expand Down Expand Up @@ -419,15 +440,24 @@ export const getL1quote = async (
outputExponent: buyAsset.precision,
}).toFixed()

const updatedMemo = addSlippageToMemo({
const limitWithManualSlippage = getLimitWithManualSlippage({
expectedAmountOutThorBaseUnit,
quotedMemo: quote.memo,
slippageBps,
isStreaming,
chainId: sellAsset.chainId,
affiliateBps,
})

if (!quote.memo) throw new Error('no memo provided')

let updatedMemo = quote.memo

// always use TC auto stream quote (0 limit = 5bps - 50bps, sometimes up to 100bps)
// see: https://discord.com/channels/838986635756044328/1166265575941619742/1166500062101250100
if (!isStreaming) {
updatedMemo = addLimitToMemo({
memo: quote.memo,
limit: limitWithManualSlippage,
})
}

return {
id: uuid(),
memo: updatedMemo,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'

import { getLimitWithManualSlippage } from './getLimitWithManualSlippage'

describe('getLimitWithManualSlippage', () => {
it('should remove slippage from expected amount out', () => {
const amountOut = '100'
const expectedLimitWithManualSlippage = '50'

const slippageBps = 5000 // 50%

const limitWithManualSlippage = getLimitWithManualSlippage({
expectedAmountOutThorBaseUnit: amountOut,
slippageBps,
})

expect(limitWithManualSlippage).toBe(expectedLimitWithManualSlippage)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { bn } from '@shapeshiftoss/chain-adapters'
import BigNumber from 'bignumber.js'
import { subtractBasisPointAmount } from 'state/slices/tradeQuoteSlice/utils'

export const getLimitWithManualSlippage = ({
expectedAmountOutThorBaseUnit,
slippageBps,
}: {
expectedAmountOutThorBaseUnit: string
slippageBps: BigNumber.Value
}) => {
const limitWithManualSlippage = subtractBasisPointAmount(
bn(expectedAmountOutThorBaseUnit).toFixed(0, BigNumber.ROUND_DOWN),
slippageBps,
BigNumber.ROUND_DOWN,
)

return limitWithManualSlippage
}
Loading