Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove oracle show, improve output #253

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ Run `$ aecli inspect <wallet address>` to ensure that it got coins.
```
$ aecli inspect ak_21A27UVVt3hDkBE5J7rhhqnH5YNb4Y1dqo4PnSybrH85pnWo7E
Account ID ______________________________ ak_21A27UVVt3hDkBE5J7rhhqnH5YNb4Y1dqo4PnSybrH85pnWo7E
Account balance _________________________ 10000000000000000000000
Account balance _________________________ 10000ae
Account nonce ___________________________ 0
Pending transactions:
No pending transactions
```
<!-- INSPECT-END -->

Expand All @@ -73,15 +73,14 @@ Transaction hash ________________________ th_2muLsbZeFaVJ3tePTnLqobPhxBzwFsm1zUv
Block hash ______________________________ mh_dnoULQWpiRtcrntd5yJPUxcu7YrTu18xZ1e9EC2b8prKdShME
Block height ____________________________ 2 (about now)
Signatures ______________________________ ["sg_SG5uW5KEGiy5iG1cCkKq4VEdpyvewcW4NjVf4vj2ZoCiap5iB7UQoknWpyWsD4FkziBuGPE88zwXemq3ZvPrdzNtXtKuD"]
Transaction type ________________________ SpendTx
Transaction type ________________________ SpendTx (ver. 1)
Sender address __________________________ ak_21A27UVVt3hDkBE5J7rhhqnH5YNb4Y1dqo4PnSybrH85pnWo7E
Recipient address _______________________ ak_AgV756Vfo99juwzNVgnjP1gXX1op1QN3NXTxvkPnHJPUDE8NT
Amount __________________________________ 42ae
Payload _________________________________ ba_Xfbg4g==
Fee _____________________________________ 0.00001684ae
Nonce ___________________________________ 1
TTL _____________________________________ 4 (about now)
Version _________________________________ 1
```
<!-- SPEND-END -->

Expand Down Expand Up @@ -119,7 +118,6 @@ Find out more in the [user guide](./user-guide.md).
- [`call`](./reference.md#call) — execute a function of the contract
- [`deploy`](./reference.md#deploy) — deploy a contract on the chain
- `oracle`
- [`get`](./reference.md#get) — print oracle details
- [`create`](./reference.md#create-1) — register current account as oracle
- [`extend`](./reference.md#extend-1) — extend oracle's time to leave
- [`create-query`](./reference.md#create-query) — create an oracle query
Expand Down
22 changes: 0 additions & 22 deletions reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
- [`call`](#call) — execute a function of the contract
- [`deploy`](#deploy) — deploy a contract on the chain
- `oracle`
- [`get`](#get) — print oracle details
- [`create`](#create-1) — register current account as oracle
- [`extend`](#extend-1) — extend oracle's time to leave
- [`create-query`](#create-query) — create an oracle query
Expand Down Expand Up @@ -691,27 +690,6 @@ $ aecli contract deploy ./wallet.json --contractBytecode ./contract.txt --contra
# oracle group


## get
```
aecli oracle get [options] <oracleId>
```

Print oracle details.

#### Options
`-u, --url [nodeUrl]`
Node to connect to (default: mainnet, env: AECLI_NODE_URL).
`-f, --force`
Ignore node version compatibility check.
`--json`
Print result in json format.

#### Example calls
```
$ aecli oracle get ok_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi
```


## create
```
aecli oracle create [options] <wallet_path> <queryFormat> <responseFormat>
Expand Down
13 changes: 5 additions & 8 deletions src/actions/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { encode } from '@aeternity/aepp-sdk';
import { initSdk, initSdkByWalletFile } from '../utils/cli.js';
import { print, printTransaction, printUnderscored } from '../utils/print.js';
import CliError from '../utils/CliError.js';
import { getFullPath } from '../utils/helpers.js';
import { formatCoins, getFullPath } from '../utils/helpers.js';

const DESCRIPTOR_VERSION = 1;

