From 60a600521a90e80cf9f0fad3f2331ae55fc9ef6b Mon Sep 17 00:00:00 2001 From: roushou Date: Wed, 26 Jun 2024 17:24:31 +0700 Subject: [PATCH] feat: allow to specify network --- .changeset/honest-peas-drive.md | 6 ++++ docs/introduction/getting-started.md | 14 +++++--- docs/onchain/client.md | 4 ++- docs/paymaster/client.md | 4 ++- .../onchain_get-address-transactions/index.ts | 5 ++- examples/onchain_get-balance-details/index.ts | 5 ++- .../onchain_get-balance-histories/index.ts | 5 ++- examples/onchain_get-balances/index.ts | 5 ++- examples/onchain_rpc-client/index.ts | 5 ++- .../index.ts | 5 ++- .../index.ts | 5 ++- .../index.ts | 5 ++- .../address/get-address-transactions.test.ts | 1 + .../src/balance/get-balance-details.test.ts | 1 + .../src/balance/get-balance-histories.test.ts | 1 + .../onchain/src/balance/get-balances.test.ts | 1 + packages/onchain/src/client.ts | 10 ++++-- packages/onchain/src/mocks/address.ts | 2 +- packages/onchain/src/mocks/balance.ts | 6 ++-- packages/onchain/src/rpc.test.ts | 30 +++++++++++++++-- packages/onchain/src/rpc.ts | 13 +++++--- packages/paymaster/src/client.ts | 12 ++++--- packages/paymaster/src/mocks/handlers.ts | 6 ++-- packages/paymaster/src/rpc.test.ts | 33 +++++++++++++++---- packages/paymaster/src/rpc.ts | 12 ++++--- packages/utils/constants.ts | 2 +- 26 files changed, 152 insertions(+), 46 deletions(-) create mode 100644 .changeset/honest-peas-drive.md diff --git a/.changeset/honest-peas-drive.md b/.changeset/honest-peas-drive.md new file mode 100644 index 0000000..37ab819 --- /dev/null +++ b/.changeset/honest-peas-drive.md @@ -0,0 +1,6 @@ +--- +"@coinbasejs/onchain": minor +"@coinbasejs/paymaster": minor +--- + +feat: allow to pass network to clients diff --git a/docs/introduction/getting-started.md b/docs/introduction/getting-started.md index 291cbd7..e02e4cc 100644 --- a/docs/introduction/getting-started.md +++ b/docs/introduction/getting-started.md @@ -90,12 +90,15 @@ $ npm install @coinbasejs/onchain ### 2. Create your client -Create your client by passing it your API key. If needed, you can also pass your `RPC URL`. Otherwise it will default to `https://api.developer.coinbase.com/rpc/v1/base`. +Create your client by passing it your API key. If needed, you can also pass your `RPC URL`. Otherwise it will default to `https://api.developer.coinbase.com/rpc/v1`. ```ts import { createClient } from "@coinbasejs/onchain"; -const client = createClient({ apiKey: "API_KEY" }); +const client = createClient({ + apiKey: "API_KEY", + network: "base", +}); ``` ### 3. Send your request @@ -103,9 +106,12 @@ const client = createClient({ apiKey: "API_KEY" }); ```ts import { createClient } from "@coinbasejs/sdk/commerce"; -const client = createClient({ apiKey: "API_KEY" }); +const client = createClient({ + apiKey: "API_KEY", + network: "base", +}); -const response = await client.getBalances([ // [!code focus:8] +const response = await client.getBalances([ // [!code focus:6] { address: "0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789", pageSize: 1, diff --git a/docs/onchain/client.md b/docs/onchain/client.md index d553f87..f70dbac 100644 --- a/docs/onchain/client.md +++ b/docs/onchain/client.md @@ -26,13 +26,14 @@ $ npm install @coinbasejs/onchain ## Usage -You can use your own RPC by passing its url in `createClient`. Otherwise it defaults to the RPC provided by Coinbase `https://api.developer.coinbase.com/rpc/v1/base`. +You can use your own RPC by passing its url in `createClient`. Otherwise it defaults to the RPC provided by Coinbase `https://api.developer.coinbase.com/rpc/v1`. ```ts import { createClient } from "@coinbasejs/onchain"; const client = createClient({ apiKey: "API_KEY", + network: "base", rpcUrl: "https://your.rpc.com", }); ``` @@ -44,6 +45,7 @@ const client = createClient({ ```ts export type ClientConfig = { apiKey: string; + network: "base" | "base-sepolia"; rpcUrl?: string; }; ``` diff --git a/docs/paymaster/client.md b/docs/paymaster/client.md index e189768..4f04aa3 100644 --- a/docs/paymaster/client.md +++ b/docs/paymaster/client.md @@ -26,13 +26,14 @@ $ npm install @coinbasejs/paymaster ## Usage -You can use your own RPC by passing its url in `createClient`. Otherwise it defaults to the RPC provided by Coinbase `https://api.developer.coinbase.com/rpc/v1/base`. +You can use your own RPC by passing its url in `createClient`. Otherwise it defaults to the RPC provided by Coinbase `https://api.developer.coinbase.com/rpc/v1`. ```ts import { createClient } from "@coinbasejs/paymaster"; const client = createClient({ apiKey: "API_KEY", + network: "base", rpcUrl: "https://your.rpc.com", }); ``` @@ -44,6 +45,7 @@ const client = createClient({ ```ts export type ClientConfig = { apiKey: string; + network: "base" | "base-sepolia"; rpcUrl?: string; }; ``` diff --git a/examples/onchain_get-address-transactions/index.ts b/examples/onchain_get-address-transactions/index.ts index 94d2750..8a8f217 100644 --- a/examples/onchain_get-address-transactions/index.ts +++ b/examples/onchain_get-address-transactions/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/onchain"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getAddressTransactions([ { diff --git a/examples/onchain_get-balance-details/index.ts b/examples/onchain_get-balance-details/index.ts index 38d446c..aab1022 100644 --- a/examples/onchain_get-balance-details/index.ts +++ b/examples/onchain_get-balance-details/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/onchain"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getBalanceDetails([ { diff --git a/examples/onchain_get-balance-histories/index.ts b/examples/onchain_get-balance-histories/index.ts index 8192057..7612864 100644 --- a/examples/onchain_get-balance-histories/index.ts +++ b/examples/onchain_get-balance-histories/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/onchain"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getBalanceHistories([ { diff --git a/examples/onchain_get-balances/index.ts b/examples/onchain_get-balances/index.ts index 095f18a..ce38e87 100644 --- a/examples/onchain_get-balances/index.ts +++ b/examples/onchain_get-balances/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/onchain"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getBalances([ { diff --git a/examples/onchain_rpc-client/index.ts b/examples/onchain_rpc-client/index.ts index bb0f478..35d99e1 100644 --- a/examples/onchain_rpc-client/index.ts +++ b/examples/onchain_rpc-client/index.ts @@ -3,7 +3,10 @@ import { createRpcClient } from "@coinbasejs/onchain"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const rpc = createRpcClient({ apiKey }); +const rpc = createRpcClient({ + apiKey, + network: "base-sepolia", +}); const response = await rpc.request({ method: "cdp_listBalances", diff --git a/examples/paymaster_get-supported-entry-points/index.ts b/examples/paymaster_get-supported-entry-points/index.ts index 3f2db22..accbb99 100644 --- a/examples/paymaster_get-supported-entry-points/index.ts +++ b/examples/paymaster_get-supported-entry-points/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/paymaster"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getSupportedEntrypoints(); diff --git a/examples/paymaster_get-user-operation-by-hash/index.ts b/examples/paymaster_get-user-operation-by-hash/index.ts index 7d84f3f..d17a03b 100644 --- a/examples/paymaster_get-user-operation-by-hash/index.ts +++ b/examples/paymaster_get-user-operation-by-hash/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/paymaster"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getUserOperationByHash([ "0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122", diff --git a/examples/paymaster_get-user-operation-receipt/index.ts b/examples/paymaster_get-user-operation-receipt/index.ts index 2dfc308..3156f46 100644 --- a/examples/paymaster_get-user-operation-receipt/index.ts +++ b/examples/paymaster_get-user-operation-receipt/index.ts @@ -3,7 +3,10 @@ import { createClient } from "@coinbasejs/paymaster"; const apiKey = process.env.API_KEY; if (!apiKey) throw new Error("API_KEY not found"); -const client = createClient({ apiKey }); +const client = createClient({ + apiKey, + network: "base-sepolia", +}); const response = await client.getUserOperationReceipt([ "0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122", diff --git a/packages/onchain/src/address/get-address-transactions.test.ts b/packages/onchain/src/address/get-address-transactions.test.ts index 1f1f324..624449d 100644 --- a/packages/onchain/src/address/get-address-transactions.test.ts +++ b/packages/onchain/src/address/get-address-transactions.test.ts @@ -7,6 +7,7 @@ describe("get-address-transactions", () => { test("should get transactions of address", async () => { const rpcClient = createRpcClient({ url: RPC_URL, + network: "base", apiKey: "API_KEY", }); await getAddressTransactions(rpcClient, [ diff --git a/packages/onchain/src/balance/get-balance-details.test.ts b/packages/onchain/src/balance/get-balance-details.test.ts index 2044403..e5edbaa 100644 --- a/packages/onchain/src/balance/get-balance-details.test.ts +++ b/packages/onchain/src/balance/get-balance-details.test.ts @@ -7,6 +7,7 @@ describe("get-balance-details", () => { test("should get balance details", async () => { const rpcClient = createRpcClient({ url: RPC_URL, + network: "base", apiKey: "API_KEY", }); await getBalanceDetails(rpcClient, [ diff --git a/packages/onchain/src/balance/get-balance-histories.test.ts b/packages/onchain/src/balance/get-balance-histories.test.ts index db72efc..78babe5 100644 --- a/packages/onchain/src/balance/get-balance-histories.test.ts +++ b/packages/onchain/src/balance/get-balance-histories.test.ts @@ -7,6 +7,7 @@ describe("get-balance-histories", () => { test("should get balance histories", async () => { const rpcClient = createRpcClient({ url: RPC_URL, + network: "base", apiKey: "API_KEY", }); await getBalancehistories(rpcClient, [ diff --git a/packages/onchain/src/balance/get-balances.test.ts b/packages/onchain/src/balance/get-balances.test.ts index 18fd8fe..20d8196 100644 --- a/packages/onchain/src/balance/get-balances.test.ts +++ b/packages/onchain/src/balance/get-balances.test.ts @@ -7,6 +7,7 @@ describe("get-balances", () => { test("should get balances", async () => { const rpcClient = createRpcClient({ url: RPC_URL, + network: "base", apiKey: "API_KEY", }); await getBalances(rpcClient, [ diff --git a/packages/onchain/src/client.ts b/packages/onchain/src/client.ts index 9345b90..6b1f9b7 100644 --- a/packages/onchain/src/client.ts +++ b/packages/onchain/src/client.ts @@ -18,7 +18,7 @@ import { type GetBalancesResponse, getBalances, } from "./balance/get-balances"; -import { createRpcClient } from "./rpc"; +import { type RpcClientConfig, createRpcClient } from "./rpc"; export type Client = { /** @@ -72,6 +72,7 @@ export type Client = { export type ClientConfig = { apiKey: string; + network: RpcClientConfig["network"]; /** * Coinbase platform RPC endpoint. * Defaults to `https://api.developer.coinbase.com/rpc/v1/base` @@ -83,19 +84,22 @@ export type ClientConfig = { * @returns The Client * * @param apiKey - Your API key - * @param rpcUrl - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1/base` + * @param network - The network name to connect to. + * @param rpcUrl - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1` * * @example * * const client = createClient({ * apiKey: API_KEY, - * rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base", + * network: "base", + * rpcUrl: "https://api.developer.coinbase.com/rpc/v1", * }); * */ export function createClient(config: ClientConfig): Client { const rpcClient = createRpcClient({ apiKey: config.apiKey, + network: config.network, url: config.rpcUrl, }); diff --git a/packages/onchain/src/mocks/address.ts b/packages/onchain/src/mocks/address.ts index c16c2ac..be7fa1b 100644 --- a/packages/onchain/src/mocks/address.ts +++ b/packages/onchain/src/mocks/address.ts @@ -5,7 +5,7 @@ import { withRpcMethod } from "./predicates"; export const handlers = [ http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "cdp_listAddressTransactions" }, async () => { return HttpResponse.json({ id: 1, diff --git a/packages/onchain/src/mocks/balance.ts b/packages/onchain/src/mocks/balance.ts index e31609d..74ecab3 100644 --- a/packages/onchain/src/mocks/balance.ts +++ b/packages/onchain/src/mocks/balance.ts @@ -5,7 +5,7 @@ import { withRpcMethod } from "./predicates"; export const handlers = [ http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "cdp_listBalanceDetails" }, async () => { return HttpResponse.json({ id: 1, @@ -28,7 +28,7 @@ export const handlers = [ }), ), http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "cdp_listBalanceHistories" }, async () => { return HttpResponse.json({ id: 1, @@ -47,7 +47,7 @@ export const handlers = [ }), ), http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "cdp_listBalances" }, async () => { return HttpResponse.json({ id: 1, diff --git a/packages/onchain/src/rpc.test.ts b/packages/onchain/src/rpc.test.ts index 001b0c2..8599a2a 100644 --- a/packages/onchain/src/rpc.test.ts +++ b/packages/onchain/src/rpc.test.ts @@ -3,16 +3,40 @@ import { describe, expect, test } from "vitest"; import { createRpcClient } from "./rpc"; describe("rpc", () => { + test("should set network", async () => { + const rpcBase = createRpcClient({ + apiKey: "API_KEY", + network: "base", + url: "https://api.developer.coinbase.com/rpc/v1", + }); + expect(rpcBase.__url).toEqual( + "https://api.developer.coinbase.com/rpc/v1/base", + ); + + const rpcBaseSepolia = createRpcClient({ + apiKey: "API_KEY", + network: "base-sepolia", + url: "https://api.developer.coinbase.com/rpc/v1", + }); + expect(rpcBaseSepolia.__url).toEqual( + "https://api.developer.coinbase.com/rpc/v1/base-sepolia", + ); + }); + test("should set RPC url", async () => { const rpc = createRpcClient({ apiKey: "API_KEY", + network: "base-sepolia", url: "https://coinbase.com/other/rpc", }); - expect(rpc.__url).toEqual("https://coinbase.com/other/rpc"); + expect(rpc.__url).toEqual("https://coinbase.com/other/rpc/base-sepolia"); }); test(`should default RPC url to ${RPC_URL}`, async () => { - const rpc = createRpcClient({ apiKey: "API_KEY" }); - expect(rpc.__url).toEqual(RPC_URL); + const rpc = createRpcClient({ + apiKey: "API_KEY", + network: "base-sepolia", + }); + expect(rpc.__url).toEqual(`${RPC_URL}/base-sepolia`); }); }); diff --git a/packages/onchain/src/rpc.ts b/packages/onchain/src/rpc.ts index 3005789..99356fb 100644 --- a/packages/onchain/src/rpc.ts +++ b/packages/onchain/src/rpc.ts @@ -22,6 +22,7 @@ export type RpcClient = { export type RpcClientConfig = { apiKey: string; + network: "base" | "base-sepolia"; /** * Coinbase platform RPC endpoint. * Defaults to `https://api.developer.coinbase.com/rpc/v1/base` @@ -33,24 +34,28 @@ export type RpcClientConfig = { * @returns The RPC client * * @param apiKey - Your API key - * @param url - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1/base` + * @param network - The network name to connect to + * @param url - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1` * * @example * * const rpcClient = createRpcClient({ * apiKey: API_KEY, - * rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base", + * network: "base", + * rpcUrl: "https://api.developer.coinbase.com/rpc/v1", * }); * */ export function createRpcClient({ apiKey, + network, url = RPC_URL, }: RpcClientConfig): RpcClient { + const rpcUrl = `${url}/${network}`; return { - __url: url, + __url: rpcUrl, request: async (config) => { - return await http.post(`${url}/${apiKey}`, { + return await http.post(`${rpcUrl}/${apiKey}`, { body: JSON.stringify({ jsonrpc: "2.0", id: 1, diff --git a/packages/paymaster/src/client.ts b/packages/paymaster/src/client.ts index d9d3068..7a65e83 100644 --- a/packages/paymaster/src/client.ts +++ b/packages/paymaster/src/client.ts @@ -22,7 +22,7 @@ import { type SendUserOperationResponse, sendUserOperation, } from "./bundler/send-user-operation"; -import { createRpcClient } from "./rpc"; +import { type RpcClientConfig, createRpcClient } from "./rpc"; import { type SponsorUserOperationParameters, type SponsorUserOperationResponse, @@ -98,9 +98,10 @@ export type Client = { export type ClientConfig = { apiKey: string; + network: RpcClientConfig["network"]; /** * Coinbase platform RPC endpoint. - * Defaults to `https://api.developer.coinbase.com/rpc/v1/base` + * Defaults to `https://api.developer.coinbase.com/rpc/v1` */ rpcUrl?: string; }; @@ -109,19 +110,22 @@ export type ClientConfig = { * @returns The Client * * @param apiKey - Your API key - * @param rpcUrl - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1/base` + * @param network - The network to connect to + * @param rpcUrl - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1` * * @example * * const client = createClient({ * apiKey: API_KEY, - * rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base", + * network: "base", + * rpcUrl: "https://api.developer.coinbase.com/rpc/v1", * }); * */ export function createClient(config: ClientConfig): Client { const rpcClient = createRpcClient({ apiKey: config.apiKey, + network: config.network, url: config.rpcUrl, }); diff --git a/packages/paymaster/src/mocks/handlers.ts b/packages/paymaster/src/mocks/handlers.ts index 5c3c3ae..5cea5e3 100644 --- a/packages/paymaster/src/mocks/handlers.ts +++ b/packages/paymaster/src/mocks/handlers.ts @@ -5,7 +5,7 @@ import { withRpcMethod } from "./predicates"; export const handlers = [ http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "eth_supportedEntryPoints" }, async () => { return HttpResponse.json({ id: 1, @@ -15,7 +15,7 @@ export const handlers = [ }), ), http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "eth_getUserOperationByHash" }, async () => { return HttpResponse.json({ id: 1, @@ -26,7 +26,7 @@ export const handlers = [ }), ), http.post( - `${RPC_URL}/API_KEY`, + `${RPC_URL}/base/API_KEY`, withRpcMethod({ method: "eth_getUserOperationReceipt" }, async () => { return HttpResponse.json({ id: 1, diff --git a/packages/paymaster/src/rpc.test.ts b/packages/paymaster/src/rpc.test.ts index 04bca8a..dcebcff 100644 --- a/packages/paymaster/src/rpc.test.ts +++ b/packages/paymaster/src/rpc.test.ts @@ -3,21 +3,42 @@ import { describe, expect, test } from "vitest"; import { createRpcClient } from "./rpc"; describe("rpc", () => { + test("should set network", async () => { + const rpcBase = createRpcClient({ + apiKey: "API_KEY", + network: "base", + url: "https://api.developer.coinbase.com/rpc/v1", + }); + expect(rpcBase.__url).toEqual( + "https://api.developer.coinbase.com/rpc/v1/base", + ); + + const rpcBaseSepolia = createRpcClient({ + apiKey: "API_KEY", + network: "base-sepolia", + url: "https://api.developer.coinbase.com/rpc/v1", + }); + expect(rpcBaseSepolia.__url).toEqual( + "https://api.developer.coinbase.com/rpc/v1/base-sepolia", + ); + }); + test("should set RPC url", async () => { const rpc = createRpcClient({ apiKey: "API_KEY", + network: "base", url: "https://coinbase.com/other/rpc", }); - expect(rpc.__url).toEqual("https://coinbase.com/other/rpc"); + expect(rpc.__url).toEqual("https://coinbase.com/other/rpc/base"); }); test(`should default RPC url to ${RPC_URL}`, async () => { - const rpc = createRpcClient({ apiKey: "API_KEY" }); - expect(rpc.__url).toEqual(RPC_URL); + const rpc = createRpcClient({ apiKey: "API_KEY", network: "base" }); + expect(rpc.__url).toEqual(`${RPC_URL}/base`); }); test("should get supported entrypoints", async () => { - const rpc = createRpcClient({ apiKey: "API_KEY" }); + const rpc = createRpcClient({ apiKey: "API_KEY", network: "base" }); const response = await rpc.request({ method: "eth_supportedEntryPoints", }); @@ -29,7 +50,7 @@ describe("rpc", () => { }); test("should get user operation by hash", async () => { - const rpc = createRpcClient({ apiKey: "API_KEY" }); + const rpc = createRpcClient({ apiKey: "API_KEY", network: "base" }); await rpc.request({ method: "eth_getUserOperationByHash", parameters: [ @@ -39,7 +60,7 @@ describe("rpc", () => { }); test("should get user operation receipt", async () => { - const rpc = createRpcClient({ apiKey: "API_KEY" }); + const rpc = createRpcClient({ apiKey: "API_KEY", network: "base" }); await rpc.request({ method: "eth_getUserOperationReceipt", parameters: [ diff --git a/packages/paymaster/src/rpc.ts b/packages/paymaster/src/rpc.ts index 2dea55b..8705c32 100644 --- a/packages/paymaster/src/rpc.ts +++ b/packages/paymaster/src/rpc.ts @@ -21,9 +21,10 @@ export type RpcClient = { export type RpcClientConfig = { apiKey: string; + network: "base" | "base-sepolia"; /** * Coinbase platform RPC endpoint. - * Defaults to `https://api.developer.coinbase.com/rpc/v1/base` + * Defaults to `https://api.developer.coinbase.com/rpc/v1` */ url?: string; }; @@ -32,24 +33,27 @@ export type RpcClientConfig = { * @returns The RPC client * * @param apiKey - Your API key - * @param url - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1/base` + * @param url - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1` * * @example * * const rpcClient = createRpcClient({ * apiKey: API_KEY, + * network: "base", * rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base", * }); * */ export function createRpcClient({ apiKey, + network, url = RPC_URL, }: RpcClientConfig): RpcClient { + const rpcUrl = `${url}/${network}`; return { - __url: url, + __url: rpcUrl, request: async (config) => { - return await http.post(`${url}/${apiKey}`, { + return await http.post(`${rpcUrl}/${apiKey}`, { body: JSON.stringify({ jsonrpc: "2.0", id: 1, diff --git a/packages/utils/constants.ts b/packages/utils/constants.ts index 8e40a86..76adafb 100644 --- a/packages/utils/constants.ts +++ b/packages/utils/constants.ts @@ -1 +1 @@ -export const RPC_URL = "https://api.developer.coinbase.com/rpc/v1/base"; +export const RPC_URL = "https://api.developer.coinbase.com/rpc/v1";