Skip to content

Commit

Permalink
fix(oracle): decoding of oracle query
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 17, 2023
1 parent a0cc66c commit fa2764b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
11 changes: 5 additions & 6 deletions src/actions/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ export async function respondToQuery(
}

// ## Get oracle
export async function queryOracle(oracleId, options) {
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 (options.json) {
print({ ...oracle, queries });
} else {
printOracle(oracle, options.json);
printQueries(queries, options.json);
if (json) print({ ...oracle, queries });
else {
printOracle(oracle, json);
printQueries(queries, json);
}
}
4 changes: 2 additions & 2 deletions src/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ export function printQueries(queries = [], json) {
printUnderscored('Query ID', q.id ?? 'N/A');
printUnderscored('Fee', q.fee ?? 'N/A');
printUnderscored('Query', q.query ?? 'N/A');
printUnderscored('Query decoded', decode(q.query, 'oq').toString() ?? 'N/A');
printUnderscored('Query decoded', decode(q.query, Encoding.OracleQuery).toString() ?? 'N/A');
printUnderscored('Response', q.response ?? 'N/A');
printUnderscored('Response decoded', decode(q.response, 'or').toString() ?? 'N/A');
printUnderscored('Response decoded', decode(q.response, Encoding.OracleResponse).toString() ?? 'N/A');
printUnderscored('Response Ttl', q.responseTtl ?? 'N/A');
printUnderscored('Sender Id', q.senderId ?? 'N/A');
printUnderscored('Sender Nonce', q.senderNonce ?? 'N/A');
Expand Down
51 changes: 47 additions & 4 deletions test/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { generateKeyPair } from '@aeternity/aepp-sdk';
import { AbiVersion, generateKeyPair } from '@aeternity/aepp-sdk';
import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import { executeProgram, getSdk, WALLET_NAME } from './index';
Expand Down Expand Up @@ -91,8 +91,51 @@ describe('Oracle Module', () => {
});

it('Get existed Oracle', async () => {
const oracle = await executeOracle(['get', oracleId, '--json']);
oracle.id.should.be.a('string');
oracle.id.split('_')[0].should.be.equal('ok');
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 fa2764b

Please sign in to comment.