Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable JSON output for all blockchain commands #22

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export abstract class BlockchainCommand extends Command {
default: "testnet",
}),
};

static enableJsonFlag = true;
}

export abstract class TransactionCommand extends BlockchainCommand {
Expand Down
9 changes: 7 additions & 2 deletions src/commands/node/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ export default class NodeList extends BlockchainCommand {
page: Flags.integer({ description: "The contract call paging size.", helpValue: "N", default: 100 }),
};

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>[]> {
const { flags } = await this.parse(NodeList);

const provider = await getProvider(flags.network);
const nodes = await getContract(flags.network, "nodes", provider);
const operatorId = normalizeHex(flags.operator);
const blockTag = await provider.getBlockNumber();
const results: Result[] = await getAll(flags.page, async (i, n) => {
return await nodes.getNodes(operatorId, flags.topology, i, n, { blockTag });
});
console.log(normalizeRecords(results.slice(flags.skip, flags.skip + flags.size)));
const records = results.slice(flags.skip, flags.skip + flags.size);

const output = normalizeRecords(records);
if (!flags.json) console.log(output);
return output;
}
}
8 changes: 6 additions & 2 deletions src/commands/node/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export default class NodeShow extends BlockchainCommand {
static usage = "<%= command.id %> ID";
static args: Arg[] = [{ name: "ID", description: "The ID of the node to show.", required: true }];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(NodeShow);

const provider = await getProvider(flags.network);
const nodes = await getContract(flags.network, "nodes", provider);
const nodeId = normalizeHex(args.ID);
const record = await nodes.getNode(nodeId);
console.log(normalizeRecord(record));

const output = normalizeRecord(record);
if (!flags.json) console.log(output);
return output;
}
}
9 changes: 6 additions & 3 deletions src/commands/project/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class ProjectContent extends TransactionCommand {
{ name: "SHA", description: "The SHA-256 checksum of the bundle." },
];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(ProjectContent);
if (args.URL === undefined || args.SHA === undefined) {
this.error("URL and SHA must be specified");
Expand All @@ -30,11 +30,14 @@ export default class ProjectContent extends TransactionCommand {
CliUx.ux.action.start("- Submitting transaction");
const tx = await projects.setProjectContent(projectId, args.URL, bundleSha);
CliUx.ux.action.stop("done");
console.log(`> ${getTxUrl(tx)}`);
this.log(`> ${getTxUrl(tx)}`);
CliUx.ux.action.start("- Processing transaction");
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const event = await decodeEvent(receipt, projects, "ProjectContentChanged");
console.log(normalizeRecord(event));

const output = normalizeRecord(event);
if (!flags.json) console.log(output);
return output;
}
}
9 changes: 6 additions & 3 deletions src/commands/project/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class ProjectCreate extends TransactionCommand {
owner: Flags.string({ description: "[default: caller] The owner for the new project.", helpValue: "ADDR" }),
};

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(ProjectCreate);
if (!!args.URL !== !!args.SHA) {
this.error("URL and SHA must be specified together");
Expand All @@ -30,11 +30,14 @@ export default class ProjectCreate extends TransactionCommand {
CliUx.ux.action.start("- Submitting transaction");
const tx = await projects.createProject([owner, args.NAME, args.EMAIL, args.URL, bundleSha]);
CliUx.ux.action.stop("done");
console.log(`> ${getTxUrl(tx)}`);
this.log(`> ${getTxUrl(tx)}`);
CliUx.ux.action.start("- Processing transaction");
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const event = await decodeEvent(receipt, projects, "ProjectCreated");
console.log(normalizeRecord(event));

const output = normalizeRecord(event);
if (!flags.json) console.log(output);
return output;
}
}
10 changes: 7 additions & 3 deletions src/commands/project/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ export default class ProjectDelete extends TransactionCommand {
static usage = "<%= command.id %> ID";
static args: Arg[] = [{ name: "ID", description: "The ID of the project to delete.", required: true }];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(ProjectDelete);

const signer = await getSigner(flags.network, flags.address, flags.signer);
const projects = await getContract(flags.network, "projects", signer);
const projectId = normalizeHex(args.ID);
CliUx.ux.action.start("- Submitting transaction");
const tx = await projects.deleteProject(projectId);
CliUx.ux.action.stop("done");
console.log(`> ${getTxUrl(tx)}`);
this.log(`> ${getTxUrl(tx)}`);
CliUx.ux.action.start("- Processing transaction");
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const event = await decodeEvent(receipt, projects, "ProjectDeleted");
console.log(normalizeRecord(event));

const output = normalizeRecord(event);
if (!flags.json) console.log(output);
return output;
}
}
8 changes: 6 additions & 2 deletions src/commands/project/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export default class ProjectList extends BlockchainCommand {
page: Flags.integer({ description: "The contract call paging size.", helpValue: "N", default: 100 }),
};

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>[]> {
const { flags } = await this.parse(ProjectList);

const provider = await getProvider(flags.network);
const projects = await getContract(flags.network, "projects", provider);
const owner = normalizeHex(flags.owner);
Expand All @@ -26,6 +27,9 @@ export default class ProjectList extends BlockchainCommand {
if (flags.owner) {
results = results.filter((v) => v.owner.toLowerCase() === owner.toLowerCase());
}
console.log(normalizeRecords(results.slice(flags.skip, flags.skip + flags.size)));

const output = normalizeRecords(results.slice(flags.skip, flags.skip + flags.size));
if (!flags.json) console.log(output);
return output;
}
}
10 changes: 7 additions & 3 deletions src/commands/project/owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ export default class ProjectOwner extends TransactionCommand {
{ name: "ADDR", description: "The address of the new project owner.", required: true },
];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(ProjectOwner);

