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

Oracle: fix duplicate symbol oracle #103

Merged
merged 5 commits into from
Nov 13, 2024
Merged
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
9 changes: 7 additions & 2 deletions x/oracle/keeper/band_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -269,11 +270,15 @@ func (k Keeper) AddNewSymbolToBandOracleRequest(ctx context.Context, symbol stri
// check if new symbol's oracle script id is existing
for _, req := range allBandOracleRequests {
if req.OracleScriptId == oracleScriptId {
if slices.Contains(req.Symbols, symbol) {
return errorsmod.Wrapf(types.ErrSetBandOracleRequest, "symbol %s already registered", symbol)
}

req.Symbols = append(req.Symbols, symbol)
err := k.SetBandOracleRequest(ctx, *req)
if err != nil {
if err := k.SetBandOracleRequest(ctx, *req); err != nil {
return errorsmod.Wrapf(types.ErrSetBandOracleRequest, "can not set symbol %s with oracle script id %v", symbol, oracleScriptId)
}

return nil
}
}
Expand Down
35 changes: 33 additions & 2 deletions x/oracle/keeper/band_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ func TestProcessBandOraclePrices(t *testing.T) {
},
oracleOutput: types.BandOutput{
Responses: []types.Response{
{Symbol: "ATOM", ResponseCode: 0, Rate: 100 * types.BandPriceMultiplier},
{Symbol: "BTC", ResponseCode: 0, Rate: 50000 * types.BandPriceMultiplier},
{Symbol: "ATOM", ResponseCode: 0, Rate: 100 * types.BandPriceMultiplier},
{Symbol: "BTC", ResponseCode: 0, Rate: 50000 * types.BandPriceMultiplier},
},
},
expectedError: false,
Expand Down Expand Up @@ -421,3 +421,34 @@ func TestProcessBandOraclePrices(t *testing.T) {
})
}
}

func (s *KeeperTestSuite) TestAddNewSymbolToBandOracleRequest() {
s.SetupTest()
var (
symbolScript = map[int64][]string{
42: {"ATOM", "OSMO"},
52: {"USD", "EUR", "JPY"},
}

setSymbolScript = func() {
for oracleScriptId, symbols := range symbolScript {
for _, symbol := range symbols {
err := s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbol, oracleScriptId)
s.Require().NoError(err)
}
}
}
)

setSymbolScript()
// duplicate AddNewSymbolToBandOracleRequest test
setSymbolScript()

err := s.k.IteratorOracleRequests(s.Ctx, func(bandOracleRequest types.BandOracleRequest) bool {
expSymbols, ok := symbolScript[bandOracleRequest.OracleScriptId]
s.Require().True(ok)
s.Require().Equal(len(expSymbols), len(bandOracleRequest.Symbols))
return false
})
s.Require().NoError(err)
}
12 changes: 10 additions & 2 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package keeper_test

import (
"testing"
testifysuite "github.com/stretchr/testify/suite"

apptesting "github.com/onomyprotocol/reserve/app/apptesting"
"github.com/onomyprotocol/reserve/x/oracle/keeper"
testifysuite "github.com/stretchr/testify/suite"
)

type KeeperTestSuite struct {
apptesting.KeeperTestHelper
k keeper.Keeper
}

func TestKeeperTestSuite(t *testing.T) {
testifysuite.Run(t, new(KeeperTestSuite))
}
}

func (s *KeeperTestSuite) SetupTest() {
s.Setup()
s.k = s.App.OracleKeeper
}
Loading