From 646aa94a592d21bf2695e4fbf1728253eebb86a4 Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Thu, 29 Aug 2024 15:10:03 -0400 Subject: [PATCH] finish query? --- src/resources/claimables/claimablesQuery.ts | 127 ++++---------------- src/resources/claimables/types.ts | 111 +++++++++++++++++ 2 files changed, 135 insertions(+), 103 deletions(-) create mode 100644 src/resources/claimables/types.ts diff --git a/src/resources/claimables/claimablesQuery.ts b/src/resources/claimables/claimablesQuery.ts index af735fc1ed4..715296dd01d 100644 --- a/src/resources/claimables/claimablesQuery.ts +++ b/src/resources/claimables/claimablesQuery.ts @@ -1,11 +1,12 @@ import { NativeCurrencyKey } from '@/entities'; import { RainbowFetchClient } from '@/rainbow-fetch'; -import { QueryConfigWithSelect, QueryFunctionArgs, QueryFunctionResult, createQueryKey } from '@/react-query'; +import { QueryConfigWithSelect, QueryFunctionArgs, QueryFunctionResult, createQueryKey, queryClient } from '@/react-query'; import { SUPPORTED_CHAIN_IDS } from '@/references'; -import { ChainId } from '@rainbow-me/swaps'; import { useQuery } from '@tanstack/react-query'; import { ADDYS_API_KEY } from 'react-native-dotenv'; import { Address } from 'viem'; +import { ConsolidatedClaimablesResponse } from './types'; +import { logger, RainbowError } from '@/logger'; const addysHttp = new RainbowFetchClient({ baseURL: 'https://addys.p.rainbow.me/v3', @@ -14,99 +15,6 @@ const addysHttp = new RainbowFetchClient({ }, }); -interface BridgeableNetwork { - bridgeable: boolean; -} - -interface TokenBridging { - bridgeable: boolean; - networks: Record; -} - -interface TokenMapping { - address: string; - decimals: number; -} - -interface Colors { - primary: string; - fallback: string; - shadow: string; -} - -interface Price { - value: number; - changed_at: number; - relative_change_24h: number; -} - -interface ClaimAction { - address_to: string; - calldata: string; - chain_id: ChainId; -} - -interface DApp { - name: string; - url: string; - icon_url: string; - colors: Colors; -} - -interface Asset { - asset_code: string; - decimals: number; - icon_url: string; - name: string; - network?: string; - chain_id?: ChainId; - price: Price; - symbol: string; - type?: string; - interface?: string; - colors?: Colors; - networks?: Record; - // Adding as pointer to avoid showing on NFTs - bridging?: TokenBridging | null; - // To avoid zerion from filtering assets themselves, we add this internal flag to verify them ourselves - probable_spam: boolean; - // New field to handle ERC-721 and ERC-1155 token ids - token_id?: string; - // For ERC-20 tokens, we show the verified status - verified?: boolean; - // Mark defi position based on token type - defi_position?: boolean; - // Transferable Making it a pointer so NFTs doesn't show this field - transferable?: boolean | null; - creation_date?: Date | null; -} - -interface Claimable { - name: string; - type: string; - network: ChainId; - asset: Asset; - amout: string; - dapp: DApp; - claim_action_type?: string | null; - claim_action?: ClaimAction[]; -} - -interface Claimables { - metadata: { - addresses: Address[]; - currency: string; - chain_ids: ChainId[]; - errors: []; - addresses_with_errors: []; - chain_ids_with_errors: []; - status: string; - }; - payload: { - claimables: Claimable[]; - }; -} - // /////////////////////////////////////////////// // Query Types @@ -128,14 +36,27 @@ type ClaimablesQueryKey = ReturnType; // Query Function async function claimablesQueryFunction({ queryKey: [{ address, currency, testnetMode }] }: QueryFunctionArgs) { - const url = `/${SUPPORTED_CHAIN_IDS({ testnetMode }).join(',')}/${address}/claimables`; - const { data } = await addysHttp.get(url, { - params: { - currency: currency.toLowerCase(), - }, - timeout: 20000, - }); - return data; + try { + const url = `/${SUPPORTED_CHAIN_IDS({ testnetMode }).join(',')}/${address}/claimables`; + const { data } = await addysHttp.get(url, { + params: { + currency: currency.toLowerCase(), + }, + timeout: 20000, + }); + + if (data.metadata.status !== 'ok') { + logger.error(new RainbowError('[userAssetsQueryFunction]: Failed to fetch user assets (API error)'), { + message: data.metadata.errors, + }); + } + + return data.payload.claimables; + } catch (e) { + logger.error(new RainbowError('[userAssetsQueryFunction]: Failed to fetch user assets (client error)'), { + message: (e as Error)?.message, + }); + } } type ClaimablesResult = QueryFunctionResult; diff --git a/src/resources/claimables/types.ts b/src/resources/claimables/types.ts new file mode 100644 index 00000000000..2c4b0d3f3c6 --- /dev/null +++ b/src/resources/claimables/types.ts @@ -0,0 +1,111 @@ +import { ChainId } from '@rainbow-me/swaps'; +import { Address } from 'viem'; + +interface BridgeableNetwork { + bridgeable: boolean; +} + +interface TokenBridging { + bridgeable: boolean; + networks: Record; +} + +interface TokenMapping { + address: Address; + decimals: number; +} + +interface Colors { + primary: string; + fallback: string; + shadow: string; +} + +interface Price { + value: number; + changed_at: number; + relative_change_24h: number; +} + +interface ClaimAction { + address_to: Address; + calldata: string; + chain_id: ChainId; +} + +interface DApp { + name: string; + url: string; + icon_url: string; + colors: Colors; +} + +interface Asset { + asset_code: string; + decimals: number; + icon_url: string; + name: string; + network?: string; + chain_id?: ChainId; + price: Price; + symbol: string; + type?: string; + interface?: string; + colors?: Colors; + networks?: Record; + // Adding as pointer to avoid showing on NFTs + bridging?: TokenBridging | null; + // To avoid zerion from filtering assets themselves, we add this internal flag to verify them ourselves + probable_spam: boolean; + // New field to handle ERC-721 and ERC-1155 token ids + token_id?: string; + // For ERC-20 tokens, we show the verified status + verified?: boolean; + // Mark defi position based on token type + defi_position?: boolean; + // Transferable Making it a pointer so NFTs doesn't show this field + transferable?: boolean | null; + creation_date?: Date | null; +} + +export interface Claimable { + name: string; + type: string; + network: ChainId; + asset: Asset; + amout: string; + dapp: DApp; + claim_action_type?: string | null; + claim_action?: ClaimAction[]; +} + +interface ConsolidatedClaimablesPayloadResponse { + claimables: Claimable[]; +} + +type Status = 'ok' | 'still_indexing' | 'not_found' | 'pending' | 'error'; + +interface ConsolidatedChainIDError { + chain_id: ChainId; + error: string; +} + +interface ConsolidatedError { + address: Address; + errors: ConsolidatedChainIDError[]; +} + +interface ConsolidatedClaimablesMetadataResponse { + addresses: Address[]; + currency: string; + chain_ids: ChainId[]; + errors: ConsolidatedError[]; + addresses_with_errors: Address[]; + chain_ids_with_errors: ChainId[]; + status: Status; +} + +export interface ConsolidatedClaimablesResponse { + metadata: ConsolidatedClaimablesMetadataResponse; + payload: ConsolidatedClaimablesPayloadResponse; +}