From 59c2e446dbdf60083c83aa6f9a98a7e49e474ae2 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:30:41 -0400 Subject: [PATCH] feat: Add WSTETH/WETH as supported conversion (backport #410) (#411) * feat: Add WSTETH/WETH as supported conversion (#410) * feat: Add WSTETH/WETH as supported conversion * one conversion path deeper * lint (cherry picked from commit 8876d360ae483f5cd4b3bcde950a4e111239571e) # Conflicts: # oracle/util.go * fix conflicts --------- Co-authored-by: ryanbajollari <54822716+rbajollari@users.noreply.github.com> Co-authored-by: rbajollari --- config/supported_assets.go | 15 ++++++++------- oracle/convert.go | 23 +++++++++++++++++++++++ oracle/util.go | 9 +++++++-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/config/supported_assets.go b/config/supported_assets.go index 0aa7b4fd..2e9a7a0c 100644 --- a/config/supported_assets.go +++ b/config/supported_assets.go @@ -47,13 +47,14 @@ var ( {Base: "INJ", Quote: "USD"}: {}, {Base: "TIA", Quote: "USD"}: {}, - {Base: "OSMO", Quote: "USDT"}: {}, - {Base: "JUNO", Quote: "USDT"}: {}, - {Base: "WETH", Quote: "USDC"}: {}, - {Base: "WBTC", Quote: "BTC"}: {}, - {Base: "WBTC", Quote: "WETH"}: {}, - {Base: "INJ", Quote: "USDT"}: {}, - {Base: "TIA", Quote: "USDT"}: {}, + {Base: "OSMO", Quote: "USDT"}: {}, + {Base: "JUNO", Quote: "USDT"}: {}, + {Base: "WETH", Quote: "USDC"}: {}, + {Base: "WBTC", Quote: "BTC"}: {}, + {Base: "WBTC", Quote: "WETH"}: {}, + {Base: "INJ", Quote: "USDT"}: {}, + {Base: "TIA", Quote: "USDT"}: {}, + {Base: "WSTETH", Quote: "WETH"}: {}, } SupportedUniswapCurrencies = map[string]struct{}{ diff --git a/oracle/convert.go b/oracle/convert.go index d1463b23..454e3f98 100644 --- a/oracle/convert.go +++ b/oracle/convert.go @@ -1,6 +1,7 @@ package oracle import ( + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/rs/zerolog" @@ -18,10 +19,32 @@ func ConvertRatesToUSD(rates types.CurrencyPairDec) types.CurrencyPairDec { continue } + var converted bool for cpConvert, rateConvert := range rates { if cpConvert.Quote == config.DenomUSD && cpConvert.Base == cp.Quote { convertedPair := types.CurrencyPair{Base: cp.Base, Quote: config.DenomUSD} convertedRates[convertedPair] = rate.Mul(rateConvert) + converted = true + } + } + + // If the rate is not converted, try one conversion path deeper. + if !converted { + for cpConvert, rateConvert := range rates { + if cpConvert.Base == cp.Quote { + var quoteRate math.LegacyDec + var foundQuoteRate bool + for cpConvert2, rateConvert2 := range rates { + if cpConvert2.Quote == config.DenomUSD && cpConvert2.Base == cpConvert.Quote { + quoteRate = rateConvert2 + foundQuoteRate = true + } + } + if foundQuoteRate { + convertedPair := types.CurrencyPair{Base: cp.Base, Quote: config.DenomUSD} + convertedRates[convertedPair] = rate.Mul(rateConvert).Mul(quoteRate) + } + } } } } diff --git a/oracle/util.go b/oracle/util.go index 1c7b9d78..de4b8dee 100644 --- a/oracle/util.go +++ b/oracle/util.go @@ -5,6 +5,7 @@ import ( "sort" "time" + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" oracletypes "github.com/ojo-network/ojo/x/oracle/types" @@ -13,8 +14,9 @@ import ( ) var ( - minimumTimeWeight = sdk.MustNewDecFromStr("0.2000") - minimumCandleVolume = sdk.MustNewDecFromStr("0.0001") + minimumTimeWeight = math.LegacyMustNewDecFromStr("0.2000") + minimumTickerVolume = math.LegacyMustNewDecFromStr("0.000000000000001") + minimumCandleVolume = math.LegacyMustNewDecFromStr("0.0001") ) const ( @@ -58,6 +60,9 @@ func ComputeVWAP(prices types.AggregatedProviderPrices) types.CurrencyPairDec { if _, ok := volumeSum[base]; !ok { volumeSum[base] = sdk.ZeroDec() } + if tp.Volume.LT(minimumTickerVolume) { + tp.Volume = minimumTickerVolume + } // weightedPrices[base] = Σ {P * V} for all TickerPrice weightedPrices[base] = weightedPrices[base].Add(tp.Price.Mul(tp.Volume))