From d8c05dd8d3173a67200f2374e1e8b3fa58a3f57e Mon Sep 17 00:00:00 2001 From: jsy1218 <91580504+jsy1218@users.noreply.github.com> Date: Tue, 17 Oct 2023 15:34:11 -0700 Subject: [PATCH] Fix: FOT token tax retrieval source (#148) * v3.2.2 * fix fot token tax retrieval source * Fix code style issues with Prettier * revert version change here --------- Co-authored-by: Lint Action --- src/entities/pair.test.ts | 24 ++++++++++++++++++++---- src/entities/pair.ts | 9 +++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/entities/pair.test.ts b/src/entities/pair.test.ts index a819dbffc..ccc45bc4a 100644 --- a/src/entities/pair.test.ts +++ b/src/entities/pair.test.ts @@ -186,6 +186,14 @@ describe('Pair', () => { BLASTBuyFeeBps, BLASTSellFeeBps ) + const BLAST_WIHTOUT_TAX = new Token( + ChainId.MAINNET, + '0x3ed643e9032230f01c6c36060e305ab53ad3b482', + 18, + 'BLAST', + 'BLAST', + false + ) const BLASTERSBuyFeeBps = BigNumber.from(300) const BLASTERSSellFeeBps = BigNumber.from(350) const BLASTERS = new Token( @@ -198,6 +206,14 @@ describe('Pair', () => { BLASTERSBuyFeeBps, BLASTERSSellFeeBps ) + const BLASTERS_WITHOUT_TAX = new Token( + ChainId.MAINNET, + '0xab98093C7232E98A47D7270CE0c1c2106f61C73b', + 9, + 'BLAST', + 'BLASTERS', + false + ) let calculateFotFees: boolean = false @@ -213,7 +229,7 @@ describe('Pair', () => { const pair = new Pair(reserveBlasterAmount, reserveBlastAmount) - const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS, '100') + const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS_WITHOUT_TAX, '100') const [outputBlastAmount] = pair.getOutputAmount(inputBlastersAmount, calculateFotFees) // Theoretical amount out: @@ -246,7 +262,7 @@ describe('Pair', () => { const pair = new Pair(reserveBlasterAmount, reserveBlastAmount) - const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '91') + const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST_WIHTOUT_TAX, '91') const [inputBlasterAmount] = pair.getInputAmount(outputBlastAmount, calculateFotFees) // Theoretical amount in: @@ -294,7 +310,7 @@ describe('Pair', () => { const pair = new Pair(reserveBlasterAmount, reserveBlastAmount) - const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS, '100') + const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS_WITHOUT_TAX, '100') const [outputBlastAmount] = pair.getOutputAmount(inputBlastersAmount, calculateFotFees) const expectedOutputBlastAmount = '0.000000000000000098' @@ -307,7 +323,7 @@ describe('Pair', () => { const pair = new Pair(reserveBlasterAmount, reserveBlastAmount) - const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '91') + const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST_WIHTOUT_TAX, '91') const [inputBlasterAmount] = pair.getInputAmount(outputBlastAmount, calculateFotFees) const expectedInputBlasterAmount = '0.000000093' diff --git a/src/entities/pair.ts b/src/entities/pair.ts index 4bac5e43c..aff809f01 100644 --- a/src/entities/pair.ts +++ b/src/entities/pair.ts @@ -177,6 +177,7 @@ export class Pair { * outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000) * * @param inputAmount + * @param calculateFotFees */ public getOutputAmount( inputAmount: CurrencyAmount, @@ -379,7 +380,9 @@ export class Pair { } private derivePercentAfterSellFees(inputAmount: CurrencyAmount): Percent { - const sellFeeBips = inputAmount.currency.sellFeeBps + const sellFeeBips = this.token0.wrapped.equals(inputAmount.wrapped.currency) + ? this.token0.wrapped.sellFeeBps + : this.token1.wrapped.sellFeeBps if (sellFeeBips?.gt(BigNumber.from(0))) { return ONE_HUNDRED_PERCENT.subtract(new Percent(JSBI.BigInt(sellFeeBips)).divide(BASIS_POINTS)) } else { @@ -388,7 +391,9 @@ export class Pair { } private derivePercentAfterBuyFees(outputAmount: CurrencyAmount): Percent { - const buyFeeBps = outputAmount.currency.buyFeeBps + const buyFeeBps = this.token0.wrapped.equals(outputAmount.wrapped.currency) + ? this.token0.wrapped.buyFeeBps + : this.token1.wrapped.buyFeeBps if (buyFeeBps?.gt(BigNumber.from(0))) { return ONE_HUNDRED_PERCENT.subtract(new Percent(JSBI.BigInt(buyFeeBps)).divide(BASIS_POINTS)) } else {