Skip to content

Commit

Permalink
feat: listen account created core
Browse files Browse the repository at this point in the history
asolovov committed May 16, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent bf3723b commit 53daf82
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/core/contract.go

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

79 changes: 79 additions & 0 deletions events/accountCreatedCore.go
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/core"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/gateway-fm/perpsv3-Go/models"
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

// AccountCreatedCoreSubscription is a struct for listening to all 'AccountCreatedCore' contract events and return them as models.Account struct
type AccountCreatedCoreSubscription struct {
*basicSubscription
NewAccountChan chan *models.Account
contractEventChan chan *core.CoreAccountCreated
}

func (e *Events) ListenAccountCreatedCore() (*AccountCreatedCoreSubscription, error) {
createdChan := make(chan *core.CoreAccountCreated)

createdSub, err := e.core.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, "AccountCreatedCore")
}

accountsSub := newAccountCreatedCoreSubscription(createdSub, createdChan)

go accountsSub.listen(e.core)

return accountsSub, nil
}

// newAccountCreatedCoreSubscription is used to get new AccountCreatedCoreSubscription instance
func newAccountCreatedCoreSubscription(
eventSub event.Subscription,
created chan *core.CoreAccountCreated,
) *AccountCreatedCoreSubscription {
return &AccountCreatedCoreSubscription{
basicSubscription: newBasicSubscription(eventSub),
NewAccountChan: make(chan *models.Account),
contractEventChan: created,
}
}

// listen is used to run events listen goroutine
func (s *AccountCreatedCoreSubscription) listen(coreContract *core.Core) {
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-AccountCreatedCore").Errorf(
"error listening account created info: %v", err.Error(),
)
s.ErrChan <- err
}
return
case newAccount := <-s.contractEventChan:
lastInteraction := getAccountLastInteractionCore(newAccount.AccountId, coreContract)

account := models.FormatAccountCore(
newAccount.AccountId,
newAccount.Owner,
lastInteraction.Uint64(),
[]core.IAccountModuleAccountPermissions{},
)

s.NewAccountChan <- account
}
}
}
16 changes: 16 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
@@ -37,6 +37,10 @@ type IEvents interface {
// struct and return errors on ErrChan chanel
ListenAccountCreated() (*AccountCreatedSubscription, error)

// ListenAccountCreatedCore is used to listen to all 'AccountCreated' core contract events and return them as models.Account
// struct and return errors on ErrChan chanel
ListenAccountCreatedCore() (*AccountCreatedCoreSubscription, error)

// ListenAccountLiquidated is used to listen to all 'AccountLiquidated' contract events and return them as models.AccountLiquidated
// struct and return errors on ErrChan chanel
ListenAccountLiquidated() (*AccountLiquidatedSubscription, error)
@@ -159,3 +163,15 @@ func getAccountLastInteraction(id *big.Int, perpsMarket *perpsMarket.PerpsMarket

return lastInteraction
}

func getAccountLastInteractionCore(id *big.Int, core *core.Core) *big.Int {
lastInteraction, err := core.GetAccountLastInteraction(nil, id)
if err != nil {
logger.Log().WithField("layer", "Events-Accounts").Errorf(
"error query account core last interaction: %v, last interaction set to 0", err.Error(),
)
lastInteraction = big.NewInt(0)
}

return lastInteraction
}
8 changes: 8 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
@@ -153,6 +153,10 @@ type IPerpsv3 interface {
// struct and return errors on ErrChan chanel
ListenAccountCreated() (*events.AccountCreatedSubscription, error)

// ListenAccountCreatedCore is used to listen to all 'AccountCreated' core contract events and return them as models.Account
// struct and return errors on ErrChan chanel
ListenAccountCreatedCore() (*events.AccountCreatedCoreSubscription, error)

// ListenAccountLiquidated is used to listen to all 'AccountLiquidated' contract events and return them as models.AccountLiquidated
// struct and return errors on ErrChan chanel
ListenAccountLiquidated() (*events.AccountLiquidatedSubscription, error)
@@ -467,6 +471,10 @@ func (p *Perpsv3) ListenAccountCreated() (*events.AccountCreatedSubscription, er
return p.events.ListenAccountCreated()
}

func (p *Perpsv3) ListenAccountCreatedCore() (*events.AccountCreatedCoreSubscription, error) {
return p.events.ListenAccountCreatedCore()
}

func (p *Perpsv3) ListenAccountLiquidated() (*events.AccountLiquidatedSubscription, error) {
return p.events.ListenAccountLiquidated()
}

0 comments on commit 53daf82

Please sign in to comment.