pair price display fixes

This commit is contained in:
Tim
2024-03-29 16:48:12 -04:00
parent 12aa7bec5d
commit bb1bc3d966
2 changed files with 19 additions and 10 deletions

View File

@@ -120,7 +120,10 @@ export function pairPriceAddr(chainId, baseTokenAddr, quoteTokenAddr, price) {
export function flipInversionPreference(chainId, base, quote) {
const k = pairKey(chainId, base, quote)
const inverted = base.a > quote.a
const token0 = !inverted ? base.a : quote.a
const token1 = inverted ? base.a : quote.a
const k = [chainId, token0, token1];
const prefs = usePrefStore()
prefs.inverted[k] = !prefs.inverted[k]
}
@@ -128,36 +131,42 @@ export function flipInversionPreference(chainId, base, quote) {
export function inversionPreference(chainId, base, quote) {
// returns the user preference for whether to invert this pair or not
const invertedKey = pairKey(chainId, base, quote);
const inputInverted = base.a > quote.a
const token0 = !inputInverted ? base.a : quote.a
const token1 = inputInverted ? base.a : quote.a
const key = [chainId, token0, token1];
const prefs = usePrefStore()
if (!(invertedKey in prefs.inverted)) {
if (!(key in prefs.inverted)) {
// todo prefer stablecoins as the quote asset
let inverted = false;
let preferInverted = false;
for (const q of QUOTE_SYMBOLS) {
if (quote.s === q)
break // definitely not inverted
if (base.s === q) {
inverted = true
preferInverted = true
break // definitely inverted
}
}
prefs.inverted[invertedKey] = inverted
prefs.inverted[key] = preferInverted
}
return prefs.inverted[invertedKey]
console.log('inversion preference', base, quote, prefs.inverted[key], inputInverted)
return prefs.inverted[key] !== inputInverted
}
export function pairPrice(chainId, baseToken, quoteToken, price) {
// console.warn('pairPrice', chainId, baseToken, quoteToken, price, decimals)
if (price === null || price === undefined)
return null
const decimals = quoteToken.d - baseToken.d
// console.log('pairPrice', chainId, baseToken, quoteToken, price, decimals)
if (decimals >= 0)
price /= 10 ** decimals
else
price *= 10 ** -decimals
// console.log('adjusted pairPrice', price)
if (inversionPreference(chainId, baseToken, quoteToken))
price = 1 / price
// console.log('inverted?', price)
return price
}