const signer = await getSigner(flags.network, flags.address, flags.signer);
const projects = await getContract(flags.network, "projects", signer);
const projectId = normalizeHex(args.ID);
CliUx.ux.action.start("- Submitting transaction");
const tx = await projects.setProjectOwner(projectId, args.ADDR);
CliUx.ux.action.stop("done");
console.log(`> ${getTxUrl(tx)}`);
this.log(`> ${getTxUrl(tx)}`);
CliUx.ux.action.start("- Processing transaction");
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const event = await decodeEvent(receipt, projects, "ProjectOwnerChanged");
console.log(normalizeRecord(event));

const output = normalizeRecord(event);
if (!flags.json) console.log(output);
return output;
}
}
8 changes: 6 additions & 2 deletions src/commands/project/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export default class ProjectProps extends TransactionCommand {
{ name: "EMAIL", description: "The new email for admin notifications.", required: true },
];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(ProjectProps);

const signer = await getSigner(flags.network, flags.address, flags.signer);
const projects = await getContract(flags.network, "projects", signer);
const projectId = normalizeHex(args.ID);
Expand All @@ -26,6 +27,9 @@ export default class ProjectProps extends TransactionCommand {
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const event = await decodeEvent(receipt, projects, "ProjectPropsChanged");
console.log(normalizeRecord(event));

const output = normalizeRecord(event);
if (!flags.json) console.log(output);
return output;
}
}
8 changes: 6 additions & 2 deletions src/commands/project/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export default class ProjectShow extends BlockchainCommand {
static usage = "<%= command.id %> ID";
static args: Arg[] = [{ name: "ID", description: "The ID of the project to show.", required: true }];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>> {
const { args, flags } = await this.parse(ProjectShow);

const provider = await getProvider(flags.network);
const projects = await getContract(flags.network, "projects", provider);
const projectId = normalizeHex(args.ID);
const record = await projects.getProject(projectId);
console.log(normalizeRecord(record));

const output = normalizeRecord(record);
if (!flags.json) console.log(output);
return output;
}
}
10 changes: 6 additions & 4 deletions src/commands/reservation/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class ReservationCreate extends TransactionCommand {
norenew: Flags.boolean({ description: "Don't renew these reservations in next epoch." }),
};

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>[]> {
const { args, flags, raw } = await this.parse(ReservationCreate);
if (!flags.spot && !!flags.norenew) {
this.error("--norenew requires --spot");
Expand All @@ -37,14 +37,16 @@ export default class ReservationCreate extends TransactionCommand {
const prices = nodeIds.map(() => parseUnits("1", 18));
CliUx.ux.action.start("- Submitting transaction");
const slot = { last: !!flags.spot, next: !flags.norenew };
console.log([projectId, nodeIds, prices, slot]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for removing this rogue debug log!

const tx = await reservations.createReservations(projectId, nodeIds, prices, slot);
CliUx.ux.action.stop("done");
console.log(`> ${getTxUrl(tx)}`);
this.log(`> ${getTxUrl(tx)}`);
CliUx.ux.action.start("- Processing transaction");
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const events = await decodeEvents(receipt, reservations, "ReservationCreated");
console.log(normalizeRecords(events));

const output = normalizeRecords(events);
if (!flags.json) console.log(output);
return output;
}
}
9 changes: 6 additions & 3 deletions src/commands/reservation/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ReservationDelete extends TransactionCommand {
{ name: "IDS", description: "The IDs of the nodes to release from the project.", required: true },
];

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>[]> {
const { args, flags, raw } = await this.parse(ReservationDelete);
// Parse vararg IDS
const nodeIds = raw
Expand All @@ -30,11 +30,14 @@ export default class ReservationDelete extends TransactionCommand {
const slot = { last: false, next: true };
const tx = await reservations.deleteReservations(projectId, nodeIds, slot);
CliUx.ux.action.stop("done");
console.log(`> ${getTxUrl(tx)}`);
this.log(`> ${getTxUrl(tx)}`);
CliUx.ux.action.start("- Processing transaction");
const receipt = await tx.wait();
CliUx.ux.action.stop("done");
const events = await decodeEvents(receipt, reservations, "ReservationDeleted");
console.log(normalizeRecords(events));

const output = normalizeRecords(events);
if (!flags.json) console.log(output);
return output;
}
}
8 changes: 6 additions & 2 deletions src/commands/reservation/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ export default class ReservationList extends BlockchainCommand {
page: Flags.integer({ description: "The contract call paging size.", helpValue: "N", default: 100 }),
};

public async run(): Promise<void> {
public async run(): Promise<Record<string, unknown>[]> {
const { args, flags } = await this.parse(ReservationList);

const provider = await getProvider(flags.network);
const reservations = await getContract(flags.network, "reservations", provider);
const projectId = normalizeHex(args.ID);
const blockTag = await provider.getBlockNumber();
const results: Result[] = await getAll(flags.page, async (i, n) => {
return await reservations.getReservations(projectId, i, n, { blockTag });
});
console.log(normalizeRecords(results.slice(flags.skip, flags.skip + flags.size)));

const output = normalizeRecords(results.slice(flags.skip, flags.skip + flags.size));
if (!flags.json) console.log(output);
return output;
}
}
15 changes: 11 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export function normalizeRecord(r: Record<string, unknown> | Result): Record<str
Object.keys(r)
.filter((k) => isNaN(Number(k)))
.map((k) => {
if (r[k] instanceof BigNumber) {
return [k, normalizeBigNumber(r[k])];
}
return [k, r[k]];
return [k, normalizeRecordValue(r[k])];
})
);
}
Expand All @@ -39,6 +36,16 @@ export function normalizeRecords(rs: (Record<string, unknown> | Result)[]): Reco
return rs.map((r) => normalizeRecord(r));
}

function normalizeRecordValue(val: unknown): unknown {
if (Array.isArray(val)) {
return val.map(normalizeRecordValue);
}
if (val instanceof BigNumber) {
return normalizeBigNumber(val);
}
return val;
}

// Prepends 0x, and replaces undefined and empty with 256-bit zero hash.
export function normalizeHex(s: string | undefined): string {
if (!s?.length) {
Expand Down