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-11-06 12:58:36 +00:00
|
|
|
"time"
|
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"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2023-10-26 06:30:18 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/community"
|
2023-07-18 15:02:56 +00:00
|
|
|
"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"
|
2023-09-14 14:44:35 +00:00
|
|
|
EventCollectiblesOwnershipUpdatePartial walletevent.EventType = "wallet-collectibles-ownership-update-partial"
|
2023-07-18 15:02:56 +00:00
|
|
|
EventCollectiblesOwnershipUpdateFinished walletevent.EventType = "wallet-collectibles-ownership-update-finished"
|
|
|
|
EventCollectiblesOwnershipUpdateFinishedWithError walletevent.EventType = "wallet-collectibles-ownership-update-finished-with-error"
|
2023-10-11 19:59:50 +00:00
|
|
|
EventCommunityCollectiblesReceived walletevent.EventType = "wallet-collectibles-community-collectibles-received"
|
2023-07-18 15:02:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-11-02 23:33:50 +00:00
|
|
|
type CollectibleDataType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
CollectibleDataTypeUniqueID CollectibleDataType = iota
|
|
|
|
CollectibleDataTypeHeader
|
|
|
|
CollectibleDataTypeDetails
|
|
|
|
CollectibleDataTypeCommunityHeader
|
|
|
|
)
|
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
type FetchType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
FetchTypeNeverFetch FetchType = iota
|
|
|
|
FetchTypeAlwaysFetch
|
|
|
|
FetchTypeFetchIfNotCached
|
|
|
|
FetchTypeFetchIfCacheOld
|
|
|
|
)
|
|
|
|
|
|
|
|
type FetchCriteria struct {
|
|
|
|
FetchType FetchType `json:"fetch_type"`
|
|
|
|
MaxCacheAgeSeconds int64 `json:"max_cache_age_seconds"`
|
|
|
|
}
|
|
|
|
|
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-10-09 12:43:53 +00:00
|
|
|
manager *Manager
|
|
|
|
controller *Controller
|
2023-11-01 19:09:10 +00:00
|
|
|
db *sql.DB
|
2023-10-09 12:43:53 +00:00
|
|
|
ownershipDB *OwnershipDB
|
2023-10-26 06:30:18 +00:00
|
|
|
communityDB *community.DataDB
|
2023-10-09 12:43:53 +00:00
|
|
|
walletFeed *event.Feed
|
|
|
|
scheduler *async.MultiClientScheduler
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 19:31:00 +00:00
|
|
|
func NewService(
|
|
|
|
db *sql.DB,
|
|
|
|
walletFeed *event.Feed,
|
|
|
|
accountsDB *accounts.Database,
|
|
|
|
accountsFeed *event.Feed,
|
2023-10-10 12:18:47 +00:00
|
|
|
settingsFeed *event.Feed,
|
2023-10-09 19:31:00 +00:00
|
|
|
networkManager *network.Manager,
|
|
|
|
manager *Manager) *Service {
|
2023-11-02 23:33:50 +00:00
|
|
|
s := &Service{
|
2023-10-09 12:43:53 +00:00
|
|
|
manager: manager,
|
2023-10-10 12:18:47 +00:00
|
|
|
controller: NewController(db, walletFeed, accountsDB, accountsFeed, settingsFeed, networkManager, manager),
|
2023-11-01 19:09:10 +00:00
|
|
|
db: db,
|
2023-10-09 12:43:53 +00:00
|
|
|
ownershipDB: NewOwnershipDB(db),
|
2023-10-26 06:30:18 +00:00
|
|
|
communityDB: community.NewDataDB(db),
|
2023-10-09 12:43:53 +00:00
|
|
|
walletFeed: walletFeed,
|
2023-11-10 00:31:48 +00:00
|
|
|
scheduler: async.NewMultiClientScheduler(),
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
2023-11-02 23:33:50 +00:00
|
|
|
s.controller.SetReceivedCollectiblesCb(s.notifyCommunityCollectiblesReceived)
|
|
|
|
return s
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorCode = int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrorCodeSuccess ErrorCode = iota + 1
|
|
|
|
ErrorCodeTaskCanceled
|
|
|
|
ErrorCodeFailed
|
|
|
|
)
|
|
|
|
|
2023-09-14 21:52:49 +00:00
|
|
|
type OwnershipStatus struct {
|
|
|
|
State OwnershipState `json:"state"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type OwnershipStatusPerChainID = map[walletCommon.ChainID]OwnershipStatus
|
|
|
|
type OwnershipStatusPerAddressAndChainID = map[common.Address]OwnershipStatusPerChainID
|
|
|
|
|
2023-11-02 23:33:50 +00:00
|
|
|
type GetOwnedCollectiblesResponse struct {
|
2023-11-10 00:31:48 +00:00
|
|
|
Collectibles []Collectible `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
|
2023-09-14 21:52:49 +00:00
|
|
|
HasMore bool `json:"hasMore"`
|
|
|
|
OwnershipStatus OwnershipStatusPerAddressAndChainID `json:"ownershipStatus"`
|
|
|
|
ErrorCode ErrorCode `json:"errorCode"`
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 23:33:50 +00:00
|
|
|
type GetCollectiblesByUniqueIDResponse struct {
|
2023-11-10 00:31:48 +00:00
|
|
|
Collectibles []Collectible `json:"collectibles"`
|
|
|
|
ErrorCode ErrorCode `json:"errorCode"`
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
type GetOwnedCollectiblesReturnType struct {
|
2023-11-10 00:31:48 +00:00
|
|
|
collectibles []Collectible
|
2023-09-14 21:52:49 +00:00
|
|
|
hasMore bool
|
|
|
|
ownershipStatus OwnershipStatusPerAddressAndChainID
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
type GetCollectiblesByUniqueIDReturnType struct {
|
2023-11-10 00:31:48 +00:00
|
|
|
collectibles []Collectible
|
2023-11-02 23:33:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
func (s *Service) GetOwnedCollectibles(
|
|
|
|
ctx context.Context,
|
2023-11-02 23:33:50 +00:00
|
|
|
chainIDs []walletCommon.ChainID,
|
|
|
|
addresses []common.Address,
|
|
|
|
filter Filter,
|
|
|
|
offset int,
|
|
|
|
limit int,
|
2023-11-06 12:58:36 +00:00
|
|
|
dataType CollectibleDataType,
|
|
|
|
fetchCriteria FetchCriteria) (*GetOwnedCollectiblesReturnType, error) {
|
|
|
|
err := s.fetchOwnedCollectiblesIfNeeded(ctx, chainIDs, addresses, fetchCriteria)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ids, hasMore, err := s.FilterOwnedCollectibles(chainIDs, addresses, filter, offset, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
collectibles, err := s.collectibleIDsToDataType(ctx, ids, dataType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-02 23:33:50 +00:00
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
ownershipStatus, err := s.GetOwnershipStatus(chainIDs, addresses)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GetOwnedCollectiblesReturnType{
|
|
|
|
collectibles: collectibles,
|
|
|
|
hasMore: hasMore,
|
|
|
|
ownershipStatus: ownershipStatus,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) needsToFetch(chainID walletCommon.ChainID, address common.Address, fetchCriteria FetchCriteria) (bool, error) {
|
|
|
|
mustFetch := false
|
|
|
|
switch fetchCriteria.FetchType {
|
|
|
|
case FetchTypeAlwaysFetch:
|
|
|
|
mustFetch = true
|
|
|
|
case FetchTypeNeverFetch:
|
|
|
|
mustFetch = false
|
|
|
|
case FetchTypeFetchIfNotCached, FetchTypeFetchIfCacheOld:
|
|
|
|
timestamp, err := s.ownershipDB.GetOwnershipUpdateTimestamp(address, chainID)
|
2023-07-18 15:02:56 +00:00
|
|
|
if err != nil {
|
2023-11-06 12:58:36 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if timestamp == InvalidTimestamp ||
|
|
|
|
(fetchCriteria.FetchType == FetchTypeFetchIfCacheOld && timestamp+fetchCriteria.MaxCacheAgeSeconds < time.Now().Unix()) {
|
|
|
|
mustFetch = true
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
2023-11-06 12:58:36 +00:00
|
|
|
}
|
|
|
|
return mustFetch, nil
|
|
|
|
}
|
2023-11-02 23:33:50 +00:00
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
func (s *Service) fetchOwnedCollectiblesIfNeeded(ctx context.Context, chainIDs []walletCommon.ChainID, addresses []common.Address, fetchCriteria FetchCriteria) error {
|
|
|
|
if fetchCriteria.FetchType == FetchTypeNeverFetch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
group := async.NewGroup(ctx)
|
|
|
|
for _, address := range addresses {
|
|
|
|
for _, chainID := range chainIDs {
|
|
|
|
mustFetch, err := s.needsToFetch(chainID, address, fetchCriteria)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mustFetch {
|
|
|
|
command := newLoadOwnedCollectiblesCommand(s.manager, s.ownershipDB, s.walletFeed, chainID, address, nil)
|
|
|
|
group.Add(command.Command())
|
|
|
|
}
|
2023-09-14 21:52:49 +00:00
|
|
|
}
|
2023-11-06 12:58:36 +00:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-group.WaitAsync():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOwnedCollectiblesAsync 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
|
|
|
|
func (s *Service) GetOwnedCollectiblesAsync(
|
|
|
|
requestID int32,
|
|
|
|
chainIDs []walletCommon.ChainID,
|
|
|
|
addresses []common.Address,
|
|
|
|
filter Filter,
|
|
|
|
offset int,
|
|
|
|
limit int,
|
|
|
|
dataType CollectibleDataType,
|
|
|
|
fetchCriteria FetchCriteria) {
|
|
|
|
s.scheduler.Enqueue(requestID, filterOwnedCollectiblesTask, func(ctx context.Context) (interface{}, error) {
|
|
|
|
return s.GetOwnedCollectibles(ctx, chainIDs, addresses, filter, offset, limit, dataType, fetchCriteria)
|
2023-07-18 15:02:56 +00:00
|
|
|
}, func(result interface{}, taskType async.TaskType, err error) {
|
2023-11-02 23:33:50 +00:00
|
|
|
res := GetOwnedCollectiblesResponse{
|
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-11-10 00:31:48 +00:00
|
|
|
fnRet := result.(*GetOwnedCollectiblesReturnType)
|
2023-11-02 23:33:50 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2023-11-10 00:31:48 +00:00
|
|
|
res.Collectibles = fnRet.collectibles
|
2023-11-02 23:33:50 +00:00
|
|
|
res.Offset = offset
|
|
|
|
res.HasMore = fnRet.hasMore
|
|
|
|
res.OwnershipStatus = fnRet.ownershipStatus
|
|
|
|
res.ErrorCode = ErrorCodeSuccess
|
|
|
|
}
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 19:39:43 +00:00
|
|
|
s.sendResponseEvent(&requestID, EventOwnedCollectiblesFilteringDone, res, err)
|
2023-07-18 15:02:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-06 12:58:36 +00:00
|
|
|
func (s *Service) GetCollectiblesByUniqueID(
|
|
|
|
ctx context.Context,
|
|
|
|
uniqueIDs []thirdparty.CollectibleUniqueID,
|
|
|
|
dataType CollectibleDataType) (*GetCollectiblesByUniqueIDReturnType, error) {
|
|
|
|
collectibles, err := s.collectibleIDsToDataType(ctx, uniqueIDs, dataType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &GetCollectiblesByUniqueIDReturnType{
|
|
|
|
collectibles: collectibles,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2023-11-02 23:33:50 +00:00
|
|
|
func (s *Service) GetCollectiblesByUniqueIDAsync(
|
|
|
|
requestID int32,
|
|
|
|
uniqueIDs []thirdparty.CollectibleUniqueID,
|
|
|
|
dataType CollectibleDataType) {
|
2023-08-10 19:39:43 +00:00
|
|
|
s.scheduler.Enqueue(requestID, getCollectiblesDataTask, func(ctx context.Context) (interface{}, error) {
|
2023-11-06 12:58:36 +00:00
|
|
|
return s.GetCollectiblesByUniqueID(ctx, uniqueIDs, dataType)
|
2023-07-18 15:02:56 +00:00
|
|
|
}, func(result interface{}, taskType async.TaskType, err error) {
|
2023-11-02 23:33:50 +00:00
|
|
|
res := GetCollectiblesByUniqueIDResponse{
|
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-11-10 00:31:48 +00:00
|
|
|
fnRet := result.(*GetCollectiblesByUniqueIDReturnType)
|
2023-11-02 23:33:50 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2023-11-10 00:31:48 +00:00
|
|
|
res.Collectibles = fnRet.collectibles
|
2023-11-02 23:33:50 +00:00
|
|
|
res.ErrorCode = ErrorCodeSuccess
|
|
|
|
}
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-09-21 21:42:02 +00:00
|
|
|
func (s *Service) RefetchOwnedCollectibles() {
|
2023-10-09 12:43:53 +00:00
|
|
|
s.controller.RefetchOwnedCollectibles()
|
2023-07-24 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Start() {
|
2023-10-09 12:43:53 +00:00
|
|
|
s.controller.Start()
|
2023-07-24 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Stop() {
|
2023-10-09 12:43:53 +00:00
|
|
|
s.controller.Stop()
|
2023-07-24 18:54:21 +00:00
|
|
|
|
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
|
|
|
|
2023-11-01 19:09:10 +00:00
|
|
|
func (s *Service) FilterOwnedCollectibles(chainIDs []walletCommon.ChainID, owners []common.Address, filter Filter, offset int, limit int) ([]thirdparty.CollectibleUniqueID, bool, error) {
|
|
|
|
ctx := context.Background()
|
2023-07-26 17:48:14 +00:00
|
|
|
// Request one more than limit, to check if DB has more available
|
2023-11-01 19:09:10 +00:00
|
|
|
ids, err := filterOwnedCollectibles(ctx, s.db, chainIDs, owners, filter, offset, limit+1)
|
2023-07-26 17:48:14 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-09-14 21:52:49 +00:00
|
|
|
|
|
|
|
func (s *Service) GetOwnershipStatus(chainIDs []walletCommon.ChainID, owners []common.Address) (OwnershipStatusPerAddressAndChainID, error) {
|
|
|
|
ret := make(OwnershipStatusPerAddressAndChainID)
|
|
|
|
for _, address := range owners {
|
|
|
|
ret[address] = make(OwnershipStatusPerChainID)
|
|
|
|
for _, chainID := range chainIDs {
|
|
|
|
timestamp, err := s.ownershipDB.GetOwnershipUpdateTimestamp(address, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret[address][chainID] = OwnershipStatus{
|
2023-10-09 12:43:53 +00:00
|
|
|
State: s.controller.GetCommandState(chainID, address),
|
2023-09-14 21:52:49 +00:00
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2023-09-21 12:40:58 +00:00
|
|
|
|
2023-11-10 00:31:48 +00:00
|
|
|
func (s *Service) collectibleIDsToDataType(ctx context.Context, ids []thirdparty.CollectibleUniqueID, dataType CollectibleDataType) ([]Collectible, error) {
|
2023-11-02 23:33:50 +00:00
|
|
|
switch dataType {
|
|
|
|
case CollectibleDataTypeUniqueID:
|
2023-11-10 00:31:48 +00:00
|
|
|
return idsToCollectibles(ids), nil
|
2023-11-02 23:33:50 +00:00
|
|
|
case CollectibleDataTypeHeader, CollectibleDataTypeDetails, CollectibleDataTypeCommunityHeader:
|
|
|
|
collectibles, err := s.manager.FetchAssetsByCollectibleUniqueID(ctx, ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch dataType {
|
|
|
|
case CollectibleDataTypeHeader:
|
|
|
|
return s.fullCollectiblesDataToHeaders(collectibles)
|
|
|
|
case CollectibleDataTypeDetails:
|
|
|
|
return s.fullCollectiblesDataToDetails(collectibles)
|
|
|
|
case CollectibleDataTypeCommunityHeader:
|
|
|
|
return s.fullCollectiblesDataToCommunityHeader(collectibles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("unknown data type")
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:31:48 +00:00
|
|
|
func idsToCollectibles(ids []thirdparty.CollectibleUniqueID) []Collectible {
|
|
|
|
res := make([]Collectible, 0, len(ids))
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
c := idToCollectible(id)
|
|
|
|
res = append(res, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) fullCollectiblesDataToHeaders(data []thirdparty.FullCollectibleData) ([]Collectible, error) {
|
|
|
|
res := make([]Collectible, 0, len(data))
|
2023-09-21 12:40:58 +00:00
|
|
|
|
|
|
|
for _, c := range data {
|
|
|
|
header := fullCollectibleDataToHeader(c)
|
|
|
|
|
|
|
|
if c.CollectibleData.CommunityID != "" {
|
2023-11-16 04:18:03 +00:00
|
|
|
communityInfo, _, err := s.communityDB.GetCommunityInfo(c.CollectibleData.CommunityID)
|
2023-09-21 12:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
communityHeader := communityInfoToHeader(c.CollectibleData.CommunityID, communityInfo, c.CommunityInfo)
|
2023-11-10 00:31:48 +00:00
|
|
|
header.CommunityData = &communityHeader
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:31:48 +00:00
|
|
|
func (s *Service) fullCollectiblesDataToDetails(data []thirdparty.FullCollectibleData) ([]Collectible, error) {
|
|
|
|
res := make([]Collectible, 0, len(data))
|
2023-09-21 12:40:58 +00:00
|
|
|
|
|
|
|
for _, c := range data {
|
|
|
|
details := fullCollectibleDataToDetails(c)
|
|
|
|
|
|
|
|
if c.CollectibleData.CommunityID != "" {
|
2023-11-16 04:18:03 +00:00
|
|
|
communityInfo, _, err := s.communityDB.GetCommunityInfo(c.CollectibleData.CommunityID)
|
2023-09-21 12:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
communityDetails := communityInfoToDetails(c.CollectibleData.CommunityID, communityInfo, c.CommunityInfo)
|
2023-11-10 00:31:48 +00:00
|
|
|
details.CommunityData = &communityDetails
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, details)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2023-11-02 23:33:50 +00:00
|
|
|
|
2023-11-10 00:31:48 +00:00
|
|
|
func (s *Service) fullCollectiblesDataToCommunityHeader(data []thirdparty.FullCollectibleData) ([]Collectible, error) {
|
|
|
|
res := make([]Collectible, 0, len(data))
|
2023-11-02 23:33:50 +00:00
|
|
|
|
|
|
|
for _, c := range data {
|
|
|
|
collectibleID := c.CollectibleData.ID
|
|
|
|
communityID := c.CollectibleData.CommunityID
|
|
|
|
|
|
|
|
if communityID == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
communityInfo, _, err := s.communityDB.GetCommunityInfo(communityID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error fetching community info", "error", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:31:48 +00:00
|
|
|
communityHeader := communityInfoToHeader(communityID, communityInfo, c.CommunityInfo)
|
|
|
|
|
|
|
|
header := Collectible{
|
|
|
|
ID: collectibleID,
|
|
|
|
CollectibleData: &CollectibleData{
|
|
|
|
Name: c.CollectibleData.Name,
|
|
|
|
},
|
|
|
|
CommunityData: &communityHeader,
|
2023-11-02 23:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) notifyCommunityCollectiblesReceived(ownedCollectibles OwnedCollectibles) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
collectiblesData, err := s.manager.FetchAssetsByCollectibleUniqueID(ctx, ownedCollectibles.ids)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error fetching collectibles data", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:31:48 +00:00
|
|
|
communityCollectibles, err := s.fullCollectiblesDataToCommunityHeader(collectiblesData)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error converting received collectibles data", "error", err)
|
|
|
|
return
|
2023-11-02 23:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(communityCollectibles) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := json.Marshal(communityCollectibles)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.walletFeed.Send(walletevent.Event{
|
|
|
|
Type: EventCommunityCollectiblesReceived,
|
|
|
|
ChainID: uint64(ownedCollectibles.chainID),
|
|
|
|
Accounts: []common.Address{
|
|
|
|
ownedCollectibles.account,
|
|
|
|
},
|
|
|
|
Message: string(encodedMessage),
|
|
|
|
})
|
|
|
|
}
|