Skip to content

Commit

Permalink
feat: vault methods
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovov committed Feb 21, 2024
1 parent f41f60d commit 6c14108
Show file tree
Hide file tree
Showing 9 changed files with 627 additions and 0 deletions.
8 changes: 8 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ type IEvents interface {
// ListenRewardClaimed is used to listen to all 'RewardClaimed' Core contract events and return them as models.RewardClaimed
// struct and return errors on ErrChan chanel
ListenRewardClaimed() (*RewardClaimedSubscription, error)

// ListenMarketUSDWithdrawn is used to listen to all 'MarketUSDWithdrawn' Core contract events and return them as models.MarketUSDWithdrawn
// struct and return errors on ErrChan chanel
ListenMarketUSDWithdrawn() (*MarketUSDWithdrawnSubscription, error)

// ListenMarketUSDDeposited is used to listen to all 'MarketUSDDeposited' Core contract events and return them as models.MarketUSDDeposited
// struct and return errors on ErrChan chanel
ListenMarketUSDDeposited() (*MarketUSDDepositedSubscription, error)
}

// Events implements IEvents interface
Expand Down
82 changes: 82 additions & 0 deletions events/marketUSDDeposited.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package events

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/event"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/gateway-fm/perpsv3-Go/models"
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

// MarketUSDDepositedSubscription is a struct for listening to all 'MarketUSDDeposited' contract events and return them as models.MarketUSDDeposited struct
type MarketUSDDepositedSubscription struct {
*basicSubscription
MarketUSDDepositedChan chan *models.MarketUSDDeposited
contractEventChan chan *core.CoreMarketUsdDeposited
}

func (e *Events) ListenMarketUSDDeposited() (*MarketUSDDepositedSubscription, error) {
contractEventChan := make(chan *core.CoreMarketUsdDeposited)

contractSub, err := e.core.WatchMarketUsdDeposited(nil, contractEventChan, nil, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenMarketUSDDeposited").Errorf("error watch deposit: %v", err.Error())
return nil, errors.GetEventListenErr(err, "MarketUSDDeposited")
}

depositSub := newMarketUSDDepositedSubscription(contractSub, contractEventChan)

go depositSub.listen(e.rpcClient)

return depositSub, nil
}

// newMarketUSDDepositedSubscription is used to create new MarketUSDDepositedSubscription instance
func newMarketUSDDepositedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreMarketUsdDeposited) *MarketUSDDepositedSubscription {
return &MarketUSDDepositedSubscription{
basicSubscription: newBasicSubscription(eventSub),
contractEventChan: contractEventChan,
MarketUSDDepositedChan: make(chan *models.MarketUSDDeposited),
}
}

// listen is used to run a goroutine
func (s *MarketUSDDepositedSubscription) listen(rpcClient *ethclient.Client) {
defer func() {
close(s.MarketUSDDepositedChan)
close(s.contractEventChan)
}()

for {
select {
case <-s.stop:
return
case err := <-s.eventSub.Err():
if err != nil {
logger.Log().WithField("layer", "Events-MarketUSDDeposited").Errorf("error listening deposit: %v", err.Error())
s.ErrChan <- err
}
return
case eventMarketUSDDeposited := <-s.contractEventChan:
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventMarketUSDDeposited.Raw.BlockNumber)))
time := uint64(0)
if err != nil {
logger.Log().WithField("layer", "Events-MarketUSDDeposited").Warningf(
"error fetching block number %v: %v; order event time set to 0 ",
eventMarketUSDDeposited.Raw.BlockNumber, err.Error(),
)
s.ErrChan <- err
} else {
time = block.Time
}

deposited := models.GetMarketUSDDepositedFromEvent(eventMarketUSDDeposited, time)

s.MarketUSDDepositedChan <- deposited
}
}
}
82 changes: 82 additions & 0 deletions events/marketUSDWithdrawn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package events

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/event"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/gateway-fm/perpsv3-Go/models"
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

// MarketUSDWithdrawnSubscription is a struct for listening to all 'MarketUSDWithdrawn' contract events and return them as models.MarketUSDWithdrawn struct
type MarketUSDWithdrawnSubscription struct {
*basicSubscription
MarketUSDWithdrawnChan chan *models.MarketUSDWithdrawn
contractEventChan chan *core.CoreMarketUsdWithdrawn
}

