Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ratios): ignore invalid and empty coingecko ids #2771

Merged
merged 1 commit into from
Mar 5, 2025

Conversation

onyb
Copy link
Collaborator

@onyb onyb commented Mar 4, 2025

Summary

Our CoinGecko API usage is through the roof, putting us at risk of service discontinuation unless we significantly reduce our requests. This PR aims to reduce Coingecko API usage.

The way clients query token prices using the Ratios service is as follows:

  1. Collect the Coingecko IDs to fetch prices for
  2. Split them in batches, and request the Ratios service for each batch (say, with size 25).
  3. If any of the batched requests fail, the system falls back to fetching prices individually for each ID in the batch.

A batched request can fail for invalid/unknown Coingecko IDs in the request, but rejecting the entire batch for even a single such ID is sub-optimal. This PR ensures that such IDs are ignored and we return prices for the remaining valid Coingecko IDs in the request (if any).

Before

GET https://ratios.bsg.brave.com/v2/relative/provider/coingecko/ethereum,onyb/usd/live
{
  "message": "Error validating Error validating coin url parameter",
  "code": 400,
  "data": {
    "validationErrors": {
      "coinIDs": "invalid coin",
      "err": "failed validation: invalid coin:  is not valid"
    }
  }
}

After

GET http://localhost:8080/v2/relative/provider/coingecko/ethereum,onyb/usd/live
{
  "payload": {
    "ethereum": {
      "usd": 2079.02,
      "usd_timeframe_change": 0.06567674390347
    }
  },
  "lastUpdated": "2025-03-04T13:47:12.306728+05:30"
}

Type of Change

  • Product feature
  • Bug fix
  • Performance improvement
  • Refactor
  • Other

Tested Environments

  • Development
  • Staging
  • Production

Before Requesting Review

  • Does your code build cleanly without any errors or warnings?
  • Have you used auto closing keywords?
  • Have you added tests for new functionality?
  • Have validated query efficiency for new database queries?
  • Have documented new functionality in README or in comments?
  • Have you squashed all intermediate commits?
  • Is there a clear title that explains what the PR does?
  • Have you used intuitive function, variable and other naming?
  • Have you requested security and/or privacy review if needed
  • Have you performed a self review of this PR?

Manual Test Plan

Tested locally on my branch. Once deployed to staging, I'll test with a local build of brave-core.

@onyb onyb self-assigned this Mar 4, 2025
@onyb onyb requested review from clD11 and jen140 as code owners March 4, 2025 15:30
@onyb onyb force-pushed the h/ratios/ignore-invalid-coins branch from 7eab30d to cd2fac3 Compare March 5, 2025 18:13
@onyb onyb merged commit 11b9512 into master Mar 5, 2025
13 checks passed
@onyb onyb deleted the h/ratios/ignore-invalid-coins branch March 5, 2025 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants