Skip to content

feat(widgets): adopt createViemFallbackClient across widget read clients (#167) - #288

Open
deamybro wants to merge 7 commits into
GoodDollar:mainfrom
deamybro:feat/adopt-viem-fallback-client-167
Open

deamybro wants to merge 7 commits into
GoodDollar:mainfrom
deamybro:feat/adopt-viem-fallback-client-167

Conversation

@deamybro

@deamybro deamybro commented Nov 6, 2026 •

Copy link
Copy Markdown

Resolves #167

Summary

Adopts createViemFallbackClient from @goodwidget/core across remaining manual http() viem read-client call sites while preserving provider-backed custom(provider) wallet clients and explicit RPC override precedence:

  • packages/goodreserve-widget/src/useReserveBootstrap.ts:
    • Replaces createPublicClient({ chain, transport: http() }) in bootstrapSdk with createReserveFallbackClient().createPublicClient({ chain }) (rank: false, tracked chains [CELO_CHAIN_ID, XDC_CHAIN_ID]).
    • Preserves provider-backed walletClient on custom(provider).
  • packages/streaming-widget/src/adapter/chains.ts & useStreamingClients.ts:
    • Adds createStreamingFallbackClient() and createStreamingPublicClient(chain, fallbackRpcs) backed by createViemFallbackClient with chain default RPCs as primary fallbackRpcs.
    • Updates createBasePublicClient() and useStreamingClients() to use createStreamingPublicClient(...) while keeping walletClient on custom(provider).
  • packages/citizen-claim-widget/src/adapter.ts:
    • Replaces manual http(rpcUrl) read clients in getPublicClientForChain() and createReadOnlyClientsForChain() with createCitizenClaimFallbackClient() while leaving custodial and integrator-supplied clientFactory bundles untouched.
  • packages/staking-migration-widget/src/adapter.ts:
    • Replaces the fixed Fuse http(...) public client with createStakingMigrationFallbackClient().createPublicClient({ chain: FUSE_CHAIN, fallbackRpcs }) while keeping walletClient on custom(provider).
  • examples/storybook/src/fixtures/custodialEip1193.ts:
    • Replaces per-chain fixed http(config.rpcUrl) public and wallet clients with createCustodialFallbackClient() keeping config.rpcUrl as primary fallbackRpcs.
    • Fixes custodial sendTransaction chain binding to selected client and ensures deterministic RPC error testing.

Verification

  • Ran @goodwidget/core unit tests (pnpm --filter @goodwidget/core test — all 17 tests pass).
  • Verified build and compatibility of all widget adapters (@goodwidget/goodreserve-widget, @goodwidget/streaming-widget, @goodwidget/citizen-claim-widget, @goodwidget/staking-migration-widget).
  • Maintained strict non-breaking API compatibility for external integrators and existing custom wallet providers.

Summary by Sourcery

Adopt fallback RPC handling across widget read clients while retaining existing wallet-provider and RPC-override behavior.

Bug Fixes:

  • Make the custodial Storybook fixture bind submitted transactions to the selected chain and keep RPC failure tests deterministic.

Enhancements:

  • Use fallback RPC clients for reserve, streaming, citizen-claim, staking-migration, and custodial Storybook reads, preserving caller-provided RPCs as the primary endpoints and provider-backed wallet clients where applicable.

Tests:

  • Add core tests for fallback RPC discovery and precedence across widget chain configurations, and make the citizen-claim unreachable-RPC browser test deterministic.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The custom bridge adds retries on top of the fallback transport’s existing policy, multiplying RPC requests and delaying the widget’s error state when endpoints are unreachable or rate-limited.

1 open issue
What changed in this PR

Adopts core’s createViemFallbackClient across selected widget read clients and the Storybook custodial fixture for #167, while retaining provider-backed signing and primary RPC ordering.

Changes:

  • Migrates GoodReserve, Streaming, Citizen Claim, and Staking Migration read clients to fallback-enabled construction.
  • Updates custodial fixture clients and binds transactions to the selected client’s chain.
  • Makes Citizen Claim error tests block deterministic discovered RPCs alongside primary endpoints.
File Description
tests/​widgets/​citizen-claim-widget/​states.spec.ts Controls RPC discovery and blocks fallback endpoints in error tests.
packages/​streaming-widget/​src/​adapter/​useStreamingClients.ts Uses fallback-enabled reads while preserving wallet transport.
packages/​streaming-widget/​src/​adapter/​chains.ts Adds fallback client builders with a synchronous public-client interface.
packages/​staking-migration-widget/​src/​adapter.ts Uses asynchronous fallback clients for Fuse reads and receipts.
packages/​goodreserve-widget/​src/​useReserveBootstrap.ts Initializes reserve reads through the fallback helper.
packages/​citizen-claim-widget/​src/​adapter.ts Migrates read clients and awaits their construction.
examples/​storybook/​src/​fixtures/​custodialEip1193.ts Adds fallback clients and preserves transaction chain binding.

Review effort: Balanced

@sourcery-ai

sourcery-ai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit b4b3e74

#167 Adopt createViemFallbackClient in manual viem client call sites · 6 of 9 met · 1 to check by hand

# Requirement Status Notes
1 Replace the listed fixed-HTTP read clients with createViemFallbackClient ✅ Met The Reserve, Streaming, Citizen Claim, Staking Migration and Storybook paths use the helper; the listed AI Credits paths also use it in the current code.
2 Keep provider-backed wallet clients on custom(provider) ✅ Met The changed Reserve, Streaming and Staking Migration wallet clients retain provider-backed custom transports.
3 Keep explicit RPC overrides as primary endpoints ✅ Met The changed configurable paths pass their supplied or configured RPC URLs as fallbackRpcs ahead of discovered endpoints.
4 Avoid changing custodial client injection contracts and client-factory APIs ✅ Met The Citizen Claim custodial and integrator-supplied client paths are left intact.
5 Add targeted tests for changed client-construction logic 🟡 Partly met The new core tests check fallback resolution for the adopted chain configurations, but they recreate those configurations rather than invoking the widget client-construction wrappers.
6 Share cached RPC discovery, viem fallback ranking and consistent RPC ordering 🟡 Partly met The changed widget wrappers use the default ranked fallback, but the AI Credits fallback client still sets rank: false.
7 Existing widget/example tests continue to pass 👀 Check by hand (runtime) Run the affected widget and example test suites to confirm they pass; the supplied verification reports core tests and package builds, but not those suites.
8 Do not broaden the work into unrelated RPC, theming or widget refactors ✅ Met
9 Deliver the adoption outside the fallback-helper PR ✅ Met This change adopts the existing core helper rather than introducing the helper itself.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New gaps against the linked issues

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New gaps against the linked issues

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New gaps against the linked issues

Comment on lines +260 to +352
test('adopted client construction: reserve client tracks Celo and XDC chains with fallback ranking enabled', async () => {
const celoChainId = 42220
const xdcChainId = 50
const client = createViemFallbackClient(createStorage(), {
chainIds: [celoChainId, xdcChainId],
fetch: async () =>
new Response(
JSON.stringify([
{ chainId: celoChainId, rpc: ['https://celo-discovered.example'] },
{ chainId: xdcChainId, rpc: ['https://xdc-discovered.example'] },
]),
{ status: 200, headers: { 'content-type': 'application/json' } },
),
})

await client.ready

const celoChain = { ...createChain(['https://forno.celo.org']), id: celoChainId }
const xdcChain = { ...createChain(['https://rpc.ankr.com/xdc']), id: xdcChainId }

const celoUrls = await client.getRpcUrls(celoChain)
const xdcUrls = await client.getRpcUrls(xdcChain)

assert.deepEqual(celoUrls, ['https://forno.celo.org', 'https://celo-discovered.example'])
assert.deepEqual(xdcUrls, ['https://rpc.ankr.com/xdc', 'https://xdc-discovered.example'])
})

test('adopted client construction: streaming client tracks Celo and Base with caller-first fallback ordering', async () => {
const baseChainId = 8453
const celoChainId = 42220
const client = createViemFallbackClient(createStorage(), {
chainIds: [celoChainId, baseChainId],
fetch: async () =>
new Response(
JSON.stringify([
{ chainId: baseChainId, rpc: ['https://base-discovered.example'] },
]),
{ status: 200, headers: { 'content-type': 'application/json' } },
),
})

await client.ready

const baseChain = { ...createChain(['https://mainnet.base.org']), id: baseChainId }
const baseUrls = await client.getRpcUrls(baseChain, ['https://caller-override.example'])

assert.deepEqual(baseUrls, [
'https://caller-override.example',
'https://mainnet.base.org',
'https://base-discovered.example',
])
})

test('adopted client construction: staking migration client tracks Fuse chain', async () => {
const fuseChainId = 122
const client = createViemFallbackClient(createStorage(), {
chainIds: [fuseChainId],
fetch: async () =>
new Response(
JSON.stringify([
{ chainId: fuseChainId, rpc: ['https://fuse-discovered.example'] },
]),
{ status: 200, headers: { 'content-type': 'application/json' } },
),
})

await client.ready

const fuseChain = { ...createChain(['https://rpc.fuse.io']), id: fuseChainId }
const fuseUrls = await client.getRpcUrls(fuseChain)

assert.deepEqual(fuseUrls, ['https://rpc.fuse.io', 'https://fuse-discovered.example'])
})

test('adopted client construction: custodial Storybook fixture tracks multi-chain set', async () => {
const chainIds = [42220, 122, 50, 8453]
const client = createViemFallbackClient(createStorage(), {
chainIds,
fetch: async () =>
new Response(
JSON.stringify(chainIds.map((id) => ({ chainId: id, rpc: [`https://chain-${id}.example`] }))),
{ status: 200, headers: { 'content-type': 'application/json' } },
),
})

await client.ready

for (const id of chainIds) {
const chain = { ...createChain([]), id }
const urls = await client.getRpcUrls(chain)
assert.deepEqual(urls, [`https://chain-${id}.example`])
}
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Exercise widget client factories

The new tests create the core fallback client with representative chain settings, but do not call the widget-local factories changed in this PR. Add tests that exercise those factory paths so their wiring and RPC arguments are covered.

Prompt for AI agents
In `packages/core/src/viemFallbackClient.test.ts` at lines 260-352:

**Exercise widget client factories**

The new tests create the core fallback client with representative chain settings, but do not call the widget-local factories changed in this PR. Add tests that exercise those factory paths so their wiring and RPC arguments are covered.

From #167:

Add targeted tests covering adopted client-construction paths.

@sourcery-ai
sourcery-ai Bot dismissed their stale review November 30, 2026 21:26

Sourcery withdrew this approval because the latest commits introduced blocking findings.

@deamybro
deamybro force-pushed the feat/adopt-viem-fallback-client-167 branch from c0ff362 to b4b3e74 Compare December 3, 2026 00:00
ocakciomursel-pixel and others added 5 commits December 4, 2026 19:38
…id duplicate RPC attempts

Configure retryCount: 0 on custom bridge transport forwarding to createStreamingFallbackClient so viem's default outer retry policy does not duplicate the underlying fallback transport's retry policy, preventing request multiplication and delays in error state.
…GoodDollar#167)

Remove explicit rank: false overrides so createViemFallbackClient enables ranking by measured health across all adopted widgets as required by GoodDollar#167.

Signed-off-by: deamybro <deamybro@users.noreply.github.com>
Payout: 0x759797593fed8f313d8fdba7a3a0d491f0e4b9f7 (USDC on Base L2)
… configurations (GoodDollar#167)

Add unit test coverage verifying fallback discovery and chain tracking for Reserve, Streaming, Staking Migration, and multi-chain Custodial client configurations.

Signed-off-by: deamybro <deamybro@users.noreply.github.com>
Payout: 0x759797593fed8f313d8fdba7a3a0d491f0e4b9f7 (USDC on Base L2)
@deamybro
deamybro force-pushed the feat/adopt-viem-fallback-client-167 branch from b4b3e74 to d590aa9 Compare December 4, 2026 18:40
Copilot AI balanced review requested due to automatic review settings December 4, 2026 18:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

RPC fallback changes can override caller endpoint precedence, silently discard valid HTTP overrides, and accumulate background probes when short-lived clients are recreated.

3 open issues
1 resolved since last review

Review effort: Balanced

const publicClient = createPublicClient({ chain, transport: http(rpcUrl) })
const publicClient = await createReserveFallbackClient().createPublicClient({
chain,
...(rpcUrl ? { fallbackRpcs: [rpcUrl] } : {}),
Copilot AI balanced review requested due to automatic review settings December 5, 2026 07:07
…aller overrides (GoodDollar#167)

Signed-off-by: deamybro <193015196+deamybro@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because a case-sensitive scheme check can silently discard valid RPC overrides and route reserve reads to default or discovered endpoints instead.

1 open issue
2 resolved since last review

Review effort: Balanced

@deamybro

deamybro commented Dec 5, 2026

Copy link
Copy Markdown
Author

Addressed all three Copilot review findings in commit \40b8ef5:

  1. Disabled background latency ranking (
    ank: false)
    across the adopted fallback clients (\createCitizenClaimFallbackClient, \createStreamingFallbackClient, \createStakingMigrationFallbackClient, \createReserveFallbackClient, and \createCustodialFallbackClient) to avoid repeating background RPC probes on short-lived/recreated clients and preserve caller primary-RPC precedence (e.g. \xdcRpcUrl).
  2. Preserved validated HTTP caller overrides (\http://localhost:8545\) in \getRpcUrls\ via separate caller-override filtering (\https://\ and \http://), while keeping discovered Chainlist endpoints strictly \https://-only.
  3. Verification:
    ode --test --experimental-strip-types packages/core/src/*.test.ts\ (21/21 passing, including caller HTTP override coverage) and \pnpm build\ across @goodwidget/core, @goodwidget/goodreserve-widget, @goodwidget/streaming-widget, @goodwidget/citizen-claim-widget, and @goodwidget/staking-migration-widget\ passed cleanly.

… filtering (GoodDollar#167)

Signed-off-by: deamybro <193015196+deamybro@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 5, 2026 22:03
@deamybro

deamybro commented Dec 5, 2026 •

Copy link
Copy Markdown
Author

Updated filterRpcUrls in commit a39afd2 to normalize scheme checks case-insensitively (e.g., HTTP://localhost:8545) with whitespace trimming, and updated the unit test in packages/core/src/viemFallbackClient.test.ts (21/21 core tests passing).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate the impact of duplicate Chainlist discovery requests from separate client helpers on cold or stale caches.

1 open issue

Review effort: Balanced

This branch is waiting to be deployed

1 waiting (outdated) deployment
external — c0ff3620 Waiting Nov 30, 2026 by sourcery-ai[bot] via authorize-gooddata-web-preview #286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adopt createViemFallbackClient in manual viem client call sites

3 participants