-
Notifications
You must be signed in to change notification settings - Fork 95
feat: Handle fee-on-transfer token taxes in price impact calculations #50
Conversation
@@ -1160,6 +1192,50 @@ describe('Trade', () => { | |||
}) | |||
// v3 sdk price impact tests | |||
describe('#priceImpact', () => { | |||
describe('with FOT sell fees', () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
src/entities/trade.ts
Outdated
|
||
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this 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.ts
Outdated
} | ||
|
||
/** | ||
* The cached result of the price impact computation, excluding FOT fees |
There was a problem hiding this comment.
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?
There was a problem hiding this 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!
Now that
sdk-core
Token returns information about token taxes and V2 trade routes are constructed withpre tax input amount
andpost-tax output amount
in mind, we should updatepriceImpact
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: