Skip to content

Commit

Permalink
API-130: GetMarketIDs method
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovov committed Sep 13, 2023
1 parent 7e0bffe commit f44ce35
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mocks/service/mockService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}
Expand Down
9 changes: 9 additions & 0 deletions services/marketData.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions services/marketData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
3 changes: 3 additions & 0 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit f44ce35

Please sign in to comment.