Repository navigation
Load Robinhood stock tokens in the background #659
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
Open
antoncoding
wants to merge
3
commits into
master
Choose a base branch
from
codex/robinhood-stock-tokens
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { unstable_cache } from 'next/cache'; | ||
| import { fetchRobinhoodStockTokens } from '@/data-sources/robinhood-api/assets'; | ||
|
|
||
| // Cache validated results. An upstream outage must not replace the last successful catalog. | ||
| const getCachedTokens = unstable_cache(fetchRobinhoodStockTokens, ['robinhood-stock-tokens:v1'], { revalidate: 1800 }); | ||
|
|
||
| export async function GET() { | ||
| try { | ||
| return Response.json(await getCachedTokens(), { headers: { 'Cache-Control': 'public, max-age=300' } }); | ||
| } catch { | ||
| return Response.json({ error: 'Robinhood Stock Token catalog unavailable.' }, { status: 502 }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { z } from 'zod'; | ||
| import { SupportedNetworks } from '@/utils/supported-networks'; | ||
|
|
||
| const ROBINHOOD_ASSETS_URL = 'https://api.robinhood.com/rhj/assets'; | ||
| const ROBINHOOD_ASSETS_TIMEOUT_MS = 8_000; | ||
|
|
||
| const addressSchema = z | ||
| .string() | ||
| .regex(/^0x[0-9a-f]{40}$/i) | ||
| .transform((address) => address.toLowerCase()); | ||
| export const robinhoodStockTokensSchema = z.array( | ||
| z.object({ | ||
| address: addressSchema, | ||
| symbol: z.string().trim().min(1), | ||
| decimals: z.number().int().min(0).max(255), | ||
| logoUrl: z.string().url().startsWith('https://'), | ||
| }), | ||
| ); | ||
|
|
||
| type RobinhoodStockToken = z.infer<typeof robinhoodStockTokensSchema>[number]; | ||
| const upstreamAssetSchema = z.object({ | ||
| tokenSymbol: robinhoodStockTokensSchema.element.shape.symbol, | ||
| tokenDecimals: robinhoodStockTokensSchema.element.shape.decimals, | ||
| logoUrl: robinhoodStockTokensSchema.element.shape.logoUrl, | ||
| status: z.enum(['ASSET_STATUS_ACTIVE', 'ASSET_STATUS_INACTIVE', 'ASSET_STATUS_UNSPECIFIED']), | ||
| deployments: z.array(z.object({ contractAddress: addressSchema, chainId: z.number().int() })), | ||
| }); | ||
| const upstreamSchema = z.object({ assets: z.array(upstreamAssetSchema) }); | ||
|
|
||
| /** Returns canonical active Robinhood Chain deployments. Request failures and malformed catalogs reject. */ | ||
| export async function fetchRobinhoodStockTokens(): Promise<RobinhoodStockToken[]> { | ||
| const response = await fetch(ROBINHOOD_ASSETS_URL, { | ||
| credentials: 'omit', | ||
| cache: 'no-store', | ||
| signal: AbortSignal.timeout(ROBINHOOD_ASSETS_TIMEOUT_MS), | ||
| }); | ||
| if (!response.ok) throw new Error(`Robinhood Stock Token API returned ${response.status}.`); | ||
|
|
||
| const { assets } = upstreamSchema.parse(await response.json()); | ||
| const tokens = new Map<string, RobinhoodStockToken>(); | ||
| for (const { status, deployments, tokenSymbol, tokenDecimals, logoUrl } of assets) { | ||
| if (status !== 'ASSET_STATUS_ACTIVE') continue; | ||
|
|
||
| for (const { chainId, contractAddress } of deployments) { | ||
| if (chainId !== SupportedNetworks.Robinhood || tokens.has(contractAddress)) continue; | ||
| tokens.set(contractAddress, { address: contractAddress, symbol: tokenSymbol, decimals: tokenDecimals, logoUrl }); | ||
| } | ||
| } | ||
|
|
||
| if (tokens.size === 0) throw new Error('Robinhood Stock Token API returned no active Robinhood Chain tokens.'); | ||
| return [...tokens.values()]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still lets an empty catalog, or any partially malformed catalog with at least one valid row, return successfully. The route then caches the empty/truncated result for 30 minutes, so recognized stock markets can disappear instead of retaining the last complete catalog. Treat catalog-integrity failures as errors (at minimum an unexpected empty list; ideally malformed records too) so
unstable_cachecan preserve the last successful value.