2023-07-18 15:02:56 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
statustypes "github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/rpc/network"
|
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
accountOwnershipUpdateInterval = 30 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
type refreshOwnedCollectiblesCommand struct {
|
|
|
|
manager *Manager
|
2023-07-25 17:51:10 +00:00
|
|
|
accountsDB *accounts.Database
|
|
|
|
walletFeed *event.Feed
|
2023-07-18 15:02:56 +00:00
|
|
|
networkManager *network.Manager
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:51:10 +00:00
|
|
|
func newRefreshOwnedCollectiblesCommand(manager *Manager, accountsDB *accounts.Database, walletFeed *event.Feed, networkManager *network.Manager) *refreshOwnedCollectiblesCommand {
|
2023-07-18 15:02:56 +00:00
|
|
|
return &refreshOwnedCollectiblesCommand{
|
|
|
|
manager: manager,
|
2023-07-25 17:51:10 +00:00
|
|
|
accountsDB: accountsDB,
|
|
|
|
walletFeed: walletFeed,
|
2023-07-18 15:02:56 +00:00
|
|
|
networkManager: networkManager,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refreshOwnedCollectiblesCommand) Command() async.Command {
|
|
|
|
return async.InfiniteCommand{
|
|
|
|
Interval: accountOwnershipUpdateInterval,
|
|
|
|
Runable: c.Run,
|
|
|
|
}.Run
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refreshOwnedCollectiblesCommand) Run(ctx context.Context) (err error) {
|
|
|
|
err = c.updateOwnershipForAllAccounts(ctx)
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateFinished, statustypes.Address{}, "Service cancelled")
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateFinishedWithError, statustypes.Address{}, err.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refreshOwnedCollectiblesCommand) triggerEvent(eventType walletevent.EventType, account statustypes.Address, message string) {
|
2023-07-25 17:51:10 +00:00
|
|
|
c.walletFeed.Send(walletevent.Event{
|
2023-07-18 15:02:56 +00:00
|
|
|
Type: eventType,
|
|
|
|
Accounts: []common.Address{
|
|
|
|
common.Address(account),
|
|
|
|
},
|
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refreshOwnedCollectiblesCommand) updateOwnershipForAllAccounts(ctx context.Context) error {
|
2023-07-25 17:51:10 +00:00
|
|
|
addresses, err := c.accountsDB.GetWalletAddresses()
|
2023-07-18 15:02:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range addresses {
|
|
|
|
_ = c.updateOwnershipForAccount(ctx, address)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refreshOwnedCollectiblesCommand) updateOwnershipForAccount(ctx context.Context, address statustypes.Address) error {
|
|
|
|
networks, err := c.networkManager.Get(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateStarted, address, "")
|
|
|
|
for _, network := range networks {
|
|
|
|
err := c.manager.UpdateOwnedCollectibles(walletCommon.ChainID(network.ChainID), common.Address(address))
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error updating collectibles ownership", "chainID", network.ChainID, "address", address.String(), "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateFinished, address, "")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|