Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
correct price negatives
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 3, 2021
1 parent a64c916 commit 0bfda08
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/custom/utils/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,18 @@ export function getValidParams(params: PriceQuoteParams) {
export function calculateFallbackPriceImpact(initialValue: string, finalValue: string) {
const initialValueBn = new BigNumberJs(initialValue)
const finalValueBn = new BigNumberJs(finalValue)
// TODO: use correct formula
// ((IV - FV) / IV / 2) * 100
const [numerator, denominator] = initialValueBn.minus(finalValueBn).div(initialValueBn).div('2').toFraction()

console.debug(
'[calculateFallbackPriceImpact]::',
initialValueBn.toString(10),
finalValueBn.toString(10),
numerator.toString(10),
denominator.toString(10)
)
// ((finalValue - initialValue) / initialValue / 2) * 100
const output = finalValueBn.minus(initialValueBn).div(initialValueBn).div('2')
const [numerator, denominator] = output.toFraction()

const isPositive = numerator.isNegative() === denominator.isNegative()

const priceImpactPercentage =
// Uni sdk hates negative numbers so we need to do this
numerator.isNegative() || denominator.isNegative()
? new Percent(numerator.absoluteValue().toString(10), denominator.absoluteValue().toString(10)).multiply('-1')
: new Percent(numerator.toString(10), denominator.toString(10))
const percentage = new Percent(numerator.abs().toString(10), denominator.abs().toString(10))
// UI shows NEGATIVE impact as a POSITIVE effect, so we need to swap the sign here
// see FiatValue: line 38
const impact = isPositive ? percentage.multiply('-1') : percentage

console.debug(`[calculateFallbackPriceImpact]::${priceImpactPercentage.toSignificant(2)}%`)
console.debug(`[calculateFallbackPriceImpact]::${impact.toSignificant(2)}%`)

return priceImpactPercentage
return impact
}

0 comments on commit 0bfda08

Please sign in to comment.