From 126151721e24ddfd1007de8e0096f8160228054d Mon Sep 17 00:00:00 2001 From: Adam Lessey Date: Tue, 28 Jan 2025 13:51:49 -0500 Subject: [PATCH] chore: add onchainkit-referrer --- src/api/buildMintTransaction.test.ts | 18 ++++- src/api/buildMintTransaction.ts | 37 +++++----- src/api/buildPayTransaction.test.ts | 24 +++--- src/api/buildPayTransaction.ts | 49 +++++++----- src/api/buildSwapTransaction.test.ts | 62 ++++++++++------ src/api/buildSwapTransaction.ts | 4 +- src/api/getMintDetails.test.ts | 24 +++--- src/api/getMintDetails.ts | 14 ++-- src/api/getPortfolios.test.ts | 1 + src/api/getPortfolios.ts | 16 +++- src/api/getSwapQuote.test.ts | 74 ++++++++++++------- src/api/getSwapQuote.ts | 4 +- src/api/getTokenDetails.test.ts | 24 +++--- src/api/getTokenDetails.ts | 28 ++++--- src/api/getTokens.test.ts | 32 +++++--- src/api/getTokens.ts | 4 +- src/buy/components/BuyProvider.tsx | 19 +++-- src/buy/utils/getBuyQuote.test.ts | 38 ++++++---- src/buy/utils/getBuyQuote.ts | 19 +++-- src/checkout/utils/handlePayRequest.test.ts | 27 ++++--- src/checkout/utils/handlePayRequest.ts | 5 +- src/core/network/constants.ts | 9 +++ src/core/network/request.test.ts | 72 ++++++++++++++++++ src/core/network/request.ts | 41 +++++++++- src/internal/hooks/useExchangeRate.tsx | 15 ++-- src/nft/hooks/useMintData.ts | 13 ++-- src/nft/hooks/useMintDetails.ts | 26 ++++--- src/nft/hooks/useNFTData.ts | 11 ++- src/nft/hooks/useTokenDetails.ts | 23 +++--- src/nft/utils/buildMintTransactionData.ts | 17 +++-- src/swap/components/SwapProvider.test.tsx | 2 + src/swap/components/SwapProvider.tsx | 38 ++++++---- .../WalletAdvancedProvider.test.tsx | 18 +++-- .../components/WalletAdvancedProvider.tsx | 2 +- src/wallet/hooks/usePortfolio.test.tsx | 9 ++- src/wallet/hooks/usePortfolio.ts | 19 +++-- 36 files changed, 566 insertions(+), 272 deletions(-) diff --git a/src/api/buildMintTransaction.test.ts b/src/api/buildMintTransaction.test.ts index 4c9324aa258..458a9aac74c 100644 --- a/src/api/buildMintTransaction.test.ts +++ b/src/api/buildMintTransaction.test.ts @@ -35,7 +35,11 @@ describe('buildMintTransaction', () => { const result = await buildMintTransaction(params); expect(result).toEqual(mockResponse.result); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_MINT_TOKEN, + [params], + 'api', + ); }); it('should return error details when request fails with an error', async () => { @@ -55,7 +59,11 @@ describe('buildMintTransaction', () => { error: 'Error building mint transaction', message: 'Not Found', }); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_MINT_TOKEN, + [params], + 'api', + ); }); it('should return uncaught error details when an exception is thrown', async () => { @@ -68,6 +76,10 @@ describe('buildMintTransaction', () => { error: 'Something went wrong', message: 'Error building mint transaction', }); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_MINT_TOKEN, + [params], + 'api', + ); }); }); diff --git a/src/api/buildMintTransaction.ts b/src/api/buildMintTransaction.ts index 4b0d72dcbf0..6794f8e6311 100644 --- a/src/api/buildMintTransaction.ts +++ b/src/api/buildMintTransaction.ts @@ -1,5 +1,5 @@ import { CDP_MINT_TOKEN } from '../core/network/definitions/nft'; -import { sendRequest } from '../core/network/request'; +import { type JSONRPCReferrer, sendRequest } from '../core/network/request'; import type { BuildMintTransactionParams, BuildMintTransactionResponse, @@ -8,26 +8,29 @@ import type { /** * Retrieves contract to mint an NFT */ -export async function buildMintTransaction({ - mintAddress, - tokenId, - network = '', - quantity, - takerAddress, -}: BuildMintTransactionParams): Promise { +export async function buildMintTransaction( + params: BuildMintTransactionParams, + _referrer: JSONRPCReferrer = 'api', +): Promise { + const { mintAddress, tokenId, network = '', quantity, takerAddress } = params; + try { const res = await sendRequest< BuildMintTransactionParams, BuildMintTransactionResponse - >(CDP_MINT_TOKEN, [ - { - mintAddress, - network, - quantity, - takerAddress, - tokenId, - }, - ]); + >( + CDP_MINT_TOKEN, + [ + { + mintAddress, + network, + quantity, + takerAddress, + tokenId, + }, + ], + _referrer, + ); if (res.error) { return { code: `${res.error.code}`, diff --git a/src/api/buildPayTransaction.test.ts b/src/api/buildPayTransaction.test.ts index 73cd3931414..2cba5ca613c 100644 --- a/src/api/buildPayTransaction.test.ts +++ b/src/api/buildPayTransaction.test.ts @@ -50,9 +50,11 @@ describe('buildPayTransaction', () => { const payTransaction = await buildPayTransaction(mockParams); expect(payTransaction).toEqual(MOCK_HYDRATE_CHARGE_SUCCESS_RESPONSE.result); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_HYDRATE_CHARGE, [ - mockAPIParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_HYDRATE_CHARGE, + [mockAPIParams], + 'api', + ); }); it('should return a Pay Transaction with productId', async () => { @@ -72,9 +74,11 @@ describe('buildPayTransaction', () => { MOCK_CREATE_PRODUCT_CHARGE_SUCCESS_RESPONSE.result, ); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_CREATE_PRODUCT_CHARGE, [ - mockAPIParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_CREATE_PRODUCT_CHARGE, + [mockAPIParams], + 'api', + ); }); it('should return an error if neither chargeId nor productId is provided', async () => { @@ -126,8 +130,10 @@ describe('buildPayTransaction', () => { message: CHECKOUT_INVALID_CHARGE_ERROR_MESSAGE, }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_HYDRATE_CHARGE, [ - mockAPIParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_HYDRATE_CHARGE, + [mockAPIParams], + 'api', + ); }); }); diff --git a/src/api/buildPayTransaction.ts b/src/api/buildPayTransaction.ts index 3767d289868..0c38c6fd337 100644 --- a/src/api/buildPayTransaction.ts +++ b/src/api/buildPayTransaction.ts @@ -2,7 +2,11 @@ import { CDP_CREATE_PRODUCT_CHARGE, CDP_HYDRATE_CHARGE, } from '../core/network/definitions/pay'; -import { type JSONRPCResult, sendRequest } from '../core/network/request'; +import { + type JSONRPCReferrer, + type JSONRPCResult, + sendRequest, +} from '../core/network/request'; import type { BuildPayTransactionParams, BuildPayTransactionResponse, @@ -11,33 +15,42 @@ import type { } from './types'; import { getPayErrorMessage } from './utils/getPayErrorMessage'; -export async function buildPayTransaction({ - address, - chargeId, - productId, -}: BuildPayTransactionParams): Promise { +export async function buildPayTransaction( + params: BuildPayTransactionParams, + _referrer: JSONRPCReferrer = 'api', +): Promise { + const { address, chargeId, productId } = params; + try { let res: JSONRPCResult; if (chargeId) { res = await sendRequest< HydrateChargeAPIParams, BuildPayTransactionResponse - >(CDP_HYDRATE_CHARGE, [ - { - sender: address, - chargeId, - }, - ]); + >( + CDP_HYDRATE_CHARGE, + [ + { + sender: address, + chargeId, + }, + ], + _referrer, + ); } else if (productId) { res = await sendRequest< CreateProductChargeParams, BuildPayTransactionResponse - >(CDP_CREATE_PRODUCT_CHARGE, [ - { - sender: address, - productId, - }, - ]); + >( + CDP_CREATE_PRODUCT_CHARGE, + [ + { + sender: address, + productId, + }, + ], + _referrer, + ); } else { return { code: 'AmBPTa01', // Api Module Build Pay Transaction Error 01 diff --git a/src/api/buildSwapTransaction.test.ts b/src/api/buildSwapTransaction.test.ts index 39b1f4798d7..b335310e169 100644 --- a/src/api/buildSwapTransaction.test.ts +++ b/src/api/buildSwapTransaction.test.ts @@ -115,9 +115,11 @@ describe('buildSwapTransaction', () => { expect(quote.fee).toEqual(expectedResponse.fee); expect(quote.warning).toEqual(expectedResponse.warning); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_TRADE, + [mockApiParams], + 'api', + ); }); it('should return a swap with useAggregator=false', async () => { @@ -197,9 +199,11 @@ describe('buildSwapTransaction', () => { expect(quote.fee).toEqual(expectedResponse.fee); expect(quote.warning).toEqual(expectedResponse.warning); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_TRADE, + [mockApiParams], + 'api', + ); }); it('should return an error for an unsupported amount reference', async () => { @@ -301,12 +305,16 @@ describe('buildSwapTransaction', () => { expect(quote.fee).toEqual(expectedResponse.fee); expect(quote.warning).toEqual(expectedResponse.warning); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [ - { - slippagePercentage: '30', - ...mockApiParams, - }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_TRADE, + [ + { + slippagePercentage: '30', + ...mockApiParams, + }, + ], + 'api', + ); }); it('should return an error if sendRequest fails', async () => { @@ -330,9 +338,11 @@ describe('buildSwapTransaction', () => { message: '', }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_TRADE, + [mockApiParams], + 'api', + ); }); it('should return an error object from buildSwapTransaction', async () => { @@ -361,9 +371,11 @@ describe('buildSwapTransaction', () => { message: '', }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_TRADE, + [mockApiParams], + 'api', + ); }); it('should return an error object from buildSwapTransaction for invalid `amount` input', async () => { @@ -396,10 +408,14 @@ describe('buildSwapTransaction', () => { }; await buildSwapTransaction(mockParams); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [ - expect.objectContaining({ - slippagePercentage: '30', - }), - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_TRADE, + [ + expect.objectContaining({ + slippagePercentage: '30', + }), + ], + 'api', + ); }); }); diff --git a/src/api/buildSwapTransaction.ts b/src/api/buildSwapTransaction.ts index 54b17731d3f..e59ba677e0c 100644 --- a/src/api/buildSwapTransaction.ts +++ b/src/api/buildSwapTransaction.ts @@ -1,7 +1,7 @@ import { SwapMessage } from '@/swap/constants'; import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants'; import { CDP_GET_SWAP_TRADE } from '../core/network/definitions/swap'; -import { sendRequest } from '../core/network/request'; +import { type JSONRPCReferrer, sendRequest } from '../core/network/request'; import type { SwapAPIResponse } from '../swap/types'; import { getSwapErrorCode } from '../swap/utils/getSwapErrorCode'; import type { @@ -17,6 +17,7 @@ import { getSwapTransaction } from './utils/getSwapTransaction'; */ export async function buildSwapTransaction( params: BuildSwapTransactionParams, + _referrer: JSONRPCReferrer = 'api', ): Promise { // Default parameters const defaultParams = { @@ -64,6 +65,7 @@ export async function buildSwapTransaction( const res = await sendRequest( CDP_GET_SWAP_TRADE, [apiParams], + _referrer, ); if (res.error) { return { diff --git a/src/api/getMintDetails.test.ts b/src/api/getMintDetails.test.ts index cb0f3fd1cc0..aa039e2f0ad 100644 --- a/src/api/getMintDetails.test.ts +++ b/src/api/getMintDetails.test.ts @@ -43,9 +43,11 @@ describe('getMintDetails', () => { const result = await getMintDetails(params); expect(result).toEqual(mockResponse.result); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [ - params, - ]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_GET_MINT_DETAILS, + [params], + 'api', + ); }); it('should return error details when request fails with an error', async () => { @@ -65,9 +67,11 @@ describe('getMintDetails', () => { error: 'Error fetching mint details', message: 'Not Found', }); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [ - params, - ]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_GET_MINT_DETAILS, + [params], + 'api', + ); }); it('should return uncaught error details when an exception is thrown', async () => { @@ -80,8 +84,10 @@ describe('getMintDetails', () => { error: 'Something went wrong', message: 'Error fetching mint details', }); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [ - params, - ]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_GET_MINT_DETAILS, + [params], + 'api', + ); }); }); diff --git a/src/api/getMintDetails.ts b/src/api/getMintDetails.ts index af3932dbf4f..30f24c9b4af 100644 --- a/src/api/getMintDetails.ts +++ b/src/api/getMintDetails.ts @@ -1,15 +1,16 @@ import { CDP_GET_MINT_DETAILS } from '../core/network/definitions/nft'; -import { sendRequest } from '../core/network/request'; +import { type JSONRPCReferrer, sendRequest } from '../core/network/request'; import type { GetMintDetailsParams, GetMintDetailsResponse } from './types'; /** * Retrieves mint details for an NFT contract and token ID */ -export async function getMintDetails({ - contractAddress, - takerAddress, - tokenId, -}: GetMintDetailsParams): Promise { +export async function getMintDetails( + params: GetMintDetailsParams, + _referrer: JSONRPCReferrer = 'api', +): Promise { + const { contractAddress, takerAddress, tokenId } = params; + try { const res = await sendRequest( CDP_GET_MINT_DETAILS, @@ -20,6 +21,7 @@ export async function getMintDetails({ tokenId, }, ], + _referrer, ); if (res.error) { return { diff --git a/src/api/getPortfolios.test.ts b/src/api/getPortfolios.test.ts index 6cc523c1f61..84c88690a58 100644 --- a/src/api/getPortfolios.test.ts +++ b/src/api/getPortfolios.test.ts @@ -50,6 +50,7 @@ describe('getPortfolios', () => { expect(mockSendRequest).toHaveBeenCalledWith( CDP_GET_PORTFOLIO_TOKEN_BALANCES, [{ addresses: mockAddresses }], + 'api', ); }); diff --git a/src/api/getPortfolios.ts b/src/api/getPortfolios.ts index ab3be1f0053..a482bbd770f 100644 --- a/src/api/getPortfolios.ts +++ b/src/api/getPortfolios.ts @@ -1,15 +1,25 @@ import { CDP_GET_PORTFOLIO_TOKEN_BALANCES } from '@/core/network/definitions/wallet'; -import { sendRequest } from '@/core/network/request'; -import type { GetPortfoliosParams, GetPortfoliosResponse } from './types'; +import { type JSONRPCReferrer, sendRequest } from '@/core/network/request'; +import type { + APIError, + GetPortfoliosParams, + GetPortfoliosResponse, +} from './types'; /** * Retrieves the portfolios for the provided addresses */ -export async function getPortfolios({ addresses }: GetPortfoliosParams) { +export async function getPortfolios( + params: GetPortfoliosParams, + _referrer: JSONRPCReferrer = 'api', +): Promise { + const { addresses } = params; + try { const res = await sendRequest( CDP_GET_PORTFOLIO_TOKEN_BALANCES, [{ addresses }], + _referrer, ); if (res.error) { return { diff --git a/src/api/getSwapQuote.test.ts b/src/api/getSwapQuote.test.ts index 27557ba5387..02ae171bfc0 100644 --- a/src/api/getSwapQuote.test.ts +++ b/src/api/getSwapQuote.test.ts @@ -48,9 +48,11 @@ describe('getSwapQuote', () => { const quote = await getSwapQuote(mockParams); expect(quote).toEqual(mockResponse.result); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_QUOTE, + [mockApiParams], + 'api', + ); }); it('should return a quote for a swap with useAggregator=false', async () => { @@ -85,12 +87,16 @@ describe('getSwapQuote', () => { const quote = await getSwapQuote(mockParams); expect(quote).toEqual(mockResponse.result); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ - { - slippagePercentage: '3', - ...mockApiParams, - }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_QUOTE, + [ + { + slippagePercentage: '3', + ...mockApiParams, + }, + ], + 'api', + ); }); it('should return an error for an unsupported amount reference', async () => { @@ -127,9 +133,11 @@ describe('getSwapQuote', () => { message: '', }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_QUOTE, + [mockApiParams], + 'api', + ); }); it('should return an error object from getSwapQuote', async () => { @@ -157,9 +165,11 @@ describe('getSwapQuote', () => { message: '', }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ - mockApiParams, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_QUOTE, + [mockApiParams], + 'api', + ); }); it('should return a SwapError from getSwapQuote for invalid `amount` input', async () => { @@ -207,12 +217,16 @@ describe('getSwapQuote', () => { (sendRequest as Mock).mockResolvedValue(mockResponse); await getSwapQuote(mockParams); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ - { - ...mockApiParams, - slippagePercentage: '30', - }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_QUOTE, + [ + { + ...mockApiParams, + slippagePercentage: '30', + }, + ], + 'api', + ); }); it('should not adjust slippage when useAggregator is false', async () => { @@ -243,12 +257,16 @@ describe('getSwapQuote', () => { (sendRequest as Mock).mockResolvedValue(mockResponse); await getSwapQuote(mockParams); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_QUOTE, [ - { - ...mockApiParams, - v2Enabled: true, - slippagePercentage: '3', - }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_GET_SWAP_QUOTE, + [ + { + ...mockApiParams, + v2Enabled: true, + slippagePercentage: '3', + }, + ], + 'api', + ); }); }); diff --git a/src/api/getSwapQuote.ts b/src/api/getSwapQuote.ts index c34d1c42027..23595378322 100644 --- a/src/api/getSwapQuote.ts +++ b/src/api/getSwapQuote.ts @@ -1,7 +1,7 @@ import { SwapMessage } from '@/swap/constants'; import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants'; import { CDP_GET_SWAP_QUOTE } from '../core/network/definitions/swap'; -import { sendRequest } from '../core/network/request'; +import { type JSONRPCReferrer, sendRequest } from '../core/network/request'; import type { SwapQuote } from '../swap/types'; import { getSwapErrorCode } from '../swap/utils/getSwapErrorCode'; import type { @@ -16,6 +16,7 @@ import { getAPIParamsForToken } from './utils/getAPIParamsForToken'; */ export async function getSwapQuote( params: GetSwapQuoteParams, + _referrer: JSONRPCReferrer = 'api', ): Promise { // Default parameters const defaultParams = { @@ -62,6 +63,7 @@ export async function getSwapQuote( const res = await sendRequest( CDP_GET_SWAP_QUOTE, [apiParams], + _referrer, ); if (res.error) { return { diff --git a/src/api/getTokenDetails.test.ts b/src/api/getTokenDetails.test.ts index 13f243526ec..d11bd345163 100644 --- a/src/api/getTokenDetails.test.ts +++ b/src/api/getTokenDetails.test.ts @@ -39,9 +39,11 @@ describe('getTokenDetails', () => { const result = await getTokenDetails(params); expect(result).toEqual(mockResponse.result); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [ - params, - ]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_GET_TOKEN_DETAILS, + [params], + 'api', + ); }); it('should return error details when request fails with an error', async () => { @@ -61,9 +63,11 @@ describe('getTokenDetails', () => { error: 'Error fetching token details', message: 'Not Found', }); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [ - params, - ]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_GET_TOKEN_DETAILS, + [params], + 'api', + ); }); it('should return uncaught error details when an exception is thrown', async () => { @@ -76,8 +80,10 @@ describe('getTokenDetails', () => { error: 'Something went wrong', message: 'Error fetching token details', }); - expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [ - params, - ]); + expect(mockSendRequest).toHaveBeenCalledWith( + CDP_GET_TOKEN_DETAILS, + [params], + 'api', + ); }); }); diff --git a/src/api/getTokenDetails.ts b/src/api/getTokenDetails.ts index 6e3bc66dc64..040621f706a 100644 --- a/src/api/getTokenDetails.ts +++ b/src/api/getTokenDetails.ts @@ -1,24 +1,30 @@ import { CDP_GET_TOKEN_DETAILS } from '../core/network/definitions/nft'; -import { sendRequest } from '../core/network/request'; +import { type JSONRPCReferrer, sendRequest } from '../core/network/request'; import type { GetTokenDetailsParams, GetTokenDetailsResponse } from './types'; /** * Retrieves token details for an NFT contract and token ID */ -export async function getTokenDetails({ - contractAddress, - tokenId, -}: GetTokenDetailsParams): Promise { +export async function getTokenDetails( + params: GetTokenDetailsParams, + _referrer: JSONRPCReferrer = 'api', +): Promise { + const { contractAddress, tokenId } = params; + try { const res = await sendRequest< GetTokenDetailsParams, GetTokenDetailsResponse - >(CDP_GET_TOKEN_DETAILS, [ - { - contractAddress, - tokenId, - }, - ]); + >( + CDP_GET_TOKEN_DETAILS, + [ + { + contractAddress, + tokenId, + }, + ], + _referrer, + ); if (res.error) { return { code: `${res.error.code}`, diff --git a/src/api/getTokens.test.ts b/src/api/getTokens.test.ts index b3d1c429207..e04c061d0d6 100644 --- a/src/api/getTokens.test.ts +++ b/src/api/getTokens.test.ts @@ -56,9 +56,11 @@ describe('getTokens', () => { }, ]); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_LIST_SWAP_ASSETS, [ - { limit: '50', page: '1' }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_LIST_SWAP_ASSETS, + [{ limit: '50', page: '1' }], + 'api', + ); }); it('should accept options parameters', async () => { @@ -88,9 +90,11 @@ describe('getTokens', () => { }, ]); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_LIST_SWAP_ASSETS, [ - { limit: '1', page: '1' }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_LIST_SWAP_ASSETS, + [{ limit: '1', page: '1' }], + 'api', + ); }); it('should return an error object if sendRequest returns an error', async () => { @@ -109,9 +113,11 @@ describe('getTokens', () => { message: 'Request failed', }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_LIST_SWAP_ASSETS, [ - { limit: '50', page: '1' }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_LIST_SWAP_ASSETS, + [{ limit: '50', page: '1' }], + 'api', + ); }); it('should rethrow the error if an error occurs during token retrieval', async () => { @@ -126,8 +132,10 @@ describe('getTokens', () => { message: 'Request failed', }); expect(sendRequest).toHaveBeenCalledTimes(1); - expect(sendRequest).toHaveBeenCalledWith(CDP_LIST_SWAP_ASSETS, [ - { limit: '50', page: '1' }, - ]); + expect(sendRequest).toHaveBeenCalledWith( + CDP_LIST_SWAP_ASSETS, + [{ limit: '50', page: '1' }], + 'api', + ); }); }); diff --git a/src/api/getTokens.ts b/src/api/getTokens.ts index eb2d38327e1..07f169c8307 100644 --- a/src/api/getTokens.ts +++ b/src/api/getTokens.ts @@ -1,5 +1,5 @@ import { CDP_LIST_SWAP_ASSETS } from '../core/network/definitions/swap'; -import { sendRequest } from '../core/network/request'; +import { type JSONRPCReferrer, sendRequest } from '../core/network/request'; import type { Token } from '../token/types'; import type { GetTokensOptions, GetTokensResponse } from './types'; @@ -8,6 +8,7 @@ import type { GetTokensOptions, GetTokensResponse } from './types'; */ export async function getTokens( options?: GetTokensOptions, + _referrer: JSONRPCReferrer = 'api', ): Promise { // Default filter values const defaultFilter: GetTokensOptions = { @@ -20,6 +21,7 @@ export async function getTokens( const res = await sendRequest( CDP_LIST_SWAP_ASSETS, [filters], + _referrer, ); if (res.error) { return { diff --git a/src/buy/components/BuyProvider.tsx b/src/buy/components/BuyProvider.tsx index b12fdef9e76..0e6627142d6 100644 --- a/src/buy/components/BuyProvider.tsx +++ b/src/buy/components/BuyProvider.tsx @@ -351,14 +351,17 @@ export function BuyProvider({ try { const maxSlippage = lifecycleStatus.statusData.maxSlippage; - const response = await buildSwapTransaction({ - amount: from.amount, - fromAddress: address, - from: from.token, - maxSlippage: String(maxSlippage), - to: to.token, - useAggregator, - }); + const response = await buildSwapTransaction( + { + amount: from.amount, + fromAddress: address, + from: from.token, + maxSlippage: String(maxSlippage), + to: to.token, + useAggregator, + }, + 'buy', + ); if (isSwapError(response)) { updateLifecycleStatus({ statusName: 'error', diff --git a/src/buy/utils/getBuyQuote.test.ts b/src/buy/utils/getBuyQuote.test.ts index 779b76d2625..f4a430e2317 100644 --- a/src/buy/utils/getBuyQuote.test.ts +++ b/src/buy/utils/getBuyQuote.test.ts @@ -100,14 +100,17 @@ describe('getBuyQuote', () => { fromSwapUnit: mockFromSwapUnit, }); - expect(getSwapQuote).toHaveBeenCalledWith({ - amount: '1', - amountReference: 'from', - from: toToken, - maxSlippage: '0.5', - to: fromToken, - useAggregator: true, - }); + expect(getSwapQuote).toHaveBeenCalledWith( + { + amount: '1', + amountReference: 'from', + from: toToken, + maxSlippage: '0.5', + to: fromToken, + useAggregator: true, + }, + 'buy', + ); expect(formatTokenAmount).toHaveBeenCalledWith('100000000000000000', 18); expect(mockFromSwapUnit.setAmountUSD).toHaveBeenCalledWith('100'); @@ -134,14 +137,17 @@ describe('getBuyQuote', () => { fromSwapUnit: mockFromSwapUnit, }); - expect(getSwapQuote).toHaveBeenCalledWith({ - amount: '1', - amountReference: 'from', - from: toToken, - maxSlippage: '0.5', - to: fromToken, - useAggregator: true, - }); + expect(getSwapQuote).toHaveBeenCalledWith( + { + amount: '1', + amountReference: 'from', + from: toToken, + maxSlippage: '0.5', + to: fromToken, + useAggregator: true, + }, + 'buy', + ); expect(formatTokenAmount).not.toHaveBeenCalled(); expect(mockFromSwapUnit.setAmountUSD).toHaveBeenCalledWith(''); diff --git a/src/buy/utils/getBuyQuote.ts b/src/buy/utils/getBuyQuote.ts index b7c33bbd096..d27a0995542 100644 --- a/src/buy/utils/getBuyQuote.ts +++ b/src/buy/utils/getBuyQuote.ts @@ -38,14 +38,17 @@ export async function getBuyQuote({ // switching to and from here // instead of getting a quote for how much of X do we need to sell to get the input token amount // we can get a quote for how much of X we will recieve if we sell the input token amount - response = await getSwapQuote({ - amount, - amountReference: 'from', - from: to, - maxSlippage, - to: from, - useAggregator, - }); + response = await getSwapQuote( + { + amount, + amountReference: 'from', + from: to, + maxSlippage, + to: from, + useAggregator, + }, + 'buy', + ); } let formattedFromAmount = ''; diff --git a/src/checkout/utils/handlePayRequest.test.ts b/src/checkout/utils/handlePayRequest.test.ts index bc3056b3b37..ec7011350fc 100644 --- a/src/checkout/utils/handlePayRequest.test.ts +++ b/src/checkout/utils/handlePayRequest.test.ts @@ -21,10 +21,13 @@ describe('handlePayRequest', () => { chargeHandler: mockChargeHandler, }); expect(mockChargeHandler).toHaveBeenCalled(); - expect(buildPayTransaction).toHaveBeenCalledWith({ - address: mockAddress, - chargeId: mockChargeId, - }); + expect(buildPayTransaction).toHaveBeenCalledWith( + { + address: mockAddress, + chargeId: mockChargeId, + }, + 'checkout', + ); expect(result).toEqual(mockResponse); }); @@ -36,10 +39,13 @@ describe('handlePayRequest', () => { address: mockAddress, productId: mockProductId, }); - expect(buildPayTransaction).toHaveBeenCalledWith({ - address: mockAddress, - productId: mockProductId, - }); + expect(buildPayTransaction).toHaveBeenCalledWith( + { + address: mockAddress, + productId: mockProductId, + }, + 'checkout', + ); expect(result).toEqual(mockResponse); }); @@ -47,7 +53,10 @@ describe('handlePayRequest', () => { const mockResponse = { success: true }; (buildPayTransaction as Mock).mockResolvedValue(mockResponse); const result = await handlePayRequest({ address: mockAddress }); - expect(buildPayTransaction).toHaveBeenCalledWith({ address: mockAddress }); + expect(buildPayTransaction).toHaveBeenCalledWith( + { address: mockAddress }, + 'checkout', + ); expect(result).toEqual(mockResponse); }); diff --git a/src/checkout/utils/handlePayRequest.ts b/src/checkout/utils/handlePayRequest.ts index 61ffbdefa05..a177946b88b 100644 --- a/src/checkout/utils/handlePayRequest.ts +++ b/src/checkout/utils/handlePayRequest.ts @@ -17,7 +17,10 @@ export const handlePayRequest = async ({ buildPayTransactionParams.productId = productId; } - const response = await buildPayTransaction(buildPayTransactionParams); + const response = await buildPayTransaction( + buildPayTransactionParams, + 'checkout', + ); if ('error' in response) { throw new Error(response.error); diff --git a/src/core/network/constants.ts b/src/core/network/constants.ts index d54d17fac7e..5b619166260 100644 --- a/src/core/network/constants.ts +++ b/src/core/network/constants.ts @@ -7,3 +7,12 @@ export const JSON_HEADERS = { }; export const JSON_RPC_VERSION = '2.0'; export const ANALYTICS_API_URL = 'https://api.developer.coinbase.com/analytics'; +export const ALLOWABLE_REFERRERS = [ + 'api', + 'buy', + 'checkout', + 'hook', + 'nft', + 'swap', + 'wallet', +]; diff --git a/src/core/network/request.test.ts b/src/core/network/request.test.ts index 49c228e2c32..d7474e754d8 100644 --- a/src/core/network/request.test.ts +++ b/src/core/network/request.test.ts @@ -54,6 +54,78 @@ describe('request', () => { expect(response).toEqual(mockResponse); }); + it('should set the Onchainkit-Referrer if one is set', async () => { + const mockResponse = { + jsonrpc: '2.0', + result: 'exampleResult', + id: 1, + }; + const mockFetch = vi.fn().mockResolvedValue({ + json: vi.fn().mockResolvedValue(mockResponse), + }); + global.fetch = mockFetch; + + const requestBody = { + id: 1, + jsonrpc: '2.0', + method: 'exampleMethod', + params: ['param1', 'param2'], + }; + + const response = await sendRequest( + 'exampleMethod', + ['param1', 'param2'], + 'api', + ); + + expect(mockFetch).toHaveBeenCalledWith(expect.any(String), { + method: 'POST', + body: JSON.stringify(requestBody), + headers: { + 'Content-Type': 'application/json', + 'OnchainKit-Version': version, + 'OnchainKit-Referrer': 'api', + }, + }); + expect(response).toEqual(mockResponse); + }); + + it('should default to api if an invalid referrer is set', async () => { + const mockResponse = { + jsonrpc: '2.0', + result: 'exampleResult', + id: 1, + }; + const mockFetch = vi.fn().mockResolvedValue({ + json: vi.fn().mockResolvedValue(mockResponse), + }); + global.fetch = mockFetch; + + const requestBody = { + id: 1, + jsonrpc: '2.0', + method: 'exampleMethod', + params: ['param1', 'param2'], + }; + + const response = await sendRequest( + 'exampleMethod', + ['param1', 'param2'], + 'fake', + ); + + expect(mockFetch).toHaveBeenCalledWith(expect.any(String), { + method: 'POST', + body: JSON.stringify(requestBody), + headers: { + 'Content-Type': 'application/json', + 'OnchainKit-Version': version, + 'OnchainKit-Referrer': 'api', + }, + }); + expect(response).toEqual(mockResponse); + }); + it('should throw an error if an error occurs while sending the request', async () => { const mockError = new Error('Example error'); const mockFetch = vi.fn().mockRejectedValue(mockError); diff --git a/src/core/network/request.ts b/src/core/network/request.ts index 7e81e8115f2..dcaae45a2f7 100644 --- a/src/core/network/request.ts +++ b/src/core/network/request.ts @@ -1,4 +1,9 @@ -import { JSON_HEADERS, JSON_RPC_VERSION, POST_METHOD } from './constants'; +import { + ALLOWABLE_REFERRERS, + JSON_HEADERS, + JSON_RPC_VERSION, + POST_METHOD, +} from './constants'; import { getRPCUrl } from './getRPCUrl'; export type JSONRPCError = { @@ -20,6 +25,8 @@ export type JSONRPCResult = { result: T; }; +export type JSONRPCReferrer = (typeof ALLOWABLE_REFERRERS)[number]; + /** * Builds a JSON-RPC request body. * @@ -40,24 +47,52 @@ export function buildRequestBody( }; } +/** + * Builds the headers for a JSON-RPC request. + * + * @params referrer - The referrer to add to the header + * @returns The headers for the JSON-RPC request. + */ +export function buildRequestHeaders( + referrer?: JSONRPCReferrer, +): Record { + if (referrer) { + // if an invalid referrer is provided, default to 'api' + if (!ALLOWABLE_REFERRERS.includes(referrer)) { + return { + ...JSON_HEADERS, + 'OnchainKit-Referrer': 'api', + }; + } + + return { + ...JSON_HEADERS, + 'OnchainKit-Referrer': referrer, + }; + } + return JSON_HEADERS; +} + /** * Sends a JSON-RPC request to configured RPC URL. * Defaults to using the Coinbase Developer Platform Node. * - * @param body - The JSON-RPC request body. + * @param method - The method name. + * @param params - The parameters for the method. * @returns A promise that resolves to the JSON-RPC response. * @throws If an error occurs while sending the request. */ export async function sendRequest( method: string, params: T[], + referrer?: JSONRPCReferrer, ): Promise> { try { const body = buildRequestBody(method, params); const url = getRPCUrl(); const response = await fetch(url, { body: JSON.stringify(body), - headers: JSON_HEADERS, + headers: buildRequestHeaders(referrer), method: POST_METHOD, }); const data: JSONRPCResult = await response.json(); diff --git a/src/internal/hooks/useExchangeRate.tsx b/src/internal/hooks/useExchangeRate.tsx index fe48f27915e..c19db3edcf6 100644 --- a/src/internal/hooks/useExchangeRate.tsx +++ b/src/internal/hooks/useExchangeRate.tsx @@ -32,12 +32,15 @@ export async function useExchangeRate({ const toToken = selectedInputType === 'crypto' ? usdcToken : token; try { - const response = await getSwapQuote({ - amount: '1', // hardcoded amount of 1 because we only need the exchange rate - from: fromToken, - to: toToken, - useAggregator: false, - }); + const response = await getSwapQuote( + { + amount: '1', // hardcoded amount of 1 because we only need the exchange rate + from: fromToken, + to: toToken, + useAggregator: false, + }, + 'wallet', + ); if (isApiError(response)) { console.error('Error fetching exchange rate:', response.error); return; diff --git a/src/nft/hooks/useMintData.ts b/src/nft/hooks/useMintData.ts index 6dd19221beb..7d3f40a0ca1 100644 --- a/src/nft/hooks/useMintData.ts +++ b/src/nft/hooks/useMintData.ts @@ -23,11 +23,14 @@ export function useMintData( } }, [error, updateLifecycleStatus]); - const { error: mintError, data: mintData } = useMintDetails({ - contractAddress, - takerAddress: address, - ...(tokenId ? { tokenId } : {}), - }); + const { error: mintError, data: mintData } = useMintDetails( + { + contractAddress, + takerAddress: address, + ...(tokenId ? { tokenId } : {}), + }, + 'nft', + ); if (mintError && !error) { setError({ diff --git a/src/nft/hooks/useMintDetails.ts b/src/nft/hooks/useMintDetails.ts index e4a4efb8f70..38abc461eda 100644 --- a/src/nft/hooks/useMintDetails.ts +++ b/src/nft/hooks/useMintDetails.ts @@ -1,23 +1,27 @@ import { getMintDetails } from '@/api/getMintDetails'; import type { MintDetails } from '@/api/types'; +import type { JSONRPCReferrer } from '@/core/network/request'; import { isNFTError } from '@/nft/utils/isNFTError'; import { type UseQueryResult, useQuery } from '@tanstack/react-query'; import type { UseMintDetailsParams } from '../types'; -export function useMintDetails({ - contractAddress, - takerAddress, - tokenId, - queryOptions, -}: UseMintDetailsParams): UseQueryResult { +export function useMintDetails( + params: UseMintDetailsParams, + _referrer: JSONRPCReferrer = 'hook', +): UseQueryResult { + const { contractAddress, takerAddress, tokenId, queryOptions } = params; + return useQuery({ queryKey: ['useMintDetails', contractAddress, takerAddress, tokenId], queryFn: async () => { - const mintDetails = await getMintDetails({ - contractAddress, - takerAddress, - tokenId, - }); + const mintDetails = await getMintDetails( + { + contractAddress, + takerAddress, + tokenId, + }, + _referrer, + ); if (isNFTError(mintDetails)) { throw mintDetails; diff --git a/src/nft/hooks/useNFTData.ts b/src/nft/hooks/useNFTData.ts index fe1babcae5a..82a2e17db65 100644 --- a/src/nft/hooks/useNFTData.ts +++ b/src/nft/hooks/useNFTData.ts @@ -21,10 +21,13 @@ export function useNFTData( } }, [error, updateLifecycleStatus]); - const { error: tokenError, data: tokenDetails } = useTokenDetails({ - contractAddress, - tokenId: tokenId, - }); + const { error: tokenError, data: tokenDetails } = useTokenDetails( + { + contractAddress, + tokenId: tokenId, + }, + 'nft', + ); if (tokenError && !error) { setError({ diff --git a/src/nft/hooks/useTokenDetails.ts b/src/nft/hooks/useTokenDetails.ts index d93519179f1..e0b715e5094 100644 --- a/src/nft/hooks/useTokenDetails.ts +++ b/src/nft/hooks/useTokenDetails.ts @@ -1,21 +1,26 @@ import { getTokenDetails } from '@/api/getTokenDetails'; import type { TokenDetails } from '@/api/types'; +import type { JSONRPCReferrer } from '@/core/network/request'; import { isNFTError } from '@/nft/utils/isNFTError'; import { type UseQueryResult, useQuery } from '@tanstack/react-query'; import type { UseTokenDetailsParams } from '../types'; -export function useTokenDetails({ - contractAddress, - tokenId, - queryOptions, -}: UseTokenDetailsParams): UseQueryResult { +export function useTokenDetails( + params: UseTokenDetailsParams, + _referrer: JSONRPCReferrer = 'hook', +): UseQueryResult { + const { contractAddress, tokenId, queryOptions } = params; + return useQuery({ queryKey: ['useTokenDetails', contractAddress, tokenId], queryFn: async () => { - const tokenDetails = await getTokenDetails({ - contractAddress, - tokenId, - }); + const tokenDetails = await getTokenDetails( + { + contractAddress, + tokenId, + }, + _referrer, + ); if (isNFTError(tokenDetails)) { throw tokenDetails; diff --git a/src/nft/utils/buildMintTransactionData.ts b/src/nft/utils/buildMintTransactionData.ts index ef785b5bbd1..b4c8809ee9e 100644 --- a/src/nft/utils/buildMintTransactionData.ts +++ b/src/nft/utils/buildMintTransactionData.ts @@ -10,13 +10,16 @@ async function getMintTransaction({ quantity, takerAddress, }: BuildMintTransactionParams): Promise { - const mintTransactions = await buildMintTransactionApi({ - mintAddress, - tokenId, - network, - quantity, - takerAddress, - }); + const mintTransactions = await buildMintTransactionApi( + { + mintAddress, + tokenId, + network, + quantity, + takerAddress, + }, + 'nft', + ); if ('error' in mintTransactions) { throw mintTransactions.message; diff --git a/src/swap/components/SwapProvider.test.tsx b/src/swap/components/SwapProvider.test.tsx index 70b8d8b2211..6ecdc67d6fc 100644 --- a/src/swap/components/SwapProvider.test.tsx +++ b/src/swap/components/SwapProvider.test.tsx @@ -594,6 +594,7 @@ describe('SwapProvider', () => { to: DEGEN_TOKEN, useAggregator: true, }), + 'swap', ); }); @@ -621,6 +622,7 @@ describe('SwapProvider', () => { to: DEGEN_TOKEN, useAggregator: true, }), + 'swap', ); }); diff --git a/src/swap/components/SwapProvider.tsx b/src/swap/components/SwapProvider.tsx index d9d1d328f7e..62dfdeffff0 100644 --- a/src/swap/components/SwapProvider.tsx +++ b/src/swap/components/SwapProvider.tsx @@ -239,14 +239,17 @@ export function SwapProvider({ try { const maxSlippage = lifecycleStatus.statusData.maxSlippage; - const response = await getSwapQuote({ - amount, - amountReference: 'from', - from: source.token, - maxSlippage: String(maxSlippage), - to: destination.token, - useAggregator, - }); + const response = await getSwapQuote( + { + amount, + amountReference: 'from', + from: source.token, + maxSlippage: String(maxSlippage), + to: destination.token, + useAggregator, + }, + 'swap', + ); // If request resolves to error response set the quoteError // property of error state to the SwapError response if (isSwapError(response)) { @@ -304,14 +307,17 @@ export function SwapProvider({ try { const maxSlippage = lifecycleStatus.statusData.maxSlippage; - const response = await buildSwapTransaction({ - amount: from.amount, - fromAddress: address, - from: from.token, - maxSlippage: String(maxSlippage), - to: to.token, - useAggregator, - }); + const response = await buildSwapTransaction( + { + amount: from.amount, + fromAddress: address, + from: from.token, + maxSlippage: String(maxSlippage), + to: to.token, + useAggregator, + }, + 'swap', + ); if (isSwapError(response)) { updateLifecycleStatus({ statusName: 'error', diff --git a/src/wallet/components/WalletAdvancedProvider.test.tsx b/src/wallet/components/WalletAdvancedProvider.test.tsx index 794a0c2d692..9fff4bcf032 100644 --- a/src/wallet/components/WalletAdvancedProvider.test.tsx +++ b/src/wallet/components/WalletAdvancedProvider.test.tsx @@ -103,9 +103,12 @@ describe('useWalletAdvancedContext', () => { wrapper: WalletAdvancedProvider, }); - expect(mockUsePortfolio).toHaveBeenCalledWith({ - address: null, - }); + expect(mockUsePortfolio).toHaveBeenCalledWith( + { + address: null, + }, + 'wallet', + ); mockUseWalletContext.mockReturnValue({ address: '0x123', @@ -114,9 +117,12 @@ describe('useWalletAdvancedContext', () => { rerender(); - expect(mockUsePortfolio).toHaveBeenCalledWith({ - address: '0x123', - }); + expect(mockUsePortfolio).toHaveBeenCalledWith( + { + address: '0x123', + }, + 'wallet', + ); }); describe('getAnimations', () => { diff --git a/src/wallet/components/WalletAdvancedProvider.tsx b/src/wallet/components/WalletAdvancedProvider.tsx index bd88debc12d..b871e27252b 100644 --- a/src/wallet/components/WalletAdvancedProvider.tsx +++ b/src/wallet/components/WalletAdvancedProvider.tsx @@ -37,7 +37,7 @@ export function WalletAdvancedProvider({ refetch: refetchPortfolioData, isFetching: isFetchingPortfolioData, dataUpdatedAt: portfolioDataUpdatedAt, - } = usePortfolio({ address }); + } = usePortfolio({ address }, 'wallet'); const portfolioFiatValue = portfolioData?.portfolioBalanceInUsd; const tokenBalances = portfolioData?.tokenBalances; diff --git a/src/wallet/hooks/usePortfolio.test.tsx b/src/wallet/hooks/usePortfolio.test.tsx index 5e953d908cd..f8534a722e4 100644 --- a/src/wallet/hooks/usePortfolio.test.tsx +++ b/src/wallet/hooks/usePortfolio.test.tsx @@ -58,9 +58,12 @@ describe('usePortfolio', () => { await waitFor(() => expect(result.current.isSuccess).toBe(true)); - expect(getPortfolios).toHaveBeenCalledWith({ - addresses: [mockAddress], - }); + expect(getPortfolios).toHaveBeenCalledWith( + { + addresses: [mockAddress], + }, + 'hook', + ); expect(result.current.data).toEqual(mockPortfolio); }); diff --git a/src/wallet/hooks/usePortfolio.ts b/src/wallet/hooks/usePortfolio.ts index b180f617954..17faa4bdc4b 100644 --- a/src/wallet/hooks/usePortfolio.ts +++ b/src/wallet/hooks/usePortfolio.ts @@ -1,5 +1,6 @@ import { getPortfolios } from '@/api/getPortfolios'; import type { Portfolio } from '@/api/types'; +import type { JSONRPCReferrer } from '@/core/network/request'; import { isApiError } from '@/internal/utils/isApiResponseError'; import { type UseQueryResult, useQuery } from '@tanstack/react-query'; import type { Address } from 'viem'; @@ -8,17 +9,19 @@ import type { Address } from 'viem'; * Retrieves the portfolio for the provided address * portfolio includes the address, the balance of the address in USD, and the tokens in the address */ -export function usePortfolio({ - address, -}: { - address: Address | undefined | null; -}): UseQueryResult { +export function usePortfolio( + { address }: { address: Address | undefined | null }, + _referrer: JSONRPCReferrer = 'hook', +): UseQueryResult { return useQuery({ queryKey: ['usePortfolio', address], queryFn: async () => { - const response = await getPortfolios({ - addresses: [address as Address], // Safe to coerce to Address because useQuery's enabled flag will prevent the query from running if address is undefined - }); + const response = await getPortfolios( + { + addresses: [address as Address], // Safe to coerce to Address because useQuery's enabled flag will prevent the query from running if address is undefined + }, + _referrer, + ); if (isApiError(response)) { throw new Error(response.message);