Skip to content

Commit d46e702

Browse files
committed
fix(synapse-core): throw UnsupportedChainError from getChain and getTransport
Normalize unsupported-chain failures onto one error type, and give devnet a default local HTTP transport so all asChain-supported networks have a getTransport path.
1 parent e6fb33f commit d46e702

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

packages/synapse-core/src/chains.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,15 @@ export const devnet: FilecoinChain = {
322322
genesisTimestamp: 0,
323323
}
324324

325+
export namespace getChain {
326+
export type ErrorType = UnsupportedChainError
327+
}
328+
325329
/**
326330
* Get a chain by id
327331
*
328332
* @param [id] - The chain id. Defaults to mainnet.
333+
* @throws Errors {@link getChain.ErrorType}
329334
*/
330335
export function getChain(id?: number): FilecoinChain {
331336
if (id == null) {
@@ -340,7 +345,7 @@ export function getChain(id?: number): FilecoinChain {
340345
case 31415926:
341346
return devnet
342347
default:
343-
throw new Error(`Chain with id ${id} not found`)
348+
throw new UnsupportedChainError(id)
344349
}
345350
}
346351

packages/synapse-core/src/client.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type Client, fallback, http, type RpcSchema, type Transport, type Chain as ViemChain } from 'viem'
22
import type { Account } from 'viem/accounts'
33
import type { FilecoinChain } from './chains.ts'
4-
import { asChain, calibration, mainnet } from './chains.ts'
4+
import { asChain, calibration, devnet, mainnet } from './chains.ts'
5+
import { UnsupportedChainError } from './errors/chains.ts'
56
import type { Extended } from './types.ts'
67

78
/**
@@ -29,11 +30,25 @@ export const calibrationTransport = fallback(
2930
)
3031

3132
/**
32-
* Get the default ranked fallback transport for a Filecoin chain.
33+
* Local HTTP transport for Filecoin Devnet.
34+
*
35+
* Uses the chain's default RPC; there is no public fallback set.
36+
*/
37+
export const devnetTransport = http(devnet.rpcUrls.default.http[0])
38+
39+
export namespace getTransport {
40+
export type ErrorType = UnsupportedChainError
41+
}
42+
43+
/**
44+
* Get the default HTTP transport for a Filecoin chain.
45+
*
46+
* Mainnet and Calibration use fallbacks across public RPC endpoints. Devnet
47+
* uses the local default RPC.
3348
*
3449
* @param chain - The viem chain to resolve a transport for.
35-
* @returns The ranked fallback transport for mainnet or Calibration.
36-
* @throws Error if the chain is not supported.
50+
* @returns The default transport for the chain.
51+
* @throws Errors {@link getTransport.ErrorType}
3752
*/
3853
export function getTransport(chain: ViemChain): Transport {
3954
if (chain.id === mainnet.id) {
@@ -44,7 +59,11 @@ export function getTransport(chain: ViemChain): Transport {
4459
return calibrationTransport
4560
}
4661

47-
throw new Error(`Unsupported chain: ${chain.id}`)
62+
if (chain.id === devnet.id) {
63+
return devnetTransport
64+
}
65+
66+
throw new UnsupportedChainError(chain.id)
4867
}
4968

5069
/**

packages/synapse-core/test/chains.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ describe('chains', () => {
2121
assert.strictEqual(getChain(31_415_926), devnet)
2222
})
2323

24-
it('should throw for unknown chain id', () => {
25-
assert.throws(() => getChain(999), /Chain with id 999 not found/)
24+
it('should throw UnsupportedChainError for unknown chain id', () => {
25+
assert.throws(
26+
() => getChain(999),
27+
(err: unknown) => UnsupportedChainError.is(err)
28+
)
2629
})
2730
})
2831

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import assert from 'assert'
2+
import type { Chain as ViemChain } from 'viem'
3+
import { calibration, devnet, mainnet } from '../src/chains.ts'
4+
import { calibrationTransport, devnetTransport, getTransport, mainnetTransport } from '../src/client.ts'
5+
import { UnsupportedChainError } from '../src/errors/chains.ts'
6+
7+
describe('client', () => {
8+
describe('getTransport', () => {
9+
it('should return the mainnet fallback transport', () => {
10+
assert.strictEqual(getTransport(mainnet), mainnetTransport)
11+
})
12+
13+
it('should return the calibration fallback transport', () => {
14+
assert.strictEqual(getTransport(calibration), calibrationTransport)
15+
})
16+
17+
it('should return the devnet HTTP transport', () => {
18+
assert.strictEqual(getTransport(devnet), devnetTransport)
19+
})
20+
21+
it('should throw UnsupportedChainError for an unknown chain', () => {
22+
const unknownChain = { id: 1 } as ViemChain
23+
assert.throws(
24+
() => getTransport(unknownChain),
25+
(err: unknown) => UnsupportedChainError.is(err)
26+
)
27+
})
28+
})
29+
})

0 commit comments

Comments
 (0)