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

refactor: Cleaned up float->dec conversions #1431

Merged
merged 11 commits into from
Sep 30, 2022
1 change: 1 addition & 0 deletions price-feeder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [#1175](https://github.com/umee-network/umee/pull/1175) Add type ProviderName.
- [#1255](https://github.com/umee-network/umee/pull/1255) Move TickerPrice and CandlePrice to types package
- [#1374](https://github.com/umee-network/umee/pull/1374) Add standard for telemetry metrics.
- [#1431](https://github.com/umee-network/umee/pull/1431) Convert floats to sdk decimal using helper functions in all providers.

### Features

Expand Down
15 changes: 5 additions & 10 deletions price-feeder/oracle/provider/ftx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rs/zerolog"
"github.com/umee-network/umee/price-feeder/oracle/types"
"github.com/umee-network/umee/v3/util/coin"
rbajollari marked this conversation as resolved.
Show resolved Hide resolved
)

const (
Expand Down Expand Up @@ -136,14 +135,12 @@ func (p *FTXProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[string]t
return nil, fmt.Errorf("duplicate token found in FTX response: %s", symbol)
}

priceRaw := strconv.FormatFloat(tr.Price, 'f', -1, 64)
price, err := sdk.NewDecFromStr(priceRaw)
price, err := coin.NewDecFromFloat(tr.Price)
if err != nil {
return nil, fmt.Errorf("failed to read FTX price (%f) for %s", tr.Price, symbol)
}

volumeRaw := strconv.FormatFloat(tr.Volume, 'f', -1, 64)
volume, err := sdk.NewDecFromStr(volumeRaw)
volume, err := coin.NewDecFromFloat(tr.Volume)
if err != nil {
return nil, fmt.Errorf("failed to read FTX volume (%f) for %s", tr.Volume, symbol)
}
Expand Down Expand Up @@ -304,11 +301,9 @@ func (p *FTXProvider) pollCandles(pairs ...types.CurrencyPair) error {
}
candleEnd := candleStart.Add(candleWindowLength).Unix() * int64(time.Second/time.Millisecond)

closeStr := fmt.Sprintf("%f", responseCandle.Price)
volumeStr := fmt.Sprintf("%f", responseCandle.Volume)
candlePrices = append(candlePrices, types.CandlePrice{
Price: sdk.MustNewDecFromStr(closeStr),
Volume: sdk.MustNewDecFromStr(volumeStr),
Price: coin.MustNewDecFromFloat(responseCandle.Price),
Volume: coin.MustNewDecFromFloat(responseCandle.Volume),
adamewozniak marked this conversation as resolved.
Show resolved Hide resolved
TimeStamp: candleEnd,
})
}
Expand Down
15 changes: 7 additions & 8 deletions price-feeder/oracle/provider/huobi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package provider

import (
"context"
"strconv"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"github.com/umee-network/umee/price-feeder/oracle/types"
"github.com/umee-network/umee/v3/util/coin"
)

func TestHuobiProvider_GetTickerPrices(t *testing.T) {
Expand Down Expand Up @@ -38,8 +37,8 @@ func TestHuobiProvider_GetTickerPrices(t *testing.T) {
prices, err := p.GetTickerPrices(types.CurrencyPair{Base: "ATOM", Quote: "USDT"})
require.NoError(t, err)
require.Len(t, prices, 1)
require.Equal(t, sdk.MustNewDecFromStr(strconv.FormatFloat(lastPrice, 'f', -1, 64)), prices["ATOMUSDT"].Price)
require.Equal(t, sdk.MustNewDecFromStr(strconv.FormatFloat(volume, 'f', -1, 64)), prices["ATOMUSDT"].Volume)
require.Equal(t, coin.MustNewDecFromFloat(lastPrice), prices["ATOMUSDT"].Price)
require.Equal(t, coin.MustNewDecFromFloat(volume), prices["ATOMUSDT"].Volume)
})

t.Run("valid_request_multi_ticker", func(t *testing.T) {
Expand Down Expand Up @@ -71,10 +70,10 @@ func TestHuobiProvider_GetTickerPrices(t *testing.T) {
)
require.NoError(t, err)
require.Len(t, prices, 2)
require.Equal(t, sdk.MustNewDecFromStr(strconv.FormatFloat(lastPriceAtom, 'f', -1, 64)), prices["ATOMUSDT"].Price)
require.Equal(t, sdk.MustNewDecFromStr(strconv.FormatFloat(volume, 'f', -1, 64)), prices["ATOMUSDT"].Volume)
require.Equal(t, sdk.MustNewDecFromStr(strconv.FormatFloat(lastPriceLuna, 'f', -1, 64)), prices["LUNAUSDT"].Price)
require.Equal(t, sdk.MustNewDecFromStr(strconv.FormatFloat(volume, 'f', -1, 64)), prices["LUNAUSDT"].Volume)
require.Equal(t, coin.MustNewDecFromFloat(lastPriceAtom), prices["ATOMUSDT"].Price)
require.Equal(t, coin.MustNewDecFromFloat(volume), prices["ATOMUSDT"].Volume)
require.Equal(t, coin.MustNewDecFromFloat(lastPriceLuna), prices["LUNAUSDT"].Price)
require.Equal(t, coin.MustNewDecFromFloat(volume), prices["LUNAUSDT"].Volume)
})

t.Run("invalid_request_invalid_ticker", func(t *testing.T) {
Expand Down
19 changes: 7 additions & 12 deletions price-feeder/oracle/provider/osmosis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/price-feeder/oracle/types"
"github.com/umee-network/umee/v3/util/coin"
)

const (
Expand Down Expand Up @@ -116,16 +115,14 @@ func (p OsmosisProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[strin
return nil, fmt.Errorf("duplicate token found in Osmosis response: %s", symbol)
}

priceRaw := strconv.FormatFloat(tr.Price, 'f', -1, 64)
price, err := sdk.NewDecFromStr(priceRaw)
price, err := coin.NewDecFromFloat(tr.Price)
if err != nil {
return nil, fmt.Errorf("failed to read Osmosis price (%s) for %s", priceRaw, symbol)
return nil, fmt.Errorf("failed to read Osmosis price (%f) for %s", tr.Price, symbol)
}

volumeRaw := strconv.FormatFloat(tr.Volume, 'f', -1, 64)
volume, err := sdk.NewDecFromStr(volumeRaw)
volume, err := coin.NewDecFromFloat(tr.Volume)
if err != nil {
return nil, fmt.Errorf("failed to read Osmosis volume (%s) for %s", volumeRaw, symbol)
return nil, fmt.Errorf("failed to read Osmosis volume (%f) for %s", tr.Volume, symbol)
}

tickerPrices[cp.String()] = types.TickerPrice{Price: price, Volume: volume}
Expand Down Expand Up @@ -177,11 +174,9 @@ func (p OsmosisProvider) GetCandlePrices(pairs ...types.CurrencyPair) (map[strin
if staleTime >= responseCandle.Time {
continue
}
closeStr := fmt.Sprintf("%f", responseCandle.Close)
volumeStr := fmt.Sprintf("%f", responseCandle.Volume)
candlePrices = append(candlePrices, types.CandlePrice{
Price: sdk.MustNewDecFromStr(closeStr),
Volume: sdk.MustNewDecFromStr(volumeStr),
Price: coin.MustNewDecFromFloat(responseCandle.Close),
Volume: coin.MustNewDecFromFloat(responseCandle.Volume),
// convert osmosis timestamp seconds -> milliseconds
TimeStamp: SecondsToMilli(responseCandle.Time),
})
Expand Down