-
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.
API-277: account listeners, liquidation retrieve method
- Loading branch information
Showing
12 changed files
with
516 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/event" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli" | ||
"github.com/gateway-fm/perpsv3-Go/errors" | ||
"github.com/gateway-fm/perpsv3-Go/models" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
// AccountCreatedSubscription is a struct for listening to all 'AccountCreated' contract events and return them as models.Account struct | ||
type AccountCreatedSubscription struct { | ||
*basicSubscription | ||
NewAccountChan chan *models.Account | ||
contractEventChan chan *perpsMarketGoerli.PerpsMarketGoerliAccountCreated | ||
} | ||
|
||
func (e *Events) ListenAccountCreated() (*AccountCreatedSubscription, error) { | ||
createdChan := make(chan *perpsMarketGoerli.PerpsMarketGoerliAccountCreated) | ||
|
||
createdSub, err := e.perpsMarket.WatchAccountCreated(nil, createdChan, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-Accounts").Errorf("error watch account created: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "AccountCreated") | ||
} | ||
|
||
accountsSub := newAccountCreatedSubscription(createdSub, createdChan) | ||
|
||
go accountsSub.listen(e.perpsMarket) | ||
|
||
return accountsSub, nil | ||
} | ||
|
||
// newAccountCreatedSubscription is used to get new AccountCreatedSubscription instance | ||
func newAccountCreatedSubscription( | ||
eventSub event.Subscription, | ||
created chan *perpsMarketGoerli.PerpsMarketGoerliAccountCreated, | ||
) *AccountCreatedSubscription { | ||
return &AccountCreatedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
NewAccountChan: make(chan *models.Account), | ||
contractEventChan: created, | ||
} | ||
} | ||
|
||
// listen is used to run events listen goroutine | ||
func (s *AccountCreatedSubscription) listen(perpsMarket *perpsMarketGoerli.PerpsMarketGoerli) { | ||
defer func() { | ||
close(s.NewAccountChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountCreated").Errorf( | ||
"error listening account created info: %v", err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case newAccount := <-s.contractEventChan: | ||
lastInteraction := getAccountLastInteraction(newAccount.AccountId, perpsMarket) | ||
|
||
account := models.FormatAccount( | ||
newAccount.AccountId, | ||
newAccount.Owner, | ||
lastInteraction.Uint64(), | ||
[]perpsMarketGoerli.IAccountModuleAccountPermissions{}, | ||
) | ||
|
||
s.NewAccountChan <- account | ||
} | ||
} | ||
} |
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,76 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/event" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli" | ||
"github.com/gateway-fm/perpsv3-Go/errors" | ||
"github.com/gateway-fm/perpsv3-Go/models" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
// AccountLiquidatedSubscription is a struct for listening to all 'AccountLiquidated' contract events and return them as models.AccountLiquidated struct | ||
type AccountLiquidatedSubscription struct { | ||
*basicSubscription | ||
AccountLiquidated chan *models.AccountLiquidated | ||
contractEventChan chan *perpsMarketGoerli.PerpsMarketGoerliAccountLiquidated | ||
} | ||
|
||
func (e *Events) ListenAccountLiquidated() (*AccountLiquidatedSubscription, error) { | ||
createdChan := make(chan *perpsMarketGoerli.PerpsMarketGoerliAccountLiquidated) | ||
|
||
liquidatedSub, err := e.perpsMarket.WatchAccountLiquidated(nil, createdChan, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountLiquidated").Errorf("error watch account liquidated: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "AccountLiquidated") | ||
} | ||
|
||
accountsSub := newAccountLiquidatedSubscription(liquidatedSub, createdChan) | ||
|
||
go accountsSub.listen(e.perpsMarket) | ||
|
||
return accountsSub, nil | ||
} | ||
|
||
// newAccountsSubscription | ||
func newAccountLiquidatedSubscription( | ||
eventSub event.Subscription, | ||
created chan *perpsMarketGoerli.PerpsMarketGoerliAccountLiquidated, | ||
) *AccountLiquidatedSubscription { | ||
return &AccountLiquidatedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
AccountLiquidated: make(chan *models.AccountLiquidated), | ||
contractEventChan: created, | ||
} | ||
} | ||
|
||
// listen is used to run events listen goroutine | ||
func (s *AccountLiquidatedSubscription) listen(perpsMarket *perpsMarketGoerli.PerpsMarketGoerli) { | ||
defer func() { | ||
close(s.AccountLiquidated) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountLiquidated").Errorf( | ||
"error listening account created info: %v", err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case contractEvent := <-s.contractEventChan: | ||
liquidated := &models.AccountLiquidated{ | ||
ID: contractEvent.AccountId, | ||
Reward: contractEvent.Reward, | ||
FullLiquidated: contractEvent.FullLiquidation, | ||
} | ||
|
||
s.AccountLiquidated <- liquidated | ||
} | ||
} | ||
} |
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,87 @@ | ||
package events | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/event" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli" | ||
"github.com/gateway-fm/perpsv3-Go/errors" | ||
"github.com/gateway-fm/perpsv3-Go/models" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
// AccountPermissionRevokedSubscription is a struct for listening to all 'PermissionRevoked' contract events and return them as models.PermissionChanged struct | ||
type AccountPermissionRevokedSubscription struct { | ||
*basicSubscription | ||
PermissionChangeChan chan *models.PermissionChanged | ||
contractEventChan chan *perpsMarketGoerli.PerpsMarketGoerliPermissionRevoked | ||
} | ||
|
||
func (e *Events) ListenAccountPermissionRevoked() (*AccountPermissionRevokedSubscription, error) { | ||
revokedChan := make(chan *perpsMarketGoerli.PerpsMarketGoerliPermissionRevoked) | ||
|
||
revokedSub, err := e.perpsMarket.WatchPermissionRevoked(nil, revokedChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountPermissionRevoked").Errorf("error watch permission revoked: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "AccountPermissionRevoked") | ||
} | ||
|
||
accountsSub := newAccountPermissionRevokedSubscription(revokedSub, revokedChan) | ||
|
||
go accountsSub.listen(e.perpsMarket) | ||
|
||
return accountsSub, nil | ||
} | ||
|
||
// newAccountPermissionRevokedSubscription is used to get new AccountPermissionRevokedSubscription instance | ||
func newAccountPermissionRevokedSubscription( | ||
eventSub event.Subscription, | ||
revoked chan *perpsMarketGoerli.PerpsMarketGoerliPermissionRevoked, | ||
) *AccountPermissionRevokedSubscription { | ||
return &AccountPermissionRevokedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
PermissionChangeChan: make(chan *models.PermissionChanged), | ||
contractEventChan: revoked, | ||
} | ||
} | ||
|
||
// listen is used to run events listen goroutine | ||
func (s *AccountPermissionRevokedSubscription) listen(perpsMarket *perpsMarketGoerli.PerpsMarketGoerli) { | ||
defer func() { | ||
close(s.PermissionChangeChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountPermissionRevoked").Errorf( | ||
"error listening account permission revoked: %v", err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case contractEvent := <-s.contractEventChan: | ||
p, err := models.PermissionFromString(strings.TrimRight(string(contractEvent.Permission[:]), string(rune(0)))) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountPermissionRevoked").Errorf( | ||
"error decode permission %v: %v", string(contractEvent.Permission[:]), err.Error(), | ||
) | ||
s.ErrChan <- err | ||
continue | ||
} | ||
|
||
change := &models.PermissionChanged{ | ||
AccountID: contractEvent.AccountId, | ||
User: contractEvent.User, | ||
Permission: p, | ||
} | ||
|
||
s.PermissionChangeChan <- change | ||
} | ||
} | ||
} |
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,87 @@ | ||
package events | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/event" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli" | ||
"github.com/gateway-fm/perpsv3-Go/errors" | ||
"github.com/gateway-fm/perpsv3-Go/models" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
// AccountPermissionGrantedSubscription is a struct for listening to all 'PermissionGranted' contract events and return them as models.PermissionChanged struct | ||
type AccountPermissionGrantedSubscription struct { | ||
*basicSubscription | ||
PermissionChangeChan chan *models.PermissionChanged | ||
contractEventChan chan *perpsMarketGoerli.PerpsMarketGoerliPermissionGranted | ||
} | ||
|
||
func (e *Events) ListenAccountPermissionGranted() (*AccountPermissionGrantedSubscription, error) { | ||
createdChan := make(chan *perpsMarketGoerli.PerpsMarketGoerliPermissionGranted) | ||
|
||
createdSub, err := e.perpsMarket.WatchPermissionGranted(nil, createdChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountPermissionGranted").Errorf("error watch permission granted: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "AccountPermissionGranted") | ||
} | ||
|
||
accountsSub := newAccountPermissionGrantedSubscription(createdSub, createdChan) | ||
|
||
go accountsSub.listen(e.perpsMarket) | ||
|
||
return accountsSub, nil | ||
} | ||
|
||
// newAccountPermissionGrantedSubscription is used to get new AccountPermissionGrantedSubscription instance | ||
func newAccountPermissionGrantedSubscription( | ||
eventSub event.Subscription, | ||
created chan *perpsMarketGoerli.PerpsMarketGoerliPermissionGranted, | ||
) *AccountPermissionGrantedSubscription { | ||
return &AccountPermissionGrantedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
PermissionChangeChan: make(chan *models.PermissionChanged), | ||
contractEventChan: created, | ||
} | ||
} | ||
|
||
// listen is used to run events listen goroutine | ||
func (s *AccountPermissionGrantedSubscription) listen(perpsMarket *perpsMarketGoerli.PerpsMarketGoerli) { | ||
defer func() { | ||
close(s.PermissionChangeChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountPermissionGranted").Errorf( | ||
"error listening account permission granted: %v", err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case contractEvent := <-s.contractEventChan: | ||
p, err := models.PermissionFromString(strings.TrimRight(string(contractEvent.Permission[:]), string(rune(0)))) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-AccountPermissionGranted").Errorf( | ||
"error decode permission %v: %v", string(contractEvent.Permission[:]), err.Error(), | ||
) | ||
s.ErrChan <- err | ||
continue | ||
} | ||
|
||
change := &models.PermissionChanged{ | ||
AccountID: contractEvent.AccountId, | ||
User: contractEvent.User, | ||
Permission: p, | ||
} | ||
|
||
s.PermissionChangeChan <- change | ||
} | ||
} | ||
} |
Oops, something went wrong.