From 2c94f1f212d35085adde3ae808c7c86203d69681 Mon Sep 17 00:00:00 2001 From: asolovov Date: Fri, 19 Jan 2024 13:18:13 +0300 Subject: [PATCH] SNX-12: add getCollateralPrice --- config/config.go | 2 +- models/collateral.go | 5 +++++ perpsv3.go | 7 +++++++ services/collateral.go | 15 +++++++++++++++ services/service.go | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index aa8a823..175bc7c 100644 --- a/config/config.go +++ b/config/config.go @@ -115,7 +115,7 @@ func GetBaseAndromedaDefaultConfig(rpcURL string) *PerpsvConfig { // GetBaseMainnetDefaultConfig is used to get default lib config for base main net func GetBaseMainnetDefaultConfig(rpcURL string) *PerpsvConfig { if rpcURL == "" { - rpcURL = "https://rpc.ankr.com/base/6259fa6541ffabb10ca241f7f437c2389ab7dda38c7be817ab0fb76992e73ae5" + rpcURL = "https://base.fastnode.cc/FNqVEtfWuTiChBumceweFPGEbNkZxtDXrAsHaoklAKNmfQhfxhVQBMijfetdNKMV/" } return &PerpsvConfig{ diff --git a/models/collateral.go b/models/collateral.go index 7dc18ea..0eefa7a 100644 --- a/models/collateral.go +++ b/models/collateral.go @@ -29,6 +29,11 @@ type CollateralWithdrawn struct { BlockTimestamp uint64 } +// CollateralPrice is a collateral price data struct +type CollateralPrice struct { + Price *big.Int +} + // GetCollateralDepositedFromEvent is used to get CollateralDeposited struct from given contract event func GetCollateralDepositedFromEvent(event *core.CoreDeposited, time uint64) *CollateralDeposited { if event == nil { diff --git a/perpsv3.go b/perpsv3.go index 97a9d3e..c128a3e 100644 --- a/perpsv3.go +++ b/perpsv3.go @@ -191,6 +191,9 @@ type IPerpsv3 interface { // GetRequiredMaintenanceMargin is used to get required maintenance margin for given account ID GetRequiredMaintenanceMargin(accountId *big.Int) (*big.Int, error) + // GetCollateralPrice is used to get collateral price for given block number and collateralType + GetCollateralPrice(blockNumber *big.Int, collateralType common.Address) (*models.CollateralPrice, 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) @@ -413,6 +416,10 @@ func (p *Perpsv3) GetRequiredMaintenanceMargin(accountId *big.Int) (*big.Int, er return p.service.GetRequiredMaintenanceMargin(accountId) } +func (p *Perpsv3) GetCollateralPrice(blockNumber *big.Int, collateralType common.Address) (*models.CollateralPrice, error) { + return p.service.GetCollateralPrice(blockNumber, collateralType) +} + func (p *Perpsv3) FormatAccounts() ([]*models.Account, error) { return p.service.FormatAccounts() } diff --git a/services/collateral.go b/services/collateral.go index c25e258..67b14c9 100644 --- a/services/collateral.go +++ b/services/collateral.go @@ -2,6 +2,7 @@ package services import ( "context" + "github.com/ethereum/go-ethereum/common" "math/big" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -12,6 +13,20 @@ import ( "github.com/gateway-fm/perpsv3-Go/pkg/logger" ) +func (s *Service) GetCollateralPrice(blockNumber *big.Int, collateralType common.Address) (*models.CollateralPrice, error) { + var opts *bind.CallOpts + if blockNumber.Int64() > 0 { + opts = &bind.CallOpts{BlockNumber: blockNumber} + } + + price, err := s.core.GetCollateralPrice(opts, collateralType) + if err != nil { + return nil, errors.GetReadContractErr(err, "core", "GetCollateralPrice") + } + + return &models.CollateralPrice{Price: price}, nil +} + func (s *Service) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) { iterations, last, err := s.getIterationsForLimitQuery(limit) if err != nil { diff --git a/services/service.go b/services/service.go index c032910..d860937 100644 --- a/services/service.go +++ b/services/service.go @@ -118,6 +118,9 @@ type IService interface { // GetRequiredMaintenanceMargin is used to get required maintenance margin for given account ID GetRequiredMaintenanceMargin(accountId *big.Int) (*big.Int, error) + // GetCollateralPrice is used to get collateral price for given block number and collateralType + GetCollateralPrice(blockNumber *big.Int, collateralType common.Address) (*models.CollateralPrice, 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)