Skip to content

Commit

Permalink
feat: allow custom rpc urls (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Jan 20, 2025
1 parent df87df1 commit 08b7dc7
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-pans-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"burner-connector": patch
---

feat: allow custom rpc urls
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v3
with:
version: 9.0.6
version: 9.14.2

- name: Setup node env
uses: actions/setup-node@v3
Expand Down
52 changes: 43 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ or
pnpm add burner-connector
```

2. Using wagmi `burner` connector :
2. Using wagmi `burner` connector:

```ts
import { burner } from "burner-connector";
import { mainnet, base } from "viem/chains";

// burner function can also called with param `{ useSessionStorage: true }` to create a new wallet for each browser tab
// - `useSessionStorage` to false (default) to persist wallet across browser tabs(incognito window will have different wallet)
// - `useSessionStorage` to true to create a new wallet for each browser tab
// Configuration options:
// - `useSessionStorage` (optional) : false (default) to persist wallet across browser tabs
// true to create a new wallet for each browser tab
// - `rpcUrls` (optional) : custom RPC URLs for specific chain IDs

// Basic usage without options
export const config = createConfig({
chains: [mainnet, base],
connectors: [burner()],
Expand All @@ -42,6 +44,24 @@ export const config = createConfig({
[base.id]: http(),
},
});

// Example with all options
export const config = createConfig({
chains: [mainnet, base],
connectors: [
burner({
useSessionStorage: true,
rpcUrls: {
1: "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
8453: "https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
},
}),
],
transports: {
[mainnet.id]: http(),
[base.id]: http(),
},
});
```

3. Integrate with rainbowkit:
Expand All @@ -52,12 +72,17 @@ import { metaMaskWallet } from "@rainbow-me/rainbowkit/wallets";
import { rainbowkitBurnerWallet } from "burner-connector";
import { mainnet, base } from "viem/chains";

const wallets = [metaMaskWallet, rainbowkitBurnerWallet];
// Configure burner wallet options
// Storage configuration:
// - useSessionStorage: false (default) to persist wallet across browser tabs
// true to create a new wallet for each browser tab
rainbowkitBurnerWallet.useSessionStorage = true;

// Configure burner wallet storage
// - `useSessionStorage` to false (default) to persist wallet across browser tabs(incognito window will have different wallet)
// - `useSessionStorage` to true to create a new wallet for each browser tab
// rainbowkitBurnerWallet.useSessionStorage = true;
// Custom RPC URLs configuration (optional):
rainbowkitBurnerWallet.rpcUrls = {
1: "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
8453: "https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
};

const wallets = [metaMaskWallet, rainbowkitBurnerWallet];

Expand Down Expand Up @@ -85,6 +110,15 @@ const wagmiConfig = createConfig({
});
```

## Configuration Options

### Burner Connector Options

| Option | Type | Default | Description |
| ------------------------------ | ------------------------ | ----------- | ----------------------------------------------------------------------------------------------- |
| `useSessionStorage` (optional) | `boolean` | `false` | When true, creates a new wallet for each browser tab. When false, persists wallet across tabs. |
| `rpcUrls` (optional) | `Record<number, string>` | `undefined` | Optional custom RPC URLs for specific chain IDs. Falls back to chain's default if not provided. |

---

Checkout [CONTRIBUTING.md](/CONTRIBUTING.md) for more details on how to set it up locally.
7 changes: 6 additions & 1 deletion example/app/wagmiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { rainbowkitBurnerWallet } from "burner-connector";
// Use this if you want to enable session storage
// rainbowkitBurnerWallet.useSessionStorage = true;

/* Use custom RPC URLs to override wagmi's default RPC URLs if needed */
/* rainbowkitBurnerWallet.rpcUrls = {
[optimismSepolia.id]: `https://opt-sepolia.g.alchemy.com/v2/${alchemyAPIKey}`,
}; */

const wallets = [metaMaskWallet, rainbowkitBurnerWallet];
const walletConnectProjectID = "3a8170812b534d0ff9d794f19a901d64";
const wagmiConnectors = connectorsForWallets(
Expand All @@ -21,7 +26,7 @@ const wagmiConnectors = connectorsForWallets(
{
appName: "scaffold-eth-2",
projectId: walletConnectProjectID,
}
},
);

export const chains = [optimismSepolia, hardhat] as const;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"license": "MIT",
"devDependencies": {
"@changesets/cli": "^2.27.1"
}
},
"packageManager": "[email protected]+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
}
52 changes: 51 additions & 1 deletion packages/burner-connector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ or
pnpm add burner-connector
```

2. Using wagmi `burner` connector :
2. Using wagmi `burner` connector:

```ts
import { burner } from "burner-connector";
import { mainnet, base } from "viem/chains";

// Configuration options:
// - `useSessionStorage` (optional) : false (default) to persist wallet across browser tabs
// true to create a new wallet for each browser tab
// - `rpcUrls` (optional) : custom RPC URLs for specific chain IDs

