From 8d8bd4fc927dc4e95704fdd667d284a8a0624178 Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Wed, 23 Aug 2023 16:29:09 -0300 Subject: [PATCH] fix: handle opensea v2 api error for accounts holding no collectibles --- services/wallet/thirdparty/opensea/client_v2.go | 11 +++++++++-- services/wallet/thirdparty/opensea/http_client.go | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/services/wallet/thirdparty/opensea/client_v2.go b/services/wallet/thirdparty/opensea/client_v2.go index 0aa1ab243..e2e9acfd4 100644 --- a/services/wallet/thirdparty/opensea/client_v2.go +++ b/services/wallet/thirdparty/opensea/client_v2.go @@ -25,9 +25,9 @@ const assetLimitV2 = 50 func getV2BaseURL(chainID walletCommon.ChainID) (string, error) { switch uint64(chainID) { case walletCommon.EthereumMainnet, walletCommon.ArbitrumMainnet, walletCommon.OptimismMainnet: - return "https://api.opensea.io/api/v2", nil + return "https://api.opensea.io/v2", nil case walletCommon.EthereumGoerli, walletCommon.EthereumSepolia, walletCommon.ArbitrumGoerli, walletCommon.OptimismGoerli: - return "https://testnets-api.opensea.io/api/v2", nil + return "https://testnets-api.opensea.io/v2", nil } return "", thirdparty.ErrChainIDNotSupported @@ -156,6 +156,13 @@ func (o *ClientV2) fetchAssets(chainID walletCommon.ChainID, pathParams []string } o.connectionStatus.SetIsConnected(true) + // If body is empty, it means the account has no collectibles for this chain. + // (Workaround implemented in http_client.go) + if body == nil { + assets.NextCursor = "" + break + } + // if Json is not returned there must be an error if !json.Valid(body) { return nil, fmt.Errorf("invalid json: %s", string(body)) diff --git a/services/wallet/thirdparty/opensea/http_client.go b/services/wallet/thirdparty/opensea/http_client.go index 875fae21a..51cfd764d 100644 --- a/services/wallet/thirdparty/opensea/http_client.go +++ b/services/wallet/thirdparty/opensea/http_client.go @@ -65,6 +65,10 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) { case http.StatusOK: body, err := ioutil.ReadAll(resp.Body) return body, err + case http.StatusBadRequest: + // The OpenSea v2 API will return error 400 if the account holds no collectibles on + // the requested chain. This shouldn't be treated as an error, return an empty body. + return nil, nil case http.StatusTooManyRequests: if retryCount < getRequestRetryMaxCount { // sleep and retry