fix: handle opensea v2 api error for accounts holding no collectibles

This commit is contained in:
Dario Gabriel Lipicar 2023-08-23 16:29:09 -03:00 committed by Stefan Dunca
parent c494904f18
commit 8d8bd4fc92
2 changed files with 13 additions and 2 deletions

View File

@ -25,9 +25,9 @@ const assetLimitV2 = 50
func getV2BaseURL(chainID walletCommon.ChainID) (string, error) { func getV2BaseURL(chainID walletCommon.ChainID) (string, error) {
switch uint64(chainID) { switch uint64(chainID) {
case walletCommon.EthereumMainnet, walletCommon.ArbitrumMainnet, walletCommon.OptimismMainnet: 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: 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 return "", thirdparty.ErrChainIDNotSupported
@ -156,6 +156,13 @@ func (o *ClientV2) fetchAssets(chainID walletCommon.ChainID, pathParams []string
} }
o.connectionStatus.SetIsConnected(true) 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 is not returned there must be an error
if !json.Valid(body) { if !json.Valid(body) {
return nil, fmt.Errorf("invalid json: %s", string(body)) return nil, fmt.Errorf("invalid json: %s", string(body))

View File

@ -65,6 +65,10 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) {
case http.StatusOK: case http.StatusOK:
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
return body, err 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: case http.StatusTooManyRequests:
if retryCount < getRequestRetryMaxCount { if retryCount < getRequestRetryMaxCount {
// sleep and retry // sleep and retry