// Basic usage without options
export const config = createConfig({
chains: [mainnet, base],
connectors: [burner()],
Expand All @@ -29,6 +36,24 @@ export const config = createConfig({
[base.id]: http(),
},
});

// Example with all options
export const config = createConfig({
chains: [mainnet, base],
connectors: [
burner({
useSessionStorage: true,
rpcUrls: {
1: "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
8453: "https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
},
}),
],
transports: {
[mainnet.id]: http(),
[base.id]: http(),
},
});
```

3. Integrate with rainbowkit:
Expand All @@ -39,6 +64,18 @@ import { metaMaskWallet } from "@rainbow-me/rainbowkit/wallets";
import { rainbowkitBurnerWallet } from "burner-connector";
import { mainnet, base } from "viem/chains";

// Configure burner wallet options
// Storage configuration:
// - useSessionStorage: false (default) to persist wallet across browser tabs
// true to create a new wallet for each browser tab
rainbowkitBurnerWallet.useSessionStorage = true;

// Custom RPC URLs configuration (optional):
rainbowkitBurnerWallet.rpcUrls = {
1: "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
8453: "https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
};

const wallets = [metaMaskWallet, rainbowkitBurnerWallet];

const wagmiConnectors = connectorsForWallets(
Expand All @@ -64,3 +101,16 @@ const wagmiConfig = createConfig({
},
});
```

## Configuration Options

### Burner Connector Options

| Option | Type | Default | Description |
| ------------------------------ | ------------------------ | ----------- | ----------------------------------------------------------------------------------------------- |
| `useSessionStorage` (optional) | `boolean` | `false` | When true, creates a new wallet for each browser tab. When false, persists wallet across tabs. |
| `rpcUrls` (optional) | `Record<number, string>` | `undefined` | Optional custom RPC URLs for specific chain IDs. Falls back to chain's default if not provided. |

---

Checkout [CONTRIBUTING.md](/CONTRIBUTING.md) for more details on how to set it up locally.
13 changes: 10 additions & 3 deletions packages/burner-connector/src/burnerConnector/burner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export class ChainNotConfiguredError extends BaseError {

type Provider = ReturnType<Transport<"custom", Record<any, any>, EIP1193RequestFn<WalletRpcSchema>>>;

export const burner = ({ useSessionStorage = false }: { useSessionStorage?: boolean } = {}) => {
type BurnerConfig = {
useSessionStorage?: boolean;
rpcUrls?: Record<number, string>;
};

export const burner = ({ useSessionStorage = false, rpcUrls = {} }: BurnerConfig = {}) => {
let connected = true;
let connectedChainId: number;
return createConnector<Provider>((config) => ({
Expand All @@ -53,13 +58,15 @@ export const burner = ({ useSessionStorage = false }: { useSessionStorage?: bool
async getProvider({ chainId } = {}) {
const chain = config.chains.find((x) => x.id === chainId) ?? config.chains[0];

const url = chain.rpcUrls.default.http[0];
// Use custom RPC URL if provided, otherwise fallback to default
const url = rpcUrls[chain.id] || chain.rpcUrls.default.http[0];
if (!url) throw new Error("No rpc url found for chain");

const burnerAccount = privateKeyToAccount(loadBurnerPK({ useSessionStorage }));
const client = createWalletClient({
chain: chain,
account: burnerAccount,
transport: http(),
transport: http(url),
});

const request: EIP1193RequestFn = async ({ method, params }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import { burner } from "../../burnerConnector/burner.js";

type Provider = ReturnType<Transport<"custom", Record<any, any>, EIP1193RequestFn<WalletRpcSchema>>>;

export const rainbowkitBurnerConnector = (walletDetails: WalletDetailsParams) => {
export const rainbowkitBurnerConnector = (
walletDetails: WalletDetailsParams,
burnerWalletConfig: { rpcUrls?: Record<number, string> },
) => {
return createConnector<Provider>((config) => ({
...burner()(config),
...burner({ rpcUrls: burnerWalletConfig?.rpcUrls })(config),
...walletDetails,
}));
};

export const rainbowkitSessionStorageBurnerConnector = (walletDetails: WalletDetailsParams) => {
export const rainbowkitSessionStorageBurnerConnector = (
walletDetails: WalletDetailsParams,
burnerWalletConfig: { rpcUrls?: Record<number, string> },
) => {
return createConnector<Provider>((config) => ({
...burner({ useSessionStorage: true })(config),
...burner({ useSessionStorage: true, rpcUrls: burnerWalletConfig?.rpcUrls })(config),
...walletDetails,
}));
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Wallet } from "@rainbow-me/rainbowkit";
import type { Wallet, WalletDetailsParams } from "@rainbow-me/rainbowkit";
import { burnerWalletId, burnerWalletName } from "../../utils/index.js";
import { rainbowkitBurnerConnector, rainbowkitSessionStorageBurnerConnector } from "./rainbowkitBurnerConnector.js";

Expand All @@ -8,6 +8,7 @@ const burnerWalletIconBase64 =
type RainbowkitBurnerWallet = {
(): Wallet;
useSessionStorage?: boolean;
rpcUrls?: Record<number, string>;
};

/**
Expand All @@ -18,7 +19,18 @@ export const rainbowkitBurnerWallet: RainbowkitBurnerWallet = () => ({
name: burnerWalletName,
iconUrl: burnerWalletIconBase64,
iconBackground: "#ffffff",
createConnector: rainbowkitBurnerWallet.useSessionStorage
? rainbowkitSessionStorageBurnerConnector
: rainbowkitBurnerConnector,
createConnector: (params: WalletDetailsParams) => {
const connector = rainbowkitBurnerWallet.useSessionStorage
? rainbowkitSessionStorageBurnerConnector
: rainbowkitBurnerConnector;

return connector(
{
...params,
},
{
rpcUrls: rainbowkitBurnerWallet.rpcUrls,
},
);
},
});

0 comments on commit 08b7dc7

Please sign in to comment.