Skip to content

Commit

Permalink
chore: plumb slippage to thor quote
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis committed Aug 18, 2023
1 parent 36d613a commit ff5533f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/lib/swapper/swappers/ThorchainSwapper/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const thorchainApi: Swapper2Api = {
}))
}

return await getThorTradeQuote(input).then(async firstQuote => {
return await getThorTradeQuote({
...input,
}).then(async firstQuote => {
// If the first quote fails there is no need to check if the donation amount is below the minimum
if (firstQuote.isErr())
return mapTradeQuoteToTradeQuote2(firstQuote, receiveAddress, affiliateBps)
Expand All @@ -65,7 +67,10 @@ export const thorchainApi: Swapper2Api = {
If the donation amount is below the minimum,
we need to fetch a new quote with no affiliate fee
*/
await getThorTradeQuote({ ...input, affiliateBps: '0' })
await getThorTradeQuote({
...input,
affiliateBps: '0',
})
: firstQuote

return mapTradeQuoteToTradeQuote2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('getTradeQuote', () => {
sellAmountIncludingProtocolFeesCryptoBaseUnit: '713014679420',
buyAsset: ETH,
sellAsset: FOX_MAINNET,
slippageTolerancePercentage: '0.04357',
}

const maybeTradeQuote = await getThorTradeQuote(input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
import type { HDWallet } from '@shapeshiftoss/hdwallet-core'
import type { Result } from '@sniptt/monads'
import { Err, Ok } from '@sniptt/monads'
import { getDefaultSlippagePercentageForSwapper } from 'constants/constants'
import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton'
import { baseUnitToPrecision, bn, bnOrZero, convertPrecision } from 'lib/bignumber/bignumber'
import { fromBaseUnit, toBaseUnit } from 'lib/math'
Expand All @@ -32,6 +33,10 @@ import { THORCHAIN_FIXED_PRECISION } from 'lib/swapper/swappers/ThorchainSwapper
import { getQuote } from 'lib/swapper/swappers/ThorchainSwapper/utils/getQuote/getQuote'
import { getUtxoTxFees } from 'lib/swapper/swappers/ThorchainSwapper/utils/txFeeHelpers/utxoTxFees/getUtxoTxFees'
import { getThorTxInfo as getUtxoThorTxInfo } from 'lib/swapper/swappers/ThorchainSwapper/utxo/utils/getThorTxData'
import {
convertBasisPointsToDecimalPercentage,
convertDecimalPercentageToBasisPoints,
} from 'state/slices/tradeQuoteSlice/utils'

import { isNativeEvmAsset } from '../../utils/helpers/helpers'
import { getEvmTxFees } from '../utils/txFeeHelpers/evmTxFees/getEvmTxFees'
Expand All @@ -58,9 +63,13 @@ export const getThorTradeQuote = async (
receiveAddress,
affiliateBps,
wallet,
slippageTolerancePercentage,
} = input

const { chainId: buyAssetChainId } = fromAssetId(buyAsset.assetId)
const slippageBps = convertDecimalPercentageToBasisPoints(
slippageTolerancePercentage ?? getDefaultSlippagePercentageForSwapper(SwapperName.Thorchain),
).toString()

const chainAdapterManager = getChainAdapterManager()
const sellAdapter = chainAdapterManager.get(chainId)
Expand Down Expand Up @@ -91,19 +100,22 @@ export const getThorTradeQuote = async (
sellAmountCryptoBaseUnit,
receiveAddress,
affiliateBps,
slippageBps,
})

if (maybeQuote.isErr()) return Err(maybeQuote.unwrapErr())

const thornodeQuote = maybeQuote.unwrap()
const {
slippage_bps: slippageBps,
slippage_bps: recommendedSlippageBps,
fees,
expected_amount_out: expectedAmountOutThorBaseUnit,
memo,
} = thornodeQuote

const slippagePercentage = bn(slippageBps).div(1000)
const recommendedSlippageDecimalPercentage = convertBasisPointsToDecimalPercentage(
recommendedSlippageBps.toString(),
).toString()

const rate = (() => {
const THOR_PRECISION = 8
Expand Down Expand Up @@ -139,7 +151,7 @@ export const getThorTradeQuote = async (
})()

const commonQuoteFields = {
recommendedSlippage: slippagePercentage.div(100).toString(),
recommendedSlippage: recommendedSlippageDecimalPercentage,
}

const commonStepFields = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export const getQuote = async ({
sellAmountCryptoBaseUnit,
receiveAddress,
affiliateBps = '0',
slippageBps,
}: {
sellAsset: Asset
buyAssetId: AssetId
sellAmountCryptoBaseUnit: string
// Receive address is optional for THOR quotes, and will be in case we are getting a quote with a missing manual receive address
receiveAddress: string | undefined
affiliateBps: string
slippageBps: string
}): Promise<Result<ThornodeQuoteResponseSuccess, SwapErrorRight>> => {
const buyPoolId = assetIdToPoolAssetId({ assetId: buyAssetId })
const sellPoolId = assetIdToPoolAssetId({ assetId: sellAsset.assetId })
Expand All @@ -61,6 +63,7 @@ export const getQuote = async ({
destination: parsedReceiveAddress,
affiliate_bps: affiliateBps,
affiliate: THORCHAIN_AFFILIATE_NAME,
tolerance_bps: slippageBps,
})
const daemonUrl = getConfig().REACT_APP_THORCHAIN_NODE_URL
const maybeData = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import { getThorTxInfo } from 'lib/swapper/swappers/ThorchainSwapper/utxo/utils/getThorTxData'
import { assertUnreachable } from 'lib/utils'
import { createBuildCustomApiTxInput } from 'lib/utils/evm'
import { convertDecimalPercentageToBasisPoints } from 'state/slices/tradeQuoteSlice/utils'

import { isNativeEvmAsset } from '../../utils/helpers/helpers'
import { getQuote } from './getQuote/getQuote'
Expand Down Expand Up @@ -46,6 +47,7 @@ export const getSignTxFromQuote = async ({
const { recommendedSlippage } = quote

const slippageTolerance = slippageTolerancePercentage ?? recommendedSlippage
const slippageBps = convertDecimalPercentageToBasisPoints(slippageTolerance).toString()

const {
buyAsset,
Expand Down Expand Up @@ -85,6 +87,7 @@ export const getSignTxFromQuote = async ({
sellAmountCryptoBaseUnit,
receiveAddress,
affiliateBps,
slippageBps,
})

if (maybeThornodeQuote.isErr()) throw maybeThornodeQuote.unwrapErr()
Expand Down Expand Up @@ -122,6 +125,7 @@ export const getSignTxFromQuote = async ({
sellAmountCryptoBaseUnit,
receiveAddress,
affiliateBps,
slippageBps,
})

if (maybeThornodeQuote.isErr()) throw maybeThornodeQuote.unwrapErr()
Expand Down

0 comments on commit ff5533f

Please sign in to comment.