Skip to content

Commit

Permalink
feat: new logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongsu-hong committed Feb 27, 2024
1 parent fcef05f commit 71f0172
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
8 changes: 6 additions & 2 deletions script/shared/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ export type ContextDeployments = {
mailbox?: typed<"hpl_mailbox">;
validator_announce?: typed<"hpl_validator_announce">;
};

isms?: ContextIsm;

hooks?: {
default?: ContextHook;
required?: ContextHook;
};

warp?: {
cw20?: typed<"hpl_warp_cw20">[];
native?: typed<"hpl_warp_native">[];
cw20?: ({ id: string } & typed<"hpl_warp_cw20">)[];
native?: ({ id: string } & typed<"hpl_warp_native">)[];
};

test?: {
msg_receiver?: typed<"hpl_test_mock_msg_receiver">;
};
Expand Down
49 changes: 37 additions & 12 deletions script/shared/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Context } from "./context";
import { waitTx } from "./utils";
import { Client } from "./config";
import { IndexedTx } from "@cosmjs/stargate";
import { Logger } from "./logger";

const logger = new Logger("contract");

export type ContractNames = (typeof contractNames)[number];

Expand All @@ -12,7 +15,7 @@ export async function deployContract<T extends ContractNames>(
contractName: T,
initMsg: any
): Promise<{ type: T; address: string }> {
console.log(`Deploying ${contractName}`);
logger.debug(`deploying ${contractName}`);

const codeId = ctx.artifacts[contractName];
const res = await wasm.instantiate(
Expand All @@ -24,10 +27,14 @@ export async function deployContract<T extends ContractNames>(
);
const receipt = await waitTx(res.transactionHash, stargate);
if (receipt.code > 0) {
throw new Error(`Error deploying ${contractName}: ${receipt.hash}`);
logger.error(
"deploy tx failed.",
`contract=${contractName}, hash=${receipt.hash}`
);
throw new Error(JSON.stringify(receipt.events));
}

console.log(`Deployed ${contractName} at ${res.contractAddress}`);
logger.info(`deployed ${contractName} at ${res.contractAddress}`);
return { type: contractName, address: res.contractAddress };
}

Expand All @@ -37,7 +44,7 @@ export async function executeContract(
msg: any,
funds: { amount: string; denom: string }[] = []
): Promise<IndexedTx> {
console.log(`Executing ${deployment.type}'s ${Object.keys(msg)[0]}`);
logger.debug(`executing ${deployment.type}'s ${Object.keys(msg)[0]}`);

const res = await wasm.execute(
signer,
Expand All @@ -49,9 +56,14 @@ export async function executeContract(
);
const receipt = await waitTx(res.transactionHash, stargate);
if (receipt.code > 0) {
throw new Error(`Error executing ${deployment.type}: ${receipt.hash}`);
logger.error(
"execute tx failed.",
`contract=${deployment.type}, hash=${receipt.hash}`
);
throw new Error(JSON.stringify(receipt.events));
}

logger.info(`executed ${deployment.type}'s ${Object.keys(msg)[0]}`);
return receipt;
}

Expand All @@ -63,12 +75,13 @@ export async function executeMultiMsg(
.map((v) => v.contract.type.length)
.reduce((max, v) => Math.max(v, max), 0);

console.log("Executing multiple messages.");
for (const msg of msgs) {
console.log(
`- ${msg.contract.type.padEnd(long, " ")}: ${Object.keys(msg.msg)[0]}`
);
}
logger.debug(
`executing ${msgs.length} msgs.\n`,
...msgs.flatMap((v) => [
`- ${v.contract.type.padEnd(long, " ")}:`,
`${Object.keys(v.msg)[0]}\n`,
])
);

const res = await wasm.executeMultiple(
signer,
Expand All @@ -80,7 +93,19 @@ export async function executeMultiMsg(
);
const receipt = await waitTx(res.transactionHash, stargate);
if (receipt.code > 0) {
throw new Error(`Error executing multiple contracts: ${receipt.hash}`);
logger.error(
`execute multiple tx failed.`,
`msgs=${msgs.length}, hash=${receipt.hash}`
);
throw new Error(JSON.stringify(receipt.events));
}

logger.info(
`executed ${msgs.length} msgs.\n`,
...msgs.flatMap((v) => [
`- ${v.contract.type.padEnd(long, " ")}:`,
`${Object.keys(v.msg)[0]}\n`,
])
);
return receipt;
}
3 changes: 2 additions & 1 deletion script/shared/ioc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Container } from "inversify";

import { Context } from "./context";
import { Client } from "./config";
import { Client, Config } from "./config";

export const CONTAINER = new Container({
autoBindInjectable: true,
Expand All @@ -14,4 +14,5 @@ export const iocContainer = CONTAINER;
export class Dependencies {
ctx: Context;
client: Client;
network: Config["networks"][number];
}
16 changes: 16 additions & 0 deletions script/shared/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class Logger {
constructor(public name: string) {}

log(...args: any[]) {
console.log(`[${this.name}]`, ...args);
}

debug = (...args: any[]) =>
console.log("DEBUG]".grey, `[${this.name}]`, ...args);

info = (...args: any[]) =>
console.log(" INFO]".cyan, `[${this.name}]`, ...args);

error = (...args: any[]) =>
console.error("ERROR]".red, `[${this.name}]`, ...args);
}
8 changes: 8 additions & 0 deletions script/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Readable } from "stream";
import { createHash } from "crypto";
import { IndexedTx, StargateClient } from "@cosmjs/stargate";
import { fromBech32 } from "@cosmjs/encoding";
import { Client } from "./config";

export const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -76,3 +77,10 @@ export const waitTx = async (
}
return found;
};

export function orSigner<SignerType extends string | "<signer>" = "<signer>">(
client: Client,
v: SignerType
): string {
return v === "<signer>" ? client.signer : v;
}
4 changes: 3 additions & 1 deletion script/shared/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function getWasmFilesPath(
.map((file) => path.join(artifactPath, file));
} catch (err) {
console.error(
"Cannot find wasm folder. Are you sure you compiled the wasm projects?"
"[error]".red,
"cannot find wasm folder.",
"did you compiled the wasm projects?"
);
process.exit(1);
}
Expand Down

0 comments on commit 71f0172

Please sign in to comment.