Expand Down Expand Up @@ -105,14 +105,11 @@ export async function call(fn, args, walletPath, options) {
});
if (json) print(callResult);
else {
if (callResult.hash) {
await printTransaction(await sdk.api.getTransactionByHash(callResult.hash), json, sdk);
}
await printTransaction(callResult.txData, json, sdk);
print('----------------------Call info-----------------------');
printUnderscored('Contract address', contract.$options.address);
printUnderscored('Gas price', callResult.result?.gasPrice);
printUnderscored('Gas used', callResult.result?.gasUsed);
printUnderscored('Return value (encoded)', callResult.result?.returnValue);
const gasCoins = BigInt(callResult.result.gasUsed) * callResult.txData.tx.gasPrice;
printUnderscored('Gas used', `${callResult.result.gasUsed} (${formatCoins(gasCoins)})`);
printUnderscored('Return value (encoded)', callResult.result.returnValue);
printUnderscored('Return value (decoded)', callResult.decodedResult);
}
}
6 changes: 3 additions & 3 deletions src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ async function getAccountByHash(hash, { json, ...options }) {
});
} else {
printUnderscored('Account ID', hash);
printUnderscored('Account balance', balance);
printUnderscored('Account balance', formatCoins(balance));
printUnderscored('Account nonce', nonce);
print('Pending transactions:');
print(transactions.length ? 'Pending transactions:' : 'No pending transactions');
printBlockTransactions(transactions);
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ async function getOracle(oracleId, { json, ...options }) {
return;
}
printOracle(oracle);
if (oracle.queries) printQueries(oracle.queries);
printQueries(oracle.queries);
}

export default async function inspect(hash, option) {
Expand Down
18 changes: 2 additions & 16 deletions src/actions/oracle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ORACLE_TTL_TYPES } from '@aeternity/aepp-sdk';
import { initSdk, initSdkByWalletFile } from '../utils/cli.js';
import { initSdkByWalletFile } from '../utils/cli.js';
import { decode } from '../utils/helpers.js';
import {
print, printOracle, printQueries, printTransaction,
} from '../utils/print.js';
import { printTransaction } from '../utils/print.js';
import CliError from '../utils/CliError.js';

function ensureTtlANumber(ttl, name) {
Expand Down Expand Up @@ -99,15 +97,3 @@ export async function respondToQuery(walletPath, queryId, response, options) {
});
await printTransaction(queryResponse, json, sdk);
}

export async function queryOracle(oracleId, { json, ...options }) {
decode(oracleId, 'ok');
const sdk = initSdk(options);
const oracle = await sdk.api.getOracleByPubkey(oracleId);
const { oracleQueries: queries } = await sdk.api.getOracleQueriesByPubkey(oracleId);
if (json) print({ ...oracle, queries });
else {
printOracle(oracle, json);
printQueries(queries, json);
}
}
28 changes: 9 additions & 19 deletions src/commands/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const program = new Command('oracle').summary('interact with oracles');

