From bd82250cf775e2f0cfd6acb37eb04349c52bd8a4 Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Fri, 17 Mar 2023 18:20:31 -0300 Subject: [PATCH] feat(Wallet): add API to fetch collectibles by owner and contract address --- services/wallet/api.go | 10 +++++++++ services/wallet/thirdparty/opensea/client.go | 22 +++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/services/wallet/api.go b/services/wallet/api.go index d52c619b8..77211defe 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -332,6 +332,16 @@ func (api *API) GetOpenseaAssetsByOwnerWithCursor(ctx context.Context, chainID u return client.FetchAllAssetsByOwner(owner, cursor, limit) } +func (api *API) GetOpenseaAssetsByOwnerAndContractAddressWithCursor(ctx context.Context, chainID uint64, owner common.Address, contractAddresses []common.Address, cursor string, limit int) (*opensea.AssetContainer, error) { + log.Debug("call to GetOpenseaAssetsByOwnerAndContractAddressWithCursor") + client, err := opensea.NewOpenseaClient(chainID, api.s.openseaAPIKey) + if err != nil { + return nil, err + } + + return client.FetchAllAssetsByOwnerAndContractAddress(owner, contractAddresses, cursor, limit) +} + func (api *API) GetOpenseaAssetsByNFTUniqueID(ctx context.Context, chainID uint64, uniqueIDs []opensea.NFTUniqueID, limit int) (*opensea.AssetContainer, error) { log.Debug("call to GetOpenseaAssetsByNFTUniqueID") diff --git a/services/wallet/thirdparty/opensea/client.go b/services/wallet/thirdparty/opensea/client.go index 22c0c7122..024fd4674 100644 --- a/services/wallet/thirdparty/opensea/client.go +++ b/services/wallet/thirdparty/opensea/client.go @@ -216,6 +216,22 @@ func (o *Client) FetchAllAssetsByOwnerAndCollection(owner common.Address, collec return o.fetchAssets(queryParams, limit) } +func (o *Client) FetchAllAssetsByOwnerAndContractAddress(owner common.Address, contractAddresses []common.Address, cursor string, limit int) (*AssetContainer, error) { + queryParams := url.Values{ + "owner": {owner.String()}, + } + + for _, contractAddress := range contractAddresses { + queryParams.Add("asset_contract_addresses", contractAddress.String()) + } + + if len(cursor) > 0 { + queryParams["cursor"] = []string{cursor} + } + + return o.fetchAssets(queryParams, limit) +} + func (o *Client) FetchAllAssetsByOwner(owner common.Address, cursor string, limit int) (*AssetContainer, error) { queryParams := url.Values{ "owner": {owner.String()}, @@ -246,8 +262,8 @@ func (o *Client) fetchAssets(queryParams url.Values, limit int) (*AssetContainer assets.PreviousCursor = queryParams["cursor"][0] } - tmpLimit := limit - if AssetLimit < limit { + tmpLimit := AssetLimit + if limit > 0 && limit < tmpLimit { tmpLimit = AssetLimit } @@ -283,7 +299,7 @@ func (o *Client) fetchAssets(queryParams url.Values, limit int) (*AssetContainer queryParams["cursor"] = []string{assets.NextCursor} - if len(assets.Assets) >= limit { + if limit > 0 && len(assets.Assets) >= limit { break } }