-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ONRAMP-4917
- Loading branch information
Showing
11 changed files
with
435 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@coinbase/onchainkit": patch | ||
--- | ||
|
||
Added throttling to the exchange rate fetch for the Fund Card |
This file contains 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 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,53 @@ | ||
# `getPortfolios` | ||
|
||
The `getPortfolios` function returns an object containing an array of | ||
portfolios for the provided addresses. Each portfolio is an object with the address | ||
of the wallet, the fiat value of the portfolio, and an array of tokens held by the | ||
provided address. | ||
|
||
:::info | ||
Before using this endpoint, make sure to obtain a [Client API Key](https://portal.cdp.coinbase.com/projects/api-keys/client-key) | ||
from Coinbase Developer Platform. | ||
::: | ||
|
||
## Usage | ||
|
||
:::code-group | ||
|
||
```tsx twoslash [code] | ||
import { setOnchainKitConfig } from '@coinbase/onchainkit'; | ||
import { getPortfolios } from '@coinbase/onchainkit/api'; | ||
|
||
const response = await getPortfolios({ | ||
addresses: ['0x...'], | ||
}); | ||
``` | ||
|
||
```json [return value] | ||
"portfolios": [ | ||
{ | ||
"address": "0x...", | ||
"portfolioBalanceInUsd": 100, | ||
"tokenBalances": [{ | ||
"address": "0x...", | ||
"chainId": 1, | ||
"decimals": 18, | ||
"image": "https://example.com/image.png", | ||
"name": "Token Name", | ||
"symbol": "TKN", | ||
"cryptoBalance": 10, | ||
"fiatBalance": 100 | ||
}] | ||
} | ||
] | ||
``` | ||
|
||
::: | ||
|
||
## Returns | ||
|
||
[`Promise<GetPortfoliosResponse>`](/api/types#getportfoliosresponse) | ||
|
||
## Parameters | ||
|
||
[`GetPortfoliosParams`](/api/types#getportfoliosparams) |
This file contains 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 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 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 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 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,108 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { quoteResponseDataMock } from '../mocks'; | ||
import type { OnrampError } from '../types'; | ||
import { fetchOnrampQuote } from '../utils/fetchOnrampQuote'; | ||
import { useOnrampExchangeRate } from './useOnrampExchangeRate'; | ||
|
||
vi.mock('../utils/fetchOnrampQuote'); | ||
|
||
describe('useOnrampExchangeRate', () => { | ||
const mockSetExchangeRate = vi.fn(); | ||
const mockOnError = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
(fetchOnrampQuote as Mock).mockResolvedValue(quoteResponseDataMock); | ||
}); | ||
|
||
it('fetches and calculates exchange rate correctly', async () => { | ||
const { result } = renderHook(() => | ||
useOnrampExchangeRate({ | ||
asset: 'ETH', | ||
currency: 'USD', | ||
country: 'US', | ||
setExchangeRate: mockSetExchangeRate, | ||
}), | ||
); | ||
|
||
await result.current.fetchExchangeRate(); | ||
|
||
// Verify exchange rate calculation | ||
expect(mockSetExchangeRate).toHaveBeenCalledWith( | ||
Number(quoteResponseDataMock.purchaseAmount.value) / | ||
Number(quoteResponseDataMock.paymentSubtotal.value), | ||
); | ||
}); | ||
|
||
it('handles API errors', async () => { | ||
const error = new Error('API Error'); | ||
(fetchOnrampQuote as Mock).mockRejectedValue(error); | ||
|
||
const { result } = renderHook(() => | ||
useOnrampExchangeRate({ | ||
asset: 'ETH', | ||
currency: 'USD', | ||
country: 'US', | ||
setExchangeRate: mockSetExchangeRate, | ||
onError: mockOnError, | ||
}), | ||
); | ||
|
||
await result.current.fetchExchangeRate(); | ||
|
||
// Should call onError with correct error object | ||
expect(mockOnError).toHaveBeenCalledWith({ | ||
errorType: 'handled_error', | ||
code: 'EXCHANGE_RATE_ERROR', | ||
debugMessage: 'API Error', | ||
} satisfies OnrampError); | ||
}); | ||
|
||
it('includes subdivision in API call when provided', async () => { | ||
const { result } = renderHook(() => | ||
useOnrampExchangeRate({ | ||
asset: 'ETH', | ||
currency: 'USD', | ||
country: 'US', | ||
subdivision: 'CA', | ||
setExchangeRate: mockSetExchangeRate, | ||
}), | ||
); | ||
|
||
await result.current.fetchExchangeRate(); | ||
|
||
expect(fetchOnrampQuote).toHaveBeenCalledWith({ | ||
purchaseCurrency: 'ETH', | ||
paymentCurrency: 'USD', | ||
paymentAmount: '100', | ||
paymentMethod: 'CARD', | ||
country: 'US', | ||
subdivision: 'CA', | ||
}); | ||
}); | ||
|
||
it('handles unknown errors', async () => { | ||
const error = { someField: 'unexpected error' }; | ||
(fetchOnrampQuote as Mock).mockRejectedValue(error); | ||
|
||
const { result } = renderHook(() => | ||
useOnrampExchangeRate({ | ||
asset: 'ETH', | ||
currency: 'USD', | ||
country: 'US', | ||
setExchangeRate: mockSetExchangeRate, | ||
onError: mockOnError, | ||
}), | ||
); | ||
|
||
await result.current.fetchExchangeRate(); | ||
|
||
// Should call onError with correct error object | ||
expect(mockOnError).toHaveBeenCalledWith({ | ||
errorType: 'unknown_error', | ||
code: 'EXCHANGE_RATE_ERROR', | ||
debugMessage: JSON.stringify(error), | ||
} satisfies OnrampError); | ||
}); | ||
}); |
Oops, something went wrong.