-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
690 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.