const addCommonOptions = (cmd, example) => {
cmd
.addOption(passwordOption)
.addOption(ttlOption(true))
.addOption(feeOption)
.option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
.addOption(nodeOption)
.addOption(forceOption)
.addOption(jsonOption);
Expand All @@ -18,44 +22,30 @@ const addCommonOptions = (cmd, example) => {
addExamples(cmd, [example]);
};

let command = program.command('get <oracleId>')
.summary('print oracle details')
.action(Oracle.queryOracle);
addCommonOptions(command, exampleOracle);

const addTxOptions = (cmd, example) => {
cmd
.addOption(passwordOption)
.addOption(ttlOption(true))
.addOption(feeOption)
.option('--nonce [nonce]', 'Override the nonce that the transaction is going to be sent with');
addCommonOptions(cmd, example);
};

command = program.command('create <wallet_path> <queryFormat> <responseFormat>')
let command = program.command('create <wallet_path> <queryFormat> <responseFormat>')
.option('--oracleTtl [oracleTtl]', 'Relative oracle time to leave', ORACLE_TTL.value)
.option('--queryFee [queryFee]', 'Oracle query fee', 0)
.summary('register current account as oracle')
.action(Oracle.createOracle);
addTxOptions(command, './wallet.json string string');
addCommonOptions(command, './wallet.json string string');

command = program.command('extend <wallet_path> <oracleTtl>')
.summary('extend oracle\'s time to leave')
.action(Oracle.extendOracle);
addTxOptions(command, './wallet.json 200');
addCommonOptions(command, './wallet.json 200');

command = program.command('create-query <wallet_path> <oracleId> <query>')
.option('--responseTtl [responseTtl]', 'Relative query response time to leave', RESPONSE_TTL.value)
.option('--queryTtl [queryTtl]', 'Relative query time to leave', QUERY_TTL.value)
.option('--queryFee [queryFee]', 'Oracle query fee', 0)
.summary('create an oracle query')
.action(Oracle.createOracleQuery);
addTxOptions(command, `./wallet.json ${exampleOracle} WhatTheWeatherIs?`);
addCommonOptions(command, `./wallet.json ${exampleOracle} WhatTheWeatherIs?`);

command = program.command('respond-query <wallet_path> <queryId> <response>')
.option('--responseTtl [responseTtl]', 'Query response time to leave', RESPONSE_TTL.value)
.summary('respond to an oracle query')
.action(Oracle.respondToQuery);
addTxOptions(command, `./wallet.json ${exampleOracleQuery} +16Degree`);
addCommonOptions(command, `./wallet.json ${exampleOracleQuery} +16Degree`);

export default program;
3 changes: 1 addition & 2 deletions src/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function printTransactionSync(_tx, json, currentHeight) {
printUnderscored('Block hash', tx.blockHash);
printTxField(tx, 'Block height', 'blockHeight', formatTtl);
printUnderscored('Signatures', tx.signatures);
printUnderscored('Transaction type', tx.type);
printUnderscored('Transaction type', `${tx.type} (ver. ${tx.version})`);
// sender
printTxField(tx, 'Account address', 'accountId');
printTxField(tx, 'Sender address', 'senderId');
Expand Down Expand Up @@ -126,7 +126,6 @@ function printTransactionSync(_tx, json, currentHeight) {
printTxField(tx, 'Fee', 'fee', formatCoins);
printTxField(tx, 'Nonce', 'nonce');
printTxField(tx, 'TTL', 'ttl', formatTtl);
printTxField(tx, 'Version', 'version');
}

export async function printTransaction(tx, json, sdk) {
Expand Down
52 changes: 42 additions & 10 deletions test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ describe('Inspect Module', () => {
const res = await executeInspect(sdk.address);
expect(res).to.equal(`
Account ID ______________________________ ${sdk.address}
Account balance _________________________ ${balance}
Account balance _________________________ 50ae
Account nonce ___________________________ ${resJson.nonce}
Pending transactions:
No pending transactions
`.trim());
});

Expand Down Expand Up @@ -63,15 +63,14 @@ Pending transactions:
`Block hash ______________________________ ${resJson.blockHash}`,
`Block height ____________________________ ${resJson.blockHeight} (about now)`,
`Signatures ______________________________ ["${resJson.signatures[0]}"]`,
'Transaction type ________________________ SpendTx',
'Transaction type ________________________ SpendTx (ver. 1)',
`Sender address __________________________ ${sdk.address}`,
`Recipient address _______________________ ${recipient}`,
'Amount __________________________________ 0.00000000000000042ae',
'Payload _________________________________ ba_Xfbg4g==',
/Fee _____________________________________ 0.000016\d+ae/,
`Nonce ___________________________________ ${resJson.tx.nonce}`,
/TTL _____________________________________ \d+ \(in [56] minutes\)/,
'Version _________________________________ 1',
]);
});

