Skip to content

Commit

Permalink
fix: enhanced provider api key fallback behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSinclair committed Sep 24, 2024
1 parent d8c64ee commit b534c9f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ env:
pnpm: 9.10.0
RAINBOW_PROVIDER_API_KEY: RAINBOW_PROVIDER_API_KEY
WALLETCONNECT_PROJECT_ID: YOUR_PROJECT_ID
ALCHEMY_ID: YOUR_ALCHEMY_ID

jobs:
tests:
Expand Down
42 changes: 27 additions & 15 deletions packages/rainbowkit/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ const getAllEntryPoints = async (rootPath) =>
);

const baseBuildConfig = (onEnd) => {
const rainbowProviderApiKey = process.env.RAINBOW_PROVIDER_API_KEY;

if (!rainbowProviderApiKey) {
throw new Error('missing RAINBOW_PROVIDER_API_KEY env variable');
}

return {
banner: {
js: '"use client";', // Required for Next 13 App Router
Expand All @@ -42,14 +36,6 @@ const baseBuildConfig = (onEnd) => {
'.json': 'text',
},
plugins: [
replace({
include:
/src\/components\/RainbowKitProvider\/useFingerprint.ts$|src\/core\/network\/enhancedProvider.ts$/,
values: {
__buildVersion: process.env.npm_package_version,
__rainbowProviderApiKey: rainbowProviderApiKey,
},
}),
vanillaExtractPlugin({
identifiers: isCssMinified ? 'short' : 'debug',
processCss: async (css) => {
Expand Down Expand Up @@ -82,8 +68,34 @@ const baseBuildConfig = (onEnd) => {
};
};

const mainBuildConfig = (onEnd) => {
const rainbowProviderApiKey = process.env.RAINBOW_PROVIDER_API_KEY;

if (!rainbowProviderApiKey) {
console.warn(
'missing RAINBOW_PROVIDER_API_KEY env variable, disabling enhanced provider',
);
}

const buildConfig = baseBuildConfig(onEnd);
return {
...buildConfig,
plugins: [
replace({
include:
/src\/components\/RainbowKitProvider\/useFingerprint.ts$|src\/core\/network\/enhancedProvider.ts$/,
values: {
__buildVersion: process.env.npm_package_version,
__rainbowProviderApiKey: rainbowProviderApiKey,
},
}),
...buildConfig.plugins,
],
};
};

const mainBuildOptions = {
...baseBuildConfig((result) => {
...mainBuildConfig((result) => {
if (result.errors.length) {
console.error('❌ main build failed:', result.errors);
} else console.log('✅ main build succeeded');
Expand Down
26 changes: 24 additions & 2 deletions packages/rainbowkit/src/core/network/enhancedProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { describe, expect, it } from 'vitest';
import { enhancedProviderHttp } from './enhancedProvider';
import {
ENHANCED_PROVIDER_ENABLED,
enhancedProviderHttp,
} from './enhancedProvider';

describe('createHttpClient', () => {
if (!process.env.RAINBOW_PROVIDER_API_KEY) {
it.skip('skips tests if RAINBOW_PROVIDER_API_KEY is missing', () => {});
return;
}

if (!ENHANCED_PROVIDER_ENABLED) {
it.skip('skips tests if enhanced provider is disabled', () => {});
return;
}

describe.skip('createHttpClient', () => {
it("should return 'ok' status for health check endpoint", async () => {
const { data } = await enhancedProviderHttp.get('/healthcheck');
expect(data).toStrictEqual({ status: 'ok' });
Expand All @@ -16,4 +29,13 @@ describe.skip('createHttpClient', () => {
'Not Found',
);
});

it('should resolve ENS name for a valid address', async () => {
const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const { data } = await enhancedProviderHttp.get<{ data: string | null }>(
'/v1/resolve-ens',
{ params: { address } },
);
expect(data.data).toBe('vitalik.eth');
});
});
6 changes: 6 additions & 0 deletions packages/rainbowkit/src/core/network/enhancedProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createHttpClient } from './internal/createHttpClient';

export const ENHANCED_PROVIDER_ENABLED = Boolean(
typeof process !== 'undefined' &&
typeof process.env !== 'undefined' &&
process.env.RAINBOW_PROVIDER_API_KEY,
);

export const enhancedProviderHttp = createHttpClient({
baseUrl: 'https://enhanced-provider.rainbow.me',
headers: {
Expand Down
7 changes: 5 additions & 2 deletions packages/rainbowkit/src/hooks/useMainnetEnsName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { useQuery } from '@tanstack/react-query';
import type { Address } from 'viem';
import { useEnsName } from 'wagmi';
import { mainnet } from 'wagmi/chains';
import { enhancedProviderHttp } from '../core/network/enhancedProvider';
import {
ENHANCED_PROVIDER_ENABLED,
enhancedProviderHttp,
} from '../core/network/enhancedProvider';
import { createQueryKey } from '../core/react-query/createQuery';
import { addEnsName, getEnsName } from '../utils/ens';
import { useIsMainnetConfigured } from './useIsMainnetConfigured';
Expand Down Expand Up @@ -41,7 +44,7 @@ export function useMainnetEnsName(address?: Address) {
const { data: enhancedProviderEnsName } = useQuery({
queryKey: createQueryKey('address', address),
queryFn: () => getEnhancedProviderEnsName({ address: address! }),
enabled: !mainnetConfigured && !!address,
enabled: !mainnetConfigured && !!address && ENHANCED_PROVIDER_ENABLED,
staleTime: 10 * (60 * 1_000), // 10 minutes
retry: 1, // Retry once before returning undefined if the request fails
});
Expand Down

0 comments on commit b534c9f

Please sign in to comment.