Skip to content

Commit

Permalink
add TestGetPrice
Browse files Browse the repository at this point in the history
  • Loading branch information
hungdinh82 committed Sep 25, 2024
1 parent eb9b486 commit 1230d5e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
5 changes: 3 additions & 2 deletions x/oracle/keeper/band_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (k *Keeper) GetAllBandPriceStates(ctx sdk.Context) []*types.BandPriceState
func (k *Keeper) GetPrice(ctx sdk.Context, base, quote string) *math.LegacyDec {
// query ref by using GetBandPriceState
basePriceState := k.GetBandPriceState(ctx, base)
if basePriceState == nil {
if basePriceState == nil || basePriceState.Rate.IsZero() {
return nil
}

Expand All @@ -228,7 +228,7 @@ func (k *Keeper) GetPrice(ctx sdk.Context, base, quote string) *math.LegacyDec {
}

quotePriceState := k.GetBandPriceState(ctx, quote)
if quotePriceState == nil {
if quotePriceState == nil || quotePriceState.Rate.IsZero() {
return nil
}

Expand All @@ -243,6 +243,7 @@ func (k *Keeper) GetPrice(ctx sdk.Context, base, quote string) *math.LegacyDec {
return &price
}


// RequestBandOraclePrices creates and sends an IBC packet to fetch band oracle price feed data through IBC.
func (k *Keeper) RequestBandOraclePrices(
ctx sdk.Context,
Expand Down
83 changes: 79 additions & 4 deletions x/oracle/keeper/band_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package keeper_test
import (
"testing"
"time"

"cosmossdk.io/math"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onomyprotocol/reserve/app"
"github.com/stretchr/testify/require"
"github.com/onomyprotocol/reserve/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestBandPriceState(t *testing.T) {
Expand All @@ -26,7 +27,7 @@ func TestBandPriceState(t *testing.T) {
require.Nil(t, price)

bandPriceState := &types.BandPriceState{
Symbol: "ATOM",
Symbol: "ATOM",
Rate: math.NewInt(10),
ResolveTime: 1,
Request_ID: 1,
Expand All @@ -50,7 +51,7 @@ func TestBandPriceState(t *testing.T) {
func TestBandOracleRequest(t *testing.T) {
app := app.Setup(t, false)
ctx := app.BaseApp.NewContextLegacy(false, tmproto.Header{Height: 1, ChainID: "3", Time: time.Unix(1618997040, 0)})

req := app.OracleKeeper.GetBandOracleRequest(ctx, 1)
require.Nil(t, req)

Expand Down Expand Up @@ -132,3 +133,77 @@ func TestBandCallDataRecord(t *testing.T) {
record = app.OracleKeeper.GetBandCallDataRecord(ctx, 1)
require.Nil(t, record)
}

func TestGetPrice(t *testing.T) {
app := app.Setup(t, false)
ctx := app.BaseApp.NewContextLegacy(false, tmproto.Header{Height: 1, ChainID: "3", Time: time.Unix(1618997040, 0)})

// Case 1: Base, quote price, does not exist, expect nil
price := app.OracleKeeper.GetPrice(ctx, "ATOM", "USD")
require.Nil(t, price)

// Case 2: Set base price state for ATOM
bandPriceStateATOM := &types.BandPriceState{
Symbol: "ATOM",
Rate: math.NewInt(10),
ResolveTime: 1,
Request_ID: 1,
PriceState: *types.NewPriceState(math.LegacyNewDec(10), 1),
}
err := app.OracleKeeper.SetBandPriceState(ctx, "ATOM", bandPriceStateATOM)
require.NoError(t, err)

// // Case 3: Set quote price state for USD
bandPriceStateUSD := &types.BandPriceState{
Symbol: "USD",
Rate: math.NewInt(1),
ResolveTime: 1,
Request_ID: 1,
PriceState: *types.NewPriceState(math.LegacyNewDec(1), 1),
}
err = app.OracleKeeper.SetBandPriceState(ctx, "USD", bandPriceStateUSD)
require.NoError(t, err)

// Case 4: Base price is invalid (rate is zero), expect nil
invalidPriceState := &types.BandPriceState{
Symbol: "ATOM",
Rate: math.NewInt(0), // Invalid base rate
ResolveTime: 1,
Request_ID: 1,
PriceState: *types.NewPriceState(math.LegacyNewDec(0), 1),
}
err = app.OracleKeeper.SetBandPriceState(ctx, "ATOM", invalidPriceState)
require.NoError(t, err)

price = app.OracleKeeper.GetPrice(ctx, "ATOM", "USD")
require.Nil(t, price)

// Case 5: Quote price is invalid (rate is zero), expect nil
invalidQuoteState := &types.BandPriceState{
Symbol: "USD",
Rate: math.NewInt(0), // Invalid quote rate
ResolveTime: 1,
Request_ID: 1,
PriceState: *types.NewPriceState(math.LegacyNewDec(0), 1),
}
err = app.OracleKeeper.SetBandPriceState(ctx, "USD", invalidQuoteState)
require.NoError(t, err)

price = app.OracleKeeper.GetPrice(ctx, "ATOM", "USD")
require.Nil(t, price)

// Case 6: Reset to valid state for both ATOM and USD, expect valid price (10)
err = app.OracleKeeper.SetBandPriceState(ctx, "ATOM", bandPriceStateATOM)
require.NoError(t, err)
err = app.OracleKeeper.SetBandPriceState(ctx, "USD", bandPriceStateUSD)
require.NoError(t, err)

price = app.OracleKeeper.GetPrice(ctx, "ATOM", "USD")
expect := math.LegacyNewDec(10)
require.Equal(t, &expect, price)

// Case 7: Reverse price (USD to ATOM), expect 0.1
price = app.OracleKeeper.GetPrice(ctx, "USD", "ATOM")
expect = math.LegacyNewDec(1).Quo(math.LegacyNewDec(10)) // 0.1
require.Equal(t, &expect, price)
}

0 comments on commit 1230d5e

Please sign in to comment.