Skip to content

Commit

Permalink
feat: rewards events
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovov committed Feb 9, 2024
1 parent 113e992 commit 0fd26f9
Show file tree
Hide file tree
Showing 11 changed files with 690 additions and 4 deletions.
8 changes: 4 additions & 4 deletions contracts/core/contract.go

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

8 changes: 8 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ type IEvents interface {
// ListenCollateralDeposited is used to listen to all 'Deposited' Core contract events and return them as models.CollateralDeposited
// struct and return errors on ErrChan chanel
ListenCollateralDeposited() (*CollateralDepositedSubscription, error)

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

// 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)
}

// Events implements IEvents interface
Expand Down
82 changes: 82 additions & 0 deletions events/rewardClaimed.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"
)

// RewardClaimedSubscription is a struct for listening to all 'RewardClaimed' contract events and return them as models.RewardClaimed struct
type RewardClaimedSubscription struct {
*basicSubscription
RewardClaimedChan chan *models.RewardClaimed
contractEventChan chan *core.CoreRewardsClaimed
}

func (e *Events) ListenRewardClaimed() (*RewardClaimedSubscription, error) {
contractEventChan := make(chan *core.CoreRewardsClaimed)

contractSub, err := e.core.WatchRewardsClaimed(nil, contractEventChan, nil, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenRewardClaimed").Errorf("error watch reward claimed: %v", err.Error())
return nil, errors.GetEventListenErr(err, "RewardClaimed")
}

rewardSub := newRewardClaimedSubscription(contractSub, contractEventChan)

go rewardSub.listen(e.rpcClient)

return rewardSub, nil
}

// newRewardClaimedSubscription is used to create new RewardClaimedSubscription instance
func newRewardClaimedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreRewardsClaimed) *RewardClaimedSubscription {
return &RewardClaimedSubscription{
basicSubscription: newBasicSubscription(eventSub),
contractEventChan: contractEventChan,
RewardClaimedChan: make(chan *models.RewardClaimed),
}
}

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

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

claimed := models.GetRewardClaimedFromEvent(eventRewardClaimed, time)

s.RewardClaimedChan <- claimed
}
}
}
82 changes: 82 additions & 0 deletions events/rewardDistributed.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"
)

// RewardDistributedSubscription is a struct for listening to all 'RewardDistributed' contract events and return them as models.RewardDistributed struct
type RewardDistributedSubscription struct {
*basicSubscription
RewardDistributedChan chan *models.RewardDistributed
contractEventChan chan *core.CoreRewardsDistributed
}

func (e *Events) ListenRewardDistributed() (*RewardDistributedSubscription, error) {
contractEventChan := make(chan *core.CoreRewardsDistributed)

contractSub, err := e.core.WatchRewardsDistributed(nil, contractEventChan, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Events-ListenRewardDistributed").Errorf("error watch reward claimed: %v", err.Error())
return nil, errors.GetEventListenErr(err, "RewardDistributed")
}

rewardSub := newRewardDistributedSubscription(contractSub, contractEventChan)

go rewardSub.listen(e.rpcClient)

return rewardSub, nil
}

// newRewardDistributedSubscription is used to create new RewardDistributedSubscription instance
func newRewardDistributedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreRewardsDistributed) *RewardDistributedSubscription {
return &RewardDistributedSubscription{
basicSubscription: newBasicSubscription(eventSub),
contractEventChan: contractEventChan,
RewardDistributedChan: make(chan *models.RewardDistributed),
}
}

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

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

distributed := models.GetRewardDistributedFromEvent(eventRewardDistributed, time)

s.RewardDistributedChan <- distributed
}
}
}
105 changes: 105 additions & 0 deletions mocks/events/mockEvents.go

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

Loading

0 comments on commit 0fd26f9

Please sign in to comment.