@blockfrost/blockfrost-cardano-cli
Version:
Drop-in(ish) replacement for cardano-cli powered by Blockfrost
63 lines (62 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Submit = void 0;
const core_1 = require("@oclif/core");
const blockfrost_js_1 = require("@blockfrost/blockfrost-js");
const fs_1 = require("fs");
const errors_1 = require("../../constants/errors");
const format_1 = require("../../utils/format");
const parsing_1 = require("../../utils/parsing");
const base_command_1 = require("../../helpers/base-command");
// cardano-cli doesn't support --out-file flag with transaction submit command, we do
// The file will contain txHash (txid) of the transaction
class Submit extends base_command_1.BaseCommand {
constructor() {
super(...arguments);
this.readFromFile = (filepath) => {
const data = (0, fs_1.readFileSync)(filepath, 'utf-8');
if (!data) {
throw new Error(`${errors_1.ERROR.FILE_CANNOT_READ} ${filepath}`);
}
const trimmedData = data.trim();
const isJson = (0, parsing_1.isJsonString)(trimmedData);
if (!isJson) {
return trimmedData;
}
const tx = JSON.parse(trimmedData);
if (typeof tx === 'object' &&
tx !== null &&
'cborHex' in tx &&
typeof tx.cborHex === 'string') {
return tx.cborHex;
}
throw new Error(errors_1.ERROR.TX_FILE_UNKNOWN_FORMAT);
};
this.prettyPrint = (_data) => {
this.log('Transaction successfully submitted.');
};
this.doWork = async () => {
const { flags } = await this.parse(Submit);
const client = await this.getClient();
const transaction = this.readFromFile(flags['tx-file']);
try {
const response = await client.txSubmit(transaction);
return response;
}
catch (error) {
if (error instanceof blockfrost_js_1.BlockfrostServerError || error instanceof blockfrost_js_1.BlockfrostClientError) {
this.error(`Command failed: ${(0, format_1.stripQuotes)(error.message)}`); // matching cardano-cli (if we want really 1:1 output compatibility we should use this.log which doesn't add visuals in front of error message) and exit the process manually)
}
else {
throw error;
}
}
};
}
}
exports.Submit = Submit;
Submit.flags = Object.assign(Object.assign({}, base_command_1.BaseCommand.flags), { 'tx-file': core_1.Flags.string({
char: 'f',
description: 'Filepath of the transaction you intend to submit.',
required: true,
}) });