Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/dapp/components/connect/connect-home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { ChainSlug } from '@intent/types'
import dynamic from 'next/dynamic'
import { useRouter, useSearchParams } from 'next/navigation'

import { installNetworkFetch } from '../../lib/api/network-fetch'
import { installPageFetch } from '../../lib/api/network-fetch'
import { rememberChain } from '../../lib/wallet/chain-cookie'
import { screenAfterConnect } from '../../lib/wallet/chain-path'

Expand All @@ -37,7 +37,7 @@ export function ConnectHome(): JSX.Element {
const network = parseChainSegment(stellarHome)?.network ?? defaultNetwork()
if (typeof window !== 'undefined') {
setClientNetwork(network)
if (isMultiNetwork()) installNetworkFetch(network)
installPageFetch(network)
}

function onConnected(slug: ChainSlug): void {
Expand Down
23 changes: 22 additions & 1 deletion apps/dapp/lib/__tests__/network-fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'

import {
installPageFetch,
networkFetch,
setAttributedRace,
setConnectedWallet,
Expand Down Expand Up @@ -112,3 +113,23 @@ describe('networkFetch wallet header', () => {
expect(seen[1]?.get('x-intent-wallet')).toBeNull()
})
})

describe('installPageFetch', () => {
it('names the wallet on every call to this app, on a deployment that serves one network', async () => {
vi.stubEnv('NEXT_PUBLIC_STELLAR_NETWORKS', 'mainnet')
const { base, calls } = spy()
const page = { fetch: base, location: { origin: ORIGIN } } as unknown as Window
vi.stubGlobal('window', page)
try {
setConnectedWallet('GWALLET')
installPageFetch('mainnet')
await page.fetch(`${ORIGIN}/api/naira/orders`)
await page.fetch(`${ORIGIN}/api/naira/rate`)
} finally {
setConnectedWallet(undefined)
vi.unstubAllGlobals()
vi.unstubAllEnvs()
}
expect(calls.map((c) => c.headers.get('x-intent-wallet'))).toEqual(['GWALLET', 'GWALLET'])
})
})
11 changes: 11 additions & 0 deletions apps/dapp/lib/api/network-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ export function walletFetch(input: string, init?: RequestInit): Promise<Response

const BASE_KEY = '__intentBaseFetch'

/**
* Every page's own API calls carry the connected wallet, whatever the deployment serves.
*
* Installing only where several networks are served left a one-network deployment
* sending no wallet at all: with sign-in paused, every route that keys records by
* wallet answered "connect your wallet" to a connected one, and showed no history.
*/
export function installPageFetch(network: StellarNetworkName): void {
installNetworkFetch(network)
}

/** Browser only. Safe to call again with another network; it wraps the original, never the wrapper. */
export function installNetworkFetch(network: StellarNetworkName): void {
if (typeof window === 'undefined') return
Expand Down
5 changes: 2 additions & 3 deletions apps/dapp/providers/chain-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import {
defaultNetwork,
isMultiNetwork,
parseChainSegment,
setClientNetwork,
type StellarNetworkName,
} from '@intent/config'
import type { ChainDescriptor, ChainSlug } from '@intent/types'
import { createContext, useContext, useMemo } from 'react'

import { installNetworkFetch } from '../lib/api/network-fetch'
import { installPageFetch } from '../lib/api/network-fetch'
import type { ChainAdapter } from '../lib/chain-adapter'
import { adapterFor } from '../lib/adapters'

Expand Down Expand Up @@ -46,7 +45,7 @@ export function ChainProvider({
// server renders overlap, and a shared variable would leak between requests.
if (typeof window !== 'undefined') {
setClientNetwork(network ?? defaultNetwork())
if (isMultiNetwork()) installNetworkFetch(network ?? defaultNetwork())
installPageFetch(network ?? defaultNetwork())
}

const value = useMemo<ChainContextValue>(() => {
Expand Down
Loading