Expand Down Expand Up @@ -205,27 +204,60 @@ deposit _________________________________ 0
`.trim());
});

it('Inspect non existing Oracle', async () => {
const fakeOracleId = generateKeyPair().publicKey.replace('ak_', 'ok_');
await executeInspect(fakeOracleId, '--json')
.should.be.rejectedWith('error: Oracle not found');
});

it('Inspect Oracle', async () => {
const { id } = await sdk.registerOracle('<request format>', '<response format>');
const resJson = await executeInspect(id, '--json');
const { id: oracleId } = await sdk.registerOracle('<request format>', '<response format>');
const { id: queryId } = await sdk.postQueryToOracle(oracleId, 'Hello?');
const resJson = await executeInspect(oracleId, '--json');
expect(resJson).to.eql({
id,
id: oracleId,
abiVersion: AbiVersion.NoAbi.toString(),
queries: [],
queries: [{
fee: '0',
id: queryId,
oracleId,
query: 'ov_SGVsbG8/0oNcUw==',
response: 'or_Xfbg4g==',
responseTtl: {
type: 'delta',
value: '10',
},
senderId: sdk.address,
senderNonce: '4',
ttl: resJson.queries[0].ttl,
}],
queryFee: '0',
queryFormat: '<request format>',
responseFormat: '<response format>',
ttl: resJson.ttl,
});
const res = await executeInspect(id);
const res = await executeInspect(oracleId);
// TODO: "no response" message instead of empty string in "Response decoded"
expect(res).to.equal(`
Oracle ID _______________________________ ${id}
Oracle ID _______________________________ ${oracleId}
Oracle Query Fee ________________________ 0
Oracle Query Format _____________________ <request format>
Oracle Response Format __________________ <response format>
Ttl _____________________________________ ${resJson.ttl}

--------------------------------- QUERIES ------------------------------------
Oracle ID _______________________________ ${oracleId}
Query ID ________________________________ ${queryId}
Fee _____________________________________ 0
Query ___________________________________ ov_SGVsbG8/0oNcUw==
Query decoded ___________________________ Hello?
Response ________________________________ or_Xfbg4g==
Response decoded ________________________${' '}
Response Ttl ____________________________ {"type":"delta","value":"10"}
Sender Id _______________________________ ${sdk.address}
Sender Nonce ____________________________ 4
Ttl _____________________________________ ${resJson.queries[0].ttl}
------------------------------------------------------------------------------
`.trim());
});

Expand Down
58 changes: 0 additions & 58 deletions test/oracle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AbiVersion, generateKeyPair } from '@aeternity/aepp-sdk';
import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import { executeProgram, getSdk, WALLET_NAME } from './index.js';
Expand Down Expand Up @@ -84,61 +83,4 @@ describe('Oracle Module', () => {
const query = await oracle.getQuery(queryId);
query.decodedResponse.should.be.equal('Hi!');
});

it('Get non existing Oracle', async () => {
const fakeOracleId = generateKeyPair().publicKey.replace('ak_', 'ok_');
await executeOracle('get', fakeOracleId, '--json')
.should.be.rejectedWith('error: Oracle not found');
await executeOracle('get', 'oq_d1sadasdasda', '--json')
.should.be.rejectedWith('Encoded string have a wrong type: oq (expected: ok)');
});

it('Get Oracle', async () => {
const resJson = await executeOracle('get', oracleId, '--json');
expect(resJson).to.eql({
abiVersion: AbiVersion.NoAbi.toString(),
id: oracleId,
queries: [{
fee: '0',
id: queryId,
oracleId,
query: 'ov_SGVsbG8/0oNcUw==',
response: 'or_SGkh73W+jw==',
responseTtl: {
type: 'delta',
value: '21',
},
senderId: sdk.address,
senderNonce: '3',
ttl: resJson.queries[0].ttl,
}],
queryFee: '0',
queryFormat: 'string',
responseFormat: 'string',
ttl: resJson.ttl,
});

const res = await executeOracle('get', oracleId);
expect(res).to.equal(`
Oracle ID _______________________________ ${oracleId}
Oracle Query Fee ________________________ 0
Oracle Query Format _____________________ string
Oracle Response Format __________________ string
Ttl _____________________________________ ${resJson.ttl}

--------------------------------- QUERIES ------------------------------------
Oracle ID _______________________________ ${oracleId}
Query ID ________________________________ ${queryId}
Fee _____________________________________ 0
Query ___________________________________ ov_SGVsbG8/0oNcUw==
Query decoded ___________________________ Hello?
Response ________________________________ or_SGkh73W+jw==
Response decoded ________________________ Hi!
Response Ttl ____________________________ {"type":"delta","value":"21"}
Sender Id _______________________________ ${sdk.address}
Sender Nonce ____________________________ 3
Ttl _____________________________________ ${resJson.queries[0].ttl}
------------------------------------------------------------------------------
`.trim());
});
});