2023-07-18 15:02:56 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-26 17:48:14 +00:00
|
|
|
"errors"
|
2023-07-18 15:02:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"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"
|
2023-07-26 17:48:14 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-07-18 15:02:56 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-07-26 17:48:14 +00:00
|
|
|
fetchLimit = 50 // Limit number of collectibles we fetch per provider call
|
2023-07-18 15:02:56 +00:00
|
|
|
accountOwnershipUpdateInterval = 30 * time.Minute
|
|
|
|
)
|
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
// Fetches owned collectibles for all chainIDs and wallet addresses
|
2023-07-18 15:02:56 +00:00
|
|
|
type refreshOwnedCollectiblesCommand struct {
|
|
|
|
manager *Manager
|
2023-07-26 17:48:14 +00:00
|
|
|
ownershipDB *OwnershipDB
|
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-26 17:48:14 +00:00
|
|
|
func newRefreshOwnedCollectiblesCommand(manager *Manager, ownershipDB *OwnershipDB, accountsDB *accounts.Database, walletFeed *event.Feed, networkManager *network.Manager) *refreshOwnedCollectiblesCommand {
|
2023-07-18 15:02:56 +00:00
|
|
|
return &refreshOwnedCollectiblesCommand{
|
|
|
|
manager: manager,
|
2023-07-26 17:48:14 +00:00
|
|
|
ownershipDB: ownershipDB,
|
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) {
|
2023-07-26 17:48:14 +00:00
|
|
|
return c.updateOwnershipForAllAccounts(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *refreshOwnedCollectiblesCommand) updateOwnershipForAllAccounts(ctx context.Context) error {
|
|
|
|
networks, err := c.networkManager.Get(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
addresses, err := c.accountsDB.GetWalletAddresses()
|
2023-07-18 15:02:56 +00:00
|
|
|
if err != nil {
|
2023-07-26 17:48:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
areTestNetworksEnabled, err := c.accountsDB.GetTestNetworksEnabled()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
group := async.NewGroup(ctx)
|
|
|
|
|
|
|
|
log.Debug("refreshOwnedCollectiblesCommand started")
|
|
|
|
|
|
|
|
for _, network := range networks {
|
|
|
|
if network.IsTest != areTestNetworksEnabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, address := range addresses {
|
|
|
|
command := newLoadOwnedCollectiblesCommand(c.manager, c.ownershipDB, c.walletFeed, walletCommon.ChainID(network.ChainID), common.Address(address))
|
|
|
|
group.Add(command.Command())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-group.WaitAsync():
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("refreshOwnedCollectiblesCommand finished", "in", time.Since(start))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetches owned collectibles for a ChainID+OwnerAddress combination in chunks
|
|
|
|
// and updates the ownershipDB when all chunks are loaded
|
|
|
|
type loadOwnedCollectiblesCommand struct {
|
|
|
|
chainID walletCommon.ChainID
|
|
|
|
account common.Address
|
|
|
|
manager *Manager
|
|
|
|
ownershipDB *OwnershipDB
|
|
|
|
walletFeed *event.Feed
|
|
|
|
|
|
|
|
// Not to be set by the caller
|
|
|
|
partialOwnership []thirdparty.CollectibleUniqueID
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLoadOwnedCollectiblesCommand(manager *Manager, ownershipDB *OwnershipDB, walletFeed *event.Feed, chainID walletCommon.ChainID, account common.Address) *loadOwnedCollectiblesCommand {
|
|
|
|
return &loadOwnedCollectiblesCommand{
|
|
|
|
manager: manager,
|
|
|
|
ownershipDB: ownershipDB,
|
|
|
|
walletFeed: walletFeed,
|
|
|
|
chainID: chainID,
|
|
|
|
account: account,
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
func (c *loadOwnedCollectiblesCommand) Command() async.Command {
|
|
|
|
return c.Run
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *loadOwnedCollectiblesCommand) triggerEvent(eventType walletevent.EventType, chainID walletCommon.ChainID, account common.Address, message string) {
|
2023-07-25 17:51:10 +00:00
|
|
|
c.walletFeed.Send(walletevent.Event{
|
2023-07-26 17:48:14 +00:00
|
|
|
Type: eventType,
|
|
|
|
ChainID: uint64(chainID),
|
2023-07-18 15:02:56 +00:00
|
|
|
Accounts: []common.Address{
|
2023-07-26 17:48:14 +00:00
|
|
|
account,
|
2023-07-18 15:02:56 +00:00
|
|
|
},
|
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
func (c *loadOwnedCollectiblesCommand) Run(parent context.Context) (err error) {
|
|
|
|
log.Debug("start loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account)
|
2023-07-18 15:02:56 +00:00
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
pageNr := 0
|
2023-07-31 23:34:53 +00:00
|
|
|
cursor := thirdparty.FetchFromStartCursor
|
2023-07-18 15:02:56 +00:00
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateStarted, c.chainID, c.account, "")
|
|
|
|
// Fetch collectibles in chunks
|
|
|
|
for {
|
|
|
|
if shouldCancel(parent) {
|
|
|
|
c.err = errors.New("context cancelled")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
partialOwnership, err := c.manager.FetchCollectibleOwnershipByOwner(c.chainID, c.account, cursor, fetchLimit)
|
2023-07-18 15:02:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-07-26 17:48:14 +00:00
|
|
|
log.Error("failed loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "page", pageNr, "error", err)
|
|
|
|
c.err = err
|
|
|
|
break
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
log.Debug("partial loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "page", pageNr, "found", len(partialOwnership.Items), "collectibles")
|
2023-07-26 17:48:14 +00:00
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
c.partialOwnership = append(c.partialOwnership, partialOwnership.Items...)
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
pageNr++
|
|
|
|
cursor = partialOwnership.NextCursor
|
|
|
|
|
2023-07-31 23:34:53 +00:00
|
|
|
if cursor == thirdparty.FetchFromStartCursor {
|
2023-07-26 17:48:14 +00:00
|
|
|
err = c.ownershipDB.Update(c.chainID, c.account, c.partialOwnership)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed updating ownershipDB in loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "error", err)
|
|
|
|
c.err = err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.err != nil {
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateFinishedWithError, c.chainID, c.account, c.err.Error())
|
|
|
|
} else {
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateFinished, c.chainID, c.account, "")
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
log.Debug("end loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account)
|
2023-07-18 15:02:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
// shouldCancel returns true if the context has been cancelled and task should be aborted
|
|
|
|
func shouldCancel(ctx context.Context) bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|