Skip to content

Commit

Permalink
finish query?
Browse files Browse the repository at this point in the history
  • Loading branch information
benisgold committed Aug 29, 2024
1 parent 3f0346b commit 646aa94
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 103 deletions.
127 changes: 24 additions & 103 deletions src/resources/claimables/claimablesQuery.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -14,99 +15,6 @@ const addysHttp = new RainbowFetchClient({
},
});

interface BridgeableNetwork {
bridgeable: boolean;
}

interface TokenBridging {
bridgeable: boolean;
networks: Record<ChainId, BridgeableNetwork>;
}

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<ChainId, TokenMapping>;
// 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

Expand All @@ -128,14 +36,27 @@ type ClaimablesQueryKey = ReturnType<typeof claimablesQueryKey>;
// Query Function

async function claimablesQueryFunction({ queryKey: [{ address, currency, testnetMode }] }: QueryFunctionArgs<typeof claimablesQueryKey>) {
const url = `/${SUPPORTED_CHAIN_IDS({ testnetMode }).join(',')}/${address}/claimables`;
const { data } = await addysHttp.get<Claimables>(url, {
params: {
currency: currency.toLowerCase(),
},
timeout: 20000,
});
return data;
try {
const url = `/${SUPPORTED_CHAIN_IDS({ testnetMode }).join(',')}/${address}/claimables`;
const { data } = await addysHttp.get<ConsolidatedClaimablesResponse>(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<typeof claimablesQueryFunction>;
Expand Down
111 changes: 111 additions & 0 deletions src/resources/claimables/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { ChainId } from '@rainbow-me/swaps';
import { Address } from 'viem';

interface BridgeableNetwork {
bridgeable: boolean;
}

interface TokenBridging {
bridgeable: boolean;
networks: Record<ChainId, BridgeableNetwork>;
}

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<ChainId, TokenMapping>;
// 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;
}

0 comments on commit 646aa94

Please sign in to comment.