func (e *Events) ListenMarketUSDWithdrawn() (*MarketUSDWithdrawnSubscription, error) {
contractEventChan := make(chan *core.CoreMarketUsdWithdrawn)

contractSub, err := e.core.WatchMarketUsdWithdrawn(nil, contractEventChan, nil, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenMarketUSDWithdrawn").Errorf("error watch withdraw: %v", err.Error())
return nil, errors.GetEventListenErr(err, "MarketUSDWithdrawn")
}

withdrawSub := newMarketUSDWithdrawnSubscription(contractSub, contractEventChan)

go withdrawSub.listen(e.rpcClient)

return withdrawSub, nil
}

// newMarketUSDWithdrawnSubscription is used to create new MarketUSDWithdrawnSubscription instance
func newMarketUSDWithdrawnSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreMarketUsdWithdrawn) *MarketUSDWithdrawnSubscription {
return &MarketUSDWithdrawnSubscription{
basicSubscription: newBasicSubscription(eventSub),
contractEventChan: contractEventChan,
MarketUSDWithdrawnChan: make(chan *models.MarketUSDWithdrawn),
}
}

// listen is used to run a goroutine
func (s *MarketUSDWithdrawnSubscription) listen(rpcClient *ethclient.Client) {
defer func() {
close(s.MarketUSDWithdrawnChan)
close(s.contractEventChan)
}()

for {
select {
case <-s.stop:
return
case err := <-s.eventSub.Err():
if err != nil {
logger.Log().WithField("layer", "Events-MarketUSDWithdrawn").Errorf("error listening withdraw: %v", err.Error())
s.ErrChan <- err
}
return
case eventMarketUSDWithdrawn := <-s.contractEventChan:
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventMarketUSDWithdrawn.Raw.BlockNumber)))
time := uint64(0)
if err != nil {
logger.Log().WithField("layer", "Events-MarketUSDWithdrawn").Warningf(
"error fetching block number %v: %v; order event time set to 0 ",
eventMarketUSDWithdrawn.Raw.BlockNumber, err.Error(),
)
s.ErrChan <- err
} else {
time = block.Time
}

withdrawn := models.GetMarketUSDWithdrawnFromEvent(eventMarketUSDWithdrawn, time)

s.MarketUSDWithdrawnChan <- withdrawn
}
}
}
72 changes: 72 additions & 0 deletions models/marketUSD.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package models

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
)

type MarketUSDDeposited struct {
MarketId *big.Int
Target common.Address
Amount *big.Int
Market common.Address
CreditCapacity *big.Int
NetIssuance *big.Int
DepositedCollateralValue *big.Int
ReportedDebt *big.Int
BlockNumber uint64
BlockTimestamp uint64
TransactionHash string
}

type MarketUSDWithdrawn struct {
MarketId *big.Int
Target common.Address
Amount *big.Int
Market common.Address
CreditCapacity *big.Int
NetIssuance *big.Int
DepositedCollateralValue *big.Int
ReportedDebt *big.Int
BlockNumber uint64
BlockTimestamp uint64
TransactionHash string
}

func GetMarketUSDDepositedFromEvent(event *core.CoreMarketUsdDeposited, time uint64) *MarketUSDDeposited {
m := &MarketUSDDeposited{}

m.MarketId = event.MarketId
m.Target = event.Target
m.Amount = event.Amount
m.Market = event.Market
m.CreditCapacity = event.CreditCapacity
m.NetIssuance = event.NetIssuance
m.DepositedCollateralValue = event.DepositedCollateralValue
m.ReportedDebt = event.ReportedDebt
m.BlockNumber = event.Raw.BlockNumber
m.BlockTimestamp = time
m.TransactionHash = event.Raw.TxHash.Hex()

return m
}

func GetMarketUSDWithdrawnFromEvent(event *core.CoreMarketUsdWithdrawn, time uint64) *MarketUSDWithdrawn {
m := &MarketUSDWithdrawn{}

m.MarketId = event.MarketId
m.Target = event.Target
m.Amount = event.Amount
m.Market = event.Market
m.CreditCapacity = event.CreditCapacity
m.NetIssuance = event.NetIssuance
m.DepositedCollateralValue = event.DepositedCollateralValue
m.ReportedDebt = event.ReportedDebt
m.BlockNumber = event.Raw.BlockNumber
m.BlockTimestamp = time
m.TransactionHash = event.Raw.TxHash.Hex()

return m
}
46 changes: 46 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ type IPerpsv3 interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveRewardDistributedLimit(limit uint64) ([]*models.RewardDistributed, error)

// RetrieveMarketUSDDepositedLimit is used to get all `MarketUSDDeposited` events from the Core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveMarketUSDDepositedLimit(limit uint64) ([]*models.MarketUSDDeposited, error)

