From c3fe704be570aab7f648826f296f6a6a0734bf3b Mon Sep 17 00:00:00 2001 From: asolovov Date: Wed, 27 Sep 2023 17:29:17 +0400 Subject: [PATCH 1/2] API-222: GetAvailableMargin --- perpsv3.go | 7 +++++++ services/accounts.go | 10 ++++++++++ services/service.go | 3 +++ 3 files changed, 20 insertions(+) diff --git a/perpsv3.go b/perpsv3.go index e695389..a7c8ad1 100644 --- a/perpsv3.go +++ b/perpsv3.go @@ -111,6 +111,9 @@ type IPerpsv3 interface { // GetFoundingRate is used to get current market founding rate by given market ID GetFoundingRate(marketId *big.Int) (*big.Int, error) + // GetAvailableMargin is used to get available margin for given account ID + GetAvailableMargin(accountId *big.Int) (*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) @@ -229,6 +232,10 @@ func (p *Perpsv3) GetFoundingRate(marketId *big.Int) (*big.Int, error) { return p.service.GetFoundingRate(marketId) } +func (p *Perpsv3) GetAvailableMargin(accountId *big.Int) (*big.Int, error) { + return p.service.GetAvailableMargin(accountId) +} + func (p *Perpsv3) FormatAccounts() ([]*models.Account, error) { return p.service.FormatAccounts() } diff --git a/services/accounts.go b/services/accounts.go index 213b59b..33e15b6 100644 --- a/services/accounts.go +++ b/services/accounts.go @@ -63,6 +63,16 @@ func (s *Service) FormatAccounts() ([]*models.Account, error) { return s.formatAccounts(opts) } +func (s *Service) GetAvailableMargin(accountId *big.Int) (*big.Int, error) { + margin, err := s.perpsMarket.GetAvailableMargin(nil, accountId) + if err != nil { + logger.Log().WithField("layer", "").Errorf("get avaliable margin error: %v", err.Error()) + return nil, errors.GetReadContractErr(err, "perps market", "GetAvailableMargin") + } + + return margin, nil +} + // formatAccounts is used to get accounts from the contract using event filter function for 'AccountCreated' event // and given filter options func (s *Service) formatAccounts(opts *bind.FilterOpts) ([]*models.Account, error) { diff --git a/services/service.go b/services/service.go index 463f469..32366c8 100644 --- a/services/service.go +++ b/services/service.go @@ -70,6 +70,9 @@ type IService interface { // GetFoundingRate is used to get current founding rate by given market ID GetFoundingRate(marketId *big.Int) (*big.Int, error) + // GetAvailableMargin is used to get available margin for given account ID + GetAvailableMargin(accountId *big.Int) (*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) From 240729ed9774e819be17ef720a13a5c7685e7758 Mon Sep 17 00:00:00 2001 From: asolovov Date: Wed, 27 Sep 2023 17:33:27 +0400 Subject: [PATCH 2/2] API-222: GetAvailableMargin --- mocks/service/mockService.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mocks/service/mockService.go b/mocks/service/mockService.go index 211a882..ddfb283 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) } +// GetAvailableMargin mocks base method. +func (m *MockIService) GetAvailableMargin(accountId *big.Int) (*big.Int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAvailableMargin", accountId) + ret0, _ := ret[0].(*big.Int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetAvailableMargin indicates an expected call of GetAvailableMargin. +func (mr *MockIServiceMockRecorder) GetAvailableMargin(accountId interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAvailableMargin", reflect.TypeOf((*MockIService)(nil).GetAvailableMargin), accountId) +} + // GetFoundingRate mocks base method. func (m *MockIService) GetFoundingRate(marketId *big.Int) (*big.Int, error) { m.ctrl.T.Helper()