Skip to content

Commit

Permalink
Merge pull request #62 from gateway-fm/feat/hist_pools
Browse files Browse the repository at this point in the history
fix: GetVaultDebt method
  • Loading branch information
asolovov authored Apr 25, 2024
2 parents b2a5449 + 197f5be commit 172443c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 84 deletions.
5 changes: 3 additions & 2 deletions examples/new_lib.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/ethereum/go-ethereum/common"
perpsv3_Go "github.com/gateway-fm/perpsv3-Go"
"log"
"math/big"
Expand All @@ -12,14 +13,14 @@ func main() {

//conf := perpsv3_Go.GetOptimismGoerliDefaultConfig(rpcURL)
// OR you can use base andromeda chain
conf := perpsv3_Go.GetBaseAndromedaDefaultConfig(rpcURL)
conf := perpsv3_Go.GetBaseMainnetDefaultConfig(rpcURL)

lib, err := perpsv3_Go.Create(conf)
if err != nil {
log.Fatal(err)
}

res, err := lib.GetAvailableMargin(big.NewInt(1))
res, err := lib.GetVaultDebtHistorical(big.NewInt(1), common.HexToAddress("0xC74eA762cF06c9151cE074E6a569a5945b6302E7"), big.NewInt(13625311))
if err != nil {
log.Fatal(err)
}
Expand Down
145 changes: 63 additions & 82 deletions services/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package services

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

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"

"github.com/gateway-fm/perpsv3-Go/contracts/core"
"github.com/gateway-fm/perpsv3-Go/contracts/forwarder"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/gateway-fm/perpsv3-Go/models"
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
Expand Down Expand Up @@ -357,113 +356,95 @@ func (s *Service) GetVaultCollateralHistorical(poolID *big.Int, collateralType c
return res.Amount, res.Value, nil
}

func (s *Service) GetVaultDebt(poolID *big.Int, collateralType common.Address) (*big.Int, error) {
func (s *Service) GetVaultDebtHistorical(poolID *big.Int, collateralType common.Address, blockNumber *big.Int) (*big.Int, error) {
if poolID == nil {
logger.Log().WithField("layer", "Service-GetGetVaultDebt").Errorf("received nil pool id")
return nil, errors.GetInvalidArgumentErr("pool id cannot be nil")
}
return s.getVaultDebtRetries(poolID, collateralType, s.multicallRetries)
return s.getVaultDebtRetries(poolID, collateralType, s.multicallRetries, blockNumber)
}

func (s *Service) getVaultDebtRetries(poolID *big.Int, collateralType common.Address, fails int) (res *big.Int, err error) {
res, err = s.getVaultDebtMultiCallNoPyth(poolID, collateralType)
if err != nil && fails <= s.multicallRetries {
time.Sleep(s.multicallWait)
return s.getVaultDebtRetries(poolID, collateralType, fails+1)
func (s *Service) GetVaultDebt(poolID *big.Int, collateralType common.Address) (*big.Int, error) {
if poolID == nil {
logger.Log().WithField("layer", "Service-GetGetVaultDebt").Errorf("received nil pool id")
return nil, errors.GetInvalidArgumentErr("pool id cannot be nil")
}

return res, err
return s.getVaultDebtRetries(poolID, collateralType, s.multicallRetries, nil)
}

func (s *Service) getVaultDebtMultiCallNoPyth(poolID *big.Int, collateralType common.Address) (res *big.Int, err error) {
getVaultDebtCallData, err := s.rawCore.GetCallDataVaultDebt(poolID, collateralType)
if err != nil {
return res, err
}
func (s *Service) getVaultDebtStaticCall(poolID *big.Int, collateralType common.Address, blockNumber *big.Int) (*big.Int, error) {
addr := s.rawCore.Address()

callVaultDebt := forwarder.TrustedMulticallForwarderCall3Value{
Target: s.rawCore.Address(),
RequireSuccess: true,
Value: big.NewInt(0),
CallData: getVaultDebtCallData,
}

call, err := s.rawForwarder.Aggregate3Value(0, []forwarder.TrustedMulticallForwarderCall3Value{callVaultDebt})
data, err := s.rawCore.GetCallDataVaultDebt(poolID, collateralType)
if err != nil {
return res, err
}

if len(call) != 1 {
logger.Log().WithField("layer", "getMarketSummaryMultiCallNoPyth").Errorf("received %v from rawForwarder contract, expected 1", len(call))
return res, errors.GetReadContractErr(fmt.Errorf("invalid call"), "rawForwarder", "Aggregate3Value")
logger.Log().WithField("layer", "Service-getVaultDebtStaticCall").Errorf("err encode getVaultDebt data: %s", err.Error())
return nil, errors.GetReadContractErr(err, "Core", "getVaultDebt")
}

if !call[0].Success {
logger.Log().WithField("layer", "getMarketSummaryMultiCallNoPyth").Error("call to perps unsuccessful")
return res, errors.GetReadContractErr(fmt.Errorf("invalid call to perps"), "rawForwarder", "Aggregate3Value")
res, err := s.rpcClient.CallContract(context.Background(), ethereum.CallMsg{
To: &addr,
Data: data,
}, blockNumber)
if err != nil {
logger.Log().WithField("layer", "Service-getVaultDebtStaticCall").Errorf("err static call getVaultDebt: %s", err.Error())
return nil, errors.GetReadContractErr(err, "Core", "getVaultDebt")
}

unpackedDebt, err := s.rawCore.UnpackVaultDebt(call[0].ReturnData)
value, err := s.rawCore.UnpackVaultDebt(res)
if err != nil {
return res, err
logger.Log().WithField("layer", "Service-getVaultDebtStaticCall").Errorf("err unpack getVaultDebt call data: %s", err.Error())
return nil, errors.GetReadContractErr(err, "Core", "getVaultDebt")
}

return unpackedDebt, nil
}

func (s *Service) GetVaultDebtHistorical(poolID *big.Int, collateralType common.Address, blockNumber *big.Int) (*big.Int, error) {
if poolID == nil {
logger.Log().WithField("layer", "Service-GetGetVaultDebt").Errorf("received nil pool id")
return nil, errors.GetInvalidArgumentErr("pool id cannot be nil")
}
return s.getVaultDebtRetriesHistorical(poolID, collateralType, s.multicallRetries, blockNumber)
return value, nil
}

func (s *Service) getVaultDebtRetriesHistorical(poolID *big.Int, collateralType common.Address, fails int, blockNumber *big.Int) (res *big.Int, err error) {
res, err = s.getVaultDebtMultiCallNoPythHistorical(poolID, collateralType, blockNumber)
func (s *Service) getVaultDebtRetries(poolID *big.Int, collateralType common.Address, fails int, blockNumber *big.Int) (res *big.Int, err error) {
res, err = s.getVaultDebtStaticCall(poolID, collateralType, blockNumber)
if err != nil && fails <= s.multicallRetries {
time.Sleep(s.multicallWait)
return s.getVaultDebtRetriesHistorical(poolID, collateralType, fails+1, blockNumber)
return s.getVaultDebtRetries(poolID, collateralType, fails+1, blockNumber)
}

return res, err
}

func (s *Service) getVaultDebtMultiCallNoPythHistorical(poolID *big.Int, collateralType common.Address, blockNumber *big.Int) (res *big.Int, err error) {
getVaultDebtCallData, err := s.rawCore.GetCallDataVaultDebt(poolID, collateralType)
if err != nil {
return res, err
}

callVaultDebt := forwarder.TrustedMulticallForwarderCall3Value{
Target: s.rawCore.Address(),
RequireSuccess: true,
Value: big.NewInt(0),
CallData: getVaultDebtCallData,
}

call, err := s.rawForwarder.Aggregate3ValueHistorical(0, []forwarder.TrustedMulticallForwarderCall3Value{callVaultDebt}, blockNumber)
if err != nil {
return res, err
}

if len(call) != 1 {
logger.Log().WithField("layer", "getMarketSummaryMultiCallNoPyth").Errorf("received %v from rawForwarder contract, expected 1", len(call))
return res, errors.GetReadContractErr(fmt.Errorf("invalid call"), "rawForwarder", "Aggregate3Value")
}

if !call[0].Success {
logger.Log().WithField("layer", "getMarketSummaryMultiCallNoPyth").Error("call to perps unsuccessful")
return res, errors.GetReadContractErr(fmt.Errorf("invalid call to perps"), "rawForwarder", "Aggregate3Value")
}

unpackedDebt, err := s.rawCore.UnpackVaultDebt(call[0].ReturnData)
if err != nil {
return res, err
}

return unpackedDebt, nil
}
// deprecated
//func (s *Service) getVaultDebtMultiCallNoPyth(poolID *big.Int, collateralType common.Address) (res *big.Int, err error) {
// getVaultDebtCallData, err := s.rawCore.GetCallDataVaultDebt(poolID, collateralType)
// if err != nil {
// return res, err
// }
//
// callVaultDebt := forwarder.TrustedMulticallForwarderCall3Value{
// Target: s.rawCore.Address(),
// RequireSuccess: true,
// Value: big.NewInt(0),
// CallData: getVaultDebtCallData,
// }
//
// call, err := s.rawForwarder.Aggregate3Value(0, []forwarder.TrustedMulticallForwarderCall3Value{callVaultDebt})
// if err != nil {
// return res, err
// }
//
// if len(call) != 1 {
// logger.Log().WithField("layer", "getMarketSummaryMultiCallNoPyth").Errorf("received %v from rawForwarder contract, expected 1", len(call))
// return res, errors.GetReadContractErr(fmt.Errorf("invalid call"), "rawForwarder", "Aggregate3Value")
// }
//
// if !call[0].Success {
// logger.Log().WithField("layer", "getMarketSummaryMultiCallNoPyth").Error("call to perps unsuccessful")
// return res, errors.GetReadContractErr(fmt.Errorf("invalid call to perps"), "rawForwarder", "Aggregate3Value")
// }
//
// unpackedDebt, err := s.rawCore.UnpackVaultDebt(call[0].ReturnData)
// if err != nil {
// return res, err
// }
//
// return unpackedDebt, nil
//}

func (s *Service) GetPoolConfiguration(poolID *big.Int) (*models.PoolConfiguration, error) {
res, err := s.core.GetPoolConfiguration(nil, poolID)
Expand Down

0 comments on commit 172443c

Please sign in to comment.