Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/rainbow-me/rainbow into …
Browse files Browse the repository at this point in the history
…brody/QR-code-scanner-fixes

* 'develop' of https://github.com/rainbow-me/rainbow:
  [APP-379]: Update client to use new token search aggregator across networks (#5190)
  tx sim: error handling for unknown urls (#5213)
  handle hex tx types (#5214)
  chore: supply metadata graphql api key (#5211)
  • Loading branch information
BrodyHughes committed Dec 1, 2023
2 parents 2bd14cc + e85c3fe commit 7011a61
Show file tree
Hide file tree
Showing 10 changed files with 805 additions and 26 deletions.
1 change: 1 addition & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ declare module 'react-native-dotenv' {
export const LOG_DEBUG: string;
export const QUIET_OLD_LOGGER: string;
export const ARC_GRAPHQL_API_KEY: string;
export const METADATA_GRAPHQL_API_KEY: string;
export const RESERVOIR_API_KEY_PROD: string;
export const RESERVOIR_API_KEY_DEV: string;
export const RPC_PROXY_BASE_URL_PROD: string;
Expand Down
24 changes: 24 additions & 0 deletions src/entities/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChainId } from '@rainbow-me/swaps';
import { AssetType } from './assetTypes';
import { EthereumAddress } from '.';
import { Network } from '@/helpers';
import { Chain } from '@wagmi/chains';

export interface ZerionAssetPrice {
value: number;
Expand Down Expand Up @@ -88,6 +89,29 @@ export interface SwappableAsset extends ParsedAddressAsset {
network?: Network;
}

export interface TokenSearchNetwork {
address: string;
decimals: number;
}

export interface TokenSearchToken {
decimals: number;
highLiquidity: boolean;
name: string;
symbol: string;
uniqueId: string;
colors: { primary: string; fallback: string };
icon_url: string;
color: string;
shadowColor: string;
rainbowMetadataId: number;
isRainbowCurated: boolean;
isVerified: boolean;
networks: {
[chainId in Chain['id']]: TokenSearchNetwork;
};
}

export interface RainbowToken extends Asset {
color?: string;
highLiquidity?: boolean;
Expand Down
15 changes: 14 additions & 1 deletion src/graphql/utils/getFetchRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { rainbowFetch, RainbowFetchRequestOpts } from '@/rainbow-fetch';
import { DocumentNode } from 'graphql';
import { resolveRequestDocument } from 'graphql-request';
import { buildGetQueryParams } from '@/graphql/utils/buildGetQueryParams';
import { ARC_GRAPHQL_API_KEY } from 'react-native-dotenv';
import {
ARC_GRAPHQL_API_KEY,
METADATA_GRAPHQL_API_KEY,
} from 'react-native-dotenv';

const allowedOperations = ['mutation', 'query'];

Expand Down Expand Up @@ -40,6 +43,16 @@ const additionalConfig: {
'x-api-key': ARC_GRAPHQL_API_KEY,
},
},
metadata: {
headers: {
Authorization: `Bearer ${METADATA_GRAPHQL_API_KEY}`,
},
},
simulation: {
headers: {
Authorization: `Bearer ${METADATA_GRAPHQL_API_KEY}`,
},
},
};

export function getFetchRequester(config: Config) {
Expand Down
67 changes: 66 additions & 1 deletion src/handlers/tokenSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import {
} from '@/entities';
import { logger, RainbowError } from '@/logger';
import { EthereumAddress } from '@rainbow-me/swaps';
import { RainbowToken, TokenSearchToken } from '@/entities/tokens';
import ethereumUtils from '@/utils/ethereumUtils';

type TokenSearchApiResponse = {
data: TokenSearchToken[];
};

const tokenSearchApi = new RainbowFetchClient({
baseURL: 'https://token-search.rainbow.me/v2',
Expand All @@ -18,7 +24,7 @@ const tokenSearchApi = new RainbowFetchClient({
timeout: 30000,
});

export const tokenSearch = async (searchParams: {
export const swapSearch = async (searchParams: {
chainId: number;
fromChainId?: number | '';
keys: TokenSearchUniswapAssetKey[];
Expand Down Expand Up @@ -60,6 +66,65 @@ export const tokenSearch = async (searchParams: {
}
};

export const tokenSearch = async (searchParams: {
chainId: number;
fromChainId?: number | '';
keys: TokenSearchUniswapAssetKey[];
list: TokenSearchTokenListId;
threshold: TokenSearchThreshold;
query: string;
}): Promise<RainbowToken[]> => {
const queryParams: {
keys: TokenSearchUniswapAssetKey[];
list: TokenSearchTokenListId;
threshold: TokenSearchThreshold;
query?: string;
fromChainId?: number;
} = {
keys: searchParams.keys,
list: searchParams.list,
threshold: searchParams.threshold,
query: searchParams.query,
};

try {
if (isAddress(searchParams.query)) {
// @ts-ignore
params.keys = `networks.${params.chainId}.address`;
}
const url = `/?${qs.stringify(queryParams)}`;
const tokenSearch = await tokenSearchApi.get<TokenSearchApiResponse>(url);
if (!tokenSearch.data?.data) {
return [];
}

return tokenSearch.data.data.map(token => {
const networkKeys = Object.keys(token.networks);
const type = ethereumUtils.getAssetTypeFromNetwork(
ethereumUtils.getNetworkFromChainId(Number(networkKeys[0]))
);
return {
...token,
address:
token.networks['1']?.address ||
token.networks[Number(networkKeys[0])]?.address,
type,
mainnet_address: token.networks['1']?.address,
};
});
} catch (e: any) {
logger.error(
new RainbowError(`An error occurred while searching for query`),
{
query: searchParams.query,
message: e.message,
}
);

return [];
}
};

export const walletFilter = async (params: {
addresses: EthereumAddress[];
fromChainId: number;
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export {
useHardwareBackOnFocus,
} from './useHardwareBack';
export { default as useSwapCurrencyList } from './useSwapCurrencyList';
export { default as useSearchCurrencyList } from './useSearchCurrencyList';
export { default as useWalletENSAvatar } from './useWalletENSAvatar';
export { default as useImagePicker } from './useImagePicker';
export { default as useLatestCallback } from './useLatestCallback';
Expand Down
Loading

0 comments on commit 7011a61

Please sign in to comment.