diff --git a/x/oracle/keeper/band_oracle.go b/x/oracle/keeper/band_oracle.go index 17e3783..ef31a0e 100644 --- a/x/oracle/keeper/band_oracle.go +++ b/x/oracle/keeper/band_oracle.go @@ -3,6 +3,7 @@ package keeper import ( "context" "fmt" + "slices" "strconv" "time" @@ -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 } } diff --git a/x/oracle/keeper/band_oracle_test.go b/x/oracle/keeper/band_oracle_test.go index b6dcdcc..3a19b8e 100644 --- a/x/oracle/keeper/band_oracle_test.go +++ b/x/oracle/keeper/band_oracle_test.go @@ -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, @@ -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) +} diff --git a/x/oracle/keeper/keeper_test.go b/x/oracle/keeper/keeper_test.go index 0148e6e..2ce7edb 100644 --- a/x/oracle/keeper/keeper_test.go +++ b/x/oracle/keeper/keeper_test.go @@ -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)) -} \ No newline at end of file +} + +func (s *KeeperTestSuite) SetupTest() { + s.Setup() + s.k = s.App.OracleKeeper +}