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-22: add delegation updates, usd burned events #51

Merged
merged 1 commit into from
Jan 16, 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 @@ -87,7 +87,7 @@ func GetBaseSepoliaDefaultConfig(rpcURL string) *PerpsvConfig {
// GetBaseAndromedaDefaultConfig is used to get default lib config for base goerli test net
func GetBaseAndromedaDefaultConfig(rpcURL string) *PerpsvConfig {
if rpcURL == "" {
rpcURL = "https://rpc.ankr.com/base_goerli/6259fa6541ffabb10ca241f7f437c2389ab7dda38c7be817ab0fb76992e73ae5"
rpcURL = "https://base.fastnode.cc/FNGfsgKFzvLlFKHWrNUYOWIgFRTdiAcVrtAxSMbMfZGdKDUDFgagkwnDgxPqgBit/"
}

return &PerpsvConfig{
Expand Down
6 changes: 3 additions & 3 deletions contracts/core/contract.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions events/delegationUpdated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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"
)

// DelegationUpdatedSubscription is a struct for listening to all 'DelegationUpdated' contract events and return them as models.DelegationUpdated struct
type DelegationUpdatedSubscription struct {
*basicSubscription
DelegationUpdatedChan chan *models.DelegationUpdated
contractEventChan chan *core.CoreDelegationUpdated
}

func (e *Events) ListenDelegationUpdated() (*DelegationUpdatedSubscription, error) {
contractEventChan := make(chan *core.CoreDelegationUpdated)

contractSub, err := e.core.WatchDelegationUpdated(nil, contractEventChan, nil, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenDelegationUpdated").Errorf("error watch usd burned: %v", err.Error())
return nil, errors.GetEventListenErr(err, "DelegationUpdated")
}

delegationSub := newDelegationUpdatedSubscription(contractSub, contractEventChan)

go delegationSub.listen(e.rpcClient)

return delegationSub, nil
}

// newDelegationUpdatedSubscription is used to create new DelegationUpdatedSubscription instance
func newDelegationUpdatedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreDelegationUpdated) *DelegationUpdatedSubscription {
return &DelegationUpdatedSubscription{
basicSubscription: newBasicSubscription(eventSub),
contractEventChan: contractEventChan,
DelegationUpdatedChan: make(chan *models.DelegationUpdated),
}
}

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

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

delegation := models.GetDelegationUpdatedFromEvent(eventDelegationUpdated, time)

s.DelegationUpdatedChan <- delegation
}
}
}
8 changes: 8 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type IEvents interface {
// ListenUSDMinted is used to listen to all 'USDMinted' Core contract events and return them as models.USDMinted
// struct and return errors on ErrChan chanel
ListenUSDMinted() (*USDMintedSubscription, error)

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

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

// Events implements IEvents interface
Expand Down
83 changes: 83 additions & 0 deletions events/usdBurned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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"
)

// USDBurnedSubscription is a struct for listening to all 'UsdBurned' contract events and return them as models.USDBurned struct
type USDBurnedSubscription struct {
*basicSubscription
USDBurnedChan chan *models.USDBurned
contractEventChan chan *core.CoreUsdBurned
}

func (e *Events) ListenUSDBurned() (*USDBurnedSubscription, error) {
contractEventChan := make(chan *core.CoreUsdBurned)

contractSub, err := e.core.WatchUsdBurned(nil, contractEventChan, nil, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenUSDBurned").Errorf("error watch usd burned: %v", err.Error())
return nil, errors.GetEventListenErr(err, "USDBurned")
}

orderSub := newUSDBurnedSubscription(contractSub, contractEventChan)

go orderSub.listen(e.rpcClient)

return orderSub, nil
}

// newUSDBurnedSubscription is used to create new USDBurnedSubscription instance
func newUSDBurnedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreUsdBurned) *USDBurnedSubscription {
return &USDBurnedSubscription{
basicSubscription: newBasicSubscription(eventSub),
contractEventChan: contractEventChan,
USDBurnedChan: make(chan *models.USDBurned),
}
}

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

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

burn := models.GetUSDBurnedFromEvent(eventUSDBurned, time)

s.USDBurnedChan <- burn
}
}
}
18 changes: 9 additions & 9 deletions events/usdMinted.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

