Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNM: TIA branch #355

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/math"
"github.com/go-playground/validator/v10"

"github.com/ojo-network/price-feeder/oracle/provider"
Expand All @@ -32,7 +32,7 @@ var (

// maxDeviationThreshold is the maxmimum allowed amount of standard
// deviations which validators are able to set for a given asset.
maxDeviationThreshold = sdk.MustNewDecFromStr("3.0")
maxDeviationThreshold = math.LegacyMustNewDecFromStr("3.0")
)

type (
Expand Down Expand Up @@ -156,7 +156,7 @@ func (c Config) Validate() (err error) {

func (c Config) validateDeviations() error {
for _, deviation := range c.Deviations {
threshold, err := sdk.NewDecFromStr(deviation.Threshold)
threshold, err := math.LegacyNewDecFromStr(deviation.Threshold)
if err != nil {
return fmt.Errorf("deviation thresholds must be numeric: %w", err)
}
Expand Down Expand Up @@ -270,11 +270,11 @@ func (c Config) ProviderEndpointsMap() map[types.ProviderName]provider.Endpoint
}

// DeviationsMap converts the deviation_thresholds from the config file into
// a map of sdk.Dec where the key is the base asset.
func (c Config) DeviationsMap() (map[string]sdk.Dec, error) {
deviations := make(map[string]sdk.Dec, len(c.Deviations))
// a map of math.LegacyDec where the key is the base asset.
func (c Config) DeviationsMap() (map[string]math.LegacyDec, error) {
deviations := make(map[string]math.LegacyDec, len(c.Deviations))
for _, deviation := range c.Deviations {
threshold, err := sdk.NewDecFromStr(deviation.Threshold)
threshold, err := math.LegacyNewDecFromStr(deviation.Threshold)
if err != nil {
return nil, err
}
Expand Down
216 changes: 121 additions & 95 deletions go.mod

Large diffs are not rendered by default.

1,063 changes: 770 additions & 293 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion oracle/client/chain_height.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ChainHeight struct {
// starts a new goroutine subscribed to EventNewBlockHeader.
func NewChainHeight(
ctx context.Context,
client client.TendermintRPC,
client client.CometRPC,
logger zerolog.Logger,
initialHeight int64,
) (*ChainHeight, error) {
Expand Down
2 changes: 1 addition & 1 deletion oracle/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func BroadcastTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) (*sd
unsignedTx.SetFeeGranter(clientCtx.GetFeeGranterAddress())
// unsignedTx.SetFeePayer(clientCtx.GetFeePayerAddress())

if err = tx.Sign(txf, clientCtx.GetFromName(), unsignedTx, true); err != nil {
if err = tx.Sign(clientCtx.CmdContext, txf, clientCtx.GetFromName(), unsignedTx, true); err != nil {
return nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions oracle/convert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package oracle

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/math"
"github.com/rs/zerolog"

"github.com/ojo-network/price-feeder/config"
Expand Down Expand Up @@ -37,7 +37,7 @@ func ConvertRatesToUSD(rates types.CurrencyPairDec) types.CurrencyPairDec {
func CalcCurrencyPairRates(
candles types.AggregatedProviderCandles,
tickers types.AggregatedProviderPrices,
deviationThresholds map[string]sdk.Dec,
deviationThresholds map[string]math.LegacyDec,
currencyPairs []types.CurrencyPair,
logger zerolog.Logger,
) (types.CurrencyPairDec, error) {
Expand Down Expand Up @@ -138,7 +138,7 @@ func ConvertAggregatedCandles(
return convertedCandles
}

func convertCandles(candles []types.CandlePrice, rate sdk.Dec) []types.CandlePrice {
func convertCandles(candles []types.CandlePrice, rate math.LegacyDec) []types.CandlePrice {
convertedCandles := []types.CandlePrice{}
for _, candle := range candles {
candle.Price = candle.Price.Mul(rate)
Expand Down Expand Up @@ -180,7 +180,7 @@ func ConvertAggregatedTickers(
return convertedTickers
}

func convertTicker(ticker types.TickerPrice, rate sdk.Dec) types.TickerPrice {
func convertTicker(ticker types.TickerPrice, rate math.LegacyDec) types.TickerPrice {
ticker.Price = ticker.Price.Mul(rate)
return ticker
}
72 changes: 36 additions & 36 deletions oracle/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ package oracle_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/math"
"github.com/ojo-network/price-feeder/oracle"
"github.com/ojo-network/price-feeder/oracle/types"
"github.com/stretchr/testify/assert"
)

func TestConvertRatesToUSD(t *testing.T) {
rates := types.CurrencyPairDec{
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: sdk.NewDec(10),
types.CurrencyPair{Base: "OSMO", Quote: "ATOM"}: sdk.NewDec(3),
types.CurrencyPair{Base: "JUNO", Quote: "ATOM"}: sdk.NewDec(20),
types.CurrencyPair{Base: "LTC", Quote: "USDT"}: sdk.NewDec(20),
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: math.LegacyNewDec(10),
types.CurrencyPair{Base: "OSMO", Quote: "ATOM"}: math.LegacyNewDec(3),
types.CurrencyPair{Base: "JUNO", Quote: "ATOM"}: math.LegacyNewDec(20),
types.CurrencyPair{Base: "LTC", Quote: "USDT"}: math.LegacyNewDec(20),
}

expected := types.CurrencyPairDec{
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: sdk.NewDec(10),
types.CurrencyPair{Base: "OSMO", Quote: "USD"}: sdk.NewDec(30),
types.CurrencyPair{Base: "JUNO", Quote: "USD"}: sdk.NewDec(200),
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: math.LegacyNewDec(10),
types.CurrencyPair{Base: "OSMO", Quote: "USD"}: math.LegacyNewDec(30),
types.CurrencyPair{Base: "JUNO", Quote: "USD"}: math.LegacyNewDec(200),
}

convertedRates := oracle.ConvertRatesToUSD(rates)
Expand All @@ -46,50 +46,50 @@ func TestConvertAggregatedCandles(t *testing.T) {
candles := types.AggregatedProviderCandles{
"Provider1": types.CurrencyPairCandles{
types.CurrencyPair{Base: "ATOM", Quote: "USDC"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("35"), Volume: sdk.MustNewDecFromStr("1000"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("40"), Volume: sdk.MustNewDecFromStr("1500"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("35"), Volume: math.LegacyMustNewDecFromStr("1000"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("40"), Volume: math.LegacyMustNewDecFromStr("1500"), TimeStamp: 2},
},
types.CurrencyPair{Base: "UMEE", Quote: "USDC"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("18"), Volume: sdk.MustNewDecFromStr("500"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("22"), Volume: sdk.MustNewDecFromStr("800"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("18"), Volume: math.LegacyMustNewDecFromStr("500"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("22"), Volume: math.LegacyMustNewDecFromStr("800"), TimeStamp: 2},
},
},
"Provider2": types.CurrencyPairCandles{
types.CurrencyPair{Base: "ATOM", Quote: "USDT"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("30"), Volume: sdk.MustNewDecFromStr("800"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("35"), Volume: sdk.MustNewDecFromStr("1000"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("30"), Volume: math.LegacyMustNewDecFromStr("800"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("35"), Volume: math.LegacyMustNewDecFromStr("1000"), TimeStamp: 2},
},
types.CurrencyPair{Base: "JUNO", Quote: "USDT"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("5"), Volume: sdk.MustNewDecFromStr("200"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("6"), Volume: sdk.MustNewDecFromStr("300"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("5"), Volume: math.LegacyMustNewDecFromStr("200"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("6"), Volume: math.LegacyMustNewDecFromStr("300"), TimeStamp: 2},
},
},
}

rates := types.CurrencyPairDec{
types.CurrencyPair{Base: "USDT", Quote: "USD"}: sdk.MustNewDecFromStr("2"),
types.CurrencyPair{Base: "USDC", Quote: "USD"}: sdk.MustNewDecFromStr("1"),
types.CurrencyPair{Base: "USDT", Quote: "USD"}: math.LegacyMustNewDecFromStr("2"),
types.CurrencyPair{Base: "USDC", Quote: "USD"}: math.LegacyMustNewDecFromStr("1"),
}

expectedResult := types.AggregatedProviderCandles{
"Provider1": types.CurrencyPairCandles{
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("35"), Volume: sdk.MustNewDecFromStr("1000"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("40"), Volume: sdk.MustNewDecFromStr("1500"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("35"), Volume: math.LegacyMustNewDecFromStr("1000"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("40"), Volume: math.LegacyMustNewDecFromStr("1500"), TimeStamp: 2},
},
types.CurrencyPair{Base: "UMEE", Quote: "USD"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("18"), Volume: sdk.MustNewDecFromStr("500"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("22"), Volume: sdk.MustNewDecFromStr("800"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("18"), Volume: math.LegacyMustNewDecFromStr("500"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("22"), Volume: math.LegacyMustNewDecFromStr("800"), TimeStamp: 2},
},
},
"Provider2": types.CurrencyPairCandles{
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("60"), Volume: sdk.MustNewDecFromStr("800"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("70"), Volume: sdk.MustNewDecFromStr("1000"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("60"), Volume: math.LegacyMustNewDecFromStr("800"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("70"), Volume: math.LegacyMustNewDecFromStr("1000"), TimeStamp: 2},
},
types.CurrencyPair{Base: "JUNO", Quote: "USD"}: []types.CandlePrice{
{Price: sdk.MustNewDecFromStr("10"), Volume: sdk.MustNewDecFromStr("200"), TimeStamp: 1},
{Price: sdk.MustNewDecFromStr("12"), Volume: sdk.MustNewDecFromStr("300"), TimeStamp: 2},
{Price: math.LegacyMustNewDecFromStr("10"), Volume: math.LegacyMustNewDecFromStr("200"), TimeStamp: 1},
{Price: math.LegacyMustNewDecFromStr("12"), Volume: math.LegacyMustNewDecFromStr("300"), TimeStamp: 2},
},
},
}
Expand All @@ -104,42 +104,42 @@ func TestConvertAggregatedTickers(t *testing.T) {
tickers := types.AggregatedProviderPrices{
"Provider1": types.CurrencyPairTickers{
types.CurrencyPair{Base: "ATOM", Quote: "USDC"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("35"), Volume: sdk.MustNewDecFromStr("1000"),
Price: math.LegacyMustNewDecFromStr("35"), Volume: math.LegacyMustNewDecFromStr("1000"),
},
types.CurrencyPair{Base: "UMEE", Quote: "USDC"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("18"), Volume: sdk.MustNewDecFromStr("500"),
Price: math.LegacyMustNewDecFromStr("18"), Volume: math.LegacyMustNewDecFromStr("500"),
},
},
"Provider2": types.CurrencyPairTickers{
types.CurrencyPair{Base: "ATOM", Quote: "USDT"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("30"), Volume: sdk.MustNewDecFromStr("800"),
Price: math.LegacyMustNewDecFromStr("30"), Volume: math.LegacyMustNewDecFromStr("800"),
},
types.CurrencyPair{Base: "JUNO", Quote: "USDT"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("5"), Volume: sdk.MustNewDecFromStr("200"),
Price: math.LegacyMustNewDecFromStr("5"), Volume: math.LegacyMustNewDecFromStr("200"),
},
},
}

rates := types.CurrencyPairDec{
types.CurrencyPair{Base: "USDT", Quote: "USD"}: sdk.MustNewDecFromStr("2"),
types.CurrencyPair{Base: "USDC", Quote: "USD"}: sdk.MustNewDecFromStr("1"),
types.CurrencyPair{Base: "USDT", Quote: "USD"}: math.LegacyMustNewDecFromStr("2"),
types.CurrencyPair{Base: "USDC", Quote: "USD"}: math.LegacyMustNewDecFromStr("1"),
}

expectedResult := types.AggregatedProviderPrices{
"Provider1": types.CurrencyPairTickers{
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("35"), Volume: sdk.MustNewDecFromStr("1000"),
Price: math.LegacyMustNewDecFromStr("35"), Volume: math.LegacyMustNewDecFromStr("1000"),
},
types.CurrencyPair{Base: "UMEE", Quote: "USD"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("18"), Volume: sdk.MustNewDecFromStr("500"),
Price: math.LegacyMustNewDecFromStr("18"), Volume: math.LegacyMustNewDecFromStr("500"),
},
},
"Provider2": types.CurrencyPairTickers{
types.CurrencyPair{Base: "ATOM", Quote: "USD"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("60"), Volume: sdk.MustNewDecFromStr("800"),
Price: math.LegacyMustNewDecFromStr("60"), Volume: math.LegacyMustNewDecFromStr("800"),
},
types.CurrencyPair{Base: "JUNO", Quote: "USD"}: types.TickerPrice{
Price: sdk.MustNewDecFromStr("10"), Volume: sdk.MustNewDecFromStr("200"),
Price: math.LegacyMustNewDecFromStr("10"), Volume: math.LegacyMustNewDecFromStr("200"),
},
},
}
Expand Down
12 changes: 6 additions & 6 deletions oracle/filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package oracle

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/math"
"github.com/rs/zerolog"

"github.com/ojo-network/price-feeder/oracle/provider"
Expand All @@ -11,14 +11,14 @@ import (
// defaultDeviationThreshold defines how many 𝜎 a provider can be away
// from the mean without being considered faulty. This can be overridden
// in the config.
var defaultDeviationThreshold = sdk.MustNewDecFromStr("1.0")
var defaultDeviationThreshold = math.LegacyMustNewDecFromStr("1.0")

// FilterTickerDeviations finds the standard deviations of the prices of
// all assets, and filters out any providers that are not within 2𝜎 of the mean.
func FilterTickerDeviations(
logger zerolog.Logger,
prices types.AggregatedProviderPrices,
deviationThresholds map[string]sdk.Dec,
deviationThresholds map[string]math.LegacyDec,
) (types.AggregatedProviderPrices, error) {
var (
filteredPrices = make(types.AggregatedProviderPrices)
Expand All @@ -28,7 +28,7 @@ func FilterTickerDeviations(
for providerName, priceTickers := range prices {
p, ok := priceMap[providerName]
if !ok {
p = map[types.CurrencyPair]sdk.Dec{}
p = map[types.CurrencyPair]math.LegacyDec{}
priceMap[providerName] = p
}
for base, tp := range priceTickers {
Expand Down Expand Up @@ -77,7 +77,7 @@ func FilterTickerDeviations(
func FilterCandleDeviations(
logger zerolog.Logger,
candles types.AggregatedProviderCandles,
deviationThresholds map[string]sdk.Dec,
deviationThresholds map[string]math.LegacyDec,
) (types.AggregatedProviderCandles, error) {
var (
filteredCandles = make(types.AggregatedProviderCandles)
Expand Down Expand Up @@ -146,7 +146,7 @@ func FilterCandleDeviations(
return filteredCandles, nil
}

func isBetween(p, mean, margin sdk.Dec) bool {
func isBetween(p, mean, margin math.LegacyDec) bool {
return p.GTE(mean.Sub(margin)) &&
p.LTE(mean.Add(margin))
}
26 changes: 13 additions & 13 deletions oracle/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/math"
"github.com/ojo-network/price-feeder/oracle/provider"
"github.com/ojo-network/price-feeder/oracle/types"
"github.com/rs/zerolog"
Expand All @@ -18,8 +18,8 @@ func TestSuccessFilterCandleDeviations(t *testing.T) {
Quote: "USDT",
}

atomPrice := sdk.MustNewDecFromStr("29.93")
atomVolume := sdk.MustNewDecFromStr("1994674.34000000")
atomPrice := math.LegacyMustNewDecFromStr("29.93")
atomVolume := math.LegacyMustNewDecFromStr("1994674.34000000")

atomCandlePrice := []types.CandlePrice{
{
Expand All @@ -41,7 +41,7 @@ func TestSuccessFilterCandleDeviations(t *testing.T) {
providerCandles[provider.ProviderCoinbase] = types.CurrencyPairCandles{
pair: {
{
Price: sdk.MustNewDecFromStr("27.1"),
Price: math.LegacyMustNewDecFromStr("27.1"),
Volume: atomVolume,
TimeStamp: provider.PastUnixTime(1 * time.Minute),
},
Expand All @@ -51,15 +51,15 @@ func TestSuccessFilterCandleDeviations(t *testing.T) {
pricesFiltered, err := FilterCandleDeviations(
zerolog.Nop(),
providerCandles,
make(map[string]sdk.Dec),
make(map[string]math.LegacyDec),
)

_, ok := pricesFiltered[provider.ProviderCoinbase]
require.NoError(t, err, "It should successfully filter out the provider using candles")
require.False(t, ok, "The filtered candle deviation price at coinbase should be empty")

customDeviations := make(map[string]sdk.Dec, 1)
customDeviations[pair.Base] = sdk.NewDec(2)
customDeviations := make(map[string]math.LegacyDec, 1)
customDeviations[pair.Base] = math.LegacyNewDec(2)

pricesFilteredCustom, err := FilterCandleDeviations(
zerolog.Nop(),
Expand All @@ -79,8 +79,8 @@ func TestSuccessFilterTickerDeviations(t *testing.T) {
Quote: "USDT",
}

atomPrice := sdk.MustNewDecFromStr("29.93")
atomVolume := sdk.MustNewDecFromStr("1994674.34000000")
atomPrice := math.LegacyMustNewDecFromStr("29.93")
atomVolume := math.LegacyMustNewDecFromStr("1994674.34000000")

atomTickerPrice := types.TickerPrice{
Price: atomPrice,
Expand All @@ -98,23 +98,23 @@ func TestSuccessFilterTickerDeviations(t *testing.T) {
}
providerTickers[provider.ProviderCoinbase] = types.CurrencyPairTickers{
pair: {
Price: sdk.MustNewDecFromStr("27.1"),
Price: math.LegacyMustNewDecFromStr("27.1"),
Volume: atomVolume,
},
}

pricesFiltered, err := FilterTickerDeviations(
zerolog.Nop(),
providerTickers,
make(map[string]sdk.Dec),
make(map[string]math.LegacyDec),
)

_, ok := pricesFiltered[provider.ProviderCoinbase]
require.NoError(t, err, "It should successfully filter out the provider using tickers")
require.False(t, ok, "The filtered ticker deviation price at coinbase should be empty")

customDeviations := make(map[string]sdk.Dec, 1)
customDeviations[pair.Base] = sdk.NewDec(2)
customDeviations := make(map[string]math.LegacyDec, 1)
customDeviations[pair.Base] = math.LegacyNewDec(2)

pricesFilteredCustom, err := FilterTickerDeviations(
zerolog.Nop(),
Expand Down
Loading
Loading