cosmic-interchain-cli
Version:
A command-line utility for Cosmic Wire's interchain messaging protocol
168 lines • 5.4 kB
JavaScript
import { createAgentConfig } from '../config/agent.js';
import { createChainConfig } from '../config/chain.js';
import { errorRed, log, logBlue, logGray, logTable } from '../logger.js';
import { chainTargetsCommandOption, outputFileCommandOption, } from './options.js';
import { ChainTypes } from './types.js';
/**
* Parent command
*/
export const registryCommand = {
command: 'registry',
describe: 'Manage Hyperlane chains in a registry',
builder: (yargs) => yargs
.command(addressesCommand)
.command(rpcCommand)
.command(createAgentConfigCommand)
.command(initCommand)
.command(listCommand)
.version(false)
.demandCommand(),
handler: () => log('Command required'),
};
/**
* List command
*/
const listCommand = {
command: 'list',
describe: 'List all chains included in a registry',
builder: {
type: {
describe: 'Specify the type of chains',
choices: ChainTypes,
},
},
handler: async ({ type, context }) => {
const logChainsForType = (type) => {
logBlue(`\nInterchain ${type} chains:`);
logGray('------------------------------');
const chains = Object.values(context.chainMetadata).filter((c) => {
if (type === 'mainnet')
return !c.isTestnet;
else
return !!c.isTestnet;
});
const tableData = chains.reduce((result, chain) => {
const { chainId, displayName } = chain;
result[chain.name] = {
'Display Name': displayName,
'Chain Id': chainId,
};
return result;
}, {});
logTable(tableData);
};
if (type) {
logChainsForType(type);
}
else {
logChainsForType('mainnet');
logChainsForType('testnet');
}
},
};
/**
* Addresses command
*/
const addressesCommand = {
command: 'addresses',
aliases: ['address', 'addy'],
describe: 'Display the addresses of core interchain contracts',
builder: {
name: {
type: 'string',
description: 'Chain to display addresses for',
alias: 'chain',
},
contract: {
type: 'string',
description: 'Specific contract name to print addresses for',
implies: 'name',
},
},
handler: async ({ name, context, contract }) => {
if (name) {
const result = await context.registry.getChainAddresses(name);
if (contract && result?.[contract.toLowerCase()]) {
// log only contract address for machine readability
log(result[contract]);
return;
}
logBlue('Interchain contract addresses for:', name);
logGray('---------------------------------');
log(JSON.stringify(result, null, 2));
}
else {
const result = await context.registry.getAddresses();
logBlue('Interchain contract addresses:');
logGray('----------------------------------');
log(JSON.stringify(result, null, 2));
}
},
};
const rpcCommand = {
command: 'rpc',
describe: 'Display the public rpc of a interchain chain',
builder: {
name: {
type: 'string',
description: 'Chain to display addresses for',
alias: 'chain',
demandOption: true,
},
index: {
type: 'number',
description: 'Index of the rpc to display',
default: 0,
demandOption: false,
},
},
handler: async ({ name, context, index }) => {
const result = await context.registry.getChainMetadata(name);
const rpcUrl = result?.rpcUrls[index]?.http;
if (!rpcUrl) {
errorRed(`❌ No rpc found for chain ${name}`);
process.exit(1);
}
log(rpcUrl);
},
};
/**
* agent-config command
*/
const createAgentConfigCommand = {
command: 'agent-config',
describe: 'Create a new agent config',
builder: {
chains: chainTargetsCommandOption,
out: outputFileCommandOption('./configs/agent-config.json', false, 'The path to output an agent config JSON file.'),
},
handler: async ({ context, chains, out, }) => {
const { multiProvider } = context;
let chainNames;
if (chains) {
chainNames = chains.split(',');
const invalidChainNames = chainNames.filter((chainName) => !multiProvider.hasChain(chainName));
if (invalidChainNames.length > 0) {
errorRed(`❌ Invalid chain names: ${invalidChainNames
.join(', ')
.replace(/, $/, '')}`);
process.exit(1);
}
}
await createAgentConfig({
context,
chains: chainNames,
out,
});
process.exit(0);
},
};
const initCommand = {
command: 'init',
describe: 'Create a new, minimal interchain chain config (aka chain metadata)',
handler: async ({ context }) => {
await createChainConfig({ context });
process.exit(0);
},
};
//# sourceMappingURL=registry.js.map