|
| 1 | +# @goodwidget/core |
| 2 | + |
| 3 | +Runtime helpers for GoodWidget providers, host detection, wallet context, and viem client setup. |
| 4 | + |
| 5 | +## viem fallback clients |
| 6 | + |
| 7 | +`createViemFallbackClient` wraps viem `createPublicClient` and `createWalletClient` so callers get a fallback HTTP transport built from cached Chainlist RPC URLs for the requested chain. |
| 8 | + |
| 9 | +The helper accepts a small key-value storage adapter. On initialization it reads cached RPC metadata from storage. If the cache is missing or older than 1 day, it starts a background refresh from Chainlist. Client creation waits for that first refresh only when no cached RPCs are available for the requested chain. |
| 10 | + |
| 11 | +```ts |
| 12 | +import { createViemFallbackClient } from '@goodwidget/core/viemFallbackClient' |
| 13 | +import { celo } from 'viem/chains' |
| 14 | + |
| 15 | +const viemClient = createViemFallbackClient(localStorage, { |
| 16 | + onError(error) { |
| 17 | + console.warn('RPC refresh failed', error) |
| 18 | + }, |
| 19 | +}) |
| 20 | + |
| 21 | +const publicClient = await viemClient.createPublicClient({ |
| 22 | + chain: celo, |
| 23 | + fallbackRpcs: ['https://forno.celo.org'], |
| 24 | +}) |
| 25 | +``` |
| 26 | + |
| 27 | +The same helper can create wallet clients. Any viem wallet client option, such as `account`, can be passed through. |
| 28 | + |
| 29 | +```ts |
| 30 | +import { privateKeyToAccount } from 'viem/accounts' |
| 31 | +import { celo } from 'viem/chains' |
| 32 | + |
| 33 | +const account = privateKeyToAccount('0x...') |
| 34 | + |
| 35 | +const walletClient = await viemClient.createWalletClient({ |
| 36 | + account, |
| 37 | + chain: celo, |
| 38 | + fallbackRpcs: ['https://forno.celo.org'], |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 42 | +If you pass `transport`, the wrapper leaves it unchanged and does not create a fallback transport for that client. |
| 43 | + |
| 44 | +```ts |
| 45 | +import { http } from 'viem' |
| 46 | +import { celo } from 'viem/chains' |
| 47 | + |
| 48 | +const client = await viemClient.createPublicClient({ |
| 49 | + chain: celo, |
| 50 | + transport: http('https://forno.celo.org'), |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +## Storage adapters |
| 55 | + |
| 56 | +The adapter supports browser-style storage and Worker-style KV storage. |
| 57 | + |
| 58 | +```ts |
| 59 | +createViemFallbackClient(localStorage) |
| 60 | +``` |
| 61 | + |
| 62 | +```ts |
| 63 | +createViemFallbackClient({ |
| 64 | + get: (key) => env.KV.get(key, 'json'), |
| 65 | + put: (key, value) => env.KV.put(key, value), |
| 66 | +}) |
| 67 | +``` |
| 68 | + |
| 69 | +Cached values use this shape: |
| 70 | + |
| 71 | +```ts |
| 72 | +type ViemRpcCacheEntry = { |
| 73 | + fetchedAt: string |
| 74 | + rpcs: Array<{ |
| 75 | + chainId: number |
| 76 | + rpcs: string[] |
| 77 | + }> |
| 78 | +} |
| 79 | +``` |
| 80 | +
|
| 81 | +Only HTTPS RPC URLs are used. URLs containing Chainlist template placeholders are ignored. |
| 82 | +
|
| 83 | +## Options |
| 84 | +
|
| 85 | +```ts |
| 86 | +createViemFallbackClient(storage, { |
| 87 | + cacheKey: 'goodwidget:viem-rpcs', |
| 88 | + chainlistRpcsUrl: 'https://chainlist.org/rpcs.json', |
| 89 | + refreshIntervalMs: 24 * 60 * 60 * 1000, |
| 90 | + fetchTimeoutMs: 10_000, |
| 91 | + fetch: globalThis.fetch, |
| 92 | + onError: console.warn, |
| 93 | +}) |
| 94 | +``` |
| 95 | +
|
| 96 | +`chainlistRpcsUrl` is intended for Chainlist's HTTPS RPC JSON endpoint. The helper blocks non-HTTPS URLs and hosts outside the expected Chainlist domain. |
0 commit comments