From 4ccb6c3009e4e8d5b6d7485b3a67711a525d7d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Wed, 13 Nov 2024 10:38:05 +0700 Subject: [PATCH 1/5] fix duplicate symbol oracle --- x/oracle/keeper/band_oracle.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/x/oracle/keeper/band_oracle.go b/x/oracle/keeper/band_oracle.go index 17e3783..379f468 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,13 @@ 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 { - req.Symbols = append(req.Symbols, symbol) - err := k.SetBandOracleRequest(ctx, *req) - if err != nil { - return errorsmod.Wrapf(types.ErrSetBandOracleRequest, "can not set symbol %s with oracle script id %v", symbol, oracleScriptId) + if !slices.Contains(req.Symbols, symbol) { + req.Symbols = append(req.Symbols, symbol) + 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 } } From a3c88d11e5917da81b55776917714487e7cf6447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Wed, 13 Nov 2024 11:13:21 +0700 Subject: [PATCH 2/5] add test for AddNewSymbolToBandOracleRequest oracle module --- x/oracle/keeper/band_oracle_test.go | 49 +++++++++++++++++++++++++++-- x/oracle/keeper/keeper_test.go | 12 +++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/x/oracle/keeper/band_oracle_test.go b/x/oracle/keeper/band_oracle_test.go index b6dcdcc..0da0fef 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,48 @@ func TestProcessBandOraclePrices(t *testing.T) { }) } } + +func (s *KeeperTestSuite) TestParams() { + s.SetupTest() + var ( + symbolScript = map[int64][]string{ + 42: {"ATOM", "OSMO"}, + 52: {"USD", "EUR", "JPY"}, + } + ) + + // 42-1.ATOM + err := s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[42][0], 42) + s.Require().NoError(err) + // 42-2.OSMO + err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[42][1], 42) + s.Require().NoError(err) + + // 42-1.ATOM duplicate test + err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[42][0], 42) + s.Require().NoError(err) + + // 52-1.USD + err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][0], 52) + s.Require().NoError(err) + + // 52-2.EUR + err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][1], 52) + s.Require().NoError(err) + + // 52-3.JPY + err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][2], 52) + s.Require().NoError(err) + + // 52-1.USD duplicate test + err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][0], 52) + s.Require().NoError(err) + + 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 +} From e35ba2dce8b8918d89798ef5ee74202cc975ddfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Wed, 13 Nov 2024 11:14:15 +0700 Subject: [PATCH 3/5] minor rename --- x/oracle/keeper/band_oracle_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/oracle/keeper/band_oracle_test.go b/x/oracle/keeper/band_oracle_test.go index 0da0fef..b015cc8 100644 --- a/x/oracle/keeper/band_oracle_test.go +++ b/x/oracle/keeper/band_oracle_test.go @@ -422,7 +422,7 @@ func TestProcessBandOraclePrices(t *testing.T) { } } -func (s *KeeperTestSuite) TestParams() { +func (s *KeeperTestSuite) TestAddNewSymbolToBandOracleRequest() { s.SetupTest() var ( symbolScript = map[int64][]string{ From 39bb8b926b3f43842604663bf61051756e15202c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Wed, 13 Nov 2024 11:21:52 +0700 Subject: [PATCH 4/5] refactor TestAddNewSymbolToBandOracleRequest --- x/oracle/keeper/band_oracle_test.go | 40 ++++++++++------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/x/oracle/keeper/band_oracle_test.go b/x/oracle/keeper/band_oracle_test.go index b015cc8..3a19b8e 100644 --- a/x/oracle/keeper/band_oracle_test.go +++ b/x/oracle/keeper/band_oracle_test.go @@ -429,36 +429,22 @@ func (s *KeeperTestSuite) TestAddNewSymbolToBandOracleRequest() { 42: {"ATOM", "OSMO"}, 52: {"USD", "EUR", "JPY"}, } - ) - - // 42-1.ATOM - err := s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[42][0], 42) - s.Require().NoError(err) - // 42-2.OSMO - err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[42][1], 42) - s.Require().NoError(err) - - // 42-1.ATOM duplicate test - err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[42][0], 42) - s.Require().NoError(err) - - // 52-1.USD - err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][0], 52) - s.Require().NoError(err) - // 52-2.EUR - err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][1], 52) - s.Require().NoError(err) - - // 52-3.JPY - err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][2], 52) - s.Require().NoError(err) + setSymbolScript = func() { + for oracleScriptId, symbols := range symbolScript { + for _, symbol := range symbols { + err := s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbol, oracleScriptId) + s.Require().NoError(err) + } + } + } + ) - // 52-1.USD duplicate test - err = s.k.AddNewSymbolToBandOracleRequest(s.Ctx, symbolScript[52][0], 52) - s.Require().NoError(err) + setSymbolScript() + // duplicate AddNewSymbolToBandOracleRequest test + setSymbolScript() - err = s.k.IteratorOracleRequests(s.Ctx, func(bandOracleRequest types.BandOracleRequest) bool { + 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)) From c2a1609780f9ac680a9b43a859aca89df06a5ad5 Mon Sep 17 00:00:00 2001 From: vuong177 Date: Wed, 13 Nov 2024 12:03:56 +0700 Subject: [PATCH 5/5] nits --- x/oracle/keeper/band_oracle.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/x/oracle/keeper/band_oracle.go b/x/oracle/keeper/band_oracle.go index 379f468..ef31a0e 100644 --- a/x/oracle/keeper/band_oracle.go +++ b/x/oracle/keeper/band_oracle.go @@ -270,11 +270,13 @@ 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) { - req.Symbols = append(req.Symbols, symbol) - 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) - } + if slices.Contains(req.Symbols, symbol) { + return errorsmod.Wrapf(types.ErrSetBandOracleRequest, "symbol %s already registered", symbol) + } + + req.Symbols = append(req.Symbols, symbol) + 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