Skip to content

Commit 9fc4990

Browse files
committed
refactor: add manual network case and console logging for faucet command
1 parent ef9ad32 commit 9fc4990

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

‎packages/cli/src/cli.ts‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as scureBip39 from '@scure/bip39';
22
import { wordlist } from '@scure/bip39/wordlists/english';
33
import { buildPreorderNameTx, buildRegisterNameTx } from '@stacks/bns';
4-
import { bytesToHex, HIRO_TESTNET_URL } from '@stacks/common';
4+
import { bytesToHex } from '@stacks/common';
55
import {
66
ACCOUNT_PATH,
77
broadcastTransaction,
@@ -1870,8 +1870,8 @@ async function preorder(_network: CLINetworkAdapter, args: string[]): Promise<st
18701870
function faucetCall(_network: CLINetworkAdapter, args: string[]): Promise<string> {
18711871
const address = args[0];
18721872

1873-
// Faucet only exists on testnet
1874-
const config = new Configuration({ basePath: HIRO_TESTNET_URL });
1873+
const network = getStacksNetwork(_network);
1874+
const config = new Configuration({ basePath: network.client.baseUrl });
18751875
const faucets = new FaucetsApi(config);
18761876

18771877
return faucets
@@ -2016,7 +2016,7 @@ export function CLIMain() {
20162016
const debug = CLIOptAsBool(opts, 'd') || Boolean(process.env.DEBUG);
20172017
const consensusHash = CLIOptAsString(opts, 'C');
20182018
const integration_test = CLIOptAsBool(opts, 'i');
2019-
const testnet = CLIOptAsBool(opts, 't');
2019+
let testnet = CLIOptAsBool(opts, 't');
20202020
const localnet = CLIOptAsBool(opts, 'l');
20212021

20222022
const magicBytes = CLIOptAsString(opts, 'm');
@@ -2031,6 +2031,22 @@ export function CLIMain() {
20312031
BLOCKSTACK_TEST = integration_test;
20322032
}
20332033

2034+
// Command specific checks and overrides
2035+
if (cmdArgs.command === 'help') {
2036+
console.log(makeCommandUsageString(cmdArgs.args[0]));
2037+
process.exit(0);
2038+
}
2039+
2040+
if (cmdArgs.command === 'faucet') {
2041+
if (!testnet && !localnet && !nodeAPIUrl && !apiUrl) {
2042+
// If not otherwise configured the `faucet` command will default to testnet for backwards compatibility
2043+
testnet = true;
2044+
}
2045+
if (localnet || nodeAPIUrl || apiUrl) {
2046+
console.info('The "faucet" feature may be unavailable for custom API setups.');
2047+
}
2048+
}
2049+
20342050
const configPath = CLIOptAsString(opts, 'c')
20352051
? CLIOptAsString(opts, 'c')
20362052
: testnet
@@ -2095,11 +2111,6 @@ export function CLIMain() {
20952111
// blockstack.config.network = blockstackNetwork;
20962112
blockstack.config.logLevel = 'error';
20972113

2098-
if (cmdArgs.command === 'help') {
2099-
console.log(makeCommandUsageString(cmdArgs.args[0]));
2100-
process.exit(0);
2101-
}
2102-
21032114
const method = COMMANDS[cmdArgs.command];
21042115
let exitcode = 0;
21052116

‎packages/cli/tests/cli.test.ts‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,11 @@ describe('CLIMain', () => {
806806
expect(exitSpy).toHaveBeenCalledWith(0);
807807
});
808808

809-
test('faucet should default to testnet Hiro URL when no custom URL provided', async () => {
809+
test('faucet should auto-default to testnet when no network flags provided for backwards compatibility', async () => {
810810
const address = 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM';
811811

812-
process.argv = ['node', 'stx', '-t', 'faucet', address];
812+
// No network flags provided, should default to testnet (faucet command only)
813+
process.argv = ['node', 'stx', 'faucet', address];
813814

814815
fetchMock.once(
815816
JSON.stringify({
@@ -821,12 +822,43 @@ describe('CLIMain', () => {
821822
CLIMain();
822823
await exit;
823824

824-
// Verify faucet used testnet Hiro URL
825+
// Should automatically use testnet URL for backwards compatibility
825826
expect(fetchMock.mock.calls[0][0]).toContain('api.testnet.hiro.so');
826827
expect(fetchMock.mock.calls[0][0]).toContain('/extended/v1/faucets/stx');
827828
expect(exitSpy).toHaveBeenCalledWith(0);
828829
});
829830

831+
test('faucet should use localnet URL when -l flag provided', async () => {
832+
const address = 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM';
833+
const localnetApiUrl = 'http://localhost:20443';
834+
835+
process.argv = ['node', 'stx', '-l', 'faucet', address];
836+
837+
// Mock console.info to capture the warning message
838+
const consoleSpy = jest.spyOn(console, 'info').mockImplementation();
839+
840+
fetchMock.once(
841+
JSON.stringify({
842+
success: true,
843+
txId: '0x94b1cfab79555b8c6725f19e4fcd6268934d905578a3e8ef7a1e542b931d3676',
844+
})
845+
);
830846

847+
CLIMain();
848+
await exit;
849+
850+
// Should use localnet URL
851+
expect(fetchMock.mock.calls[0][0]).toContain(localnetApiUrl);
852+
expect(fetchMock.mock.calls[0][0]).toContain('/extended/v1/faucets/stx');
853+
854+
// Should show warning about faucet availability
855+
expect(consoleSpy).toHaveBeenCalledWith(
856+
'The "faucet" feature may be unavailable for custom API setups.'
857+
);
858+
859+
expect(exitSpy).toHaveBeenCalledWith(0);
860+
861+
consoleSpy.mockRestore();
862+
});
831863
});
832864
});

0 commit comments

Comments
 (0)