diff --git a/README.md b/README.md index 55cc19ba..44d7782d 100644 --- a/README.md +++ b/README.md @@ -275,7 +275,7 @@ Replace `` with the name of the network you wish to switch to (`mainnet`, **2. Listing Available Networks:** -This command lists all available networks that the CLI tool can interact with. It's useful for confirming the network options and ensuring correct network names are used when switching networks. +This command lists all available networks you've configured for the CLI tool. It's useful for confirming the network options and ensuring correct network names are used when switching networks. ```sh hcli network list diff --git a/__tests__/commands/network/list.test.ts b/__tests__/commands/network/list.test.ts index 37015cc3..402cdee1 100644 --- a/__tests__/commands/network/list.test.ts +++ b/__tests__/commands/network/list.test.ts @@ -1,10 +1,24 @@ +import { baseState } from '../../helpers/state'; import { Command } from "commander"; import commands from "../../../src/commands"; +import stateController from '../../../src/state/stateController'; + +jest.mock('../../../src/state/state'); // Mock the original module -> looks for __mocks__/state.ts in same directory describe("network list command", () => { const logSpy = jest.spyOn(console, 'log'); describe("network list - success path", () => { + beforeEach(() => { + const stateCopy = { + ...baseState, + // Provide a bogus mainnet operator ID and key + localnetOperatorKey: 'mykey', + }; + + stateController.saveState(stateCopy); + }); + afterEach(() => { // Spy cleanup logSpy.mockClear(); @@ -19,7 +33,9 @@ describe("network list command", () => { await program.parse(["node", "hedera-cli.ts", "network", "list"]); // Assert - expect(logSpy).toHaveBeenCalledWith(`Available networks: mainnet, testnet, previewnet, localnet`); + expect(logSpy).toHaveBeenCalledWith(`Available networks:`); + expect(logSpy).toHaveBeenLastCalledWith(`- localnet`); + expect(logSpy).toHaveBeenCalledTimes(2); }); }); }); diff --git a/src/commands/network/list.ts b/src/commands/network/list.ts index 18603078..55ab6484 100644 --- a/src/commands/network/list.ts +++ b/src/commands/network/list.ts @@ -18,6 +18,9 @@ export default (program: any) => { .description('List all available networks') .action(() => { logger.verbose('Listing networks'); - logger.log('Available networks: mainnet, testnet, previewnet, localnet'); + logger.log('Available networks:'); + stateUtils.getAvailableNetworks().forEach((network) => { + logger.log(`- ${network}`); + }); }); }; diff --git a/src/state/state.json b/src/state/state.json index db3bc6ec..fe20faa2 100644 --- a/src/state/state.json +++ b/src/state/state.json @@ -1,31 +1,72 @@ { - "network": "testnet", + "network": "localnet", "mirrorNodeLocalnet": "http://localhost:5551/api/v1", "mirrorNodePreviewnet": "https://previewnet.mirrornode.hedera.com/api/v1", "mirrorNodeTestnet": "https://testnet.mirrornode.hedera.com/api/v1", "mirrorNodeMainnet": "https://mainnet.mirrornode.hedera.com/api/v1", - "testnetOperatorKey": "", - "testnetOperatorId": "", - "mainnetOperatorKey": "", - "mainnetOperatorId": "", - "previewnetOperatorId": "", - "previewnetOperatorKey": "", - "localnetOperatorKey": "", - "localnetOperatorId": "", "recording": 0, "recordingScriptName": "", - "scriptExecution": 0, - "scriptExecutionName": "", + "scriptExecution": 1, + "scriptExecutionName": "account-create-simple", "accounts": {}, - "tokens": {}, - "topics": {}, "scripts": { - "script-init": { - "name": "init", - "commands": [] + "script-token": { + "name": "token", + "creation": 1737051452976, + "commands": [ + "account create -a random --args privateKey,privKeyAcc1 --args alias,aliasAcc1 --args accountId,idAcc1", + "account create -a random --args privateKey,privKeyAcc2 --args alias,aliasAcc2 --args accountId,idAcc2", + "account create -a random --args privateKey,privKeyAcc3 --args alias,aliasAcc3 --args accountId,idAcc3", + "token create -n mytoken -s MTK -d 2 -i 1000 --supply-type infinite -a {{privKeyAcc1}} -t {{idAcc2}} -k {{privKeyAcc2}} --args tokenId,tokenId", + "token associate --account-id {{idAcc3}} --token-id {{tokenId}}", + "token transfer -t {{tokenId}} -b 1 --from {{aliasAcc2}} --to {{aliasAcc3}}", + "wait 3", + "account balance --account-id-or-alias {{aliasAcc3}} --token-id {{tokenId}}", + "state view --token-id {{tokenId}}" + ], + "args": {} + }, + "script-account-create": { + "name": "account-create", + "creation": 1737051452983, + "commands": [ + "account create -a random -b 10000000 --type ecdsa --args privateKey,privKeyAcc1 --args alias,aliasAcc1 --args accountId,idAcc1", + "wait 5", + "account balance --account-id-or-alias {{idAcc1}} --only-hbar" + ], + "args": {} + }, + "script-account-create-simple": { + "name": "account-create-simple", + "creation": 1737051452984, + "commands": [ + "account create -a random -b 10000000 --type ecdsa --args privateKey,privKeyAcc1 --args alias,aliasAcc1 --args accountId,idAcc1", + "wait 5" + ], + "args": {} + }, + "script-topic-create": { + "name": "topic-create", + "creation": 1737051452984, + "commands": [ + "account create -a random --args privateKey,privKeyAdmin", + "account create -a random --args privateKey,privKeySubmit", + "topic create --admin-key {{privKeyAdmin}} --submit-key {{privKeySubmit}} --args topicId,topicId" + ], + "args": {} } }, + "tokens": {}, + "topics": {}, + "testnetOperatorKey": "", + "testnetOperatorId": "", + "mainnetOperatorKey": "", + "mainnetOperatorId": "", + "previewnetOperatorKey": "", + "previewnetOperatorId": "", + "localnetOperatorKey": "302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137", + "localnetOperatorId": "0.0.2", "localNodeAddress": "127.0.0.1:50211", "localNodeAccountId": "0.0.3", "localNodeMirrorAddressGRPC": "127.0.0.1:5600" -} +} \ No newline at end of file diff --git a/src/utils/state.ts b/src/utils/state.ts index c6bd4111..e867cdba 100644 --- a/src/utils/state.ts +++ b/src/utils/state.ts @@ -43,6 +43,29 @@ function getMirrorNodeURL(): string { return mirrorNodeURL; } +function getAvailableNetworks(): string[] { + const mainnet = stateController.get('mainnetOperatorKey'); + const testnet = stateController.get('testnetOperatorKey'); + const previewnet = stateController.get('previewnetOperatorKey'); + const localnet = stateController.get('localnetOperatorKey'); + + const networks = []; + if (mainnet) { + networks.push('mainnet'); + } + if (testnet) { + networks.push('testnet'); + } + if (previewnet) { + networks.push('previewnet'); + } + if (localnet) { + networks.push('localnet'); + } + + return networks; +} + function getMirrorNodeURLByNetwork(network: string): string { let mirrorNodeURL = stateController.get('mirrorNodeTestnet'); switch (network) { @@ -160,9 +183,10 @@ function getOperator(): { operatorId: string; operatorKey: string } { } function switchNetwork(name: string) { - if (!['mainnet', 'testnet', 'previewnet', 'localnet'].includes(name)) { + const networks = getAvailableNetworks(); + if (!networks.includes(name)) { logger.error( - 'Invalid network name. Available networks: mainnet, testnet, previewnet, and localnet', + 'Invalid network name. Available networks: ' + networks.join(', '), ); process.exit(1); } @@ -430,6 +454,7 @@ const stateUtils = { clearState, downloadState, importState, + getAvailableNetworks, }; export default stateUtils;