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

feat: Handle fee-on-transfer token taxes in price impact calculations #50

Merged
merged 6 commits into from
Sep 20, 2023

Conversation

tinaszheng
Copy link
Contributor

Now that sdk-core Token returns information about token taxes and V2 trade routes are constructed with pre tax input amount and post-tax output amount in mind, we should update priceImpact calculations to exclude taxed differences. More in the diagram here, but we should be reading from the values right before and after the "Swap" step:

image

@@ -1160,6 +1192,50 @@ describe('Trade', () => {
})
// v3 sdk price impact tests
describe('#priceImpact', () => {
describe('with FOT sell fees', () => {
Copy link
Member

@jsy1218 jsy1218 Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - technically we only support FOT that goes through v2-pools, not v3-pools. But I think it's fine to write unit test coverage against v3 routes, since Trade object is being used by v2, v3 and mixed routes, as a common object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, updated to v2 routes since it should match our real world use case :D

Comment on lines 197 to 203

const postTaxInputAmount = inputAmount.multiply(new Fraction(ONE).subtract(this.inputTax))
spotOutputAmount = spotOutputAmount.add(midPrice.quote(postTaxInputAmount))
}

const priceImpact = spotOutputAmount.subtract(this.outputAmount).divide(spotOutputAmount)
const preTaxOutputAmount = this.outputAmount.divide(new Fraction(ONE).subtract(this.outputTax))
const priceImpact = spotOutputAmount.subtract(preTaxOutputAmount).divide(spotOutputAmount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - do we need to distinguish the math between exact in and exact out for FOT swap?

Copy link

@adjkant adjkant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! a few style/docs/tests nits

src/entities/trade.test.ts Outdated Show resolved Hide resolved
src/entities/trade.test.ts Show resolved Hide resolved
src/entities/trade.ts Outdated Show resolved Hide resolved
}

/**
* The cached result of the price impact computation, excluding FOT fees
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this makes it sound like it doesn't consider FOT fees - maybe remove that part and update the getter function's comment to explain in 1-2 lines how this formula works?

Copy link

@adjkant adjkant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving assuming comments settled on the doc/spaces nits!

@tinaszheng
Copy link
Contributor Author

image

@tinaszheng tinaszheng merged commit 756e560 into main Sep 20, 2023
1 check passed
@tinaszheng tinaszheng deleted the tina/price-impact-fot branch September 20, 2023 16:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants