Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
address feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Sep 11, 2023
1 parent 9080090 commit ff3671b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Percent } from '@uniswap/sdk-core'
import JSBI from 'jsbi'
import { Fraction } from '@uniswap/sdk-core'

export const FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'

Expand All @@ -13,7 +13,7 @@ export const ONE = JSBI.BigInt(1)
export const FIVE = JSBI.BigInt(5)
export const _997 = JSBI.BigInt(997)
export const _1000 = JSBI.BigInt(1000)
export const _10000 = JSBI.BigInt(10000)
export const BASIS_POINTS = JSBI.BigInt(10000)

export const ZERO_FRACTION = new Fraction(ZERO, ONE)
export const ONE_FRACTION = new Fraction(ONE)
export const ZERO_PERCENT = new Percent(ZERO)
export const ONE_HUNDRED_PERCENT = new Percent(ONE)
9 changes: 4 additions & 5 deletions src/entities/pair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ChainId, CurrencyAmount, Price, Token, WETH9 } from '@uniswap/sdk-core'
import { InsufficientInputAmountError } from '../errors'
import { computePairAddress, Pair } from './pair'
import { BigNumber } from '@ethersproject/bignumber'
import JSBI from 'jsbi'

describe('computePairAddress', () => {
it('should correctly compute the pool address', () => {
Expand Down Expand Up @@ -230,8 +229,8 @@ describe('Pair', () => {
// = 94 * 0.96
// = 90.24
// = 90 (rounded down)
const expectedOutputBlastAmount = JSBI.BigInt(90)
expect(outputBlastAmount.quotient).toEqual(expectedOutputBlastAmount)
const expectedOutputBlastAmount = "0.00000000000000009"
expect(outputBlastAmount.toExact()).toEqual(expectedOutputBlastAmount)
})

it('getInputAmount for input token BLASTERS and output token BLAST', () => {
Expand Down Expand Up @@ -270,8 +269,8 @@ describe('Pair', () => {
// = 100.518134715 + 1
// = 100 (rounded down) + 1
// = 101
const expectedInputBlasterAmount = JSBI.BigInt(101)
expect(inputBlasterAmount.quotient).toEqual(expectedInputBlasterAmount)
const expectedInputBlasterAmount = "0.000000101"
expect(inputBlasterAmount.toExact()).toEqual(expectedInputBlasterAmount)
})
})
})
Expand Down
61 changes: 29 additions & 32 deletions src/entities/pair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigintIsh, Price, sqrt, Token, CurrencyAmount, Fraction } from '@uniswap/sdk-core'
import { BigintIsh, Price, sqrt, Token, CurrencyAmount, Percent } from '@uniswap/sdk-core'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
import { pack, keccak256 } from '@ethersproject/solidity'
Expand All @@ -14,9 +14,7 @@ import {
_1000,
ONE,
ZERO,
_10000,
ZERO_FRACTION,
ONE_FRACTION
BASIS_POINTS, ONE_HUNDRED_PERCENT, ZERO_PERCENT
} from '../constants'
import { InsufficientReservesError, InsufficientInputAmountError } from '../errors'

Expand Down Expand Up @@ -186,10 +184,12 @@ export class Pair {
const inputReserve = this.reserveOf(inputAmount.currency)
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0)

const percentAfterSellFeesInDecimal = this.derivePercentAfterSellFeesInDecimal(inputAmount)
const inputAmountAfterTax = percentAfterSellFeesInDecimal.greaterThan(ZERO)
? CurrencyAmount.fromRawAmount(inputAmount.currency, percentAfterSellFeesInDecimal.multiply(inputAmount).quotient) // fraction.quotient will round down by itself, which is desired
: inputAmount
const percentAfterSellFees = this.derivePercentAfterSellFees(inputAmount)
const inputAmountAfterTax = percentAfterSellFees.greaterThan(ZERO_PERCENT) ?
CurrencyAmount.fromRawAmount(
inputAmount.currency,
percentAfterSellFees.multiply(inputAmount).quotient // fraction.quotient will round down by itself, which is desired
) : inputAmount;

const inputAmountWithFeeAndAfterTax = JSBI.multiply(inputAmountAfterTax.quotient, _997)
const numerator = JSBI.multiply(inputAmountWithFeeAndAfterTax, outputReserve.quotient)
Expand All @@ -203,13 +203,12 @@ export class Pair {
throw new InsufficientInputAmountError()
}

const percentAfterBuyFeesInDecimal = this.derivePercentAfterBuyFeesInDecimal(outputAmount)
const outputAmountAfterTax = percentAfterBuyFeesInDecimal.greaterThan(ZERO)
? CurrencyAmount.fromRawAmount(
const percentAfterBuyFees = this.derivePercentAfterBuyFees(outputAmount)
const outputAmountAfterTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT) ?
CurrencyAmount.fromRawAmount(
outputAmount.currency,
outputAmount.multiply(percentAfterBuyFeesInDecimal).quotient // fraction.quotient will round down by itself, which is desired
)
: outputAmount
outputAmount.multiply(percentAfterBuyFees).quotient // fraction.quotient will round down by itself, which is desired
) : outputAmount;
if (JSBI.equal(outputAmountAfterTax.quotient, ZERO)) {
throw new InsufficientInputAmountError()
}
Expand Down Expand Up @@ -261,13 +260,12 @@ export class Pair {
*/
public getInputAmount(outputAmount: CurrencyAmount<Token>): [CurrencyAmount<Token>, Pair] {
invariant(this.involvesToken(outputAmount.currency), 'TOKEN')
const percentAfterBuyFeesInDecimal = this.derivePercentAfterBuyFeesInDecimal(outputAmount)
const outputAmountBeforeTax = percentAfterBuyFeesInDecimal.greaterThan(ZERO)
? CurrencyAmount.fromRawAmount(
const percentAfterBuyFees = this.derivePercentAfterBuyFees(outputAmount)
const outputAmountBeforeTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT) ?
CurrencyAmount.fromRawAmount(
outputAmount.currency,
JSBI.add(outputAmount.divide(percentAfterBuyFeesInDecimal).quotient, ONE) // add 1 for rounding up
)
: outputAmount
JSBI.add(outputAmount.divide(percentAfterBuyFees).quotient, ONE) // add 1 for rounding up
) : outputAmount;

if (
JSBI.equal(this.reserve0.quotient, ZERO) ||
Expand All @@ -288,13 +286,12 @@ export class Pair {
JSBI.add(JSBI.divide(numerator, denominator), ONE) // add 1 here is part of the formula, no rounding needed here, since there will not be decimal at this point
)

const percentAfterSellFeesInDecimal = this.derivePercentAfterSellFeesInDecimal(inputAmount)
const inputAmountBeforeTax = percentAfterSellFeesInDecimal.greaterThan(ZERO)
? CurrencyAmount.fromRawAmount(
const percentAfterSellFees = this.derivePercentAfterSellFees(inputAmount)
const inputAmountBeforeTax = percentAfterSellFees.greaterThan(ZERO_PERCENT) ?
CurrencyAmount.fromRawAmount(
inputAmount.currency,
JSBI.add(inputAmount.divide(percentAfterSellFeesInDecimal).quotient, ONE) // add 1 for rounding up
)
: inputAmount
JSBI.add(inputAmount.divide(percentAfterSellFees).quotient, ONE) // add 1 for rounding up
) : inputAmount;
return [inputAmountBeforeTax, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))]
}

Expand Down Expand Up @@ -366,21 +363,21 @@ export class Pair {
)
}

private derivePercentAfterSellFeesInDecimal(inputAmount: CurrencyAmount<Token>): Fraction {
private derivePercentAfterSellFees(inputAmount: CurrencyAmount<Token>): Percent {
const sellFeeBips = inputAmount.currency.sellFeeBps
if (sellFeeBips?.gt(BigNumber.from(0))) {
return ONE_FRACTION.subtract(new Fraction(JSBI.BigInt(sellFeeBips), _10000))
return ONE_HUNDRED_PERCENT.subtract(new Percent(JSBI.BigInt(sellFeeBips)).divide(BASIS_POINTS))
} else {
return ZERO_FRACTION
return ZERO_PERCENT
}
}

private derivePercentAfterBuyFeesInDecimal(outputAmount: CurrencyAmount<Token>): Fraction {
private derivePercentAfterBuyFees(outputAmount: CurrencyAmount<Token>): Percent {
const buyFeeBps = outputAmount.currency.buyFeeBps
if (buyFeeBps?.gt(BigNumber.from(0))) {
return ONE_FRACTION.subtract(new Fraction(JSBI.BigInt(buyFeeBps), _10000))
return ONE_HUNDRED_PERCENT.subtract(new Percent(JSBI.BigInt(buyFeeBps)).divide(BASIS_POINTS))
} else {
return ZERO_FRACTION
return ZERO_PERCENT
}
}
}

0 comments on commit ff3671b

Please sign in to comment.