// RetrieveMarketUSDWithdrawnLimit is used to get all `MarketUSDWithdrawn` events from the Core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveMarketUSDWithdrawnLimit(limit uint64) ([]*models.MarketUSDWithdrawn, error)

// ListenTrades is used to subscribe on the contract "OrderSettled" event. The goroutine will return events on the
// TradesChan chanel and errors on the ErrChan chanel.
// To close the subscription use events.TradeSubscription `Close` function
Expand Down Expand Up @@ -169,6 +177,14 @@ type IPerpsv3 interface {
// struct and return errors on ErrChan chanel
ListenRewardClaimed() (*events.RewardClaimedSubscription, error)

// ListenMarketUSDWithdrawn is used to listen to all 'MarketUSDWithdrawn' Core contract events and return them as models.MarketUSDWithdrawn
// struct and return errors on ErrChan chanel
ListenMarketUSDWithdrawn() (*events.MarketUSDWithdrawnSubscription, error)

// ListenMarketUSDDeposited is used to listen to all 'MarketUSDDeposited' Core contract events and return them as models.MarketUSDDeposited
// struct and return errors on ErrChan chanel
ListenMarketUSDDeposited() (*events.MarketUSDDepositedSubscription, error)

// GetPosition is used to get position data struct from latest block with given params
// Function can return contract error if market ID is invalid
GetPosition(accountID *big.Int, marketID *big.Int) (*models.Position, error)
Expand Down Expand Up @@ -210,6 +226,12 @@ type IPerpsv3 interface {
// GetCollateralPrice is used to get collateral price for given block number and collateralType
GetCollateralPrice(blockNumber *big.Int, collateralType common.Address) (*models.CollateralPrice, error)

// GetVaultDebt is used to get vault debt for given pool ID and collateralType
GetVaultDebt(poolID *big.Int, collateralType common.Address) (*big.Int, error)

// GetVaultCollateral is used to get vault collateral for given pool ID and collateralType
GetVaultCollateral(poolID *big.Int, collateralType common.Address) (amount *big.Int, value *big.Int, err 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 @@ -336,6 +358,14 @@ func (p *Perpsv3) RetrieveRewardDistributedLimit(limit uint64) ([]*models.Reward
return p.service.RetrieveRewardDistributedLimit(limit)
}

func (p *Perpsv3) RetrieveMarketUSDDepositedLimit(limit uint64) ([]*models.MarketUSDDeposited, error) {
return p.service.RetrieveMarketUSDDepositedLimit(limit)
}

func (p *Perpsv3) RetrieveMarketUSDWithdrawnLimit(limit uint64) ([]*models.MarketUSDWithdrawn, error) {
return p.service.RetrieveMarketUSDWithdrawnLimit(limit)
}

func (p *Perpsv3) ListenTrades() (*events.TradeSubscription, error) {
return p.events.ListenTrades()
}
Expand Down Expand Up @@ -400,6 +430,14 @@ func (p *Perpsv3) ListenRewardClaimed() (*events.RewardClaimedSubscription, erro
return p.events.ListenRewardClaimed()
}

func (p *Perpsv3) ListenMarketUSDWithdrawn() (*events.MarketUSDWithdrawnSubscription, error) {
return p.events.ListenMarketUSDWithdrawn()
}

func (p *Perpsv3) ListenMarketUSDDeposited() (*events.MarketUSDDepositedSubscription, error) {
return p.events.ListenMarketUSDDeposited()
}

func (p *Perpsv3) GetPosition(accountID *big.Int, marketID *big.Int) (*models.Position, error) {
return p.service.GetPosition(accountID, marketID)
}
Expand Down Expand Up @@ -452,6 +490,14 @@ func (p *Perpsv3) GetCollateralPrice(blockNumber *big.Int, collateralType common
return p.service.GetCollateralPrice(blockNumber, collateralType)
}

func (p *Perpsv3) GetVaultDebt(poolID *big.Int, collateralType common.Address) (*big.Int, error) {
return p.service.GetVaultDebt(poolID, collateralType)
}

func (p *Perpsv3) GetVaultCollateral(poolID *big.Int, collateralType common.Address) (amount *big.Int, value *big.Int, err error) {
return p.service.GetVaultCollateral(poolID, collateralType)
}

func (p *Perpsv3) FormatAccounts() ([]*models.Account, error) {
return p.service.FormatAccounts()
}
Expand Down
Loading

0 comments on commit 6c14108

Please sign in to comment.