Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
pass sellAmountBeforeFee to necessary places
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Mar 14, 2022
1 parent b5c1c00 commit a65fa69
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/custom/hooks/useSwapCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ async function _swap(params: SwapParams): Promise<string> {
}),
// unadjusted outputAmount
outputAmount: outputAmountWithSlippage,
sellAmountBeforeFee: trade.inputAmountWithoutFee,
// pass Trade feeAmount as raw string or give 0
feeAmount: fee?.feeAsCurrency,
sellToken,
Expand Down
1 change: 1 addition & 0 deletions src/custom/state/orders/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const generateOrder = ({ owner, sellToken, buyToken }: GenerateOrderParam
sellToken: sellToken.address.replace('0x', ''), // address, without '0x' prefix
buyToken: buyToken.address.replace('0x', ''), // address, without '0x' prefix
sellAmount: sellAmount.toString(RADIX_DECIMAL), // in atoms
sellAmountBeforeFee: sellAmount.toString(RADIX_DECIMAL), // in atoms
buyAmount: buyAmount.toString(RADIX_DECIMAL), // in atoms
// 20sec - 4min
validTo: Date.now() / 1000 + randomIntInRangeExcept(20, 240), // uint32. unix timestamp, seconds, use new Date(validTo * 1000)
Expand Down
1 change: 1 addition & 0 deletions src/custom/state/orders/updaters/GpOrdersUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function _transformGpOrderToStoreOrder(

const storeOrder: Order = {
...order,
sellAmountBeforeFee: order.executedSellAmountBeforeFees,
inputToken,
outputToken,
id,
Expand Down
25 changes: 23 additions & 2 deletions src/custom/state/orders/updaters/UnfillableOrdersUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getBestQuote, PriceInformation } from 'utils/price'
import { isOrderUnfillable } from 'state/orders/utils'
import useGetGpPriceStrategy, { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy'
import { getPromiseFulfilledValue } from 'utils/misc'
import { BigNumber } from '@ethersproject/bignumber'

/**
* Thin wrapper around `getBestPrice` that builds the params and returns null on failure
Expand All @@ -24,9 +25,22 @@ async function _getOrderPrice(chainId: ChainId, order: Order, strategy: GpPriceS
let amount, baseToken, quoteToken

if (order.kind === 'sell') {
amount = order.sellAmount.toString()
// this order sell amount is sellAmountAfterFees..
// this is an issue as it will be adjusted again in the backend
// e.g order submitted w/sellAmount adjusted for fee: 995, we re-query 995
// e.g backend adjusts for fee again, 990 is used. We need to avoid double fee adjusting
// e.g so here we need to pass the sellAmountBeforeFees
amount = order.sellAmountBeforeFee.toString()
baseToken = order.sellToken
quoteToken = order.buyToken

console.debug('[UNFILLABLES]::SELL AMOUNT', order.sellAmount.toString())
console.debug('[UNFILLABLES]::FEE AMOUNT', order.feeAmount.toString())
console.debug(
'[UNFILLABLES]::SELL AMOUNT + FEE AMOUNT',
BigNumber.from(order.sellAmount).add(BigNumber.from(order.feeAmount)).toString()
)
console.debug('[UNFILLABLES]::EXECUTED SELL AMOUNT BEFORE FEES', amount)
} else {
amount = order.buyAmount.toString()
baseToken = order.buyToken
Expand All @@ -45,8 +59,15 @@ async function _getOrderPrice(chainId: ChainId, order: Order, strategy: GpPriceS
toDecimals: order.outputToken.decimals,
validTo: timestamp(order.validTo),
}

// console.debug('[UNFILLABLE]::BEFORE PRICE::', quoteParams.amount)
try {
// if (order.kind === 'sell') {
// // we need to calculate the fee separately to add to the sellAmount here
// const { quote } = await getQuote(quoteParams)
// const { feeAmount } = quote
// quoteParams.amount = BigNumber.from(quoteParams.amount).add(BigNumber.from(feeAmount)).toString()
// console.debug('[UNFILLABLE]::AFTER PRICE::', quoteParams.amount)
// }
return getBestQuote({ strategy, quoteParams, fetchFee: false, isPriceRefresh: false })
} catch (e) {
return null
Expand Down

0 comments on commit a65fa69

Please sign in to comment.