// USDMintedSubscription is a struct for listening to all 'PositionLiquidated' contract events and return them as models.USDMinted struct
// USDMintedSubscription is a struct for listening to all 'UsdMinted' contract events and return them as models.USDMinted struct
type USDMintedSubscription struct {
*basicSubscription
USDMintedsChan chan *models.USDMinted
Expand All @@ -25,7 +25,7 @@ func (e *Events) ListenUSDMinted() (*USDMintedSubscription, error) {

contractSub, err := e.core.WatchUsdMinted(nil, contractEventChan, nil, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenUSDMinteds").Errorf("error watch usd minted: %v", err.Error())
logger.Log().WithField("layer", "Events-ListenUSDMinted").Errorf("error watch usd minted: %v", err.Error())
return nil, errors.GetEventListenErr(err, "USDMinted")
}

Expand Down Expand Up @@ -58,26 +58,26 @@ func (s *USDMintedSubscription) listen(rpcClient *ethclient.Client) {
return
case err := <-s.eventSub.Err():
if err != nil {
logger.Log().WithField("layer", "Events-PositionLiquidated").Errorf("error listening position liquidated: %v", err.Error())
logger.Log().WithField("layer", "Events-UsdMinted").Errorf("error listening usd minted: %v", err.Error())
s.ErrChan <- err
}
return
case positionLiquidated := <-s.contractEventChan:
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(positionLiquidated.Raw.BlockNumber)))
case eventUsdMinted := <-s.contractEventChan:
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventUsdMinted.Raw.BlockNumber)))
time := uint64(0)
if err != nil {
logger.Log().WithField("layer", "Events-PositionLiquidated").Warningf(
logger.Log().WithField("layer", "Events-UsdMinted").Warningf(
"error fetching block number %v: %v; order event time set to 0 ",
positionLiquidated.Raw.BlockNumber, err.Error(),
eventUsdMinted.Raw.BlockNumber, err.Error(),
)
s.ErrChan <- err
} else {
time = block.Time
}

order := models.GetUSDMintedFromEvent(positionLiquidated, time)
mint := models.GetUSDMintedFromEvent(eventUsdMinted, time)

s.USDMintedsChan <- order
s.USDMintedsChan <- mint
}
}
}
38 changes: 38 additions & 0 deletions models/delegationUpdated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package models

import (
"github.com/ethereum/go-ethereum/common"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
"math/big"
)

// DelegationUpdated is a `DelegationUpdated` Core smart-contract event struct
type DelegationUpdated struct {
AccountId *big.Int
PoolId *big.Int
CollateralType common.Address
Amount *big.Int
Leverage *big.Int
Sender common.Address
BlockNumber uint64
BlockTimestamp uint64
}

// GetDelegationUpdatedFromEvent is used to get DelegationUpdated struct from given contract event
func GetDelegationUpdatedFromEvent(event *core.CoreDelegationUpdated, time uint64) *DelegationUpdated {
if event == nil {
logger.Log().WithField("layer", "Models-CoreDelegationUpdated").Warning("nil event received")
return &DelegationUpdated{}
}

return &DelegationUpdated{
AccountId: event.AccountId,
PoolId: event.PoolId,
CollateralType: event.CollateralType,
Amount: event.Amount,
Sender: event.Sender,
BlockNumber: event.Raw.BlockNumber,
BlockTimestamp: time,
}
}
39 changes: 39 additions & 0 deletions models/usdBurned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package models

import (
"math/big"

"github.com/ethereum/go-ethereum/common"

"github.com/gateway-fm/perpsv3-Go/contracts/core"
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

// USDBurned is a `usdBurned` Core smart-contract event struct
type USDBurned struct {
AccountId *big.Int
PoolId *big.Int
CollateralType common.Address
Amount *big.Int
Sender common.Address
BlockNumber uint64
BlockTimestamp uint64
}

// GetUSDBurnedFromEvent is used to get USDBurned struct from given contract event
func GetUSDBurnedFromEvent(event *core.CoreUsdBurned, time uint64) *USDBurned {
if event == nil {
logger.Log().WithField("layer", "Models-USDBurned").Warning("nil event received")
return &USDBurned{}
}

return &USDBurned{
AccountId: event.AccountId,
PoolId: event.PoolId,
CollateralType: event.CollateralType,
Amount: event.Amount,
Sender: event.Sender,
BlockNumber: event.Raw.BlockNumber,
BlockTimestamp: time,
}
}
Loading