diff --git a/packages/cli/bin/run b/packages/cli/bin/run index a7635de..926705f 100755 --- a/packages/cli/bin/run +++ b/packages/cli/bin/run @@ -1,5 +1,7 @@ #!/usr/bin/env node -const oclif = require('@oclif/core') +import {run, handle, flush} from '@oclif/core' -oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle')) +await run(process.argv.slice(2), import.meta.url) + .catch(async (error) => handle(error)) + .finally(async () => flush()) \ No newline at end of file diff --git a/packages/cli/src/baseCommand.ts b/packages/cli/src/baseCommand.ts index 6bd40c7..63be308 100644 --- a/packages/cli/src/baseCommand.ts +++ b/packages/cli/src/baseCommand.ts @@ -1,11 +1,7 @@ import { Command } from '@oclif/core'; -import { color } from '@oclif/color'; export abstract class BaseCommand extends Command { protected async catch(err: Error & { exitCode?: number }): Promise { - // add any custom logic to handle errors from the command - // or simply return the parent class error handling - console.log({ err }); - return this.log(color.red(err.message)); + return super.catch(err); } } diff --git a/packages/cli/src/commands/acc/send-sk.ts b/packages/cli/src/commands/acc/send-sk.ts index ccee312..76d5f73 100644 --- a/packages/cli/src/commands/acc/send-sk.ts +++ b/packages/cli/src/commands/acc/send-sk.ts @@ -5,7 +5,6 @@ import { colorize } from 'json-colorizer'; import color from '@oclif/color'; import { initializeNetworkApi, loadWallet } from '../../helpers/network.helper'; import { BaseCommand } from '../../baseCommand'; -import { TxStatus } from '../../types/tx-status.type'; import cliConfig from '../../config/cli'; export default class AccSendSk extends BaseCommand { @@ -72,13 +71,11 @@ export default class AccSendSk extends BaseCommand { gasValue: gasValue ? BigInt(gasValue) : undefined, }); - const { txId } = result as TxStatus; - ux.action.stop(); - if (txId) { + if (result?.txId) { this.log(colorize(result)); - this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${txId}`)); + this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${result.txId}`)); } else { this.log(color.red('No result.')); } diff --git a/packages/cli/src/commands/container/create.ts b/packages/cli/src/commands/container/create.ts index 85d46fc..975f1e9 100644 --- a/packages/cli/src/commands/container/create.ts +++ b/packages/cli/src/commands/container/create.ts @@ -10,7 +10,6 @@ import cliConfig from '../../config/cli'; import abis from '../../abis'; import { createCompactPublicKey, stringToBytes32 } from '../../helpers/container.helper'; import { BaseCommand } from '../../baseCommand'; -import { TxStatus } from '../../types/tx-status.type'; export default class ContainerCreate extends BaseCommand { static override description = 'Create a new container with a given name and key pair'; @@ -73,13 +72,11 @@ export default class ContainerCreate extends BaseCommand { ], }, { key: importedWallet, sponsor: sponsorAddress }); - const { retval, txId } = mintResponse as TxStatus; - ux.action.stop(); - if (txId) { - this.log(color.green(`Container ${containerName} created with order ID: ${retval}`)); - this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${txId}`)); + if (mintResponse?.txId) { + this.log(color.green(`Container ${containerName} created with order ID: ${mintResponse.retval}`)); + this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${mintResponse.txId}`)); } else { this.log(color.red(`Container ${containerName} creation failed`)); } diff --git a/packages/cli/src/commands/container/update.ts b/packages/cli/src/commands/container/update.ts index 77cce59..0c07870 100644 --- a/packages/cli/src/commands/container/update.ts +++ b/packages/cli/src/commands/container/update.ts @@ -9,7 +9,6 @@ import { createCompactPublicKey, importContainerKey, stringToBytes32, } from '../../helpers/container.helper'; import { BaseCommand } from '../../baseCommand'; -import { TxStatus } from '../../types/tx-status.type'; export default class ContainerUpdate extends BaseCommand { static override description = 'Update container details'; @@ -73,13 +72,11 @@ export default class ContainerUpdate extends BaseCommand { { key: importedWallet, sponsor: sponsorAddress }, ); - const { txId } = taskUpdateResponse as TxStatus; - ux.action.stop(); - if (txId) { + if (taskUpdateResponse?.txId) { this.log(color.green(`Container ${containerName} updated with ID: ${containerId}`)); - this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${txId}`)); + this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${taskUpdateResponse.txId}`)); } else { this.log(color.red('No result.')); } diff --git a/packages/cli/src/commands/contract/deploy.ts b/packages/cli/src/commands/contract/deploy.ts index cf4e01b..f507db0 100644 --- a/packages/cli/src/commands/contract/deploy.ts +++ b/packages/cli/src/commands/contract/deploy.ts @@ -7,7 +7,6 @@ import { colorize } from 'json-colorizer'; import { initializeNetworkApi, loadWallet } from '../../helpers/network.helper'; import { BaseCommand } from '../../baseCommand'; import { ParamsParser } from '../../helpers/params-parser.helper'; -import { TxStatus } from '../../types/tx-status.type'; import cliConfig from '../../config/cli'; export default class ContractDeploy extends BaseCommand { @@ -58,71 +57,66 @@ export default class ContractDeploy extends BaseCommand { }; public async run(): Promise { - try { - const { flags } = await this.parse(ContractDeploy); - const { - abiPath, - binPath, - gasToken, - gasValue, - initParams, - keyFilePath, - password, - } = flags; - const paramsParser = new ParamsParser(); + const { flags } = await this.parse(ContractDeploy); + const { + abiPath, + binPath, + gasToken, + gasValue, + initParams, + keyFilePath, + password, + } = flags; + const paramsParser = new ParamsParser(); - const parsedParams = initParams && paramsParser.parse(initParams); - // Read and parse the ABI and binary files - const abi = JSON.parse(readFileSync(abiPath, 'utf8')); - const code = readFileSync(binPath, 'utf8'); + const parsedParams = initParams && paramsParser.parse(initParams); + // Read and parse the ABI and binary files + const abi = JSON.parse(readFileSync(abiPath, 'utf8')); + const code = readFileSync(binPath, 'utf8'); - ux.action.start('Loading'); + ux.action.start('Loading'); - // Load wallet - const importedWallet = await loadWallet(keyFilePath, password); + // Load wallet + const importedWallet = await loadWallet(keyFilePath, password); - // Initialize network API - const networkApi = await initializeNetworkApi({ - address: importedWallet.address, - }); + // Initialize network API + const networkApi = await initializeNetworkApi({ + address: importedWallet.address, + }); - const sequence = await networkApi.getWalletSequence(importedWallet.address); - const newSequence = sequence + 1; + const sequence = await networkApi.getWalletSequence(importedWallet.address); + const newSequence = sequence + 1; - // Compose the deployment transaction - const deployTX = TransactionsApi.composeDeployTX( - { abi, bytecode: `0x${code}`, args: parsedParams || [] }, - { - address: importedWallet.address, - seq: newSequence, - feeSettings: networkApi.feeSettings, - gasSettings: networkApi.gasSettings, - gasToken, - gasValue: BigInt(gasValue), - wif: importedWallet.wif, - }, - ); + // Compose the deployment transaction + const deployTX = TransactionsApi.composeDeployTX( + { abi, bytecode: `0x${code}`, args: parsedParams || [] }, + { + address: importedWallet.address, + seq: newSequence, + feeSettings: networkApi.feeSettings, + gasSettings: networkApi.gasSettings, + gasToken, + gasValue: BigInt(gasValue), + wif: importedWallet.wif, + }, + ); - // Send the prepared transaction - const result: any = await networkApi.sendPreparedTX(deployTX); + // Send the prepared transaction + const result: any = await networkApi.sendPreparedTX(deployTX); - const { txId } = result as TxStatus; - ux.action.stop(); + ux.action.stop(); - if (txId) { - this.log(colorize(result)); - this.log( - color.yellow( - `Transaction: ${ - cliConfig.explorerUrl - }/${networkApi.getChain()}/transaction/${txId}`, - ), - ); - } else { - this.log(color.red('No result')); - } - } catch (error) { - console.log(error); + if (result?.txId) { + this.log(colorize(result)); + this.log( + color.yellow( + `Transaction: ${ + cliConfig.explorerUrl + }/${networkApi.getChain()}/transaction/${result.txId}`, + ), + ); + } else { + this.log(color.red('No result')); } } } diff --git a/packages/cli/src/commands/contract/set.ts b/packages/cli/src/commands/contract/set.ts index edffb1a..d2f74e5 100644 --- a/packages/cli/src/commands/contract/set.ts +++ b/packages/cli/src/commands/contract/set.ts @@ -6,7 +6,6 @@ import color from '@oclif/color'; import { initializeNetworkApi, loadWallet } from '../../helpers/network.helper'; import { BaseCommand } from '../../baseCommand'; import { ParamsParser } from '../../helpers/params-parser.helper'; -import { TxStatus } from '../../types/tx-status.type'; import cliConfig from '../../config/cli'; export default class ContractSet extends BaseCommand { @@ -93,7 +92,7 @@ export default class ContractSet extends BaseCommand { // Execute the smart contract method // eslint-disable-next-line @typescript-eslint/no-explicit-any - const setResult: TxStatus = await smartContract.scSet( + const setResult = await smartContract.scSet( { abi, args: parsedParams, functionName: method }, { key: importedWallet, sponsor: sponsorAddress, amount: amount ? BigInt(amount) : undefined }, ); diff --git a/packages/cli/src/commands/provider/create.ts b/packages/cli/src/commands/provider/create.ts index b945cbd..27cfe9d 100644 --- a/packages/cli/src/commands/provider/create.ts +++ b/packages/cli/src/commands/provider/create.ts @@ -51,13 +51,11 @@ export default class ProviderCreate extends BaseCommand { ], }, { key: importedWallet, sponsor: sponsorAddress }); - const { retval, txId } = mintResponse; - ux.action.stop(); - if (txId) { - this.log(color.green(`Container ${providerName} created with order ID: ${retval}`)); - this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${txId}`)); + if (mintResponse?.txId) { + this.log(color.green(`Container ${providerName} created with order ID: ${mintResponse.retval}`)); + this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${mintResponse.txId}`)); } else { this.log(color.red(`Container ${providerName} creation failed`)); } diff --git a/packages/cli/src/commands/provider/set-url.ts b/packages/cli/src/commands/provider/set-url.ts index 2257eed..6a4927b 100644 --- a/packages/cli/src/commands/provider/set-url.ts +++ b/packages/cli/src/commands/provider/set-url.ts @@ -55,13 +55,11 @@ export default class ProviderSetUrl extends BaseCommand { ], }, { key: importedWallet, sponsor: sponsorAddress }); - const { txId } = setUrlResponse; - ux.action.stop(); - if (txId) { + if (setUrlResponse?.txId) { this.log(color.green(`Provider url ${providerUrl} updated with provider ID: ${providerId}`)); - this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${txId}`)); + this.log(color.yellow(`Transaction: ${cliConfig.explorerUrl}/${networkApi.getChain()}/transaction/${setUrlResponse.txId}`)); } else { this.log(color.red(`Provider url ${providerUrl} updating failed`)); } diff --git a/packages/cli/src/types/tx-status.type.ts b/packages/cli/src/types/tx-status.type.ts deleted file mode 100644 index efb1e4f..0000000 --- a/packages/cli/src/types/tx-status.type.ts +++ /dev/null @@ -1,6 +0,0 @@ -export type TxStatus = { - txId: string; - res: string; - block: string; - retval?: any; -}; diff --git a/packages/tssdk/src/typings.ts b/packages/tssdk/src/typings.ts index 3faad2c..f2e80a2 100644 --- a/packages/tssdk/src/typings.ts +++ b/packages/tssdk/src/typings.ts @@ -15,17 +15,17 @@ export type PKCS5PEMInfoType = { }; export interface RawNodes { - [key: string] : { + [key: string]: { host: string[]; ip: string[]; - } + }; } export interface ChainNode { address: string; nodeId: string; time?: number; - height?: number + height?: number; } export interface AccountKey { @@ -41,15 +41,15 @@ export interface RegisteredAccount { } export interface ChainBootstrapConfig { - [key: number] : RawNodes; + [key: number]: RawNodes; } export interface ChainNetwork { - [key: string] : number[]; + [key: string]: number[]; } export interface ChainGlobalConfig { - chains: ChainBootstrapConfig, + chains: ChainBootstrapConfig; settings: ChainNetwork; } @@ -61,8 +61,8 @@ export interface ChainConfig { } export type TxResponse = { - txId: string, - res: string, - block: string, - retval: T + txId?: string; + res?: string; + block?: string; + retval?: T; };