2023-07-18 15:02:56 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-08-24 08:45:14 +00:00
|
|
|
"math/big"
|
2023-07-18 15:02:56 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
|
2023-07-24 18:54:21 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2023-07-18 15:02:56 +00:00
|
|
|
"github.com/status-im/status-go/rpc/network"
|
|
|
|
|
2023-07-24 18:54:21 +00:00
|
|
|
"github.com/status-im/status-go/services/accounts/accountsevent"
|
|
|
|
walletaccounts "github.com/status-im/status-go/services/wallet/accounts"
|
2023-07-18 15:02:56 +00:00
|
|
|
"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/thirdparty"
|
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These events are used to notify the UI of state changes
|
|
|
|
const (
|
|
|
|
EventCollectiblesOwnershipUpdateStarted walletevent.EventType = "wallet-collectibles-ownership-update-started"
|
|
|
|
EventCollectiblesOwnershipUpdateFinished walletevent.EventType = "wallet-collectibles-ownership-update-finished"
|
|
|
|
EventCollectiblesOwnershipUpdateFinishedWithError walletevent.EventType = "wallet-collectibles-ownership-update-finished-with-error"
|
|
|
|
|
|
|
|
EventOwnedCollectiblesFilteringDone walletevent.EventType = "wallet-owned-collectibles-filtering-done"
|
2023-07-31 19:41:14 +00:00
|
|
|
EventGetCollectiblesDetailsDone walletevent.EventType = "wallet-get-collectibles-details-done"
|
2023-07-18 15:02:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
filterOwnedCollectiblesTask = async.TaskType{
|
|
|
|
ID: 1,
|
|
|
|
Policy: async.ReplacementPolicyCancelOld,
|
|
|
|
}
|
|
|
|
getCollectiblesDataTask = async.TaskType{
|
|
|
|
ID: 2,
|
|
|
|
Policy: async.ReplacementPolicyCancelOld,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2023-07-24 18:54:21 +00:00
|
|
|
manager *Manager
|
2023-07-26 17:48:14 +00:00
|
|
|
ownershipDB *OwnershipDB
|
2023-07-24 18:54:21 +00:00
|
|
|
walletFeed *event.Feed
|
|
|
|
accountsDB *accounts.Database
|
|
|
|
accountsFeed *event.Feed
|
|
|
|
|
2023-07-18 15:02:56 +00:00
|
|
|
networkManager *network.Manager
|
|
|
|
cancelFn context.CancelFunc
|
|
|
|
|
2023-07-24 18:54:21 +00:00
|
|
|
group *async.Group
|
2023-08-10 19:39:43 +00:00
|
|
|
scheduler *async.MultiClientScheduler
|
2023-07-24 18:54:21 +00:00
|
|
|
accountsWatcher *walletaccounts.Watcher
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 18:54:21 +00:00
|
|
|
func NewService(db *sql.DB, walletFeed *event.Feed, accountsDB *accounts.Database, accountsFeed *event.Feed, networkManager *network.Manager, manager *Manager) *Service {
|
2023-07-18 15:02:56 +00:00
|
|
|
return &Service{
|
|
|
|
manager: manager,
|
2023-07-26 17:48:14 +00:00
|
|
|
ownershipDB: NewOwnershipDB(db),
|
2023-07-24 18:54:21 +00:00
|
|
|
walletFeed: walletFeed,
|
|
|
|
accountsDB: accountsDB,
|
|
|
|
accountsFeed: accountsFeed,
|
2023-07-18 15:02:56 +00:00
|
|
|
networkManager: networkManager,
|
2023-08-10 19:39:43 +00:00
|
|
|
scheduler: async.NewMultiClientScheduler(),
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorCode = int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrorCodeSuccess ErrorCode = iota + 1
|
|
|
|
ErrorCodeTaskCanceled
|
|
|
|
ErrorCodeFailed
|
|
|
|
)
|
|
|
|
|
|
|
|
type FilterOwnedCollectiblesResponse struct {
|
2023-07-31 19:41:14 +00:00
|
|
|
Collectibles []CollectibleHeader `json:"collectibles"`
|
|
|
|
Offset int `json:"offset"`
|
2023-07-18 15:02:56 +00:00
|
|
|
// Used to indicate that there might be more collectibles that were not returned
|
|
|
|
// based on a simple heuristic
|
|
|
|
HasMore bool `json:"hasMore"`
|
|
|
|
ErrorCode ErrorCode `json:"errorCode"`
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
type GetCollectiblesDetailsResponse struct {
|
|
|
|
Collectibles []CollectibleDetails `json:"collectibles"`
|
|
|
|
ErrorCode ErrorCode `json:"errorCode"`
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type filterOwnedCollectiblesTaskReturnType struct {
|
2023-07-31 19:41:14 +00:00
|
|
|
collectibles []CollectibleHeader
|
2023-07-18 15:02:56 +00:00
|
|
|
hasMore bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterOwnedCollectiblesResponse allows only one filter task to run at a time
|
|
|
|
// and it cancels the current one if a new one is started
|
|
|
|
// All calls will trigger an EventOwnedCollectiblesFilteringDone event with the result of the filtering
|
2023-08-10 19:39:43 +00:00
|
|
|
func (s *Service) FilterOwnedCollectiblesAsync(requestID int32, chainIDs []walletCommon.ChainID, addresses []common.Address, offset int, limit int) {
|
|
|
|
s.scheduler.Enqueue(requestID, filterOwnedCollectiblesTask, func(ctx context.Context) (interface{}, error) {
|
2023-07-26 17:48:14 +00:00
|
|
|
collectibles, hasMore, err := s.GetOwnedCollectibles(chainIDs, addresses, offset, limit)
|
2023-07-18 15:02:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data, err := s.manager.FetchAssetsByCollectibleUniqueID(collectibles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filterOwnedCollectiblesTaskReturnType{
|
2023-07-31 19:41:14 +00:00
|
|
|
collectibles: fullCollectiblesDataToHeaders(data),
|
2023-07-18 15:02:56 +00:00
|
|
|
hasMore: hasMore,
|
|
|
|
}, err
|
|
|
|
}, func(result interface{}, taskType async.TaskType, err error) {
|
|
|
|
res := FilterOwnedCollectiblesResponse{
|
|
|
|
ErrorCode: ErrorCodeFailed,
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, async.ErrTaskOverwritten) {
|
|
|
|
res.ErrorCode = ErrorCodeTaskCanceled
|
|
|
|
} else if err == nil {
|
|
|
|
fnRet := result.(filterOwnedCollectiblesTaskReturnType)
|
|
|
|
res.Collectibles = fnRet.collectibles
|
|
|
|
res.Offset = offset
|
|
|
|
res.HasMore = fnRet.hasMore
|
|
|
|
res.ErrorCode = ErrorCodeSuccess
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
s.sendResponseEvent(&requestID, EventOwnedCollectiblesFilteringDone, res, err)
|
2023-07-18 15:02:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
func (s *Service) GetCollectiblesDetailsAsync(requestID int32, uniqueIDs []thirdparty.CollectibleUniqueID) {
|
|
|
|
s.scheduler.Enqueue(requestID, getCollectiblesDataTask, func(ctx context.Context) (interface{}, error) {
|
2023-07-18 15:02:56 +00:00
|
|
|
collectibles, err := s.manager.FetchAssetsByCollectibleUniqueID(uniqueIDs)
|
|
|
|
return collectibles, err
|
|
|
|
}, func(result interface{}, taskType async.TaskType, err error) {
|
2023-07-31 19:41:14 +00:00
|
|
|
res := GetCollectiblesDetailsResponse{
|
2023-07-18 15:02:56 +00:00
|
|
|
ErrorCode: ErrorCodeFailed,
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, async.ErrTaskOverwritten) {
|
|
|
|
res.ErrorCode = ErrorCodeTaskCanceled
|
|
|
|
} else if err == nil {
|
2023-07-31 19:41:14 +00:00
|
|
|
collectibles := result.([]thirdparty.FullCollectibleData)
|
|
|
|
res.Collectibles = fullCollectiblesDataToDetails(collectibles)
|
2023-07-18 15:02:56 +00:00
|
|
|
res.ErrorCode = ErrorCodeSuccess
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
s.sendResponseEvent(&requestID, EventGetCollectiblesDetailsDone, res, err)
|
2023-07-18 15:02:56 +00:00
|
|
|
})
|
|
|
|
}
|
2023-07-24 18:54:21 +00:00
|
|
|
|
|
|
|
func (s *Service) startPeriodicalOwnershipFetch() {
|
2023-07-18 15:02:56 +00:00
|
|
|
if s.group != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
s.cancelFn = cancel
|
|
|
|
|
|
|
|
s.group = async.NewGroup(ctx)
|
|
|
|
|
|
|
|
command := newRefreshOwnedCollectiblesCommand(
|
|
|
|
s.manager,
|
2023-07-26 17:48:14 +00:00
|
|
|
s.ownershipDB,
|
2023-07-25 17:51:10 +00:00
|
|
|
s.accountsDB,
|
2023-07-24 18:54:21 +00:00
|
|
|
s.walletFeed,
|
2023-07-18 15:02:56 +00:00
|
|
|
s.networkManager,
|
|
|
|
)
|
|
|
|
|
|
|
|
s.group.Add(command.Command())
|
|
|
|
}
|
|
|
|
|
2023-07-24 18:54:21 +00:00
|
|
|
func (s *Service) stopPeriodicalOwnershipFetch() {
|
2023-07-18 15:02:56 +00:00
|
|
|
if s.cancelFn != nil {
|
|
|
|
s.cancelFn()
|
|
|
|
s.cancelFn = nil
|
|
|
|
}
|
|
|
|
if s.group != nil {
|
|
|
|
s.group.Stop()
|
|
|
|
s.group.Wait()
|
|
|
|
s.group = nil
|
|
|
|
}
|
2023-07-24 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) startAccountsWatcher() {
|
|
|
|
if s.accountsWatcher != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accountChangeCb := func(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address) {
|
|
|
|
// Whenever an account gets added, restart fetch
|
|
|
|
// TODO: Fetch only added accounts
|
|
|
|
if eventType == accountsevent.EventTypeAdded {
|
|
|
|
s.stopPeriodicalOwnershipFetch()
|
|
|
|
s.startPeriodicalOwnershipFetch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.accountsWatcher = walletaccounts.NewWatcher(s.accountsDB, s.accountsFeed, accountChangeCb)
|
|
|
|
|
|
|
|
s.accountsWatcher.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) stopAccountsWatcher() {
|
|
|
|
if s.accountsWatcher != nil {
|
|
|
|
s.accountsWatcher.Stop()
|
|
|
|
s.accountsWatcher = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Start() {
|
|
|
|
// Setup periodical collectibles refresh
|
|
|
|
s.startPeriodicalOwnershipFetch()
|
|
|
|
|
|
|
|
// Setup collectibles fetch when a new account gets added
|
|
|
|
s.startAccountsWatcher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Stop() {
|
|
|
|
s.stopAccountsWatcher()
|
|
|
|
|
|
|
|
s.stopPeriodicalOwnershipFetch()
|
|
|
|
|
2023-07-18 15:02:56 +00:00
|
|
|
s.scheduler.Stop()
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
func (s *Service) sendResponseEvent(requestID *int32, eventType walletevent.EventType, payloadObj interface{}, resErr error) {
|
2023-07-18 15:02:56 +00:00
|
|
|
payload, err := json.Marshal(payloadObj)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error marshaling response: %v; result error: %w", err, resErr)
|
|
|
|
} else {
|
|
|
|
err = resErr
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
log.Debug("wallet.api.collectibles.Service RESPONSE", "requestID", requestID, "eventType", eventType, "error", err, "payload.len", len(payload))
|
2023-07-18 15:02:56 +00:00
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
event := walletevent.Event{
|
2023-07-18 15:02:56 +00:00
|
|
|
Type: eventType,
|
|
|
|
Message: string(payload),
|
2023-08-10 19:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if requestID != nil {
|
|
|
|
event.RequestID = new(int)
|
|
|
|
*event.RequestID = int(*requestID)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.walletFeed.Send(event)
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
2023-07-26 17:48:14 +00:00
|
|
|
|
|
|
|
func (s *Service) GetOwnedCollectibles(chainIDs []walletCommon.ChainID, owners []common.Address, offset int, limit int) ([]thirdparty.CollectibleUniqueID, bool, error) {
|
|
|
|
// Request one more than limit, to check if DB has more available
|
|
|
|
ids, err := s.ownershipDB.GetOwnedCollectibles(chainIDs, owners, offset, limit+1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hasMore := len(ids) > limit
|
|
|
|
if hasMore {
|
|
|
|
ids = ids[:limit]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids, hasMore, nil
|
|
|
|
}
|
2023-08-24 08:45:14 +00:00
|
|
|
|
|
|
|
func (s *Service) GetOwnedCollectible(chainID walletCommon.ChainID, owner common.Address, contractAddress common.Address, tokenID *big.Int) (*thirdparty.CollectibleUniqueID, error) {
|
|
|
|
return s.ownershipDB.GetOwnedCollectible(chainID, owner, contractAddress, tokenID)
|
|
|
|
}
|