2023-10-09 19:31:00 +00:00
|
|
|
package accountsevent
|
2023-07-24 18:54:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-07-24 18:54:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-07-24 18:54:21 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
|
|
|
)
|
|
|
|
|
2023-10-09 19:31:00 +00:00
|
|
|
type AccountsChangeCb func(changedAddresses []common.Address, eventType EventType, currentAddresses []common.Address)
|
2023-07-24 18:54:21 +00:00
|
|
|
|
|
|
|
// Watcher executes a given callback whenever an account gets added/removed
|
|
|
|
type Watcher struct {
|
|
|
|
accountsDB *accounts.Database
|
|
|
|
accountFeed *event.Feed
|
|
|
|
group *async.Group
|
|
|
|
callback AccountsChangeCb
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWatcher(accountsDB *accounts.Database, accountFeed *event.Feed, callback AccountsChangeCb) *Watcher {
|
|
|
|
return &Watcher{
|
|
|
|
accountsDB: accountsDB,
|
|
|
|
accountFeed: accountFeed,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Watcher) Start() {
|
|
|
|
if w.group != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.group = async.NewGroup(context.Background())
|
|
|
|
w.group.Add(func(ctx context.Context) error {
|
|
|
|
return watch(ctx, w.accountsDB, w.accountFeed, w.callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Watcher) Stop() {
|
|
|
|
if w.group != nil {
|
|
|
|
w.group.Stop()
|
|
|
|
w.group.Wait()
|
|
|
|
w.group = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:31:00 +00:00
|
|
|
func onAccountsChange(accountsDB *accounts.Database, callback AccountsChangeCb, changedAddresses []common.Address, eventType EventType) {
|
2023-07-24 18:54:21 +00:00
|
|
|
currentEthAddresses, err := accountsDB.GetWalletAddresses()
|
|
|
|
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed getting wallet addresses", zap.Error(err))
|
2023-07-24 18:54:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
currentAddresses := make([]common.Address, 0, len(currentEthAddresses))
|
|
|
|
for _, ethAddress := range currentEthAddresses {
|
|
|
|
currentAddresses = append(currentAddresses, common.Address(ethAddress))
|
|
|
|
}
|
|
|
|
|
|
|
|
if callback != nil {
|
|
|
|
callback(changedAddresses, eventType, currentAddresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func watch(ctx context.Context, accountsDB *accounts.Database, accountFeed *event.Feed, callback AccountsChangeCb) error {
|
2023-10-09 19:31:00 +00:00
|
|
|
ch := make(chan Event, 1)
|
2023-07-24 18:54:21 +00:00
|
|
|
sub := accountFeed.Subscribe(ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case err := <-sub.Err():
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("accounts watcher subscription failed", zap.Error(err))
|
2023-07-24 18:54:21 +00:00
|
|
|
}
|
|
|
|
case ev := <-ch:
|
|
|
|
onAccountsChange(accountsDB, callback, ev.Accounts, ev.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|