Skip to content

Commit 1197879

Browse files
authored
feat: use api route for CDP RPC URL (coinbase#486)
1 parent 2b3e4cc commit 1197879

File tree

10 files changed

+53
-17
lines changed

10 files changed

+53
-17
lines changed

.changeset/five-goats-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@coinbase/build-onchain-apps': minor
3+
---
4+
5+
**feat** use api route for CDP RPC URL

src/create/setupEnvFiles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export function setupEnvFiles(projectDir: string, envVar: EnvVar) {
88
const envLocalPath = path.join(projectDir, './web/.env.local');
99
const content = `NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=
1010
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=${envVar.walletConnectProjectID ?? '"GET_ID_FROM_WALLET_CONNET" # See https://cloud.walletconnect.com'}
11-
NEXT_PUBLIC_RPC_URL=${envVar.rpcUrl ?? '"GET_FROM_COINBASE_DEVELOPER_PLATFORM" # See https://portal.cdp.coinbase.com/products/base'}
12-
NEXT_PUBLIC_PAYMASTER_URL=${envVar.rpcUrl ?? '"GET_FROM_COINBASE_DEVELOPER_PLATFORM" # See https://portal.cdp.coinbase.com/products/base'}
11+
NEXT_PRIVATE_RPC_URL=${envVar.rpcUrl ?? '"GET_FROM_COINBASE_DEVELOPER_PLATFORM" # See https://www.coinbase.com/developer-platform/products/base-node?utm_source=boat'}
12+
NEXT_PRIVATE_PAYMASTER_URL=${envVar.rpcUrl ?? '"GET_FROM_COINBASE_DEVELOPER_PLATFORM" # See https://www.coinbase.com/developer-platform/products/base-node?utm_source=boat'}
1313
NEXT_PUBLIC_PRIVY_ID="GET_FROM_PRIVY"
1414
ENVIRONMENT=localhost
1515
`;

template/web/.env.local.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=
22
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID="GET_ID_FROM_WALLET_CONNET" # See https://cloud.walletconnect.com
3-
NEXT_PUBLIC_RPC_URL="GET_FROM_COINBASE_CLOUD"
4-
NEXT_PUBLIC_PAYMASTER_URL="GET_FROM_COINBASE_CLOUD"
3+
NEXT_PRIVATE_RPC_URL="GET_FROM_COINBASE_DEVELOPER_PLATFORM" # See https://www.coinbase.com/developer-platform/products/base-node?utm_source=boat
4+
NEXT_PRIVATE_PAYMASTER_URL="GET_FROM_COINBASE_DEVELOPER_PLATFORM" # See https://www.coinbase.com/developer-platform/products/base-node?utm_source=boat
55
NEXT_PUBLIC_PRIVY_ID="GET_FROM_PRIVY"
66
ENVIRONMENT=localhost

template/web/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=GA_TEST_1234567890
22
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=TEST_1234567890
3-
NEXT_PUBLIC_RPC_URL=https://sepolia.base.org
3+
NEXT_PRIVATE_RPC_URL=https://sepolia.base.org
44
ENVIRONMENT=localhost

template/web/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
```bash
1111
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=ADD_WALLET_CONNECT_PROJECT_ID_HERE
12-
NEXT_PUBLIC_RPC_URL=ADD_RPC_URL_HERE
12+
NEXT_PRIVATE_RPC_URL=ADD_RPC_URL_HERE
1313
```
1414

1515
#### Step 2: Install and Run your onchain app

template/web/app/api/rpc/route.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { NextRequest, NextResponse } from 'next/server';
2+
3+
const rpcUrl = process.env.NEXT_PRIVATE_RPC_URL;
4+
5+
export async function POST(req: NextRequest) {
6+
if (rpcUrl === undefined) {
7+
return NextResponse.json(
8+
{},
9+
{
10+
status: 401,
11+
statusText:
12+
'You need a RPC URL! Get yours at https://www.coinbase.com/developer-platform/products/base-node?utm_source=boat',
13+
},
14+
);
15+
}
16+
17+
// forward to Coinbase Developer Platform RPC
18+
return fetch(rpcUrl, req)
19+
.then(async (response) => {
20+
// Return the response data to the client
21+
return NextResponse.json(await response.json(), {
22+
status: response.status,
23+
statusText: response.statusText,
24+
});
25+
})
26+
.catch((error) => {
27+
console.error('Error:', error);
28+
return NextResponse.json({}, { status: 500, statusText: 'Internal Server Error' });
29+
});
30+
}

template/web/app/paymaster-bundler/_components/Guide.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export default function Guide() {
9696
Base Node homepage
9797
</A>
9898
</Li>
99-
<Li>Sign up for a Coinbase Cloud account, if you don&apos;t have one already</Li>
99+
<Li>
100+
Sign up for a Coinbase Developer Platform account, if you don&apos;t have one
101+
already
102+
</Li>
100103
<Li>
101104
Create a <p className="inline font-bold">Base</p> project under{' '}
102105
<p className="inline font-bold"> Start a New Project</p>
@@ -119,8 +122,10 @@ export default function Guide() {
119122
</Ul>
120123
<Li>
121124
Copy your RPC URL, and set it as{' '}
122-
<p className="inline font-bold text-boat-color-orange">NEXT_PUBLIC_RPC_URL</p> and{' '}
123-
<p className="inline font-bold text-boat-color-orange">NEXT_PUBLIC_PAYMASTER_URL</p>{' '}
125+
<p className="inline font-bold text-boat-color-orange">NEXT_PRIVATE_RPC_URL</p> and{' '}
126+
<p className="inline font-bold text-boat-color-orange">
127+
NEXT_PRIVATE_PAYMASTER_URL
128+
</p>{' '}
124129
in the .env file.
125130
</Li>
126131
</Ul>

template/web/app/paymaster-bundler/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const rpcUrl = process.env.NEXT_PUBLIC_RPC_URL;
2-
export const paymasterUrl = process.env.NEXT_PUBLIC_PAYMASTER_URL;
1+
export const rpcUrl = '/api/rpc';
2+
export const paymasterUrl = '/api/rpc';
33
export const privyID = process.env.NEXT_PUBLIC_PRIVY_ID;
44
export const entryPoint = '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789';
55
export const contractAddress = '0x66519FCAee1Ed65bc9e0aCc25cCD900668D3eD49';

template/web/src/OnchainProviders.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ type Props = { children: ReactNode };
1111

1212
const queryClient = new QueryClient();
1313

14-
const rpcUrl = process.env.NEXT_PUBLIC_RPC_URL ?? '';
15-
if (!rpcUrl) {
16-
const rpcErrMessage =
17-
'To connect to the blockchain you need to provide a NEXT_PUBLIC_RPC_URL env variable';
18-
throw new Error(rpcErrMessage);
19-
}
14+
const rpcUrl = '/api/rpc';
2015

2116
const wagmiConfig = createWagmiConfig(rpcUrl);
2217

template/web/src/store/createWagmiConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function createWagmiConfig(rpcUrl: string, projectId?: string) {
88
console.log('projectId:', projectId);
99
}
1010

11+
// Temporary hack, until we configure a FE page in OnchainKit to copy just the API key
1112
const baseUrl = rpcUrl.replace(/\/v1\/(.+?)\//, '/v1/base/');
1213
const baseSepoliaUrl = rpcUrl.replace(/\/v1\/(.+?)\//, '/v1/base-sepolia/');
1314

0 commit comments

Comments
 (0)