Skip to content

Commit

Permalink
Add calc for fees without frontend fee
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Oct 6, 2023
1 parent 31ce9db commit 4db9909
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
94 changes: 61 additions & 33 deletions packages/swap/src/helpers/orders/makeOrderCalculations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ export const makeOrderCalculations = ({
tokenId: buy.tokenId,
}

const poolSupply =
buy.tokenId === pool.tokenA.tokenId
? pool.tokenA.quantity
: pool.tokenB.quantity
const hasSupply = !Quantities.isGreaterThan(
buy.quantity,
poolSupply ?? Quantities.zero,
)

// lf is sell side % of quantity ie. XToken 100 * 1% = 1 XToken
const liquidityFee: Balance.Amount = getLiquidityProviderFee(pool.fee, sell)

Expand Down Expand Up @@ -98,41 +107,57 @@ export const makeOrderCalculations = ({
.integerValue(BigNumber.ROUND_CEIL),
}

// add up all that's being sold in sell terms
const sellWithFees = new BigNumber(sell.quantity)
.plus(feeInSellSideQuantities.batcherFee)
.plus(feeInSellSideQuantities.frontendFee)
const priceWithSlippage = Quantities.isZero(buyAmountWithSlippage.quantity)
? Quantities.zero
: asQuantity(
new BigNumber(sell.quantity)
.dividedBy(buyAmountWithSlippage.quantity)
.toString(),
)

const priceWithFees = Quantities.isZero(buy.quantity)
? new BigNumber(0)
: sellWithFees.dividedBy(buy.quantity)
const calculatePricesWithFees = (withFrontendFee?: boolean) => {
// add up all that's being sold in sell terms
const sellWithBatcher = new BigNumber(sell.quantity).plus(
feeInSellSideQuantities.batcherFee,
)
const sellWithFees = withFrontendFee
? sellWithBatcher.plus(feeInSellSideQuantities.frontendFee)
: sellWithBatcher

const priceWithSlippage = Quantities.isZero(buy.quantity)
? Quantities.zero
: new BigNumber(sell.quantity)
.dividedBy(buyAmountWithSlippage.quantity)
.toString()
const priceWithFees = Quantities.isZero(buy.quantity)
? new BigNumber(0)
: sellWithFees.dividedBy(buy.quantity)

const priceWithFeesAndSlippage = Quantities.isZero(buy.quantity)
? Quantities.zero
: sellWithFees.dividedBy(buyAmountWithSlippage.quantity).toString()
const priceWithFeesAndSlippage = Quantities.isZero(
buyAmountWithSlippage.quantity,
)
? Quantities.zero
: sellWithFees.dividedBy(buyAmountWithSlippage.quantity).toString()

// always based, if is limit it can lead to a weird percentage
const priceDifference = priceWithFees
.minus(priceBase)
.dividedBy(priceBase)
.times(100)
.toString()
// always based, if is limit it can lead to a weird percentage
const priceDifference = priceWithFees
.minus(priceBase)
.dividedBy(priceBase)
.times(100)
.toString()

const poolSupply =
buy.tokenId === pool.tokenA.tokenId
? pool.tokenA.quantity
: pool.tokenB.quantity
const hasSupply = !Quantities.isGreaterThan(
buy.quantity,
poolSupply ?? Quantities.zero,
)
return {
priceWithFees: asQuantity(priceWithFees),
priceWithFeesAndSlippage: asQuantity(priceWithFeesAndSlippage),
priceDifference: asQuantity(priceDifference),
}
}

const {
priceWithFees: withFees,
priceWithFeesAndSlippage: withFeesAndSlippage,
priceDifference: difference,
} = calculatePricesWithFees(true)
const {
priceWithFees: withFeesNoFEF,
priceWithFeesAndSlippage: withFeesAndSlippageNoFEF,
priceDifference: differenceNoFEF,
} = calculatePricesWithFees(false)
return {
cost: {
batcherFee: pool.batcherFee,
Expand All @@ -145,10 +170,13 @@ export const makeOrderCalculations = ({
prices: {
base: priceBase,
market: marketPrice,
withFees: asQuantity(priceWithFees),
withSlippage: asQuantity(priceWithSlippage),
withFeesAndSlippage: asQuantity(priceWithFeesAndSlippage),
difference: asQuantity(priceDifference),
withSlippage: priceWithSlippage,
withFees,
withFeesAndSlippage,
difference,
withFeesNoFEF,
withFeesAndSlippageNoFEF,
differenceNoFEF,
},
pool,
}
Expand Down
7 changes: 5 additions & 2 deletions packages/swap/src/translators/reactjs/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export type SwapOrderCalculation = Readonly<{
prices: {
base: Balance.Quantity
market: Balance.Quantity
withFees: Balance.Quantity
difference: Balance.Quantity
withSlippage: Balance.Quantity
withFees: Balance.Quantity
withFeesAndSlippage: Balance.Quantity
difference: Balance.Quantity
withFeesNoFEF: Balance.Quantity
withFeesAndSlippageNoFEF: Balance.Quantity
differenceNoFEF: Balance.Quantity
}
hasSupply: boolean
buyAmountWithSlippage: Balance.Amount
Expand Down

0 comments on commit 4db9909

Please sign in to comment.