Skip to content

Commit

Permalink
feat: Only list available networks (#578)
Browse files Browse the repository at this point in the history
Signed-off-by: Michiel Mulders <[email protected]>
  • Loading branch information
michielmulders authored Jan 17, 2025
1 parent b776270 commit 3d15b0c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Replace `<name>` 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
Expand Down
18 changes: 17 additions & 1 deletion __tests__/commands/network/list.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
});
});
});
5 changes: 4 additions & 1 deletion src/commands/network/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
});
};
75 changes: 58 additions & 17 deletions src/state/state.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 27 additions & 2 deletions src/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -430,6 +454,7 @@ const stateUtils = {
clearState,
downloadState,
importState,
getAvailableNetworks,
};

export default stateUtils;

0 comments on commit 3d15b0c

Please sign in to comment.