Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNX-12: add getCollateralPrice #53

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 5 additions & 0 deletions models/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}
Expand Down
15 changes: 15 additions & 0 deletions services/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"github.com/ethereum/go-ethereum/common"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down