Skip to content

Commit

Permalink
feat(account): show approximate tx execution cost before asking password
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 11, 2024
1 parent 54d70fb commit 9c97844
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fs from 'fs-extra';
import {
AeSdk, Node, MemoryAccount, CompilerCli, CompilerCli8, CompilerHttpNode, recover, sign,
getExecutionCost, unpackTx, Tag,
} from '@aeternity/aepp-sdk';
import { PROMPT_TYPE, prompt } from './prompt.js';
import { getFullPath } from './helpers.js';
Expand Down Expand Up @@ -55,6 +56,13 @@ export class AccountCli extends MemoryAccount {
return sign(data, Buffer.from(secretKey, 'hex'));
}

async signTransaction(transaction, options) {
const cost = Number(getExecutionCost(transaction)) / 1e18;
const txType = Tag[unpackTx(transaction).tag];
console.warn(`Cost of ${txType} execution ≈ ${cost}ae`);
return super.signTransaction(transaction, options);
}

// Get account file by path, decrypt it using password and return AccountCli
static async read(path, password) {
const keyFile = await fs.readJson(getFullPath(path));
Expand Down
7 changes: 6 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ export async function executeProgram(program, args) {
.configureOutput({ writeOut: (str) => { result += str; } })
.exitOverride();

const { log } = console;
const { log, warn } = console;
console.log = (...data) => {
if (result) result += '\n';
result += data.join(' ');
};
console.warn = (...data) => {
if (/Cost of .+ execution ≈ .+ae/.test(data[0])) return;
warn(...data);
};
const options = getProgramOptions(program);
try {
const allArgs = [
Expand All @@ -100,6 +104,7 @@ export async function executeProgram(program, args) {
await program.parseAsync(allArgs, { from: 'user' });
} finally {
console.log = log;
console.warn = warn;
isProgramExecuting = false;
setProgramOptions(program, options);
}
Expand Down

0 comments on commit 9c97844

Please sign in to comment.