-
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.
SNX-12: add collateral deposited and withdrawn events
- Loading branch information
Showing
9 changed files
with
462 additions
and
6 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
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" | ||
) | ||
|
||
// CollateralDepositedSubscription is a struct for listening to all 'Deposited' contract events and return them as models.CollateralDeposited struct | ||
type CollateralDepositedSubscription struct { | ||
*basicSubscription | ||
CollateralDepositedChan chan *models.CollateralDeposited | ||
contractEventChan chan *core.CoreDeposited | ||
} | ||
|
||
func (e *Events) ListenCollateralDeposited() (*CollateralDepositedSubscription, error) { | ||
contractEventChan := make(chan *core.CoreDeposited) | ||
|
||
contractSub, err := e.core.WatchDeposited(nil, contractEventChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-ListenCollateralDeposited").Errorf("error watch withdrawn: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "CollateralDeposited") | ||
} | ||
|
||
depositSub := newCollateralDepositedSubscription(contractSub, contractEventChan) | ||
|
||
go depositSub.listen(e.rpcClient) | ||
|
||
return depositSub, nil | ||
} | ||
|
||
// newCollateralDepositedSubscription is used to create new CollateralDepositedSubscription instance | ||
func newCollateralDepositedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreDeposited) *CollateralDepositedSubscription { | ||
return &CollateralDepositedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
contractEventChan: contractEventChan, | ||
CollateralDepositedChan: make(chan *models.CollateralDeposited), | ||
} | ||
} | ||
|
||
// listen is used to run a goroutine | ||
func (s *CollateralDepositedSubscription) listen(rpcClient *ethclient.Client) { | ||
defer func() { | ||
close(s.CollateralDepositedChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-CollateralDeposited").Errorf("error listening withdrawn: %v", err.Error()) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case eventCollateralDeposited := <-s.contractEventChan: | ||
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventCollateralDeposited.Raw.BlockNumber))) | ||
time := uint64(0) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-CollateralDeposited").Warningf( | ||
"error fetching block number %v: %v; deposit event time set to 0 ", | ||
eventCollateralDeposited.Raw.BlockNumber, err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} else { | ||
time = block.Time | ||
} | ||
|
||
deposit := models.GetCollateralDepositedFromEvent(eventCollateralDeposited, time) | ||
|
||
s.CollateralDepositedChan <- deposit | ||
} | ||
} | ||
} |
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,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" | ||
) | ||
|
||
// CollateralWithdrawnSubscription is a struct for listening to all 'Withdrawn' contract events and return them as models.CollateralWithdrawn struct | ||
type CollateralWithdrawnSubscription struct { | ||
*basicSubscription | ||
CollateralWithdrawnChan chan *models.CollateralWithdrawn | ||
contractEventChan chan *core.CoreWithdrawn | ||
} | ||
|
||
func (e *Events) ListenCollateralWithdrawn() (*CollateralWithdrawnSubscription, error) { | ||
contractEventChan := make(chan *core.CoreWithdrawn) | ||
|
||
contractSub, err := e.core.WatchWithdrawn(nil, contractEventChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-ListenCollateralWithdrawn").Errorf("error watch withdrawn: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "CollateralWithdrawn") | ||
} | ||
|
||
withdrawnSub := newCollateralWithdrawnSubscription(contractSub, contractEventChan) | ||
|
||
go withdrawnSub.listen(e.rpcClient) | ||
|
||
return withdrawnSub, nil | ||
} | ||
|
||
// newCollateralWithdrawnSubscription is used to create new CollateralWithdrawnSubscription instance | ||
func newCollateralWithdrawnSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreWithdrawn) *CollateralWithdrawnSubscription { | ||
return &CollateralWithdrawnSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
contractEventChan: contractEventChan, | ||
CollateralWithdrawnChan: make(chan *models.CollateralWithdrawn), | ||
} | ||
} | ||
|
||
// listen is used to run a goroutine | ||
func (s *CollateralWithdrawnSubscription) listen(rpcClient *ethclient.Client) { | ||
defer func() { | ||
close(s.CollateralWithdrawnChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-CollateralWithdrawn").Errorf("error listening withdrawn: %v", err.Error()) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case eventCollateralWithdrawn := <-s.contractEventChan: | ||
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventCollateralWithdrawn.Raw.BlockNumber))) | ||
time := uint64(0) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-CollateralWithdrawn").Warningf( | ||
"error fetching block number %v: %v; withdraw event time set to 0 ", | ||
eventCollateralWithdrawn.Raw.BlockNumber, err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} else { | ||
time = block.Time | ||
} | ||
|
||
withdraw := models.GetCollateralWithdrawnFromEvent(eventCollateralWithdrawn, time) | ||
|
||
s.CollateralWithdrawnChan <- withdraw | ||
} | ||
} | ||
} |
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,64 @@ | ||
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" | ||
) | ||
|
||
// CollateralDeposited is a `Deposited` Core smart-contract event struct | ||
type CollateralDeposited struct { | ||
AccountId *big.Int | ||
CollateralType common.Address | ||
TokenAmount *big.Int | ||
Sender common.Address | ||
BlockNumber uint64 | ||
BlockTimestamp uint64 | ||
} | ||
|
||
// CollateralWithdrawn is a `Withdrawn` Core smart-contract event struct | ||
type CollateralWithdrawn struct { | ||
AccountId *big.Int | ||
CollateralType common.Address | ||
TokenAmount *big.Int | ||
Sender common.Address | ||
BlockNumber uint64 | ||
BlockTimestamp uint64 | ||
} | ||
|
||
// GetCollateralDepositedFromEvent is used to get CollateralDeposited struct from given contract event | ||
func GetCollateralDepositedFromEvent(event *core.CoreDeposited, time uint64) *CollateralDeposited { | ||
if event == nil { | ||
logger.Log().WithField("layer", "Models-CoreDeposited").Warning("nil event received") | ||
return &CollateralDeposited{} | ||
} | ||
|
||
return &CollateralDeposited{ | ||
AccountId: event.AccountId, | ||
CollateralType: event.CollateralType, | ||
TokenAmount: event.TokenAmount, | ||
Sender: event.Sender, | ||
BlockNumber: event.Raw.BlockNumber, | ||
BlockTimestamp: time, | ||
} | ||
} | ||
|
||
// GetCollateralWithdrawnFromEvent is used to get CollateralWithdrawn struct from given contract event | ||
func GetCollateralWithdrawnFromEvent(event *core.CoreWithdrawn, time uint64) *CollateralWithdrawn { | ||
if event == nil { | ||
logger.Log().WithField("layer", "Models-CoreWithdrawn").Warning("nil event received") | ||
return &CollateralWithdrawn{} | ||
} | ||
|
||
return &CollateralWithdrawn{ | ||
AccountId: event.AccountId, | ||
CollateralType: event.CollateralType, | ||
TokenAmount: event.TokenAmount, | ||
Sender: event.Sender, | ||
BlockNumber: event.Raw.BlockNumber, | ||
BlockTimestamp: time, | ||
} | ||
} |
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
Oops, something went wrong.