Skip to content

Commit

Permalink
feat: allow to specify network
Browse files Browse the repository at this point in the history
  • Loading branch information
roushou committed Jun 26, 2024
1 parent 8c0fe99 commit 60a6005
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 46 deletions.
6 changes: 6 additions & 0 deletions .changeset/honest-peas-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@coinbasejs/onchain": minor
"@coinbasejs/paymaster": minor
---

feat: allow to pass network to clients
14 changes: 10 additions & 4 deletions docs/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,28 @@ $ 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

```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,
Expand Down
4 changes: 3 additions & 1 deletion docs/onchain/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
```
Expand All @@ -44,6 +45,7 @@ const client = createClient({
```ts
export type ClientConfig = {
apiKey: string;
network: "base" | "base-sepolia";
rpcUrl?: string;
};
```
Expand Down
4 changes: 3 additions & 1 deletion docs/paymaster/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
```
Expand All @@ -44,6 +45,7 @@ const client = createClient({
```ts
export type ClientConfig = {
apiKey: string;
network: "base" | "base-sepolia";
rpcUrl?: string;
};
```
Expand Down
5 changes: 4 additions & 1 deletion examples/onchain_get-address-transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
5 changes: 4 additions & 1 deletion examples/onchain_get-balance-details/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
5 changes: 4 additions & 1 deletion examples/onchain_get-balance-histories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
5 changes: 4 additions & 1 deletion examples/onchain_get-balances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
5 changes: 4 additions & 1 deletion examples/onchain_rpc-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion examples/paymaster_get-supported-entry-points/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 4 additions & 1 deletion examples/paymaster_get-user-operation-by-hash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion examples/paymaster_get-user-operation-receipt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/balance/get-balance-details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/balance/get-balance-histories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
1 change: 1 addition & 0 deletions packages/onchain/src/balance/get-balances.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
10 changes: 7 additions & 3 deletions packages/onchain/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -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`
Expand All @@ -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,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/onchain/src/mocks/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { withRpcMethod } from "./predicates";

export const handlers = [
http.post<PathParams, { method: string }, DefaultBodyType>(
`${RPC_URL}/API_KEY`,
`${RPC_URL}/base/API_KEY`,
withRpcMethod({ method: "cdp_listAddressTransactions" }, async () => {
return HttpResponse.json({
id: 1,
Expand Down
6 changes: 3 additions & 3 deletions packages/onchain/src/mocks/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -47,7 +47,7 @@ export const handlers = [
}),
),
http.post<PathParams, { method: string }, DefaultBodyType>(
`${RPC_URL}/API_KEY`,
`${RPC_URL}/base/API_KEY`,
withRpcMethod({ method: "cdp_listBalances" }, async () => {
return HttpResponse.json({
id: 1,
Expand Down
30 changes: 27 additions & 3 deletions packages/onchain/src/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
13 changes: 9 additions & 4 deletions packages/onchain/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 60a6005

Please sign in to comment.