Skip to content

Commit 40c400b

Browse files
SgtPookirjan90
andcommitted
fix: network option works (#243)
Fixes issues with --network flag implementation from #240: - Fixed --network and --mainnet compatibility with --rpc-url - Fixed pinning server to use getRpcUrl properly - Fixed getRpcUrl to return url immediately if given - Fixed help output for network option - Created re-usable network option with ENV and choice validation - Formatted long option strings in CLI commands for Biome compliance Co-authored-by: Phi <[email protected]>
1 parent ced63da commit 40c400b

File tree

9 files changed

+87
-56
lines changed

9 files changed

+87
-56
lines changed

src/commands/server.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { RPC_URLS } from '@filoz/synapse-sdk'
2-
import { Command } from 'commander'
1+
import { Command, Option } from 'commander'
32
import { startServer } from '../server.js'
43
import { addNetworkOptions } from '../utils/cli-options.js'
54

@@ -12,10 +11,9 @@ export const serverCommand = new Command('server')
1211
.option('--private-key <key>', 'private key for Synapse (or use PRIVATE_KEY env var)')
1312

1413
addNetworkOptions(serverCommand)
15-
.option(
16-
'--rpc-url <url>',
17-
'RPC URL for Filecoin network (overrides --network, can also use RPC_URL env)',
18-
RPC_URLS.calibration.websocket
14+
.addOption(
15+
new Option('--rpc-url <url>', 'RPC URL for Filecoin network (overrides --network)').env('RPC_URL')
16+
// default rpcUrl value is defined in ../common/get-rpc-url.ts
1917
)
2018
.action(async (options) => {
2119
// Override environment variables with CLI options if provided

src/common/get-rpc-url.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { RPC_URLS } from '@filoz/synapse-sdk'
22
import type { CLIAuthOptions } from '../utils/cli-auth.js'
33

44
/**
5-
* Get the RPC URL from the CLI options and environment variables
5+
* Get the RPC URL from the CLI options.
6+
*
7+
* This should only be called from commands using commander, so ENV vars are already handled.
68
*
79
* Network selection priority:
810
* 1. Explicit --rpc-url (highest priority)
@@ -13,15 +15,13 @@ import type { CLIAuthOptions } from '../utils/cli-auth.js'
1315
export function getRpcUrl(options: CLIAuthOptions): string {
1416
// Determine RPC URL with priority: explicit rpcUrl > RPC_URL env > network flag/env > default
1517
let rpcUrl: string | undefined
16-
if (options.rpcUrl || process.env.RPC_URL) {
18+
if (options.rpcUrl) {
1719
// Explicit RPC URL takes highest priority
18-
rpcUrl = options.rpcUrl || process.env.RPC_URL
19-
}
20-
if (rpcUrl) {
21-
return rpcUrl
20+
return options.rpcUrl
2221
}
23-
// Try to use network flag/env var
24-
const network = (options.network || process.env.NETWORK)?.toLowerCase().trim()
22+
23+
// Try to use network flag
24+
const network = options.network?.toLowerCase().trim()
2525
if (network) {
2626
// Validate network value
2727
if (network !== 'mainnet' && network !== 'calibration') {

src/payments/auto.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ export async function runAutoSetup(options: PaymentSetupOptions): Promise<void>
4949

5050
try {
5151
// Parse and validate authentication
52-
const authConfig = parseCLIAuth({
53-
privateKey: options.privateKey,
54-
walletAddress: options.walletAddress,
55-
sessionKey: options.sessionKey,
56-
rpcUrl: options.rpcUrl,
57-
})
52+
const authConfig = parseCLIAuth(options)
5853

5954
const logger = getCLILogger()
6055
const synapse = await initializeSynapse(

src/payments/deposit.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ export async function runDeposit(options: DepositOptions): Promise<void> {
5050
spinner.start('Connecting...')
5151
try {
5252
// Parse and validate authentication
53-
const authConfig = parseCLIAuth({
54-
privateKey: options.privateKey,
55-
walletAddress: options.walletAddress,
56-
sessionKey: options.sessionKey,
57-
rpcUrl: options.rpcUrl,
58-
})
53+
const authConfig = parseCLIAuth(options)
5954

6055
const logger = getCLILogger()
6156
const synapse = await initializeSynapse(

src/payments/fund.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,7 @@ export async function runFund(options: FundOptions): Promise<void> {
235235
spinner.start('Connecting...')
236236
try {
237237
// Parse and validate authentication
238-
const authConfig = parseCLIAuth({
239-
privateKey: options.privateKey,
240-
walletAddress: options.walletAddress,
241-
sessionKey: options.sessionKey,
242-
rpcUrl: options.rpcUrl,
243-
})
238+
const authConfig = parseCLIAuth(options)
244239

245240
const logger = getCLILogger()
246241
const synapse = await initializeSynapse(

src/payments/status.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ export async function showPaymentStatus(options: StatusOptions): Promise<void> {
8585

8686
try {
8787
// Parse and validate authentication
88-
const authConfig = parseCLIAuth({
89-
privateKey: options.privateKey,
90-
walletAddress: options.walletAddress,
91-
sessionKey: options.sessionKey,
92-
rpcUrl: options.rpcUrl,
93-
})
88+
const authConfig = parseCLIAuth(options)
9489

9590
const logger = getCLILogger()
9691
const synapse = await initializeSynapse(

src/payments/withdraw.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ export async function runWithdraw(options: WithdrawOptions): Promise<void> {
3535
spinner.start('Connecting...')
3636
try {
3737
// Parse and validate authentication
38-
const authConfig = parseCLIAuth({
39-
privateKey: options.privateKey,
40-
walletAddress: options.walletAddress,
41-
sessionKey: options.sessionKey,
42-
rpcUrl: options.rpcUrl,
43-
})
38+
const authConfig = parseCLIAuth(options)
4439

4540
const logger = getCLILogger()
4641
const synapse = await initializeSynapse(

src/test/unit/get-rpc-url.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { RPC_URLS } from '@filoz/synapse-sdk'
2+
import { describe, expect, it } from 'vitest'
3+
import { getRpcUrl } from '../../common/get-rpc-url.js'
4+
import type { CLIAuthOptions } from '../../utils/cli-auth.js'
5+
6+
/**
7+
* In production Commander already maps env vars (NETWORK/RPC_URL) into CLI options.
8+
* These tests cover the behavior of getRpcUrl given the options object Commander
9+
* would pass, without interacting with process.env directly.
10+
*/
11+
12+
describe('getRpcUrl', () => {
13+
it('returns explicit rpcUrl even when network is provided', () => {
14+
const options: CLIAuthOptions = {
15+
rpcUrl: 'wss://custom.rpc.url/ws',
16+
network: 'mainnet',
17+
}
18+
19+
expect(getRpcUrl(options)).toBe('wss://custom.rpc.url/ws')
20+
})
21+
22+
it.each([
23+
['mainnet', RPC_URLS.mainnet.websocket],
24+
['calibration', RPC_URLS.calibration.websocket],
25+
])('returns RPC URL for %s network', (network, expected) => {
26+
expect(getRpcUrl({ network } satisfies CLIAuthOptions)).toBe(expected)
27+
})
28+
29+
it('normalizes network casing and whitespace', () => {
30+
expect(
31+
getRpcUrl({
32+
network: ' MAINNET ',
33+
})
34+
).toBe(RPC_URLS.mainnet.websocket)
35+
36+
expect(
37+
getRpcUrl({
38+
network: '\tCaLiBrAtIoN\n',
39+
})
40+
).toBe(RPC_URLS.calibration.websocket)
41+
})
42+
43+
it('defaults to calibration when network is missing or blank', () => {
44+
expect(getRpcUrl({})).toBe(RPC_URLS.calibration.websocket)
45+
expect(getRpcUrl({ network: '' })).toBe(RPC_URLS.calibration.websocket)
46+
expect(getRpcUrl({ network: ' ' })).toBe(RPC_URLS.calibration.websocket)
47+
})
48+
49+
it('treats empty rpcUrl as falsy and falls back to defaults', () => {
50+
expect(getRpcUrl({ rpcUrl: '' })).toBe(RPC_URLS.calibration.websocket)
51+
})
52+
53+
it('throws for unsupported networks', () => {
54+
expect(() => getRpcUrl({ network: 'invalid' })).toThrow(
55+
'Invalid network: "invalid". Must be "mainnet" or "calibration"'
56+
)
57+
})
58+
})

src/utils/cli-options.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* This module provides reusable option definitions for Commander.js commands
55
* to ensure consistency across all CLI commands.
66
*/
7-
8-
import { RPC_URLS } from '@filoz/synapse-sdk'
97
import { type Command, Option } from 'commander'
108

119
/**
@@ -45,10 +43,9 @@ export function addAuthOptions(command: Command): Command {
4543
.option('--session-key <key>', 'Session key for session key auth (can also use SESSION_KEY env)')
4644

4745
return addNetworkOptions(command)
48-
.option(
49-
'--rpc-url <url>',
50-
'RPC endpoint (can also use RPC_URL env, overrides --network)',
51-
RPC_URLS.calibration.websocket
46+
.addOption(
47+
new Option('--rpc-url <url>', 'RPC endpoint').env('RPC_URL')
48+
// default rpcUrl value is defined in ../common/get-rpc-url.ts
5249
)
5350
.option(
5451
'--warm-storage-address <address>',
@@ -89,10 +86,13 @@ export function addProviderOptions(command: Command): Command {
8986
}
9087

9188
export function addNetworkOptions(command: Command): Command {
92-
return command.addOption(
93-
new Option('--network <network>', 'Filecoin network to use')
94-
.choices(['mainnet', 'calibration'])
95-
.env('NETWORK')
96-
.default('calibration')
97-
)
89+
command
90+
.addOption(
91+
new Option('--network <network>', 'Filecoin network to use')
92+
.choices(['mainnet', 'calibration'])
93+
.env('NETWORK')
94+
.default('calibration')
95+
)
96+
.addOption(new Option('--mainnet', 'Use mainnet (shorthand for --network mainnet)').implies({ network: 'mainnet' }))
97+
return command
9898
}

0 commit comments

Comments
 (0)