-
Notifications
You must be signed in to change notification settings - Fork 38
fix(checkout): Fetch crypto to USD conversion via our own subgraph #2620
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f1689a
use separate endpoint for swap
jeetparikh 7988a93
Merge branch 'main' into feat/GFI-319-new-cryptofiat-class
jeetparikh 8b42300
process correctly
jeetparikh 8ccaa35
tests
jeetparikh 883bbea
return err too
jeetparikh 347e660
remove commented code
jeetparikh d772961
Merge branch 'main' into feat/GFI-319-new-cryptofiat-class
jeetparikh 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
59 changes: 59 additions & 0 deletions
59
packages/checkout/widgets-lib/src/context/crypto-fiat-context/CryptoFiat.test.ts
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,59 @@ | ||
| /* eslint @typescript-eslint/naming-convention: off */ | ||
|
|
||
| import axios from 'axios'; | ||
| import { Environment } from '@imtbl/config'; | ||
| import { getCryptoToUSDConversion, TokenPriceResponse } from './CryptoFiat'; | ||
|
|
||
| jest.mock('axios'); | ||
| const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
|
||
| const conversionsMap = new Map<string, number>(); | ||
|
|
||
| const mockResponse: TokenPriceResponse = { | ||
| result: [ | ||
| { | ||
| symbol: 'WOMBAT', | ||
| token_address: '0x0219d987a75f860e55d936646c60ba9a021e52ac', | ||
| usd_price: '0.0001359733469', | ||
| }, | ||
| { | ||
| symbol: 'BZAI', | ||
| token_address: '0x0bbb2db8d777c72516a344506fa2130040b48c13', | ||
| usd_price: '0.03', | ||
| }, | ||
| { | ||
| symbol: 'WIMX', | ||
| token_address: '0x3a0c2ba54d6cbd3121f01b96dfd20e99d1696c9d', | ||
| usd_price: '0.89', | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| describe('getCryptoToUSDConversion', () => { | ||
| beforeEach(() => { | ||
| conversionsMap.set('wombat', 0.0001359733469); | ||
| conversionsMap.set('bzai', 0.03); | ||
| conversionsMap.set('wimx', 0.89); | ||
| conversionsMap.set('imx', 0.89); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| conversionsMap.clear(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should return the correct conversion for all tokens', async () => { | ||
| mockedAxios.get.mockResolvedValue({ data: mockResponse }); | ||
|
|
||
| const conversions = await getCryptoToUSDConversion(Environment.SANDBOX); | ||
| expect(conversions.size).toEqual(4); | ||
| expect(conversions).toEqual(conversionsMap); | ||
| }); | ||
|
|
||
| it('adds IMX to the conversions map', async () => { | ||
| mockedAxios.get.mockResolvedValue({ data: mockResponse }); | ||
|
|
||
| const conversions = await getCryptoToUSDConversion(Environment.SANDBOX); | ||
| expect(conversions.get('imx')).toEqual(0.89); | ||
| }); | ||
| }); |
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
32 changes: 32 additions & 0 deletions
32
packages/checkout/widgets-lib/src/lib/hooks/useCryptoUSDConversion.ts
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,32 @@ | ||
| import { useState, useEffect } from 'react'; | ||
| import { Environment } from '@imtbl/config'; | ||
| import { getCryptoToUSDConversion } from '../../context/crypto-fiat-context/CryptoFiat'; | ||
|
|
||
| export function useCryptoUSDConversion(environment: Environment | undefined) { | ||
| const [conversions, setConversions] = useState<Map<string, number>>(new Map<string, number>()); | ||
| const [error, setError] = useState<Error | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| async function fetchConversions() { | ||
| if (!environment) return; | ||
|
|
||
| try { | ||
| const data = await getCryptoToUSDConversion(environment); | ||
| setConversions(data); | ||
| setError(null); | ||
| } catch (err) { | ||
| setError(err instanceof Error ? err : new Error('Failed to fetch conversions')); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| fetchConversions(); | ||
| const interval = setInterval(fetchConversions, 30000); | ||
|
|
||
| return () => clearInterval(interval); | ||
| }, [environment]); | ||
|
|
||
| return { conversions, error, loading }; | ||
| } | ||
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.