2023-07-18 15:02:56 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-26 17:48:14 +00:00
|
|
|
"errors"
|
2023-09-14 21:52:49 +00:00
|
|
|
"sync/atomic"
|
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/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-10-09 12:43:53 +00:00
|
|
|
fetchLimit = 50 // Limit number of collectibles we fetch per provider call
|
2023-11-16 04:18:03 +00:00
|
|
|
accountOwnershipUpdateInterval = 60 * time.Minute
|
2023-10-09 12:43:53 +00:00
|
|
|
accountOwnershipUpdateDelayInterval = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type OwnershipState = int
|
|
|
|
|
2023-10-11 19:59:50 +00:00
|
|
|
type OwnedCollectibles struct {
|
|
|
|
chainID walletCommon.ChainID
|
|
|
|
account common.Address
|
|
|
|
ids []thirdparty.CollectibleUniqueID
|
|
|
|
}
|
|
|
|
|
|
|
|
type OwnedCollectiblesCb func(OwnedCollectibles)
|
|
|
|
|
2023-10-09 12:43:53 +00:00
|
|
|
const (
|
|
|
|
OwnershipStateIdle OwnershipState = iota + 1
|
|
|
|
OwnershipStateDelayed
|
|
|
|
OwnershipStateUpdating
|
|
|
|
OwnershipStateError
|
2023-07-18 15:02:56 +00:00
|
|
|
)
|
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
type periodicRefreshOwnedCollectiblesCommand struct {
|
2023-10-11 19:59:50 +00:00
|
|
|
chainID walletCommon.ChainID
|
|
|
|
account common.Address
|
|
|
|
manager *Manager
|
|
|
|
ownershipDB *OwnershipDB
|
|
|
|
walletFeed *event.Feed
|
|
|
|
receivedCollectiblesCb OwnedCollectiblesCb
|
2023-09-14 14:44:35 +00:00
|
|
|
|
|
|
|
group *async.Group
|
2023-09-14 21:52:49 +00:00
|
|
|
state atomic.Value
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 19:59:50 +00:00
|
|
|
func newPeriodicRefreshOwnedCollectiblesCommand(
|
|
|
|
manager *Manager,
|
|
|
|
ownershipDB *OwnershipDB,
|
|
|
|
walletFeed *event.Feed,
|
|
|
|
chainID walletCommon.ChainID,
|
|
|
|
account common.Address,
|
|
|
|
receivedCollectiblesCb OwnedCollectiblesCb) *periodicRefreshOwnedCollectiblesCommand {
|
2023-09-14 21:52:49 +00:00
|
|
|
ret := &periodicRefreshOwnedCollectiblesCommand{
|
2023-10-11 19:59:50 +00:00
|
|
|
manager: manager,
|
|
|
|
ownershipDB: ownershipDB,
|
|
|
|
walletFeed: walletFeed,
|
|
|
|
chainID: chainID,
|
|
|
|
account: account,
|
|
|
|
receivedCollectiblesCb: receivedCollectiblesCb,
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
2023-09-14 21:52:49 +00:00
|
|
|
ret.state.Store(OwnershipStateIdle)
|
|
|
|
return ret
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 12:43:53 +00:00
|
|
|
func (c *periodicRefreshOwnedCollectiblesCommand) DelayedCommand() async.Command {
|
|
|
|
return async.SingleShotCommand{
|
|
|
|
Interval: accountOwnershipUpdateDelayInterval,
|
|
|
|
Init: func(ctx context.Context) (err error) {
|
|
|
|
c.state.Store(OwnershipStateDelayed)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Runable: c.Command(),
|
|
|
|
}.Run
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
func (c *periodicRefreshOwnedCollectiblesCommand) Command() async.Command {
|
2023-07-18 15:02:56 +00:00
|
|
|
return async.InfiniteCommand{
|
|
|
|
Interval: accountOwnershipUpdateInterval,
|
|
|
|
Runable: c.Run,
|
|
|
|
}.Run
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
func (c *periodicRefreshOwnedCollectiblesCommand) Run(ctx context.Context) (err error) {
|
|
|
|
return c.loadOwnedCollectibles(ctx)
|
2023-07-26 17:48:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 21:52:49 +00:00
|
|
|
func (c *periodicRefreshOwnedCollectiblesCommand) GetState() OwnershipState {
|
|
|
|
return c.state.Load().(OwnershipState)
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
func (c *periodicRefreshOwnedCollectiblesCommand) Stop() {
|
|
|
|
if c.group != nil {
|
|
|
|
c.group.Stop()
|
|
|
|
c.group.Wait()
|
|
|
|
c.group = nil
|
2023-07-26 17:48:14 +00:00
|
|
|
}
|
2023-09-14 14:44:35 +00:00
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
func (c *periodicRefreshOwnedCollectiblesCommand) loadOwnedCollectibles(ctx context.Context) error {
|
|
|
|
c.group = async.NewGroup(ctx)
|
2023-10-11 19:59:50 +00:00
|
|
|
|
|
|
|
receivedCollectiblesCh := make(chan OwnedCollectibles)
|
|
|
|
command := newLoadOwnedCollectiblesCommand(c.manager, c.ownershipDB, c.walletFeed, c.chainID, c.account, receivedCollectiblesCh)
|
2023-09-14 21:52:49 +00:00
|
|
|
|
|
|
|
c.state.Store(OwnershipStateUpdating)
|
|
|
|
defer func() {
|
|
|
|
if command.err != nil {
|
|
|
|
c.state.Store(OwnershipStateError)
|
|
|
|
} else {
|
|
|
|
c.state.Store(OwnershipStateIdle)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
c.group.Add(command.Command())
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
select {
|
2023-10-11 19:59:50 +00:00
|
|
|
case ownedCollectibles := <-receivedCollectiblesCh:
|
|
|
|
if c.receivedCollectiblesCb != nil {
|
|
|
|
c.receivedCollectiblesCb(ownedCollectibles)
|
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2023-09-14 14:44:35 +00:00
|
|
|
case <-c.group.WaitAsync():
|
2023-10-11 19:59:50 +00:00
|
|
|
return nil
|
2023-07-26 17:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetches owned collectibles for a ChainID+OwnerAddress combination in chunks
|
|
|
|
// and updates the ownershipDB when all chunks are loaded
|
|
|
|
type loadOwnedCollectiblesCommand struct {
|
2023-10-11 19:59:50 +00:00
|
|
|
chainID walletCommon.ChainID
|
|
|
|
account common.Address
|
|
|
|
manager *Manager
|
|
|
|
ownershipDB *OwnershipDB
|
|
|
|
walletFeed *event.Feed
|
|
|
|
receivedCollectiblesCh chan<- OwnedCollectibles
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
// Not to be set by the caller
|
|
|
|
partialOwnership []thirdparty.CollectibleUniqueID
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2023-10-11 19:59:50 +00:00
|
|
|
func newLoadOwnedCollectiblesCommand(
|
|
|
|
manager *Manager,
|
|
|
|
ownershipDB *OwnershipDB,
|
|
|
|
walletFeed *event.Feed,
|
|
|
|
chainID walletCommon.ChainID,
|
|
|
|
account common.Address,
|
|
|
|
receivedCollectiblesCh chan<- OwnedCollectibles) *loadOwnedCollectiblesCommand {
|
2023-07-26 17:48:14 +00:00
|
|
|
return &loadOwnedCollectiblesCommand{
|
2023-10-11 19:59:50 +00:00
|
|
|
manager: manager,
|
|
|
|
ownershipDB: ownershipDB,
|
|
|
|
walletFeed: walletFeed,
|
|
|
|
chainID: chainID,
|
|
|
|
account: account,
|
|
|
|
receivedCollectiblesCh: receivedCollectiblesCh,
|
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-10-04 16:21:45 +00:00
|
|
|
providerID := thirdparty.FetchFromAnyProvider
|
2023-09-12 18:35:31 +00:00
|
|
|
start := time.Now()
|
2023-07-18 15:02:56 +00:00
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdateStarted, c.chainID, c.account, "")
|
2023-07-18 15:02:56 +00:00
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
lastFetchTimestamp, err := c.ownershipDB.GetOwnershipUpdateTimestamp(c.account, c.chainID)
|
|
|
|
if err != nil {
|
|
|
|
c.err = err
|
|
|
|
} else {
|
|
|
|
initialFetch := lastFetchTimestamp == InvalidTimestamp
|
|
|
|
// Fetch collectibles in chunks
|
|
|
|
for {
|
2023-11-14 17:16:39 +00:00
|
|
|
if walletCommon.ShouldCancel(parent) {
|
2023-09-14 14:44:35 +00:00
|
|
|
c.err = errors.New("context cancelled")
|
|
|
|
break
|
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
pageStart := time.Now()
|
|
|
|
log.Debug("start loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "page", pageNr)
|
2023-07-26 17:48:14 +00:00
|
|
|
|
2023-11-14 17:16:39 +00:00
|
|
|
partialOwnership, err := c.manager.FetchCollectibleOwnershipByOwner(parent, c.chainID, c.account, cursor, fetchLimit, providerID)
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-09-14 14:44:35 +00:00
|
|
|
log.Error("failed loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "page", pageNr, "error", err)
|
2023-07-26 17:48:14 +00:00
|
|
|
c.err = err
|
2023-09-14 14:44:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
log.Debug("partial loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "page", pageNr, "in", time.Since(pageStart), "found", len(partialOwnership.Items))
|
2023-09-14 14:44:35 +00:00
|
|
|
|
|
|
|
c.partialOwnership = append(c.partialOwnership, partialOwnership.Items...)
|
|
|
|
|
|
|
|
pageNr++
|
|
|
|
cursor = partialOwnership.NextCursor
|
2023-10-04 16:21:45 +00:00
|
|
|
providerID = partialOwnership.Provider
|
2023-09-14 14:44:35 +00:00
|
|
|
|
|
|
|
finished := cursor == thirdparty.FetchFromStartCursor
|
|
|
|
|
|
|
|
// Normally, update the DB once we've finished fetching
|
|
|
|
// If this is the first fetch, make partial updates to the client to get a better UX
|
|
|
|
if initialFetch || finished {
|
2023-10-11 19:59:50 +00:00
|
|
|
receivedIDs, err := c.ownershipDB.GetIDsNotInDB(c.chainID, c.account, c.partialOwnership)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed GetIDsNotInDB in processOwnedIDs", "chain", c.chainID, "account", c.account, "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:44:35 +00:00
|
|
|
err = c.ownershipDB.Update(c.chainID, c.account, c.partialOwnership, start.Unix())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed updating ownershipDB in loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "error", err)
|
|
|
|
c.err = err
|
|
|
|
}
|
2023-10-11 19:59:50 +00:00
|
|
|
|
|
|
|
c.receivedCollectiblesCh <- OwnedCollectibles{
|
|
|
|
chainID: c.chainID,
|
|
|
|
account: c.account,
|
|
|
|
ids: receivedIDs,
|
|
|
|
}
|
2023-09-14 14:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if finished || c.err != nil {
|
|
|
|
break
|
|
|
|
} else if initialFetch {
|
|
|
|
c.triggerEvent(EventCollectiblesOwnershipUpdatePartial, c.chainID, c.account, "")
|
2023-07-26 17:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-14 14:44:35 +00:00
|
|
|
log.Debug("end loadOwnedCollectiblesCommand", "chain", c.chainID, "account", c.account, "in", time.Since(start))
|
2023-07-18 15:02:56 +00:00
|
|
|
return nil
|
|
|
|
}
|