From f44ce354b9131ca37f88fadd2d6e12ac5b3a4885 Mon Sep 17 00:00:00 2001 From: asolovov Date: Thu, 14 Sep 2023 01:01:30 +0400 Subject: [PATCH] API-130: GetMarketIDs method --- mocks/service/mockService.go | 15 ++++++++++++++ perpsv3.go | 7 +++++++ services/marketData.go | 9 +++++++++ services/marketData_test.go | 38 ++++++++++++++++++++++++++++++++++++ services/service.go | 3 +++ 5 files changed, 72 insertions(+) diff --git a/mocks/service/mockService.go b/mocks/service/mockService.go index c8c14dd..e8a713e 100644 --- a/mocks/service/mockService.go +++ b/mocks/service/mockService.go @@ -80,6 +80,21 @@ func (mr *MockIServiceMockRecorder) FormatAccountsLimit(limit interface{}) *gomo return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FormatAccountsLimit", reflect.TypeOf((*MockIService)(nil).FormatAccountsLimit), limit) } +// GetMarketIDs mocks base method. +func (m *MockIService) GetMarketIDs() ([]*big.Int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetMarketIDs") + ret0, _ := ret[0].([]*big.Int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetMarketIDs indicates an expected call of GetMarketIDs. +func (mr *MockIServiceMockRecorder) GetMarketIDs() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMarketIDs", reflect.TypeOf((*MockIService)(nil).GetMarketIDs)) +} + // GetMarketMetadata mocks base method. func (m *MockIService) GetMarketMetadata(marketID *big.Int) (*models.MarketMetadata, error) { m.ctrl.T.Helper() diff --git a/perpsv3.go b/perpsv3.go index 10aec3d..62a4386 100644 --- a/perpsv3.go +++ b/perpsv3.go @@ -88,6 +88,9 @@ type IPerpsv3 interface { // GetMarketSummary is used to get market summary by given market ID. Given market id cannot be nil GetMarketSummary(marketID *big.Int) (*models.MarketSummary, error) + // GetMarketIDs is used to get market IDs from the smart contract + GetMarketIDs() ([]*big.Int, error) + // FormatAccount is used to get account, and it's additional data from the contract by given account id FormatAccount(id *big.Int) (*models.Account, error) @@ -186,6 +189,10 @@ func (p *Perpsv3) GetMarketSummary(marketID *big.Int) (*models.MarketSummary, er return p.service.GetMarketSummary(marketID) } +func (p *Perpsv3) GetMarketIDs() ([]*big.Int, error) { + return p.service.GetMarketIDs() +} + func (p *Perpsv3) FormatAccounts() ([]*models.Account, error) { return p.service.FormatAccounts() } diff --git a/services/marketData.go b/services/marketData.go index 6878749..7676017 100644 --- a/services/marketData.go +++ b/services/marketData.go @@ -98,6 +98,15 @@ func (s *Service) GetMarketSummary(marketID *big.Int) (*models.MarketSummary, er return models.GetMarketSummaryFromContractModel(res, marketID), nil } +func (s *Service) GetMarketIDs() ([]*big.Int, error) { + res, err := s.perpsMarket.GetMarkets(nil) + if err != nil { + return nil, errors.GetReadContractErr(err, "perpsMarket", "getMarkets") + } + + return res, nil +} + // retrieveMarketUpdates is used to get retrieve market updates with given filter options func (s *Service) retrieveMarketUpdates(opts *bind.FilterOpts) ([]*models.MarketUpdate, error) { iterator, err := s.perpsMarket.FilterMarketUpdated(opts) diff --git a/services/marketData_test.go b/services/marketData_test.go index 001fb7c..251793a 100644 --- a/services/marketData_test.go +++ b/services/marketData_test.go @@ -219,3 +219,41 @@ func TestService_GetMarketSummary(t *testing.T) { }) } } + +func TestService_GetMarketIDs(t *testing.T) { + rpc := os.Getenv("TEST_RPC") + if rpc == "" { + log.Fatal("no rpc in env vars") + } + + rpcClient, _ := ethclient.Dial(rpc) + + coreC, _ := coreGoerli.NewCoreGoerli(common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"), rpcClient) + spot, _ := spotMarketGoerli.NewSpotMarketGoerli(common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"), rpcClient) + perps, _ := perpsMarketGoerli.NewPerpsMarketGoerli(common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"), rpcClient) + + testCases := []struct { + name string + want []*big.Int + wantErr error + }{ + { + name: "get ids", + want: []*big.Int{big.NewInt(100), big.NewInt(200)}, + }, + } + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + s := NewService(rpcClient, coreC, 11664658, spot, 10875051, perps, 0) + + res, err := s.GetMarketIDs() + + if tt.wantErr == nil { + require.Equal(t, tt.want, res) + } else { + require.Error(t, err) + require.ErrorIs(t, err, tt.wantErr) + } + }) + } +} diff --git a/services/service.go b/services/service.go index ed85839..3968e31 100644 --- a/services/service.go +++ b/services/service.go @@ -56,6 +56,9 @@ type IService interface { // GetMarketSummary is used to get market summary by given market ID GetMarketSummary(marketID *big.Int) (*models.MarketSummary, error) + // GetMarketIDs is used to get market IDs from the smart contract + GetMarketIDs() ([]*big.Int, error) + // FormatAccount is used to get account, and it's additional data from the contract by given account id FormatAccount(id *big.Int) (*models.Account, error)