Skip to content

Commit

Permalink
fix: always pass slippage to /portal endpoint (#8293)
Browse files Browse the repository at this point in the history
* feat: align portals default slippage with upstream default

* fix: always pass slippage to /portal endpoint

---------

Co-authored-by: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com>
Co-authored-by: woody <125113430+woodenfurniture@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 5, 2024
1 parent 54b7579 commit 58e6d60
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Result } from '@sniptt/monads'
import { Err, Ok } from '@sniptt/monads'
import { zeroAddress } from 'viem'

import { getDefaultSlippageDecimalPercentageForSwapper } from '../../..'
import type {
GetEvmTradeQuoteInputBase,
SingleHopTradeQuoteSteps,
Expand Down Expand Up @@ -78,8 +79,10 @@ export async function getPortalsTradeQuote(
.toNumber()

const userSlippageTolerancePercentageDecimalOrDefault = input.slippageTolerancePercentageDecimal
? Number(input.slippageTolerancePercentageDecimal)
: undefined // Use auto slippage if no user preference is provided
? bnOrZero(input.slippageTolerancePercentageDecimal).times(100).toNumber()
: bnOrZero(getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Portals))
.times(100)
.toNumber()

if (!sendAddress) return Err(makeSwapErrorRight({ message: 'missing sendAddress' }))

Expand Down Expand Up @@ -111,9 +114,7 @@ export async function getPortalsTradeQuote(
inputToken,
outputToken,
inputAmount: sellAmountIncludingProtocolFeesCryptoBaseUnit,
slippageTolerancePercentage: userSlippageTolerancePercentageDecimalOrDefault
? userSlippageTolerancePercentageDecimalOrDefault * 100
: undefined,
slippageTolerancePercentage: userSlippageTolerancePercentageDecimalOrDefault,
partner: getTreasuryAddressFromChainId(sellAsset.chainId),
feePercentage: affiliateBpsPercentage,
validate: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,16 @@ export async function getPortalsTradeRate(
const outputToken = `${portalsNetwork}:${buyAssetAddress}`

const userSlippageTolerancePercentageDecimalOrDefault = input.slippageTolerancePercentageDecimal
? Number(input.slippageTolerancePercentageDecimal)
: undefined // Use auto slippage if no user preference is provided
? bnOrZero(input.slippageTolerancePercentageDecimal).times(100).toNumber()
: bnOrZero(getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Portals))
.times(100)
.toNumber()

const quoteEstimateResponse = await fetchPortalsTradeEstimate({
inputToken,
outputToken,
inputAmount: sellAmountIncludingProtocolFeesCryptoBaseUnit,
slippageTolerancePercentage: userSlippageTolerancePercentageDecimalOrDefault
? userSlippageTolerancePercentageDecimalOrDefault * 100
: bnOrZero(getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Portals))
.times(100)
.toNumber(),
slippageTolerancePercentage: userSlippageTolerancePercentageDecimalOrDefault,
swapperConfig,
})
// Use the quote estimate endpoint to get a quote without a wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type PortalsTradeOrderParams = {
inputToken: string
inputAmount: string
outputToken: string
slippageTolerancePercentage?: number
slippageTolerancePercentage: number
// Technically optional, but we always want to use an affiliate addy
partner: string
feePercentage?: number
Expand Down Expand Up @@ -97,9 +97,7 @@ export const fetchPortalsTradeOrder = async ({
validate: validate.toString(),
})

if (slippageTolerancePercentage !== undefined) {
params.append('slippageTolerancePercentage', slippageTolerancePercentage.toFixed(2)) // Portals API expects a string with at most 2 decimal places
}
params.append('slippageTolerancePercentage', slippageTolerancePercentage.toFixed(2)) // Portals API expects a string with at most 2 decimal places

if (feePercentage) {
params.append('feePercentage', feePercentage.toString())
Expand Down

0 comments on commit 58e6d60

Please sign in to comment.