chore: rename and reorder types

This commit is contained in:
Dario Gabriel Lipicar 2023-07-05 06:33:48 -03:00 committed by dlipicar
parent 07a3726b1d
commit b5224b3cc5
10 changed files with 105 additions and 104 deletions

View File

@ -118,7 +118,7 @@ func (b *StatusNode) initServices(config *params.NodeConfig, mediaServer *server
services = append(services, wakuext)
b.SetWalletNFTMetadataProvider(wakuext)
b.SetWalletCollectibleMetadataProvider(wakuext)
}
if config.WakuV2Config.Enabled {
@ -145,7 +145,7 @@ func (b *StatusNode) initServices(config *params.NodeConfig, mediaServer *server
services = append(services, wakuext)
b.SetWalletNFTMetadataProvider(wakuext)
b.SetWalletCollectibleMetadataProvider(wakuext)
}
// We ignore for now local notifications flag as users who are upgrading have no mean to enable it
@ -487,9 +487,9 @@ func (b *StatusNode) WalletService() *wallet.Service {
return b.walletSrvc
}
func (b *StatusNode) SetWalletNFTMetadataProvider(provider thirdparty.NFTMetadataProvider) {
func (b *StatusNode) SetWalletCollectibleMetadataProvider(provider thirdparty.CollectibleMetadataProvider) {
if b.walletSrvc != nil {
b.walletSrvc.SetNFTMetadataProvider(provider)
b.walletSrvc.SetCollectibleMetadataProvider(provider)
}
}

View File

@ -62,7 +62,7 @@ type EnvelopeEventsHandler interface {
// Service is a service that provides some additional API to whisper-based protocols like Whisper or Waku.
type Service struct {
thirdparty.NFTMetadataProvider
thirdparty.CollectibleMetadataProvider
messenger *protocol.Messenger
identity *ecdsa.PrivateKey
cancelMessenger chan struct{}
@ -545,12 +545,12 @@ func tokenURIToCommunityID(tokenURI string) string {
return communityID
}
func (s *Service) CanProvideNFTMetadata(chainID uint64, id thirdparty.NFTUniqueID, tokenURI string) (bool, error) {
func (s *Service) CanProvideCollectibleMetadata(chainID uint64, id thirdparty.CollectibleUniqueID, tokenURI string) (bool, error) {
ret := tokenURI != "" && tokenURIToCommunityID(tokenURI) != ""
return ret, nil
}
func (s *Service) FetchNFTMetadata(chainID uint64, id thirdparty.NFTUniqueID, tokenURI string) (*thirdparty.NFTMetadata, error) {
func (s *Service) FetchCollectibleMetadata(chainID uint64, id thirdparty.CollectibleUniqueID, tokenURI string) (*thirdparty.CollectibleMetadata, error) {
if s.messenger == nil {
return nil, fmt.Errorf("messenger not ready")
}
@ -574,7 +574,7 @@ func (s *Service) FetchNFTMetadata(chainID uint64, id thirdparty.NFTUniqueID, to
for _, tokenMetadata := range tokensMetadata {
contractAddresses := tokenMetadata.GetContractAddresses()
if contractAddresses[chainID] == id.ContractAddress.Hex() {
return &thirdparty.NFTMetadata{
return &thirdparty.CollectibleMetadata{
Name: tokenMetadata.GetName(),
Description: tokenMetadata.GetDescription(),
CollectionImageURL: tokenMetadata.GetImage(),

View File

@ -328,14 +328,14 @@ func (api *API) GetOpenseaAssetsByOwnerAndContractAddressWithCursor(ctx context.
return api.s.collectiblesManager.FetchAllAssetsByOwnerAndContractAddress(chainID, owner, contractAddresses, cursor, limit)
}
func (api *API) GetOpenseaAssetsByNFTUniqueID(ctx context.Context, chainID uint64, uniqueIDs []thirdparty.NFTUniqueID, limit int) (*opensea.AssetContainer, error) {
func (api *API) GetOpenseaAssetsByNFTUniqueID(ctx context.Context, chainID uint64, uniqueIDs []thirdparty.CollectibleUniqueID, limit int) (*opensea.AssetContainer, error) {
log.Debug("call to GetOpenseaAssetsByNFTUniqueID")
return api.s.collectiblesManager.FetchAssetsByNFTUniqueID(chainID, uniqueIDs, limit)
}
func (api *API) GetCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.NFTContractOwnership, error) {
func (api *API) GetCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
log.Debug("call to GetCollectibleOwnersByContractAddress")
return api.s.collectiblesManager.FetchNFTOwnersByContractAddress(chainID, contractAddress)
return api.s.collectiblesManager.FetchCollectibleOwnersByContractAddress(chainID, contractAddress)
}
func (api *API) FetchBalancesByOwnerAndContractAddress(chainID uint64, ownerAddress common.Address, contractAddresses []common.Address) (thirdparty.TokenBalancesPerContractAddress, error) {

View File

@ -35,16 +35,16 @@ var noTokenURIErrorPrefixes = []string{
type Manager struct {
rpcClient *rpc.Client
mainContractOwnershipProvider thirdparty.NFTContractOwnershipProvider
fallbackContractOwnershipProvider thirdparty.NFTContractOwnershipProvider
metadataProvider thirdparty.NFTMetadataProvider
mainContractOwnershipProvider thirdparty.CollectibleContractOwnershipProvider
fallbackContractOwnershipProvider thirdparty.CollectibleContractOwnershipProvider
metadataProvider thirdparty.CollectibleMetadataProvider
openseaAPIKey string
nftCache map[uint64]map[string]opensea.Asset
nftCacheLock sync.RWMutex
walletFeed *event.Feed
}
func NewManager(rpcClient *rpc.Client, mainContractOwnershipProvider thirdparty.NFTContractOwnershipProvider, fallbackContractOwnershipProvider thirdparty.NFTContractOwnershipProvider, openseaAPIKey string, walletFeed *event.Feed) *Manager {
func NewManager(rpcClient *rpc.Client, mainContractOwnershipProvider thirdparty.CollectibleContractOwnershipProvider, fallbackContractOwnershipProvider thirdparty.CollectibleContractOwnershipProvider, openseaAPIKey string, walletFeed *event.Feed) *Manager {
hystrix.ConfigureCommand(hystrixContractOwnershipClientName, hystrix.CommandConfig{
Timeout: 10000,
MaxConcurrentRequests: 100,
@ -92,7 +92,7 @@ func makeContractOwnershipCall(main func() (any, error), fallback func() (any, e
}
// Used to break circular dependency, call once as soon as possible after initialization
func (o *Manager) SetMetadataProvider(metadataProvider thirdparty.NFTMetadataProvider) {
func (o *Manager) SetMetadataProvider(metadataProvider thirdparty.CollectibleMetadataProvider) {
o.metadataProvider = metadataProvider
}
@ -136,7 +136,7 @@ func (o *Manager) FetchBalancesByOwnerAndContractAddress(chainID uint64, ownerAd
if err == opensea.ErrChainIDNotSupported {
// Use contract ownership providers
for _, contractAddress := range contractAddresses {
ownership, err := o.FetchNFTOwnersByContractAddress(chainID, contractAddress)
ownership, err := o.FetchCollectibleOwnersByContractAddress(chainID, contractAddress)
if err != nil {
return nil, err
}
@ -203,7 +203,7 @@ func (o *Manager) FetchAllAssetsByOwner(chainID uint64, owner common.Address, cu
return assetContainer, nil
}
func (o *Manager) FetchAssetsByNFTUniqueID(chainID uint64, uniqueIDs []thirdparty.NFTUniqueID, limit int) (*opensea.AssetContainer, error) {
func (o *Manager) FetchAssetsByNFTUniqueID(chainID uint64, uniqueIDs []thirdparty.CollectibleUniqueID, limit int) (*opensea.AssetContainer, error) {
assetContainer := new(opensea.AssetContainer)
idsToFetch := o.getIDsNotInCache(chainID, uniqueIDs)
@ -232,14 +232,14 @@ func (o *Manager) FetchAssetsByNFTUniqueID(chainID uint64, uniqueIDs []thirdpart
return assetContainer, nil
}
func (o *Manager) FetchNFTOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.NFTContractOwnership, error) {
func (o *Manager) FetchCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
mainFunc := func() (any, error) {
return o.mainContractOwnershipProvider.FetchNFTOwnersByContractAddress(chainID, contractAddress)
return o.mainContractOwnershipProvider.FetchCollectibleOwnersByContractAddress(chainID, contractAddress)
}
var fallbackFunc func() (any, error) = nil
if o.fallbackContractOwnershipProvider != nil && o.fallbackContractOwnershipProvider.IsChainSupported(chainID) {
fallbackFunc = func() (any, error) {
return o.fallbackContractOwnershipProvider.FetchNFTOwnersByContractAddress(chainID, contractAddress)
return o.fallbackContractOwnershipProvider.FetchCollectibleOwnersByContractAddress(chainID, contractAddress)
}
}
owners, err := makeContractOwnershipCall(mainFunc, fallbackFunc)
@ -247,7 +247,7 @@ func (o *Manager) FetchNFTOwnersByContractAddress(chainID uint64, contractAddres
return nil, err
}
return owners.(*thirdparty.NFTContractOwnership), nil
return owners.(*thirdparty.CollectibleContractOwnership), nil
}
func isMetadataEmpty(asset opensea.Asset) bool {
@ -257,7 +257,7 @@ func isMetadataEmpty(asset opensea.Asset) bool {
asset.TokenURI == ""
}
func (o *Manager) fetchTokenURI(chainID uint64, id thirdparty.NFTUniqueID) (string, error) {
func (o *Manager) fetchTokenURI(chainID uint64, id thirdparty.CollectibleUniqueID) (string, error) {
backend, err := o.rpcClient.EthClient(chainID)
if err != nil {
return "", err
@ -297,14 +297,14 @@ func (o *Manager) processAssets(chainID uint64, assets []opensea.Asset) error {
}
for idx, asset := range assets {
id := thirdparty.NFTUniqueID{
id := thirdparty.CollectibleUniqueID{
ContractAddress: common.HexToAddress(asset.Contract.Address),
TokenID: asset.TokenID,
}
if isMetadataEmpty(asset) {
if o.metadataProvider == nil {
return fmt.Errorf("NFTMetadataProvider not available")
return fmt.Errorf("CollectibleMetadataProvider not available")
}
tokenURI, err := o.fetchTokenURI(chainID, id)
@ -314,14 +314,14 @@ func (o *Manager) processAssets(chainID uint64, assets []opensea.Asset) error {
assets[idx].TokenURI = tokenURI
canProvide, err := o.metadataProvider.CanProvideNFTMetadata(chainID, id, tokenURI)
canProvide, err := o.metadataProvider.CanProvideCollectibleMetadata(chainID, id, tokenURI)
if err != nil {
return err
}
if canProvide {
metadata, err := o.metadataProvider.FetchNFTMetadata(chainID, id, tokenURI)
metadata, err := o.metadataProvider.FetchCollectibleMetadata(chainID, id, tokenURI)
if err != nil {
return err
}
@ -347,11 +347,11 @@ func (o *Manager) processAssets(chainID uint64, assets []opensea.Asset) error {
return nil
}
func (o *Manager) getIDsNotInCache(chainID uint64, uniqueIDs []thirdparty.NFTUniqueID) []thirdparty.NFTUniqueID {
func (o *Manager) getIDsNotInCache(chainID uint64, uniqueIDs []thirdparty.CollectibleUniqueID) []thirdparty.CollectibleUniqueID {
o.nftCacheLock.RLock()
defer o.nftCacheLock.RUnlock()
idsToFetch := make([]thirdparty.NFTUniqueID, 0, len(uniqueIDs))
idsToFetch := make([]thirdparty.CollectibleUniqueID, 0, len(uniqueIDs))
if _, ok := o.nftCache[chainID]; !ok {
idsToFetch = uniqueIDs
} else {
@ -364,7 +364,7 @@ func (o *Manager) getIDsNotInCache(chainID uint64, uniqueIDs []thirdparty.NFTUni
return idsToFetch
}
func (o *Manager) getCachedAssets(chainID uint64, uniqueIDs []thirdparty.NFTUniqueID) []opensea.Asset {
func (o *Manager) getCachedAssets(chainID uint64, uniqueIDs []thirdparty.CollectibleUniqueID) []opensea.Asset {
o.nftCacheLock.RLock()
defer o.nftCacheLock.RUnlock()

View File

@ -181,7 +181,7 @@ func (s *Service) GetFeed() *event.Feed {
}
// Set external Collectibles metadata provider
func (s *Service) SetNFTMetadataProvider(provider thirdparty.NFTMetadataProvider) {
func (s *Service) SetCollectibleMetadataProvider(provider thirdparty.CollectibleMetadataProvider) {
s.collectiblesManager.SetMetadataProvider(provider)
}

View File

@ -58,18 +58,18 @@ type TokenBalance struct {
Balance *bigint.BigInt `json:"balance"`
}
type NFTOwner struct {
type CollectibleOwner struct {
OwnerAddress common.Address `json:"ownerAddress"`
TokenBalances []TokenBalance `json:"tokenBalances"`
}
type NFTContractOwnership struct {
Owners []NFTOwner `json:"ownerAddresses"`
PageKey string `json:"pageKey"`
type CollectibleContractOwnership struct {
Owners []CollectibleOwner `json:"ownerAddresses"`
PageKey string `json:"pageKey"`
}
type Client struct {
thirdparty.NFTContractOwnershipProvider
thirdparty.CollectibleContractOwnershipProvider
client *http.Client
apiKeys map[uint64]string
IsConnected bool
@ -98,8 +98,8 @@ func (o *Client) IsChainSupported(chainID uint64) bool {
return err == nil
}
func alchemyOwnershipToCommon(contractAddress common.Address, alchemyOwnership NFTContractOwnership) (*thirdparty.NFTContractOwnership, error) {
owners := make([]thirdparty.NFTOwner, 0, len(alchemyOwnership.Owners))
func alchemyOwnershipToCommon(contractAddress common.Address, alchemyOwnership CollectibleContractOwnership) (*thirdparty.CollectibleContractOwnership, error) {
owners := make([]thirdparty.CollectibleOwner, 0, len(alchemyOwnership.Owners))
for _, alchemyOwner := range alchemyOwnership.Owners {
balances := make([]thirdparty.TokenBalance, 0, len(alchemyOwner.TokenBalances))
@ -109,7 +109,7 @@ func alchemyOwnershipToCommon(contractAddress common.Address, alchemyOwnership N
Balance: alchemyBalance.Balance,
})
}
owner := thirdparty.NFTOwner{
owner := thirdparty.CollectibleOwner{
OwnerAddress: alchemyOwner.OwnerAddress,
TokenBalances: balances,
}
@ -117,7 +117,7 @@ func alchemyOwnershipToCommon(contractAddress common.Address, alchemyOwnership N
owners = append(owners, owner)
}
ownership := thirdparty.NFTContractOwnership{
ownership := thirdparty.CollectibleContractOwnership{
ContractAddress: contractAddress,
Owners: owners,
}
@ -125,7 +125,7 @@ func alchemyOwnershipToCommon(contractAddress common.Address, alchemyOwnership N
return &ownership, nil
}
func (o *Client) FetchNFTOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.NFTContractOwnership, error) {
func (o *Client) FetchCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
queryParams := url.Values{
"contractAddress": {contractAddress.String()},
"withTokenBalances": {"true"},
@ -152,7 +152,7 @@ func (o *Client) FetchNFTOwnersByContractAddress(chainID uint64, contractAddress
return nil, err
}
var alchemyOwnership NFTContractOwnership
var alchemyOwnership CollectibleContractOwnership
err = json.Unmarshal(body, &alchemyOwnership)
if err != nil {
return nil, err

View File

@ -0,0 +1,49 @@
package thirdparty
import (
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/services/wallet/bigint"
)
type CollectibleUniqueID struct {
ContractAddress common.Address `json:"contractAddress"`
TokenID *bigint.BigInt `json:"tokenID"`
}
func (k *CollectibleUniqueID) HashKey() string {
return k.ContractAddress.String() + "+" + k.TokenID.String()
}
type CollectibleMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
CollectionImageURL string `json:"collection_image"`
ImageURL string `json:"image"`
}
type CollectibleMetadataProvider interface {
CanProvideCollectibleMetadata(chainID uint64, id CollectibleUniqueID, tokenURI string) (bool, error)
FetchCollectibleMetadata(chainID uint64, id CollectibleUniqueID, tokenURI string) (*CollectibleMetadata, error)
}
type TokenBalance struct {
TokenID *bigint.BigInt `json:"tokenId"`
Balance *bigint.BigInt `json:"balance"`
}
type TokenBalancesPerContractAddress = map[common.Address][]TokenBalance
type CollectibleOwner struct {
OwnerAddress common.Address `json:"ownerAddress"`
TokenBalances []TokenBalance `json:"tokenBalances"`
}
type CollectibleContractOwnership struct {
ContractAddress common.Address `json:"contractAddress"`
Owners []CollectibleOwner `json:"owners"`
}
type CollectibleContractOwnershipProvider interface {
FetchCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*CollectibleContractOwnership, error)
IsChainSupported(chainID uint64) bool
}

View File

@ -16,21 +16,21 @@ import (
const baseURL = "https://nft.api.infura.io"
type NFTOwner struct {
type CollectibleOwner struct {
ContractAddress common.Address `json:"tokenAddress"`
TokenID *bigint.BigInt `json:"tokenId"`
Amount *bigint.BigInt `json:"amount"`
OwnerAddress common.Address `json:"ownerOf"`
}
type NFTContractOwnership struct {
Owners []NFTOwner `json:"owners"`
Network string `json:"network"`
Cursor string `json:"cursor"`
type CollectibleContractOwnership struct {
Owners []CollectibleOwner `json:"owners"`
Network string `json:"network"`
Cursor string `json:"cursor"`
}
type Client struct {
thirdparty.NFTContractOwnershipProvider
thirdparty.CollectibleContractOwnershipProvider
client *http.Client
apiKey string
apiKeySecret string
@ -71,8 +71,8 @@ func (o *Client) IsChainSupported(chainID uint64) bool {
return false
}
func infuraOwnershipToCommon(contractAddress common.Address, ownersMap map[common.Address][]NFTOwner) (*thirdparty.NFTContractOwnership, error) {
owners := make([]thirdparty.NFTOwner, 0, len(ownersMap))
func infuraOwnershipToCommon(contractAddress common.Address, ownersMap map[common.Address][]CollectibleOwner) (*thirdparty.CollectibleContractOwnership, error) {
owners := make([]thirdparty.CollectibleOwner, 0, len(ownersMap))
for ownerAddress, ownerTokens := range ownersMap {
tokenBalances := make([]thirdparty.TokenBalance, 0, len(ownerTokens))
@ -84,13 +84,13 @@ func infuraOwnershipToCommon(contractAddress common.Address, ownersMap map[commo
})
}
owners = append(owners, thirdparty.NFTOwner{
owners = append(owners, thirdparty.CollectibleOwner{
OwnerAddress: ownerAddress,
TokenBalances: tokenBalances,
})
}
ownership := thirdparty.NFTContractOwnership{
ownership := thirdparty.CollectibleContractOwnership{
ContractAddress: contractAddress,
Owners: owners,
}
@ -98,9 +98,9 @@ func infuraOwnershipToCommon(contractAddress common.Address, ownersMap map[commo
return &ownership, nil
}
func (o *Client) FetchNFTOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.NFTContractOwnership, error) {
func (o *Client) FetchCollectibleOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
cursor := ""
ownersMap := make(map[common.Address][]NFTOwner)
ownersMap := make(map[common.Address][]CollectibleOwner)
for {
url := fmt.Sprintf("%s/networks/%d/nfts/%s/owners", baseURL, chainID, contractAddress.String())
@ -121,7 +121,7 @@ func (o *Client) FetchNFTOwnersByContractAddress(chainID uint64, contractAddress
return nil, err
}
var infuraOwnership NFTContractOwnership
var infuraOwnership CollectibleContractOwnership
err = json.Unmarshal(body, &infuraOwnership)
if err != nil {
return nil, err

View File

@ -381,7 +381,7 @@ func (o *Client) FetchAllAssetsByOwner(owner common.Address, cursor string, limi
return o.fetchAssets(queryParams, limit)
}
func (o *Client) FetchAssetsByNFTUniqueID(uniqueIDs []thirdparty.NFTUniqueID, limit int) (*AssetContainer, error) {
func (o *Client) FetchAssetsByNFTUniqueID(uniqueIDs []thirdparty.CollectibleUniqueID, limit int) (*AssetContainer, error) {
queryParams := url.Values{}
for _, uniqueID := range uniqueIDs {

View File

@ -1,10 +1,5 @@
package thirdparty
import (
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/services/wallet/bigint"
)
type HistoricalPrice struct {
Timestamp int64 `json:"time"`
Value float64 `json:"close"`
@ -41,49 +36,6 @@ type MarketDataProvider interface {
FetchTokenDetails(symbols []string) (map[string]TokenDetails, error)
}
type NFTUniqueID struct {
ContractAddress common.Address `json:"contractAddress"`
TokenID *bigint.BigInt `json:"tokenID"`
}
func (k *NFTUniqueID) HashKey() string {
return k.ContractAddress.String() + "+" + k.TokenID.String()
}
type NFTMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
CollectionImageURL string `json:"collection_image"`
ImageURL string `json:"image"`
}
type NFTMetadataProvider interface {
CanProvideNFTMetadata(chainID uint64, id NFTUniqueID, tokenURI string) (bool, error)
FetchNFTMetadata(chainID uint64, id NFTUniqueID, tokenURI string) (*NFTMetadata, error)
}
type TokenBalance struct {
TokenID *bigint.BigInt `json:"tokenId"`
Balance *bigint.BigInt `json:"balance"`
}
type TokenBalancesPerContractAddress = map[common.Address][]TokenBalance
type NFTOwner struct {
OwnerAddress common.Address `json:"ownerAddress"`
TokenBalances []TokenBalance `json:"tokenBalances"`
}
type NFTContractOwnership struct {
ContractAddress common.Address `json:"contractAddress"`
Owners []NFTOwner `json:"owners"`
}
type NFTContractOwnershipProvider interface {
FetchNFTOwnersByContractAddress(chainID uint64, contractAddress common.Address) (*NFTContractOwnership, error)
IsChainSupported(chainID uint64) bool
}
type DataParsed struct {
Name string `json:"name"`
ID string `json:"id"`