fix: dont block trade when price impact is favorable (#6261)

* fix: dont block trade when price impact is favorable

* fix: add comment
This commit is contained in:
eddie 2023-04-03 14:13:27 -07:00 committed by GitHub
parent 7b9a23d920
commit fb8217ddea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

@ -122,6 +122,9 @@ describe('prices', () => {
it('0 for undefined', () => {
expect(warningSeverity(undefined)).toEqual(0)
})
it('0 for negative', () => {
expect(warningSeverity(new Percent(-1))).toEqual(0)
})
it('correct for 0', () => {
expect(warningSeverity(new Percent(0))).toEqual(0)
})

@ -85,6 +85,12 @@ const IMPACT_TIERS = [
type WarningSeverity = 0 | 1 | 2 | 3 | 4
export function warningSeverity(priceImpact: Percent | undefined): WarningSeverity {
if (!priceImpact) return 0
// This function is used to calculate the Severity level for % changes in USD value and Price Impact.
// Price Impact is always an absolute value (conceptually always negative, but represented in code with a positive value)
// The USD value change can be positive or negative, and it follows the same standard as Price Impact (positive value is the typical case of a loss due to slippage).
// We don't want to return a warning level for a favorable/profitable change, so when the USD value change is negative we return 0.
// TODO (WEB-3133): Disambiguate Price Impact and USD value change, and flip the sign of USD Value change.
if (priceImpact.lessThan(0)) return 0
let impact: WarningSeverity = IMPACT_TIERS.length as WarningSeverity
for (const impactLevel of IMPACT_TIERS) {
if (impactLevel.lessThan(priceImpact)) return impact