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

refactor: use coinmarketcap to generate static fiat estimates #671

Merged
merged 7 commits into from
Sep 18, 2023

Conversation

efstajas
Copy link
Contributor

@efstajas efstajas commented Sep 16, 2023

It turns out that the Binance WS endpoint isn't accessible globally, so this PR changes our provider for fiat estimates to the Coinmarketcap API.

Important: Adds a new env variable COINMARKETCAP_API_KEY. To get one for local development, sign up for a free developer account with Coinmarketcap. To test this PR with mainnet, check out the branch, set .env value PUBLIC_NETWORK to 1, and populate COINMARKETCAP_API_KEY with your own key.

This is largely re-using the existing API surface of the fiat-estimates util, in order to keep the door open to bring back real-time (or periodic) price updates in the future.

All Coinmarketcap API calls are proxied through our SvelteKit backend. On call to fiatEstimates.start, the app fetches a map of ERC-20 token addresses to Coinmarketcap IDs, which is a lot safer than relying on symbols, the uniqueness of which is not guaranteed. On call to track, it now fetches the current USD value of the given tokens once. There's currently no logic for updating currently-tracked tokens over time, but we can bring this back in the future while keeping the current API surface.

@netlify
Copy link

netlify bot commented Sep 16, 2023

Deploy Preview for drips-app-v2 ready!

Name Link
🔨 Latest commit a0035c1
🔍 Latest deploy log https://app.netlify.com/sites/drips-app-v2/deploys/65081d98ddae5300080f7b34
😎 Deploy Preview https://deploy-preview-671--drips-app-v2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@efstajas efstajas marked this pull request as draft September 16, 2023 12:16
@efstajas efstajas marked this pull request as ready for review September 16, 2023 14:26
Copy link
Collaborator

@jtourkos jtourkos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good 👍

Just a thing I noticed while testing locally:

If you manually refresh the page (I was testing http://localhost:5173/app/projects/github/libgit2/libgit2), you sometimes get the estimation, but some other times you get:

Uncaught (in promise) Error: Token info not found for tracked token
    at assert (assert.ts:9:11)
    at Object.convert (fiat-estimates.ts:139:3)
    at token-amounts-table.svelte:37:49
    at Array.forEach (<anonymous>)
    at $$self.$$.update (token-amounts-table.svelte:36:13)
    at update (index.mjs:1343:12)
    at flush (index.mjs:1307:17)

Comment on lines +9 to +10
type TokenAddress = string;
type DataProviderTokenId = number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR: In case you want to take this further, check branded types. 🤓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, never heard of this before!

@efstajas
Copy link
Contributor Author

@jtourkos great catch, I think I know why it's happening. Will push a fix shortly

@efstajas efstajas marked this pull request as draft September 18, 2023 09:38
@efstajas efstajas requested a review from jtourkos September 18, 2023 09:51
@efstajas efstajas marked this pull request as ready for review September 18, 2023 09:58
@efstajas efstajas merged commit 69298e4 into main Sep 18, 2023
10 checks passed
@efstajas efstajas deleted the jason/coinmarketcap-fiat-estimates branch September 18, 2023 10:01
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