Skip to content

Commit

Permalink
fix(oracle): remove extra arguments in tx builder, refactor, fix exam…
Browse files Browse the repository at this point in the history
…ples
  • Loading branch information
davidyuk committed Apr 15, 2024
1 parent 3f42215 commit e3be365
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 221 deletions.
33 changes: 2 additions & 31 deletions src/actions/transaction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// # æternity CLI `transaction` file
//
// This script initialize all `transaction` function

import {
Tag, ORACLE_TTL_TYPES,
Node, genSalt, unpackTx, commitmentHash, buildContractId, verifyTransaction,
Expand All @@ -10,7 +6,6 @@ import {
import { print, printUnderscored, printValidation } from '../utils/print.js';
import { validateName, decode } from '../utils/helpers.js';

// Print `Builder Transaction`
function buildAndPrintTx(params, json, extraKeys = {}) {
const tx = buildTx(params);
const txObject = unpackTx(tx);
Expand All @@ -30,9 +25,7 @@ function buildAndPrintTx(params, json, extraKeys = {}) {
print('This is an unsigned transaction. Use `account sign` and `tx broadcast` to submit the transaction to the network, or verify that it will be accepted with `tx verify`.');
}

// ## Build `spend` transaction
export function spend(senderId, recipientId, amount, nonce, { json, payload, ...options }) {
// Build params
const params = {
tag: Tag.SpendTx,
...options,
Expand All @@ -45,15 +38,10 @@ export function spend(senderId, recipientId, amount, nonce, { json, payload, ...
buildAndPrintTx(params, json);
}

// ## Build `namePreClaim` transaction
export function namePreClaim(accountId, name, nonce, { json, ...options }) {
// Validate `name`(check if `name` end on `.chain`)
validateName(name);

// Generate `salt` and `commitmentId` and build `name` hash
const salt = genSalt();
const commitmentId = commitmentHash(name, salt);

const params = {
tag: Tag.NamePreclaimTx,
...options,
Expand All @@ -64,9 +52,7 @@ export function namePreClaim(accountId, name, nonce, { json, ...options }) {
buildAndPrintTx(params, json, { salt });
}

// ## Build `nameClaim` transaction
export function nameClaim(accountId, nameSalt, name, nonce, { json, ...options }) {
// Validate `name`(check if `name` end on `.chain`)
validateName(name);
const params = {
tag: Tag.NameClaimTx,
Expand All @@ -79,7 +65,6 @@ export function nameClaim(accountId, nameSalt, name, nonce, { json, ...options }
buildAndPrintTx(params, json);
}

// ## Build `nameUpdate` transaction
export function nameUpdate(accountId, nameId, nonce, pointers, { json, ...options }) {
const params = {
tag: Tag.NameUpdateTx,
Expand All @@ -92,9 +77,7 @@ export function nameUpdate(accountId, nameId, nonce, pointers, { json, ...option
buildAndPrintTx(params, json);
}

// ## Build `nameTransfer` transaction
export function nameTransfer(accountId, recipientId, nameId, nonce, { json, ...options }) {
// Create `transfer` transaction
const params = {
tag: Tag.NameTransferTx,
...options,
Expand All @@ -106,7 +89,6 @@ export function nameTransfer(accountId, recipientId, nameId, nonce, { json, ...o
buildAndPrintTx(params, json);
}

// ## Build `nameRevoke` transaction
export function nameRevoke(accountId, nameId, nonce, { json, ...options }) {
const params = {
tag: Tag.NameRevokeTx,
Expand All @@ -118,7 +100,6 @@ export function nameRevoke(accountId, nameId, nonce, { json, ...options }) {
buildAndPrintTx(params, json);
}

// ## Build `contractDeploy` transaction
export function contractDeploy(ownerId, code, callData, nonce, { json, gas, ...options }) {
const params = {
tag: Tag.ContractCreateTx,
Expand All @@ -134,7 +115,6 @@ export function contractDeploy(ownerId, code, callData, nonce, { json, gas, ...o
});
}

// ## Build `contractCall` transaction
export function contractCall(callerId, contractId, callData, nonce, { json, gas, ...options }) {
const params = {
tag: Tag.ContractCallTx,
Expand All @@ -148,7 +128,6 @@ export function contractCall(callerId, contractId, callData, nonce, { json, gas,
buildAndPrintTx(params, json);
}

// ## Build `oracleRegister` transaction
export function oracleRegister(accountId, queryFormat, responseFormat, nonce, {
json, oracleTtl, ...options
}) {
Expand All @@ -165,7 +144,6 @@ export function oracleRegister(accountId, queryFormat, responseFormat, nonce, {
buildAndPrintTx(params, json);
}

// ## Build `oraclePostQuery` transaction
export function oraclePostQuery(senderId, oracleId, query, nonce, {
json, queryTtl, responseTtl, ...options
}) {
Expand All @@ -184,12 +162,10 @@ export function oraclePostQuery(senderId, oracleId, query, nonce, {
buildAndPrintTx(params, json);
}

// ## Build `oracleExtend` transaction
export function oracleExtend(callerId, oracleId, oracleTtl, nonce, { json, ...options }) {
export function oracleExtend(oracleId, oracleTtl, nonce, { json, ...options }) {
const params = {
tag: Tag.OracleExtendTx,
...options,
callerId,
oracleId,
oracleTtlType: ORACLE_TTL_TYPES.delta,
oracleTtlValue: oracleTtl,
Expand All @@ -198,8 +174,7 @@ export function oracleExtend(callerId, oracleId, oracleTtl, nonce, { json, ...op
buildAndPrintTx(params, json);
}

// ## Build `oracleRespond` transaction
export function oracleRespond(callerId, oracleId, queryId, response, nonce, {
export function oracleRespond(oracleId, queryId, response, nonce, {
json, responseTtl, ...options
}) {
const params = {
Expand All @@ -208,19 +183,15 @@ export function oracleRespond(callerId, oracleId, queryId, response, nonce, {
oracleId,
responseTtlType: ORACLE_TTL_TYPES.delta,
responseTtlValue: responseTtl,
callerId,
queryId,
response,
nonce,
};
buildAndPrintTx(params, json);
}

// ## Verify 'transaction'
export async function verify(transaction, { json, ...options }) {
// Validate input
decode(transaction, 'tx');
// Call `getStatus` API and print it
const validation = await verifyTransaction(transaction, new Node(options.url));
const { tag, ...tx } = unpackTx(transaction);
if (json) {
Expand Down
5 changes: 1 addition & 4 deletions src/arguments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Argument, Option } from 'commander';
import { Option } from 'commander';
import BigNumber from 'bignumber.js';
import { MIN_GAS_PRICE } from '@aeternity/aepp-sdk';
import { noValue } from './utils/default-option-description.js';
Expand All @@ -15,9 +15,6 @@ export const amountOption = new Option('-a, --amount [amount]', 'Amount of coins
export const feeOption = new Option('-F, --fee [fee]', 'Override the transaction fee')
.argParser(coinAmountParser);

export const nonceArgument = new Argument('<nonce>', 'Unique number that is required to sign transaction securely')
.argParser((nonce) => +nonce);

export const nodeOption = new Option('-u, --url [nodeUrl]', 'Node to connect to')
.default('https://mainnet.aeternity.io', 'mainnet')
.env('AECLI_NODE_URL');
Expand Down
9 changes: 1 addition & 8 deletions src/commands/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// # æternity CLI `root` file
//
// This script initialize all `cli` commands
// We'll use `commander` for parsing options
import { Command } from 'commander';
import prompts from 'prompts';
import fs from 'fs-extra';
Expand All @@ -16,14 +12,13 @@ import CliError from '../utils/CliError.js';

const program = new Command();

// Array of child command's
const EXECUTABLE_CMD = [
{ name: 'chain', desc: 'Interact with the blockchain' },
{ name: 'inspect', desc: 'Get information on transactions, blocks,...' },
{ name: 'account', desc: 'Handle wallet operations' },
{ name: 'contract', desc: 'Contract interactions' },
{ name: 'name', desc: 'AENS system' },
{ name: 'tx', desc: 'Transaction builder' },
{ name: 'tx', desc: 'Generates transactions to sign and submit manually' },
{ name: 'oracle', desc: 'Interact with oracles' },
{ name: 'spend', desc: 'Send coins to account or contract' },
];
Expand All @@ -33,13 +28,11 @@ const EXECUTABLE_CMD = [
resolve(fileURLToPath(import.meta.url), '../../../package.json'),
);

// You get get CLI version by exec `aecli version`
program.version(version);

updateNotifier({ pkg: { name, version } }).notify();
})();

// ## Initialize `child` command's
EXECUTABLE_CMD.forEach(({ name, desc }) => program.command(name, desc));

async function getNodeDescription(url) {
Expand Down
1 change: 0 additions & 1 deletion src/commands/spend.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
export default new Command('aecli spend')
.description('Sends coins to another account or contract.')
.addHelpText('after', `
Example call:
$ aecli spend ./wallet.json ak_2GN72... 100
$ aecli spend ./wallet.json aens-name.chain 1.23ae
Expand Down

0 comments on commit e3be365

Please sign in to comment.