Skip to content

Commit

Permalink
fix(oracle): providing oracleTtl, queryTtl, responseTtl
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 17, 2023
1 parent 5484692 commit a0cc66c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
47 changes: 31 additions & 16 deletions src/actions/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,36 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { ORACLE_TTL_TYPES } from '@aeternity/aepp-sdk';
import { initSdk, initSdkByWalletFile } from '../utils/cli';
import { BUILD_ORACLE_TTL } from '../utils/constant';
import { decode } from '../utils/helpers';
import {
print, printOracle, printQueries, printTransaction,
} from '../utils/print';
import CliError from '../utils/CliError';

function ensureTtlANumber(ttl, name) {
if (isNaN(+ttl)) throw new CliError(`${name} TTL should be a number`);
}

// ## Create Oracle
export async function createOracle(walletPath, queryFormat, responseFormat, options) {
const {
ttl, fee, nonce, waitMined, json, oracleTtl, queryFee,
} = options;

ensureTtlANumber(oracleTtl, 'Oracle');
const sdk = await initSdkByWalletFile(walletPath, options);
// Register Oracle
const oracle = await sdk.registerOracle(queryFormat, responseFormat, {
ttl,
waitMined,
nonce,
fee,
oracleTtl: isNaN(+oracleTtl)
? oracleTtl
: BUILD_ORACLE_TTL(oracleTtl),
...oracleTtl && {
oracleTtlType: ORACLE_TTL_TYPES.delta,
oracleTtlValue: oracleTtl,
},
queryFee,
});
if (waitMined) {
Expand All @@ -57,7 +63,7 @@ export async function extendOracle(walletPath, oracleId, oracleTtl, options) {
ttl, fee, nonce, waitMined, json,
} = options;

if (isNaN(+oracleTtl)) throw new CliError('Oracle Ttl should be a number');
ensureTtlANumber(oracleTtl, 'Oracle');
decode(oracleId, 'ok');
const sdk = await initSdkByWalletFile(walletPath, options);
const oracle = await sdk.getOracleObject(oracleId);
Expand All @@ -66,7 +72,10 @@ export async function extendOracle(walletPath, oracleId, oracleTtl, options) {
waitMined,
nonce,
fee,
...BUILD_ORACLE_TTL(oracleTtl),
...oracleTtl && {
oracleTtlType: ORACLE_TTL_TYPES.delta,
oracleTtlValue: oracleTtl,
},
});
if (waitMined) {
printTransaction(extended, json);
Expand All @@ -78,24 +87,28 @@ export async function extendOracle(walletPath, oracleId, oracleTtl, options) {
// ## Create Oracle Query
export async function createOracleQuery(walletPath, oracleId, query, options) {
const {
ttl, fee, nonce, waitMined, json, queryTll, queryFee, responseTtl,
ttl, fee, nonce, waitMined, json, queryTtl, queryFee, responseTtl,
} = options;

decode(oracleId, 'ok');
const sdk = await initSdkByWalletFile(walletPath, options);

ensureTtlANumber(queryTtl, 'Query');
ensureTtlANumber(responseTtl, 'Response');
const oracle = await sdk.getOracleObject(oracleId);
const oracleQuery = await oracle.postQuery(query, {
ttl,
waitMined,
nonce,
fee,
queryTll: isNaN(+queryTll)
? queryTll
: BUILD_ORACLE_TTL(queryTll),
responseTtl: isNaN(+responseTtl)
? responseTtl
: BUILD_ORACLE_TTL(responseTtl),
...queryTtl && {
queryTtlType: ORACLE_TTL_TYPES.delta,
queryTtlValue: queryTtl,
},
...responseTtl && {
responseTtlType: ORACLE_TTL_TYPES.delta,
responseTtlValue: responseTtl,
},
queryFee,
});
if (waitMined) {
Expand All @@ -119,6 +132,7 @@ export async function respondToQuery(

decode(oracleId, 'ok');
decode(queryId, 'oq');
ensureTtlANumber(responseTtl, 'Response');
const sdk = await initSdkByWalletFile(walletPath, options);

const oracle = await sdk.getOracleObject(oracleId);
Expand All @@ -127,9 +141,10 @@ export async function respondToQuery(
waitMined,
nonce,
fee,
responseTtl: isNaN(+responseTtl)
? responseTtl
: BUILD_ORACLE_TTL(responseTtl),
...responseTtl && {
responseTtlType: ORACLE_TTL_TYPES.delta,
responseTtlValue: responseTtl,
},
});
if (waitMined) {
printTransaction(queryResponse, json);
Expand Down
11 changes: 5 additions & 6 deletions src/commands/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
*/
// We'll use `commander` for parsing options
import { Command } from 'commander';
import { ORACLE_TTL, QUERY_TTL } from '@aeternity/aepp-sdk';
import { RESPONSE_TTL } from '../utils/constant';
import { ORACLE_TTL, QUERY_TTL, RESPONSE_TTL } from '@aeternity/aepp-sdk';
import * as Oracle from '../actions/oracle';
import {
nodeOption, jsonOption, feeOption, forceOption, passwordOption, ttlOption, networkIdOption,
Expand Down Expand Up @@ -51,7 +50,7 @@ const addCommonOptions = (p) => p
addCommonOptions(program
.command('create <wallet_path> <queryFormat> <responseFormat>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.option('--oracleTtl [oracleTtl]', 'Relative Oracle time to leave', ORACLE_TTL)
.option('--oracleTtl [oracleTtl]', 'Relative oracle time to leave', ORACLE_TTL.value)
.option('--queryFee [queryFee]', 'Oracle query fee', 0)
.description('Register Oracle')
.action(Oracle.createOracle));
Expand Down Expand Up @@ -83,8 +82,8 @@ addCommonOptions(program
addCommonOptions(program
.command('create-query <wallet_path> <oracleId> <query>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.option('--responseTtl [responseTtl]', 'Query response time to leave', RESPONSE_TTL)
.option('--queryTtl [queryTtl]', 'Query time to leave', QUERY_TTL)
.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)
.description('Create Oracle query')
.action(Oracle.createOracleQuery));
Expand All @@ -101,7 +100,7 @@ addCommonOptions(program
addCommonOptions(program
.command('respond-query <wallet_path> <oracleId> <queryId> <response>')
.option('-M, --no-waitMined', 'Do not wait until transaction will be mined')
.option('--responseTtl [responseTtl]', 'Query response time to leave', RESPONSE_TTL)
.option('--responseTtl [responseTtl]', 'Query response time to leave', RESPONSE_TTL.value)
.description('Respond to Oracle Query')
.action(Oracle.respondToQuery));

Expand Down
7 changes: 3 additions & 4 deletions src/commands/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
// Also we need `esm` package to handle `ES imports`
import { Command } from 'commander';
import {
NAME_TTL, CLIENT_TTL, MIN_GAS_PRICE, ORACLE_TTL, QUERY_TTL,
NAME_TTL, CLIENT_TTL, MIN_GAS_PRICE, ORACLE_TTL, QUERY_TTL, RESPONSE_TTL,
} from '@aeternity/aepp-sdk';
import { RESPONSE_TTL } from '../utils/constant';
import * as Transaction from '../actions/transaction';
import {
nodeOption,
Expand Down Expand Up @@ -186,7 +185,7 @@ addCommonOptions(program
.addOption(feeOption)
.option('--queryFee [queryFee]', 'Oracle Query fee.', 0)
.option('--queryTtl [oracleTtl]', 'Oracle Ttl.', QUERY_TTL.value)
.option('--responseTtl [oracleTtl]', 'Oracle Ttl.', RESPONSE_TTL)
.option('--responseTtl [oracleTtl]', 'Oracle Ttl.', RESPONSE_TTL.value)
.description('Build oracle post query transaction.')
.action(Transaction.oraclePostQuery));

Expand All @@ -213,7 +212,7 @@ addCommonOptions(program
.addArgument(nonceArgument)
.addOption(ttlOption)
.addOption(feeOption)
.option('--responseTtl [oracleTtl]', 'Oracle Ttl.', RESPONSE_TTL)
.option('--responseTtl [oracleTtl]', 'Oracle Ttl.', RESPONSE_TTL.value)
.description('Build oracle extend transaction.')
.action(Transaction.oracleRespond));

Expand Down
4 changes: 0 additions & 4 deletions src/utils/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@
// ## CONNECTION
export const NODE_URL = 'https://testnet.aeternity.io';
export const COMPILER_URL = 'https://v7.compiler.aepps.com';

// ## ORACLE
export const BUILD_ORACLE_TTL = (ttl) => ({ type: 'delta', value: ttl });
export const RESPONSE_TTL = 10;
12 changes: 10 additions & 2 deletions test/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { generateKeyPair } from '@aeternity/aepp-sdk';
import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import { executeProgram, getSdk, WALLET_NAME } from './index';
import oracleProgram from '../src/commands/oracle';

Expand All @@ -36,7 +37,9 @@ describe('Oracle Module', () => {
it('Oracle create', async () => {
const oracleCreate = await executeOracle([
'create', WALLET_NAME, '--password', 'test', oracleFormat, responseFormat, '--json',
'--oracleTtl', 420,
]);
expect(oracleCreate.ttl).to.be.equal(oracleCreate.blockHeight + 420);
oracleCreate.blockHeight.should.be.gt(0);
oracleCreate.queryFormat.should.be.equal(oracleFormat);
oracleCreate.responseFormat.should.be.equal(responseFormat);
Expand All @@ -46,16 +49,19 @@ describe('Oracle Module', () => {
it('Oracle extend', async () => {
const oracle = await sdk.getOracleObject(oracleId);
const oracleExtend = await executeOracle([
'extend', WALLET_NAME, '--password', 'test', oracleId, 100, '--json',
'extend', WALLET_NAME, '--password', 'test', oracleId, 42, '--json',
]);
oracleExtend.blockHeight.should.be.gt(0);
oracleExtend.ttl.should.be.gte(oracle.ttl + 100);
expect(oracleExtend.ttl).to.be.equal(oracle.ttl + 42);
});

it('Oracle create query', async () => {
const oracleQuery = await executeOracle([
'create-query', WALLET_NAME, '--password', 'test', oracleId, 'Hello?', '--json',
'--queryTtl', 42, '--responseTtl', 21,
]);
expect(oracleQuery.ttl).to.be.equal(oracleQuery.blockHeight + 42);
expect(oracleQuery.responseTtl).to.be.eql({ type: 'delta', value: '21' });
oracleQuery.blockHeight.should.be.gt(0);
oracleQuery.decodedQuery.should.be.equal('Hello?');
oracleQuery.id.split('_')[0].should.be.equal('oq');
Expand All @@ -67,7 +73,9 @@ describe('Oracle Module', () => {
it('Oracle respond to query', async () => {
const oracleQueryResponse = await executeOracle([
'respond-query', WALLET_NAME, '--password', 'test', oracleId, queryId, 'Hi!', '--json',
'--responseTtl', 21,
]);
expect(oracleQueryResponse.queries[0].ttl).to.be.equal(oracleQueryResponse.blockHeight + 21);
oracleQueryResponse.blockHeight.should.be.gt(0);
const oracle = await sdk.getOracleObject(oracleId);
const query = await oracle.getQuery(queryId);
Expand Down

0 comments on commit a0cc66c

Please sign in to comment.