-
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 status checks…
feat: listen account created core
Showing
4 changed files
with
104 additions
and
1 deletion.
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/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 | ||
} | ||
} | ||
} |
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