Commit 53e3b8e9 authored by Miguel Cervera's avatar Miguel Cervera Committed by GitHub

Receive an option to decide if we calculate FOT tax or not (#146)

* Receive an option to decide if we calculate FOT tax or not

* Fix code style issues with Prettier

---------
Co-authored-by: default avatarLint Action <lint-action@samuelmeuli.com>
parent 4411fae6
dist dist
node_modules node_modules
.idea
...@@ -199,6 +199,13 @@ describe('Pair', () => { ...@@ -199,6 +199,13 @@ describe('Pair', () => {
BLASTERSSellFeeBps BLASTERSSellFeeBps
) )
let calculateFotFees: boolean = false
describe('when calculating FOT fees', () => {
beforeEach(() => {
calculateFotFees = true
})
describe('getOutputAmount', () => { describe('getOutputAmount', () => {
it('getOutputAmount for input token BLASTERS and output token BLAST', () => { it('getOutputAmount for input token BLASTERS and output token BLAST', () => {
const reserveBlasterAmount = CurrencyAmount.fromRawAmount(BLASTERS, '10000') const reserveBlasterAmount = CurrencyAmount.fromRawAmount(BLASTERS, '10000')
...@@ -207,7 +214,7 @@ describe('Pair', () => { ...@@ -207,7 +214,7 @@ describe('Pair', () => {
const pair = new Pair(reserveBlasterAmount, reserveBlastAmount) const pair = new Pair(reserveBlasterAmount, reserveBlastAmount)
const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS, '100') const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS, '100')
const [outputBlastAmount] = pair.getOutputAmount(inputBlastersAmount) const [outputBlastAmount] = pair.getOutputAmount(inputBlastersAmount, calculateFotFees)
// Theoretical amount out: // Theoretical amount out:
// (10000 * 997 * 100 * (1 - 3.5%) / (10000 * 1000 + 997 * 100 * (1 - 3.5%))) * (1 - 4%) // (10000 * 997 * 100 * (1 - 3.5%) / (10000 * 1000 + 997 * 100 * (1 - 3.5%))) * (1 - 4%)
...@@ -240,7 +247,7 @@ describe('Pair', () => { ...@@ -240,7 +247,7 @@ describe('Pair', () => {
const pair = new Pair(reserveBlasterAmount, reserveBlastAmount) const pair = new Pair(reserveBlasterAmount, reserveBlastAmount)
const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '91') const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '91')
const [inputBlasterAmount] = pair.getInputAmount(outputBlastAmount) const [inputBlasterAmount] = pair.getInputAmount(outputBlastAmount, calculateFotFees)
// Theoretical amount in: // Theoretical amount in:
// 10000 * 100 * (1 - 4%) * 1000 / ((10000 - 100 * (1 - 4%)) * 997) / (1 - 3.5%) // 10000 * 100 * (1 - 4%) * 1000 / ((10000 - 100 * (1 - 4%)) * 997) / (1 - 3.5%)
...@@ -274,6 +281,41 @@ describe('Pair', () => { ...@@ -274,6 +281,41 @@ describe('Pair', () => {
}) })
}) })
}) })
describe('when NOT calculating FOT fees', () => {
beforeEach(() => {
calculateFotFees = false
})
describe('getOutputAmount', () => {
it('getOutputAmount for input token BLASTERS and output token BLAST', () => {
const reserveBlasterAmount = CurrencyAmount.fromRawAmount(BLASTERS, '10000')
const reserveBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '10000')
const pair = new Pair(reserveBlasterAmount, reserveBlastAmount)
const inputBlastersAmount = CurrencyAmount.fromRawAmount(BLASTERS, '100')
const [outputBlastAmount] = pair.getOutputAmount(inputBlastersAmount, calculateFotFees)
const expectedOutputBlastAmount = '0.000000000000000098'
expect(outputBlastAmount.toExact()).toEqual(expectedOutputBlastAmount)
})
it('getInputAmount for input token BLASTERS and output token BLAST', () => {
const reserveBlasterAmount = CurrencyAmount.fromRawAmount(BLASTERS, '10000')
const reserveBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '10000')
const pair = new Pair(reserveBlasterAmount, reserveBlastAmount)
const outputBlastAmount = CurrencyAmount.fromRawAmount(BLAST, '91')
const [inputBlasterAmount] = pair.getInputAmount(outputBlastAmount, calculateFotFees)
const expectedInputBlasterAmount = '0.000000093'
expect(inputBlasterAmount.toExact()).toEqual(expectedInputBlasterAmount)
})
})
})
})
describe('miscellaneous', () => { describe('miscellaneous', () => {
it('getLiquidityMinted:0', async () => { it('getLiquidityMinted:0', async () => {
const tokenA = new Token(3, '0x0000000000000000000000000000000000000001', 18) const tokenA = new Token(3, '0x0000000000000000000000000000000000000001', 18)
......
...@@ -178,7 +178,10 @@ export class Pair { ...@@ -178,7 +178,10 @@ export class Pair {
* *
* @param inputAmount * @param inputAmount
*/ */
public getOutputAmount(inputAmount: CurrencyAmount<Token>): [CurrencyAmount<Token>, Pair] { public getOutputAmount(
inputAmount: CurrencyAmount<Token>,
calculateFotFees: boolean = false
): [CurrencyAmount<Token>, Pair] {
invariant(this.involvesToken(inputAmount.currency), 'TOKEN') invariant(this.involvesToken(inputAmount.currency), 'TOKEN')
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO)) { if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO)) {
throw new InsufficientReservesError() throw new InsufficientReservesError()
...@@ -186,7 +189,7 @@ export class Pair { ...@@ -186,7 +189,7 @@ export class Pair {
const inputReserve = this.reserveOf(inputAmount.currency) const inputReserve = this.reserveOf(inputAmount.currency)
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0) const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0)
const percentAfterSellFees = this.derivePercentAfterSellFees(inputAmount) const percentAfterSellFees = calculateFotFees ? this.derivePercentAfterSellFees(inputAmount) : ZERO_PERCENT
const inputAmountAfterTax = percentAfterSellFees.greaterThan(ZERO_PERCENT) const inputAmountAfterTax = percentAfterSellFees.greaterThan(ZERO_PERCENT)
? CurrencyAmount.fromRawAmount( ? CurrencyAmount.fromRawAmount(
inputAmount.currency, inputAmount.currency,
...@@ -206,7 +209,7 @@ export class Pair { ...@@ -206,7 +209,7 @@ export class Pair {
throw new InsufficientInputAmountError() throw new InsufficientInputAmountError()
} }
const percentAfterBuyFees = this.derivePercentAfterBuyFees(outputAmount) const percentAfterBuyFees = calculateFotFees ? this.derivePercentAfterBuyFees(outputAmount) : ZERO_PERCENT
const outputAmountAfterTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT) const outputAmountAfterTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT)
? CurrencyAmount.fromRawAmount( ? CurrencyAmount.fromRawAmount(
outputAmount.currency, outputAmount.currency,
...@@ -265,9 +268,12 @@ export class Pair { ...@@ -265,9 +268,12 @@ export class Pair {
* *
* @param outputAmount * @param outputAmount
*/ */
public getInputAmount(outputAmount: CurrencyAmount<Token>): [CurrencyAmount<Token>, Pair] { public getInputAmount(
outputAmount: CurrencyAmount<Token>,
calculateFotFees: boolean = false
): [CurrencyAmount<Token>, Pair] {
invariant(this.involvesToken(outputAmount.currency), 'TOKEN') invariant(this.involvesToken(outputAmount.currency), 'TOKEN')
const percentAfterBuyFees = this.derivePercentAfterBuyFees(outputAmount) const percentAfterBuyFees = calculateFotFees ? this.derivePercentAfterBuyFees(outputAmount) : ZERO_PERCENT
const outputAmountBeforeTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT) const outputAmountBeforeTax = percentAfterBuyFees.greaterThan(ZERO_PERCENT)
? CurrencyAmount.fromRawAmount( ? CurrencyAmount.fromRawAmount(
outputAmount.currency, outputAmount.currency,
...@@ -294,7 +300,7 @@ export class Pair { ...@@ -294,7 +300,7 @@ 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 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 percentAfterSellFees = this.derivePercentAfterSellFees(inputAmount) const percentAfterSellFees = calculateFotFees ? this.derivePercentAfterSellFees(inputAmount) : ZERO_PERCENT
const inputAmountBeforeTax = percentAfterSellFees.greaterThan(ZERO_PERCENT) const inputAmountBeforeTax = percentAfterSellFees.greaterThan(ZERO_PERCENT)
? CurrencyAmount.fromRawAmount( ? CurrencyAmount.fromRawAmount(
inputAmount.currency, inputAmount.currency,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment