Skip to content

Commit

Permalink
Use oclif logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanco committed Jan 17, 2024
1 parent 044e9c7 commit 2a0a7fa
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/tree-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"postpack": "shx rm -f oclif.manifest.json",
"prepack": "yarn build && oclif manifest && oclif readme",
"prepare": "yarn build",
"test": "mocha --forbid-only \"test/**/*.ts\"",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "oclif readme && git add README.md"
},
"oclif": {
Expand All @@ -42,12 +42,12 @@
"topicSeparator": " "
},
"dependencies": {
"@liskhq/lisk-cryptography": "4.1.0",
"@oclif/core": "^3",
"@oclif/plugin-help": "^6",
"@openzeppelin/merkle-tree": "^1.0.5",
"ethereumjs-util": "^7.1.5",
"ethers": "^6.8.1",
"@liskhq/lisk-cryptography": "4.1.0",
"sinon": "^17.0.1",
"tweetnacl": "^1.0.3"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'fs';
import * as path from 'path';
import { address } from '@liskhq/lisk-cryptography';
import { ExampleKey } from '../../interface';

Expand Down Expand Up @@ -45,7 +44,6 @@ const multiSigs = [
const randomBalance = (range: number): number => Number((range * Math.random()).toFixed(8));

export function createAccounts(numberOfAccounts = 54) {
console.log(path.join(__dirname, '../../../../../../data/example/accounts.json'));
const keyPairs = JSON.parse(
fs.readFileSync('../../data/example/key-pairs.json', 'utf-8'),
) as ExampleKey[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StandardMerkleTree } from '@openzeppelin/merkle-tree';
import { Account, Leaf } from '../../interface';
import { LEAF_ENCODING } from '../../constants';
import { append0x } from '../../utils';
import { debug, log } from 'oclif/lib/log';

export function createPayload(account: Account) {
return [
Expand Down Expand Up @@ -33,7 +34,7 @@ export function build_tree(accounts: Account[]): {
}
}

console.log(`${accounts.length} Accounts to generate:`);
log(`${accounts.length} Accounts to generate:`);

const leaves: Leaf[] = [];
const tree = StandardMerkleTree.of(
Expand All @@ -47,7 +48,7 @@ export function build_tree(accounts: Account[]): {
const addressHex = address.getAddressFromLisk32Address(account.lskAddress);
const payload = createPayload(account);

console.log(
debug(
`${account.lskAddress}: ${account.balance} LSK (Multisig=${
account.numberOfSignatures && account.numberOfSignatures > 0 ? 'Y' : 'N'
})`,
Expand All @@ -70,6 +71,8 @@ export function build_tree(accounts: Account[]): {
});
}

debug('===== ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== =====');

return {
tree,
leaves,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { Account } from '../../interface';
import { build_tree } from './build_tree';
import { log } from 'oclif/lib/log';

export function buildTreeJson(outputPath: string) {
let accounts: Account[];
Expand All @@ -10,17 +11,15 @@ export function buildTreeJson(outputPath: string) {
try {
accounts = JSON.parse(fs.readFileSync(accountsPath, 'utf-8')) as Account[];
} catch (err) {
console.log(`Error occurred reading ${accountsPath}`);
log(`Error occurred reading ${accountsPath}`);
if (err instanceof Error) {
console.log(err.message);
log(err.message);
}
process.exit(1);
}

const { tree, leaves } = build_tree(accounts);

console.log('===== ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== =====');

const merkleTreeResultDetailedJSONPath = path.join(
outputPath,
'merkle-tree-result-detailed.json',
Expand All @@ -33,7 +32,7 @@ export function buildTreeJson(outputPath: string) {
}),
'utf-8',
);
console.log(`Detailed result outputted to: ${merkleTreeResultDetailedJSONPath}`);
log(`Detailed result outputted to: ${merkleTreeResultDetailedJSONPath}`);

const merkleTreeResultJSONPath = path.join(outputPath, 'merkle-tree-result.json');
fs.writeFileSync(
Expand All @@ -51,7 +50,7 @@ export function buildTreeJson(outputPath: string) {
}),
'utf-8',
);
console.log(`Lightweight result outputted to: ${merkleTreeResultJSONPath}`);
log(`Lightweight result outputted to: ${merkleTreeResultJSONPath}`);

const merkleRootJSONPath = path.join(outputPath, 'merkle-root.json');
fs.writeFileSync(
Expand All @@ -61,5 +60,5 @@ export function buildTreeJson(outputPath: string) {
}),
'utf-8',
);
console.log(`MerkleRoot outputted to: ${merkleRootJSONPath}`);
log(`MerkleRoot outputted to: ${merkleRootJSONPath}`);
}
4 changes: 2 additions & 2 deletions packages/tree-builder/src/commands/example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class Example extends Command {

static description = 'Generate example data for demo purpose';

static examples = [`$ oex generate`];
static examples = [`$ oex example`];

async run(): Promise<void> {
const { flags } = await this.parse(Example);
Expand All @@ -33,7 +33,7 @@ export default class Example extends Command {
createAccounts(flags.amountOfLeaves);

// Build MerkleTree to example
buildTreeJson(`../../data/example`);
buildTreeJson('../../data/example');

// Sign all leaves using key-pairs.json
signAccounts(flags.recipient);
Expand Down

0 comments on commit 2a0a7fa

Please sign in to comment.