Skip to content

Commit

Permalink
fix(inspect): encoded tx, plain contract output, oracle queries in json
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 17, 2023
1 parent 4ce5348 commit dae4df9
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docker/accounts_test.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"ak_2dATVcZ9KJU5a8hdsVtTv21pYiGWiPbmVcU1Pz72FFqpk9pSRR": 100000000000001000000000000000000000
"ak_2dATVcZ9KJU5a8hdsVtTv21pYiGWiPbmVcU1Pz72FFqpk9pSRR": 10000000000000001000000000000000000000
}
36 changes: 21 additions & 15 deletions src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { Encoding, unpackTx as _unpackTx } from '@aeternity/aepp-sdk';
import { Encoding, unpackTx as _unpackTx, Tag } from '@aeternity/aepp-sdk';
import { initSdk } from '../utils/cli';
import {
print,
Expand All @@ -33,6 +33,10 @@ import {
} from '../utils/helpers';
import CliError from '../utils/CliError';

function printEntries(object) {
Object.entries(object).forEach((entry) => printUnderscored(...entry));
}

// ## Inspect helper function's
async function getBlockByHash(hash, { json, ...options }) {
checkPref(hash, [Encoding.KeyBlockHash, Encoding.MicroBlockHash]);
Expand All @@ -46,15 +50,11 @@ async function getTransactionByHash(hash, { json, ...options }) {
printTransaction(await sdk.api.getTransactionByHash(hash), json);
}

async function unpackTx(hash, { json }) {
checkPref(hash, Encoding.Transaction);
const { tx, txType: type } = _unpackTx(hash);
if (json) {
print({ tx, type });
return;
}
printUnderscored('Tx Type', type);
Object.entries(tx).forEach((entry) => printUnderscored(...entry));
async function unpackTx(encodedTx, { json }) {
checkPref(encodedTx, Encoding.Transaction);
const txUnpacked = _unpackTx(encodedTx);
if (json) print(txUnpacked);
else printEntries({ 'Tx Type': Tag[txUnpacked.tag], ...txUnpacked });
}

async function getAccountByHash(hash, { json, ...options }) {
Expand Down Expand Up @@ -92,15 +92,21 @@ async function getName(name, { json, ...options }) {

async function getContract(contractId, { json, ...options }) {
const sdk = initSdk(options);
printTransaction(await sdk.api.getContract(contractId), json);
const contract = await sdk.api.getContract(contractId);
if (json) print(contract);
else printEntries(contract);
}

async function getOracle(oracleId, { json, ...options }) {
const sdk = initSdk(options);
// printTransaction(await sdk.api.getContract(contractId), json)
printOracle(await sdk.api.getOracleByPubkey(oracleId), json);
const { oracleQueries: queries } = await sdk.api.getOracleQueriesByPubkey(oracleId);
if (queries) printQueries(queries, json);
const oracle = await sdk.api.getOracleByPubkey(oracleId);
oracle.queries = (await sdk.api.getOracleQueriesByPubkey(oracleId)).oracleQueries;
if (json) {
print(oracle);
return;
}
printOracle(oracle);
if (oracle.queries) printQueries(oracle.queries);
}

// ## Inspect function
Expand Down
11 changes: 7 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const Sdk = (params = {}) => {
const spendPromise = (async () => {
const sdk = Sdk();
await sdk.awaitHeight(2);
await sdk.spend(1e26, keypair.publicKey);
await sdk.spend(1e28, keypair.publicKey);
})();

function getProgramOptions(command) {
Expand Down Expand Up @@ -116,11 +116,14 @@ export async function executeProgram(program, args) {

export async function getSdk() {
await spendPromise;

const tempKeyPair = generateKeyPair();
const sdk = Sdk({
accounts: [new MemoryAccount(keypair.secretKey)],
accounts: [new MemoryAccount(tempKeyPair.secretKey)],
});
await executeProgram(accountProgram, ['save', WALLET_NAME, '--password', 'test', keypair.secretKey, '--overwrite']);
await Promise.all([
executeProgram(accountProgram, ['save', WALLET_NAME, '--password', 'test', tempKeyPair.secretKey, '--overwrite']),
sdk.spend(1e26, tempKeyPair.publicKey, { onAccount: new MemoryAccount(keypair.secretKey) }),
]);
return sdk;
}

Expand Down
226 changes: 208 additions & 18 deletions test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import { before, describe, it } from 'mocha';
import { expect } from 'chai';
import { generateKeyPair } from '@aeternity/aepp-sdk';
import {
AbiVersion, generateKeyPair, Tag, VmVersion,
} from '@aeternity/aepp-sdk';
import { executeProgram, getSdk } from './index';
import inspectProgram from '../src/commands/inspect';
import chainProgram from '../src/commands/chain';
Expand All @@ -34,37 +36,225 @@ describe('Inspect Module', () => {

it('Inspect Account', async () => {
const balance = await sdk.getBalance(sdk.address);
const { balance: cliBalance } = await executeInspect([sdk.address, '--json']);
const isEqual = `${balance}` === `${cliBalance}`;
isEqual.should.equal(true);
const resJson = await executeInspect([sdk.address, '--json']);
expect(resJson).to.eql({
balance,
hash: sdk.address,
nonce: resJson.nonce,
transactions: [],
});
const res = await executeInspect([sdk.address]);
expect(res).to.equal(`
Account ID ______________________________ ${sdk.address}
Account balance _________________________ ${balance}
Account nonce ___________________________ ${resJson.nonce}
Pending transactions:
`.trim());
});

it('Inspect Transaction', async () => {
it('Inspect Transaction Hash', async () => {
const recipient = (generateKeyPair()).publicKey;
const amount = '420';
const { hash } = await sdk.spend(amount, recipient);
const resJson = await executeInspect([hash, '--json']);
expect(resJson).to.eql({
blockHash: resJson.blockHash,
blockHeight: resJson.blockHeight,
hash: resJson.hash,
signatures: [resJson.signatures[0]],
tx: {
recipientId: recipient,
senderId: sdk.address,
amount,
fee: '16700000000000',
nonce: resJson.tx.nonce,
payload: 'ba_Xfbg4g==',
type: 'SpendTx',
version: 1,
},
});
const res = await executeInspect([hash]);
expect(res).to.equal(`
Tx hash _________________________________ ${resJson.hash}
Block hash ______________________________ ${resJson.blockHash}
Block height ____________________________ ${resJson.blockHeight}
Signatures ______________________________ ["${resJson.signatures[0]}"]
Tx Type _________________________________ SpendTx
Sender account __________________________ ${sdk.address}
Recipient account _______________________ ${recipient}
Amount __________________________________ 420
Payload _________________________________ ba_Xfbg4g==
Fee _____________________________________ 16700000000000
Nonce ___________________________________ ${resJson.tx.nonce}
TTL _____________________________________ N/A
Version _________________________________ 1
`.trim());
});

const res = await executeInspect([hash, '--json']);
res.tx.recipientId.should.equal(recipient);
res.tx.senderId.should.be.equal(sdk.address);
res.tx.amount.should.equal(amount);
it('Inspect Transaction', async () => {
const recipientId = (generateKeyPair()).publicKey;
const amount = '420';
const tx = await sdk.buildTx({
tag: Tag.SpendTx, amount, recipientId, senderId: sdk.address,
});
const resJson = await executeInspect([tx, '--json']);
expect(resJson).to.eql({
amount,
fee: '16700000000000',
nonce: resJson.nonce,
payload: 'ba_Xfbg4g==',
recipientId,
senderId: sdk.address,
tag: Tag.SpendTx,
ttl: 0,
version: 1,
});
const res = await executeInspect([tx]);
expect(res).to.equal(`
Tx Type _________________________________ SpendTx
tag _____________________________________ 12
version _________________________________ 1
senderId ________________________________ ${sdk.address}
recipientId _____________________________ ${recipientId}
amount __________________________________ 420
fee _____________________________________ 16700000000000
ttl _____________________________________ 0
nonce ___________________________________ ${resJson.nonce}
payload _________________________________ ba_Xfbg4g==
`.trim());
});

it('Inspect Block', async () => {
const top = await executeChain(['top', '--json']);
const inspectRes = await executeInspect([top.hash, '--json']);
top.hash.should.equal(inspectRes.hash);
const resJson = await executeInspect([top.hash, '--json']);
expect(resJson).to.eql({
hash: top.hash,
height: top.height,
pofHash: 'no_fraud',
prevHash: resJson.prevHash,
prevKeyHash: resJson.prevKeyHash,
signature: resJson.signature,
stateHash: resJson.stateHash,
time: resJson.time,
transactions: resJson.transactions,
txsHash: resJson.txsHash,
version: 5,
});
const res = await executeInspect([top.hash]);
expect(res.split('\nTransactions')[0]).to.equal(`
<<--------------- MicroBlock --------------->>
Block hash ______________________________ ${top.hash}
Block height ____________________________ ${top.height}
State hash ______________________________ ${resJson.stateHash}
Nonce ___________________________________ N/A
Miner ___________________________________ N/A
Time ____________________________________ ${new Date(resJson.time).toString()}
Previous block hash _____________________ ${resJson.prevHash}
Previous key block hash _________________ ${resJson.prevKeyHash}
Version _________________________________ 5
Target __________________________________ N/A
`.trim());
});

it('Inspect Height', async () => {
const top = await executeChain(['top', '--json']);
const inspectRes = await executeInspect([top.hash, '--json']);
top.hash.should.equal(inspectRes.hash);
it('Inspect Contract', async () => {
const contract = await sdk.initializeContract({
sourceCode: `
contract Identity =
entrypoint foo() = "test"
`,
});
const { address } = await contract.$deploy([]);
const resJson = await executeInspect([address, '--json']);
expect(resJson).to.eql({
abiVersion: AbiVersion.Fate.toString(),
active: true,
deposit: '0',
id: address,
ownerId: sdk.address,
referrerIds: [],
vmVersion: VmVersion.Fate2.toString(),
});
const res = await executeInspect([address]);
expect(res).to.equal(`
id ______________________________________ ${address}
ownerId _________________________________ ${sdk.address}
vmVersion _______________________________ 7
abiVersion ______________________________ 3
active __________________________________ true
referrerIds _____________________________ []
deposit _________________________________ 0
`.trim());
});

it('Inspect Oracle', async () => {
const { id } = await sdk.registerOracle('<request format>', '<response format>');
const resJson = await executeInspect([id, '--json']);
expect(resJson).to.eql({
id,
abiVersion: AbiVersion.NoAbi.toString(),
queries: [],
queryFee: '0',
queryFormat: '<request format>',
responseFormat: '<response format>',
ttl: resJson.ttl,
});
const res = await executeInspect([id]);
expect(res).to.equal(`
Oracle ID _______________________________ ${id}
Oracle Query Fee ________________________ 0
Oracle Query Format _____________________ <request format>
Oracle Response Format __________________ <response format>
Ttl _____________________________________ ${resJson.ttl}
--------------------------------- QUERIES ------------------------------------
`.trim());
});

it('Inspect Name', async () => {
it('Inspect Invalid Name', async () => {
await expect(executeInspect(['asd', '--json'])).to.be.rejectedWith('Name should end with .chain');
const validName = await executeInspect(['nazdou2222222.chain', '--json']);
validName.status.should.be.equal('AVAILABLE');
});

const name = `nazdou${Math.random().toString().slice(2, 9)}.chain`;

it('Inspect Unclaimed Name', async () => {
const resJson = await executeInspect([name, '--json']);
expect(resJson).to.eql({
name,
status: 'AVAILABLE',
});
const res = await executeInspect([name]);
expect(res).to.equal(`
Status __________________________________ AVAILABLE
Name hash _______________________________ N/A
Pointers ________________________________ N/A
TTL _____________________________________ 0
`.trim());
});

it('Inspect Claimed Name', async () => {
await (await (await sdk.aensPreclaim(name)).claim()).update({
myKey: sdk.address,
account_pubkey: sdk.address,
oracle_pubkey: sdk.address,
});
const resJson = await executeInspect([name, '--json']);
expect(resJson).to.eql({
id: resJson.id,
owner: sdk.address,
pointers: [
{ id: sdk.address, key: 'myKey' },
{ id: sdk.address, key: 'account_pubkey' },
{ id: sdk.address, key: 'oracle_pubkey' },
],
status: 'CLAIMED',
ttl: resJson.ttl,
});
const res = await executeInspect([name]);
expect(res).to.equal(`
Status __________________________________ CLAIMED
Name hash _______________________________ ${resJson.id}
Pointers ________________________________ [{"key":"myKey","id":"${sdk.address}"},{"key":"account_pubkey","id":"${sdk.address}"},{"key":"oracle_pubkey","id":"${sdk.address}"}]
TTL _____________________________________ ${resJson.ttl}
`.trim());
}).timeout(4000);
});

0 comments on commit dae4df9

Please sign in to comment.