Skip to content

Commit

Permalink
Merge pull request #163 from thepower/fix/cli_error_handling
Browse files Browse the repository at this point in the history
fix(cli,tssdk): cli error handling, fixed TxResponse type
  • Loading branch information
jackkru69 authored Sep 25, 2024
2 parents ef3db24 + 774c93d commit 486f4c9
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 107 deletions.
6 changes: 4 additions & 2 deletions packages/cli/bin/run
Original file line number Diff line number Diff line change
@@ -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())
6 changes: 1 addition & 5 deletions packages/cli/src/baseCommand.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
// 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);
}
}
7 changes: 2 additions & 5 deletions packages/cli/src/commands/acc/send-sk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.'));
}
Expand Down
9 changes: 3 additions & 6 deletions packages/cli/src/commands/container/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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`));
}
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/commands/container/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.'));
}
Expand Down
108 changes: 51 additions & 57 deletions packages/cli/src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,71 +57,66 @@ export default class ContractDeploy extends BaseCommand {
};

public async run(): Promise<void> {
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'));
}
}
}
3 changes: 1 addition & 2 deletions packages/cli/src/commands/contract/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 },
);
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/commands/provider/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
}
Expand Down
6 changes: 2 additions & 4 deletions packages/cli/src/commands/provider/set-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
}
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/src/types/tx-status.type.ts

This file was deleted.

20 changes: 10 additions & 10 deletions packages/tssdk/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand All @@ -61,8 +61,8 @@ export interface ChainConfig {
}

export type TxResponse<T> = {
txId: string,
res: string,
block: string,
retval: T
txId?: string;
res?: string;
block?: string;
retval?: T;
};

0 comments on commit 486f4c9

Please sign in to comment.