Skip to content

Commit

Permalink
refactor(oracle)!: remove oracle get
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `aecli oracle get` removed
Use `aecli inspect` instead.
  • Loading branch information
davidyuk committed Apr 16, 2024
1 parent fbc4de7 commit 6db5dc2
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 123 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,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
2 changes: 1 addition & 1 deletion src/actions/inspect.js
Original file line number Diff line number Diff line change
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;
45 changes: 39 additions & 6 deletions test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,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());
});
});

0 comments on commit 6db5dc2

Please sign in to comment.