2023-03-21 13:52:14 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-01 23:17:59 +00:00
|
|
|
"errors"
|
2023-06-28 10:48:33 +00:00
|
|
|
"fmt"
|
2023-07-13 17:26:17 +00:00
|
|
|
"math/big"
|
2023-07-31 23:34:53 +00:00
|
|
|
"net/http"
|
2023-03-21 13:52:14 +00:00
|
|
|
"strings"
|
2023-03-30 21:01:28 +00:00
|
|
|
"sync"
|
2023-03-21 13:52:14 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
|
|
|
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-07-31 23:34:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/contracts/collectibles"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2023-07-13 17:26:17 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2023-07-18 15:01:53 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/opensea"
|
|
|
|
)
|
|
|
|
|
|
|
|
const requestTimeout = 5 * time.Second
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
const hystrixContractOwnershipClientName = "contractOwnershipClient"
|
|
|
|
|
2023-06-06 17:49:36 +00:00
|
|
|
// ERC721 does not support function "TokenURI" if call
|
|
|
|
// returns error starting with one of these strings
|
|
|
|
var noTokenURIErrorPrefixes = []string{
|
|
|
|
"execution reverted",
|
|
|
|
"abi: attempting to unmarshall",
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
var (
|
|
|
|
ErrNoProvidersAvailableForChainID = errors.New("no providers available for chainID")
|
|
|
|
)
|
|
|
|
|
2023-03-21 13:52:14 +00:00
|
|
|
type Manager struct {
|
2023-08-01 23:17:59 +00:00
|
|
|
rpcClient *rpc.Client
|
|
|
|
contractOwnershipProviders []thirdparty.CollectibleContractOwnershipProvider
|
|
|
|
accountOwnershipProviders []thirdparty.CollectibleAccountOwnershipProvider
|
2023-08-03 12:24:23 +00:00
|
|
|
collectibleDataProviders []thirdparty.CollectibleDataProvider
|
2023-08-01 23:17:59 +00:00
|
|
|
metadataProvider thirdparty.CollectibleMetadataProvider
|
|
|
|
opensea *opensea.Client
|
|
|
|
httpClient *http.Client
|
|
|
|
collectiblesDataCache map[string]thirdparty.CollectibleData
|
|
|
|
collectiblesDataCacheLock sync.RWMutex
|
|
|
|
collectionsDataCache map[string]thirdparty.CollectionData
|
|
|
|
collectionsDataCacheLock sync.RWMutex
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
func NewManager(rpcClient *rpc.Client, contractOwnershipProviders []thirdparty.CollectibleContractOwnershipProvider, accountOwnershipProviders []thirdparty.CollectibleAccountOwnershipProvider, collectibleDataProviders []thirdparty.CollectibleDataProvider, opensea *opensea.Client) *Manager {
|
2023-04-17 11:42:01 +00:00
|
|
|
hystrix.ConfigureCommand(hystrixContractOwnershipClientName, hystrix.CommandConfig{
|
|
|
|
Timeout: 10000,
|
|
|
|
MaxConcurrentRequests: 100,
|
|
|
|
SleepWindow: 300000,
|
|
|
|
ErrorPercentThreshold: 25,
|
|
|
|
})
|
|
|
|
|
2023-03-21 13:52:14 +00:00
|
|
|
return &Manager{
|
2023-08-01 23:17:59 +00:00
|
|
|
rpcClient: rpcClient,
|
|
|
|
contractOwnershipProviders: contractOwnershipProviders,
|
|
|
|
accountOwnershipProviders: accountOwnershipProviders,
|
2023-08-03 12:24:23 +00:00
|
|
|
collectibleDataProviders: collectibleDataProviders,
|
2023-08-01 23:17:59 +00:00
|
|
|
opensea: opensea,
|
2023-07-31 23:34:53 +00:00
|
|
|
httpClient: &http.Client{
|
|
|
|
Timeout: requestTimeout,
|
|
|
|
},
|
|
|
|
collectiblesDataCache: make(map[string]thirdparty.CollectibleData),
|
|
|
|
collectionsDataCache: make(map[string]thirdparty.CollectionData),
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
func refMapToList[K comparable, T any](m map[K]*T) []T {
|
|
|
|
list := make([]T, 0, len(m))
|
|
|
|
for _, v := range m {
|
|
|
|
list = append(list, *v)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
func makeContractOwnershipCall(main func() (any, error), fallback func() (any, error)) (any, error) {
|
|
|
|
resultChan := make(chan any, 1)
|
|
|
|
errChan := hystrix.Go(hystrixContractOwnershipClientName, func() error {
|
|
|
|
res, err := main()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resultChan <- res
|
|
|
|
return nil
|
|
|
|
}, func(err error) error {
|
|
|
|
if fallback == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := fallback()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resultChan <- res
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case result := <-resultChan:
|
|
|
|
return result, nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 23:34:53 +00:00
|
|
|
func (o *Manager) doContentTypeRequest(url string) (string, error) {
|
|
|
|
req, err := http.NewRequest(http.MethodHead, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := o.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
log.Error("failed to close head request body", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resp.Header.Get("Content-Type"), nil
|
|
|
|
}
|
|
|
|
|
2023-07-12 18:27:36 +00:00
|
|
|
// Used to break circular dependency, call once as soon as possible after initialization
|
2023-07-05 09:33:48 +00:00
|
|
|
func (o *Manager) SetMetadataProvider(metadataProvider thirdparty.CollectibleMetadataProvider) {
|
2023-07-12 18:27:36 +00:00
|
|
|
o.metadataProvider = metadataProvider
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Manager) FetchAllCollectionsByOwner(chainID walletCommon.ChainID, owner common.Address) ([]opensea.OwnedCollection, error) {
|
2023-07-10 09:02:17 +00:00
|
|
|
return o.opensea.FetchAllCollectionsByOwner(chainID, owner)
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Manager) FetchAllOpenseaAssetsByOwnerAndCollection(chainID walletCommon.ChainID, owner common.Address, collectionSlug string, cursor string, limit int) (*opensea.AssetContainer, error) {
|
|
|
|
return o.opensea.FetchAllOpenseaAssetsByOwnerAndCollection(chainID, owner, collectionSlug, cursor, limit)
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) FetchAllAssetsByOwnerAndCollection(chainID walletCommon.ChainID, owner common.Address, collectionSlug string, cursor string, limit int) (*thirdparty.FullCollectibleDataContainer, error) {
|
2023-07-10 09:02:17 +00:00
|
|
|
assetContainer, err := o.opensea.FetchAllAssetsByOwnerAndCollection(chainID, owner, collectionSlug, cursor, limit)
|
2023-03-21 13:52:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
err = o.processFullCollectibleData(assetContainer.Items)
|
2023-03-21 13:52:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return assetContainer, nil
|
|
|
|
}
|
|
|
|
|
2023-07-13 17:26:17 +00:00
|
|
|
// Need to combine different providers to support all needed ChainIDs
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Manager) FetchBalancesByOwnerAndContractAddress(chainID walletCommon.ChainID, ownerAddress common.Address, contractAddresses []common.Address) (thirdparty.TokenBalancesPerContractAddress, error) {
|
2023-07-13 17:26:17 +00:00
|
|
|
ret := make(thirdparty.TokenBalancesPerContractAddress)
|
|
|
|
|
|
|
|
for _, contractAddress := range contractAddresses {
|
|
|
|
ret[contractAddress] = make([]thirdparty.TokenBalance, 0)
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
// Try with account ownership providers first
|
2023-07-31 23:34:53 +00:00
|
|
|
assetsContainer, err := o.FetchAllAssetsByOwnerAndContractAddress(chainID, ownerAddress, contractAddresses, thirdparty.FetchFromStartCursor, thirdparty.FetchNoLimit)
|
2023-08-01 23:17:59 +00:00
|
|
|
if err == ErrNoProvidersAvailableForChainID {
|
2023-07-13 17:26:17 +00:00
|
|
|
// Use contract ownership providers
|
|
|
|
for _, contractAddress := range contractAddresses {
|
2023-07-05 09:33:48 +00:00
|
|
|
ownership, err := o.FetchCollectibleOwnersByContractAddress(chainID, contractAddress)
|
2023-07-13 17:26:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, nftOwner := range ownership.Owners {
|
|
|
|
if nftOwner.OwnerAddress == ownerAddress {
|
|
|
|
ret[contractAddress] = nftOwner.TokenBalances
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
2023-08-01 23:17:59 +00:00
|
|
|
// Account ownership providers succeeded
|
2023-07-31 19:41:14 +00:00
|
|
|
for _, fullData := range assetsContainer.Items {
|
|
|
|
contractAddress := fullData.CollectibleData.ID.ContractID.Address
|
2023-07-13 17:26:17 +00:00
|
|
|
balance := thirdparty.TokenBalance{
|
2023-07-31 19:41:14 +00:00
|
|
|
TokenID: fullData.CollectibleData.ID.TokenID,
|
2023-07-13 17:26:17 +00:00
|
|
|
Balance: &bigint.BigInt{Int: big.NewInt(1)},
|
|
|
|
}
|
|
|
|
ret[contractAddress] = append(ret[contractAddress], balance)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// OpenSea could have provided, but returned error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) FetchAllAssetsByOwnerAndContractAddress(chainID walletCommon.ChainID, owner common.Address, contractAddresses []common.Address, cursor string, limit int) (*thirdparty.FullCollectibleDataContainer, error) {
|
2023-08-01 23:17:59 +00:00
|
|
|
for _, provider := range o.accountOwnershipProviders {
|
|
|
|
if !provider.IsChainSupported(chainID) {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
assetContainer, err := provider.FetchAllAssetsByOwnerAndContractAddress(chainID, owner, contractAddresses, cursor, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = o.processFullCollectibleData(assetContainer.Items)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return assetContainer, nil
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
return nil, ErrNoProvidersAvailableForChainID
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) FetchAllAssetsByOwner(chainID walletCommon.ChainID, owner common.Address, cursor string, limit int) (*thirdparty.FullCollectibleDataContainer, error) {
|
2023-08-01 23:17:59 +00:00
|
|
|
for _, provider := range o.accountOwnershipProviders {
|
|
|
|
if !provider.IsChainSupported(chainID) {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
assetContainer, err := provider.FetchAllAssetsByOwner(chainID, owner, cursor, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = o.processFullCollectibleData(assetContainer.Items)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return assetContainer, nil
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
return nil, ErrNoProvidersAvailableForChainID
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
func (o *Manager) FetchCollectibleOwnershipByOwner(chainID walletCommon.ChainID, owner common.Address, cursor string, limit int) (*thirdparty.CollectibleOwnershipContainer, error) {
|
2023-07-31 19:41:14 +00:00
|
|
|
// We don't yet have an API that will return only Ownership data
|
|
|
|
// Use the full Ownership + Metadata endpoint and use the data we need
|
2023-07-26 17:48:14 +00:00
|
|
|
assetContainer, err := o.FetchAllAssetsByOwner(chainID, owner, cursor, limit)
|
2023-07-18 15:02:56 +00:00
|
|
|
if err != nil {
|
2023-07-26 17:48:14 +00:00
|
|
|
return nil, err
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:48:14 +00:00
|
|
|
ret := assetContainer.ToOwnershipContainer()
|
|
|
|
return &ret, nil
|
2023-07-18 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) FetchAssetsByCollectibleUniqueID(uniqueIDs []thirdparty.CollectibleUniqueID) ([]thirdparty.FullCollectibleData, error) {
|
2023-08-03 12:24:23 +00:00
|
|
|
idsPerChainID := thirdparty.GroupCollectibleUIDsByChainID(o.getIDsNotInCollectiblesDataCache(uniqueIDs))
|
2023-03-30 21:01:28 +00:00
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
for chainID, idsToFetch := range idsPerChainID {
|
|
|
|
for _, provider := range o.collectibleDataProviders {
|
|
|
|
if !provider.IsChainSupported(chainID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchedAssets, err := o.opensea.FetchAssetsByCollectibleUniqueID(idsToFetch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = o.processFullCollectibleData(fetchedAssets)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
2023-03-30 21:01:28 +00:00
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
return o.getCacheFullCollectibleData(uniqueIDs), nil
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
func (o *Manager) FetchCollectionsDataByContractID(ids []thirdparty.ContractID) ([]thirdparty.CollectionData, error) {
|
|
|
|
idsPerChainID := thirdparty.GroupContractIDsByChainID(o.getIDsNotInCollectionDataCache(ids))
|
|
|
|
|
|
|
|
for chainID, idsToFetch := range idsPerChainID {
|
|
|
|
for _, provider := range o.collectibleDataProviders {
|
|
|
|
if !provider.IsChainSupported(chainID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchedCollections, err := provider.FetchCollectionsDataByContractID(idsToFetch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = o.processCollectionData(fetchedCollections)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return refMapToList(o.getCacheCollectionData(ids)), nil
|
|
|
|
}
|
|
|
|
|
2023-08-01 23:17:59 +00:00
|
|
|
func (o *Manager) getContractOwnershipProviders(chainID walletCommon.ChainID) (mainProvider thirdparty.CollectibleContractOwnershipProvider, fallbackProvider thirdparty.CollectibleContractOwnershipProvider) {
|
|
|
|
mainProvider = nil
|
|
|
|
fallbackProvider = nil
|
|
|
|
|
|
|
|
for _, provider := range o.contractOwnershipProviders {
|
|
|
|
if provider.IsChainSupported(chainID) {
|
|
|
|
if mainProvider == nil {
|
|
|
|
// First provider found
|
|
|
|
mainProvider = provider
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Second provider found
|
|
|
|
fallbackProvider = provider
|
|
|
|
break
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-01 23:17:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCollectibleOwnersByContractAddressFunc(chainID walletCommon.ChainID, contractAddress common.Address, provider thirdparty.CollectibleContractOwnershipProvider) func() (any, error) {
|
|
|
|
if provider == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return func() (any, error) {
|
|
|
|
return provider.FetchCollectibleOwnersByContractAddress(chainID, contractAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Manager) FetchCollectibleOwnersByContractAddress(chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
|
|
|
|
mainProvider, fallbackProvider := o.getContractOwnershipProviders(chainID)
|
|
|
|
if mainProvider == nil {
|
|
|
|
return nil, ErrNoProvidersAvailableForChainID
|
|
|
|
}
|
|
|
|
|
|
|
|
mainFn := getCollectibleOwnersByContractAddressFunc(chainID, contractAddress, mainProvider)
|
|
|
|
fallbackFn := getCollectibleOwnersByContractAddressFunc(chainID, contractAddress, fallbackProvider)
|
|
|
|
|
|
|
|
owners, err := makeContractOwnershipCall(mainFn, fallbackFn)
|
2023-04-17 11:42:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:33:48 +00:00
|
|
|
return owners.(*thirdparty.CollectibleContractOwnership), nil
|
2023-08-01 23:17:59 +00:00
|
|
|
|
2023-04-17 11:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func isMetadataEmpty(asset thirdparty.CollectibleData) bool {
|
2023-03-21 13:52:14 +00:00
|
|
|
return asset.Name == "" &&
|
|
|
|
asset.Description == "" &&
|
|
|
|
asset.ImageURL == "" &&
|
|
|
|
asset.TokenURI == ""
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Manager) fetchTokenURI(id thirdparty.CollectibleUniqueID) (string, error) {
|
2023-07-31 19:41:14 +00:00
|
|
|
backend, err := o.rpcClient.EthClient(uint64(id.ContractID.ChainID))
|
2023-03-21 13:52:14 +00:00
|
|
|
if err != nil {
|
2023-03-29 16:36:23 +00:00
|
|
|
return "", err
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
caller, err := collectibles.NewCollectiblesCaller(id.ContractID.Address, backend)
|
2023-03-21 13:52:14 +00:00
|
|
|
if err != nil {
|
2023-03-29 16:36:23 +00:00
|
|
|
return "", err
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timeoutContext, timeoutCancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
|
|
defer timeoutCancel()
|
|
|
|
|
2023-03-29 16:36:23 +00:00
|
|
|
tokenURI, err := caller.TokenURI(&bind.CallOpts{
|
2023-03-21 13:52:14 +00:00
|
|
|
Context: timeoutContext,
|
2023-03-29 16:36:23 +00:00
|
|
|
}, id.TokenID.Int)
|
2023-03-21 13:52:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-06-06 17:49:36 +00:00
|
|
|
for _, errorPrefix := range noTokenURIErrorPrefixes {
|
|
|
|
if strings.HasPrefix(err.Error(), errorPrefix) {
|
|
|
|
// Contract doesn't support "TokenURI" method
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:36:23 +00:00
|
|
|
return tokenURI, err
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) processFullCollectibleData(assets []thirdparty.FullCollectibleData) error {
|
2023-08-03 12:24:23 +00:00
|
|
|
missingCollectionIDs := make([]thirdparty.ContractID, 0)
|
|
|
|
|
2023-03-21 13:52:14 +00:00
|
|
|
for idx, asset := range assets {
|
2023-07-31 19:41:14 +00:00
|
|
|
id := asset.CollectibleData.ID
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2023-07-31 23:34:53 +00:00
|
|
|
// Get Metadata from alternate source if empty
|
2023-07-31 19:41:14 +00:00
|
|
|
if isMetadataEmpty(asset.CollectibleData) {
|
2023-06-28 10:48:33 +00:00
|
|
|
if o.metadataProvider == nil {
|
2023-07-05 09:33:48 +00:00
|
|
|
return fmt.Errorf("CollectibleMetadataProvider not available")
|
2023-06-28 10:48:33 +00:00
|
|
|
}
|
2023-07-18 15:01:53 +00:00
|
|
|
tokenURI, err := o.fetchTokenURI(id)
|
2023-03-21 13:52:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
assets[idx].CollectibleData.TokenURI = tokenURI
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
canProvide, err := o.metadataProvider.CanProvideCollectibleMetadata(id, tokenURI)
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2023-03-29 16:36:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2023-03-29 16:36:23 +00:00
|
|
|
if canProvide {
|
2023-07-18 15:01:53 +00:00
|
|
|
metadata, err := o.metadataProvider.FetchCollectibleMetadata(id, tokenURI)
|
2023-03-21 13:52:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:36:23 +00:00
|
|
|
if metadata != nil {
|
2023-07-18 15:01:53 +00:00
|
|
|
assets[idx] = *metadata
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 21:01:28 +00:00
|
|
|
|
2023-07-31 23:34:53 +00:00
|
|
|
// Get Animation MediaType
|
|
|
|
if len(assets[idx].CollectibleData.AnimationURL) > 0 {
|
|
|
|
contentType, err := o.doContentTypeRequest(assets[idx].CollectibleData.AnimationURL)
|
|
|
|
if err != nil {
|
|
|
|
assets[idx].CollectibleData.AnimationURL = ""
|
|
|
|
}
|
|
|
|
assets[idx].CollectibleData.AnimationMediaType = contentType
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
o.setCacheCollectibleData(assets[idx].CollectibleData)
|
|
|
|
if assets[idx].CollectionData != nil {
|
|
|
|
o.setCacheCollectionData(*assets[idx].CollectionData)
|
2023-08-03 12:24:23 +00:00
|
|
|
} else {
|
|
|
|
missingCollectionIDs = append(missingCollectionIDs, id.ContractID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missingCollectionIDs) > 0 {
|
|
|
|
// Calling this ensures collection data is fetched and cached (if not already available)
|
|
|
|
_, err := o.FetchCollectionsDataByContractID(missingCollectionIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-31 19:41:14 +00:00
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-30 21:01:28 +00:00
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
func (o *Manager) processCollectionData(collections []thirdparty.CollectionData) error {
|
|
|
|
for _, collection := range collections {
|
|
|
|
o.setCacheCollectionData(collection)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:01:53 +00:00
|
|
|
func (o *Manager) isIDInCollectiblesDataCache(id thirdparty.CollectibleUniqueID) bool {
|
2023-07-31 19:41:14 +00:00
|
|
|
o.collectiblesDataCacheLock.RLock()
|
|
|
|
defer o.collectiblesDataCacheLock.RUnlock()
|
|
|
|
if _, ok := o.collectiblesDataCache[id.HashKey()]; ok {
|
|
|
|
return true
|
2023-07-18 15:01:53 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Manager) getIDsNotInCollectiblesDataCache(uniqueIDs []thirdparty.CollectibleUniqueID) []thirdparty.CollectibleUniqueID {
|
2023-07-05 09:33:48 +00:00
|
|
|
idsToFetch := make([]thirdparty.CollectibleUniqueID, 0, len(uniqueIDs))
|
2023-07-18 15:01:53 +00:00
|
|
|
for _, id := range uniqueIDs {
|
|
|
|
if o.isIDInCollectiblesDataCache(id) {
|
|
|
|
continue
|
2023-03-30 21:01:28 +00:00
|
|
|
}
|
2023-07-18 15:01:53 +00:00
|
|
|
idsToFetch = append(idsToFetch, id)
|
2023-03-30 21:01:28 +00:00
|
|
|
}
|
|
|
|
return idsToFetch
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) getCacheCollectiblesData(uniqueIDs []thirdparty.CollectibleUniqueID) map[string]*thirdparty.CollectibleData {
|
|
|
|
o.collectiblesDataCacheLock.RLock()
|
|
|
|
defer o.collectiblesDataCacheLock.RUnlock()
|
2023-03-30 21:01:28 +00:00
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
collectibles := make(map[string]*thirdparty.CollectibleData)
|
2023-07-18 15:01:53 +00:00
|
|
|
for _, id := range uniqueIDs {
|
2023-07-31 19:41:14 +00:00
|
|
|
if collectible, ok := o.collectiblesDataCache[id.HashKey()]; ok {
|
|
|
|
collectibles[id.HashKey()] = &collectible
|
|
|
|
continue
|
2023-07-18 15:01:53 +00:00
|
|
|
}
|
2023-03-30 21:01:28 +00:00
|
|
|
}
|
2023-07-31 19:41:14 +00:00
|
|
|
return collectibles
|
2023-03-30 21:01:28 +00:00
|
|
|
}
|
2023-07-18 15:01:53 +00:00
|
|
|
|
|
|
|
func (o *Manager) setCacheCollectibleData(data thirdparty.CollectibleData) {
|
2023-07-31 19:41:14 +00:00
|
|
|
o.collectiblesDataCacheLock.Lock()
|
|
|
|
defer o.collectiblesDataCacheLock.Unlock()
|
|
|
|
|
|
|
|
o.collectiblesDataCache[data.ID.HashKey()] = data
|
|
|
|
}
|
2023-07-18 15:01:53 +00:00
|
|
|
|
2023-08-03 12:24:23 +00:00
|
|
|
func (o *Manager) isIDInCollectionDataCache(id thirdparty.ContractID) bool {
|
|
|
|
o.collectionsDataCacheLock.RLock()
|
|
|
|
defer o.collectionsDataCacheLock.RUnlock()
|
|
|
|
if _, ok := o.collectionsDataCache[id.HashKey()]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Manager) getIDsNotInCollectionDataCache(ids []thirdparty.ContractID) []thirdparty.ContractID {
|
|
|
|
idsToFetch := make([]thirdparty.ContractID, 0, len(ids))
|
|
|
|
for _, id := range ids {
|
|
|
|
if o.isIDInCollectionDataCache(id) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
idsToFetch = append(idsToFetch, id)
|
|
|
|
}
|
|
|
|
return idsToFetch
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:41:14 +00:00
|
|
|
func (o *Manager) getCacheCollectionData(ids []thirdparty.ContractID) map[string]*thirdparty.CollectionData {
|
|
|
|
o.collectionsDataCacheLock.RLock()
|
|
|
|
defer o.collectionsDataCacheLock.RUnlock()
|
|
|
|
|
|
|
|
collections := make(map[string]*thirdparty.CollectionData)
|
|
|
|
for _, id := range ids {
|
|
|
|
if collection, ok := o.collectionsDataCache[id.HashKey()]; ok {
|
|
|
|
collections[id.HashKey()] = &collection
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return collections
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Manager) setCacheCollectionData(data thirdparty.CollectionData) {
|
|
|
|
o.collectionsDataCacheLock.Lock()
|
|
|
|
defer o.collectionsDataCacheLock.Unlock()
|
|
|
|
|
|
|
|
o.collectionsDataCache[data.ID.HashKey()] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Manager) getCacheFullCollectibleData(uniqueIDs []thirdparty.CollectibleUniqueID) []thirdparty.FullCollectibleData {
|
|
|
|
ret := make([]thirdparty.FullCollectibleData, 0, len(uniqueIDs))
|
|
|
|
|
|
|
|
collectiblesData := o.getCacheCollectiblesData(uniqueIDs)
|
|
|
|
|
|
|
|
contractIDs := make([]thirdparty.ContractID, 0, len(uniqueIDs))
|
|
|
|
for _, id := range uniqueIDs {
|
|
|
|
contractIDs = append(contractIDs, id.ContractID)
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionsData := o.getCacheCollectionData(contractIDs)
|
|
|
|
|
|
|
|
for _, id := range uniqueIDs {
|
|
|
|
collectibleData := collectiblesData[id.HashKey()]
|
|
|
|
if collectibleData == nil {
|
|
|
|
// Use empty data, set only ID
|
|
|
|
collectibleData = &thirdparty.CollectibleData{
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullData := thirdparty.FullCollectibleData{
|
|
|
|
CollectibleData: *collectibleData,
|
|
|
|
CollectionData: collectionsData[id.ContractID.HashKey()],
|
|
|
|
}
|
|
|
|
ret = append(ret, fullData)
|
|
|
|
}
|
|
|
|
return ret
|
2023-07-18 15:01:53 +00:00
|
|
|
}
|