Skip to content

Commit 1b8b54e

Browse files
authored
feat: support custom client for reads (#909)
1 parent 3b0af02 commit 1b8b54e

101 files changed

Lines changed: 605 additions & 850 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/package.json

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,43 @@
44
"version": "0.0.1",
55
"private": true,
66
"scripts": {
7-
"dev": "astro dev",
8-
"build": "astro build",
7+
"dev": "wireit",
8+
"build": "wireit",
99
"preview": "astro preview",
1010
"astro": "astro"
1111
},
12+
"wireit": {
13+
"build": {
14+
"command": "astro build",
15+
"files": [
16+
"src/**/*.ts",
17+
"tsconfig.json",
18+
"astro.config.mjs"
19+
],
20+
"output": [
21+
"dist/**"
22+
],
23+
"dependencies": [
24+
"../packages/synapse-core:build",
25+
"../packages/synapse-sdk:build",
26+
"../packages/synapse-react:build"
27+
]
28+
},
29+
"dev": {
30+
"command": "astro dev",
31+
"service": true,
32+
"files": [
33+
"src/**/*.ts",
34+
"tsconfig.json",
35+
"astro.config.mjs"
36+
],
37+
"dependencies": [
38+
"../packages/synapse-core:build",
39+
"../packages/synapse-sdk:build",
40+
"../packages/synapse-react:build"
41+
]
42+
}
43+
},
1244
"dependencies": {
1345
"@filoz/synapse-core": "workspace:*",
1446
"@filoz/synapse-react": "workspace:*",

docs/src/components/contract-addresses.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
import type { Chain } from '@filoz/synapse-core/chains'
2+
import type { FilecoinChain } from '@filoz/synapse-core/chains'
33
44
interface Props {
5-
chain: Chain
5+
chain: FilecoinChain
66
}
77
88
const { chain } = Astro.props

docs/src/content/docs/developer-guides/synapse-core.mdx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,29 @@ All Synapse Core functions accept a viem `Client` as their first argument. Creat
4747

4848
```ts twoslash
4949
// @lib: esnext,dom
50-
import { createPublicClient, createWalletClient, http } from "viem"
50+
import { createPublicClient, createWalletClient } from "viem"
5151
import { privateKeyToAccount } from "viem/accounts"
52-
import { calibration, mainnet } from "@filoz/synapse-core/chains"
52+
import { calibration } from "@filoz/synapse-core/chains"
53+
import { getTransport } from "@filoz/synapse-core/client"
5354

5455
// Read-only client for queries
5556
const publicClient = createPublicClient({
5657
chain: calibration, // or mainnet
57-
transport: http(),
58+
transport: getTransport(calibration),
5859
})
5960

6061
// Wallet client for transactions
6162
const account = privateKeyToAccount("0x...")
6263
const walletClient = createWalletClient({
6364
account,
6465
chain: calibration, // or mainnet
65-
transport: http(),
66+
transport: getTransport(calibration),
6667
})
6768
```
6869

69-
The `@filoz/synapse-core/chains` subpath exports chain definitions with all contract addresses pre-configured for Filecoin Mainnet (`mainnet`) and Filecoin testnet (`calibration`) networks.
70+
The `@filoz/synapse-core/chains` subpath exports chain definitions with all contract addresses pre-configured for Filecoin Mainnet (`mainnet`) and Filecoin testnet (`calibration`) networks. Import the FOC chain type as `FilecoinChain` when you need it in TypeScript.
71+
72+
The `@filoz/synapse-core/client` subpath exports `getTransport` (ranked Filecoin RPC fallbacks), `asClient`, and `toReadClient`. Prefer `getTransport(chain)` over a bare `http()` endpoint when talking to mainnet or Calibration.
7073

7174
## Pagination
7275

docs/src/content/docs/developer-guides/synapse.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ graph LR
5555
- **`StorageManager`**, **`StorageContext`**: Storage operation modules
5656
- **`WarmStorageService`**: Storage coordination and pricing module
5757

58+
`Synapse` exposes `client` (account client for signing and writes) and `readClient` (for queries). When constructing from an existing viem client, you can pass an optional public `readClient`:
59+
60+
```ts
61+
const synapse = new Synapse({
62+
client: walletClient,
63+
readClient: publicClient, // optional; derived from client when omitted
64+
})
65+
```
66+
5867
## Payment Operations
5968

6069
Fund your account and manage payments for Filecoin storage services.

examples/cli/src/client.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { execSync } from 'node:child_process'
22
import { basename, dirname } from 'node:path'
33
import * as p from '@clack/prompts'
4-
import { type Chain, getChain } from '@filoz/synapse-core/chains'
5-
import {
6-
createPublicClient,
7-
createWalletClient,
8-
type Hex,
9-
type HttpTransport,
10-
http,
11-
type PrivateKeyAccount,
12-
type PublicClient,
13-
type WalletClient,
14-
} from 'viem'
15-
import { privateKeyToAccount } from 'viem/accounts'
4+
import type { AccountClient, ReadClient } from '@filoz/synapse-core'
5+
import { type FilecoinChain, getChain } from '@filoz/synapse-core/chains'
6+
import { getTransport } from '@filoz/synapse-core/client'
7+
import { createPublicClient, createWalletClient, type Hex } from 'viem'
8+
import { type Address, privateKeyToAccount } from 'viem/accounts'
169
import config from './config.ts'
1710

1811
function privateKeyFromConfig() {
@@ -47,8 +40,9 @@ function privateKeyFromConfig() {
4740
}
4841

4942
export function privateKeyClient(chainId: number): {
50-
client: WalletClient<HttpTransport, Chain, PrivateKeyAccount>
51-
chain: Chain
43+
client: AccountClient
44+
chain: FilecoinChain
45+
address: Address
5246
} {
5347
const chain = getChain(chainId)
5448

@@ -58,21 +52,20 @@ export function privateKeyClient(chainId: number): {
5852
const client = createWalletClient({
5953
account,
6054
chain,
61-
transport: http(),
55+
transport: getTransport(chain),
6256
})
6357
return {
6458
client,
6559
chain,
60+
address: account.address,
6661
}
6762
}
6863

69-
export function publicClient(
70-
chainId: number
71-
): PublicClient<HttpTransport, Chain> {
64+
export function publicClient(chainId: number): ReadClient {
7265
const chain = getChain(chainId)
7366
const publicClient = createPublicClient({
7467
chain,
75-
transport: http(),
68+
transport: getTransport(chain),
7669
})
7770
return publicClient
7871
}

examples/cli/src/commands/datasets-create.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const datasetsCreate: Command = command(
3131
},
3232
async (argv) => {
3333
const { client, chain } = privateKeyClient(argv.flags.chain)
34-
3534
try {
3635
const provider = argv._.providerId
3736
? await getPDPProvider(client, {

examples/cli/src/commands/datasets-terminate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ export const datasetsTerminate: Command = command(
2929
},
3030
},
3131
async (argv) => {
32-
const { client, chain } = privateKeyClient(argv.flags.chain)
33-
32+
const { client, chain, address } = privateKeyClient(argv.flags.chain)
3433
try {
3534
const dataSetId = argv._.dataSetId
3635
? BigInt(argv._.dataSetId)
37-
: await selectDataSet(client, argv.flags)
36+
: await selectDataSet(client, address, argv.flags)
3837
p.log.info(`Terminating data set ${dataSetId}...`)
3938

4039
let endEpoch: bigint

examples/cli/src/commands/pieces-removal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export const piecesRemoval: Command = command(
2222
},
2323
},
2424
async (argv) => {
25-
const { client, chain } = privateKeyClient(argv.flags.chain)
25+
const { client, chain, address } = privateKeyClient(argv.flags.chain)
2626

2727
try {
2828
const dataSetId = argv._.dataSetId
2929
? BigInt(argv._.dataSetId)
30-
: await selectDataSet(client, argv.flags)
30+
: await selectDataSet(client, address, argv.flags)
3131

3232
const dataSet = await getPdpDataSet(client, {
3333
dataSetId,
@@ -39,7 +39,7 @@ export const piecesRemoval: Command = command(
3939

4040
const pieceId = argv._.pieceId
4141
? BigInt(argv._.pieceId)
42-
: await selectPiece(client, dataSet, argv.flags)
42+
: await selectPiece(client, address, dataSet, argv.flags)
4343

4444
p.log.info(`Removing piece ${pieceId} from data set ${dataSetId}...`)
4545
const result = await schedulePieceDeletions(client, {

examples/cli/src/commands/pieces.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import { metadataArrayToObject } from '@filoz/synapse-core/utils'
66
import { getPdpDataSets, type Piece } from '@filoz/synapse-core/warm-storage'
77
import { Synapse } from '@filoz/synapse-sdk'
88
import { type Command, command } from 'cleye'
9-
import { createPublicClient, type Hex, http, stringify } from 'viem'
9+
import { type Hex, stringify } from 'viem'
1010
import { readContract, waitForTransactionReceipt } from 'viem/actions'
1111
import { privateKeyClient } from '../client.ts'
1212
import { globalFlags } from '../flags.ts'
1313

14-
const publicClient = createPublicClient({
15-
chain: calibration,
16-
transport: http(),
17-
})
18-
1914
export const pieces: Command = command(
2015
{
2116
name: 'pieces',
@@ -30,16 +25,15 @@ export const pieces: Command = command(
3025
},
3126
},
3227
async (argv) => {
33-
const { client } = privateKeyClient(argv.flags.chain)
34-
28+
const { client, address } = privateKeyClient(argv.flags.chain)
3529
const spinner = p.spinner()
3630

3731
spinner.start('Fetching data sets...')
3832
try {
3933
const dataSets = await Array.fromAsync(
4034
paginate(({ cursor }) =>
4135
getPdpDataSets(client, {
42-
address: client.account.address,
36+
address,
4337
cursor,
4438
})
4539
)
@@ -68,7 +62,7 @@ export const pieces: Command = command(
6862
dataSet: dataSets.find(
6963
(dataSet) => dataSet.dataSetId === dataSetId
7064
)!,
71-
address: client.account.address,
65+
address,
7266
cursor,
7367
})
7468
)
@@ -111,7 +105,7 @@ export const pieces: Command = command(
111105
if (group.action === 'info') {
112106
// biome-ignore lint/style/noNonNullAssertion: pieceId is guaranteed to be found
113107
const piece = pieces.find((piece) => piece.id === group.pieceId)!
114-
const metadata = await readContract(publicClient, {
108+
const metadata = await readContract(client, {
115109
address: calibration.contracts.fwssView.address,
116110
abi: calibration.contracts.fwssView.abi,
117111
functionName: 'getAllPieceMetadata',
@@ -140,7 +134,7 @@ export const pieces: Command = command(
140134
})
141135
const txHash = await context.deletePiece({ piece: piece.cid })
142136
spinner.message('Waiting for transaction to be mined...')
143-
await waitForTransactionReceipt(publicClient, { hash: txHash as Hex })
137+
await waitForTransactionReceipt(client, { hash: txHash as Hex })
144138
spinner.stop('Piece deleted')
145139
} else {
146140
return

examples/cli/src/commands/upload-dataset.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export const uploadDataset: Command = command(
3131

3232
const filePath = argv._.path
3333
const provider = argv._.providerId
34-
? await getPDPProvider(client, { providerId: BigInt(argv._.providerId) })
34+
? await getPDPProvider(client, {
35+
providerId: BigInt(argv._.providerId),
36+
})
3537
: await selectProvider(client, argv.flags)
3638

3739
if (!provider) {

0 commit comments

Comments
 (0)