Skip to content

Commit 5ddc665

Browse files
committed
send: try-catch parseLog
1 parent c2b24be commit 5ddc665

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

src/tasks/send.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { task } from "hardhat/config";
2+
import { Log } from "@ethersproject/abstract-provider";
23
import _ from "lodash";
34

45
import { FromArgType, resolveFuncArgs, normalizeCallResult, normalizeRpcResult } from "../helpers";
56
import "../type-extensions";
67

78
export const TASK_SEND = "send";
89

10+
interface ParsedLog extends Log {
11+
eventName?: string;
12+
eventArgs?: string;
13+
}
14+
915
task(TASK_SEND, "Send a transaction to a contract")
1016
.addOptionalParam("from", "Caller address or index", 0, FromArgType)
1117
.addOptionalParam("to", "Target address. Loaded from deployments if empty.", "")
@@ -15,33 +21,37 @@ task(TASK_SEND, "Send a transaction to a contract")
1521
.addPositionalParam("func", "Function name or signature (example: 'number()', 'balanceOf(address)')")
1622
.addVariadicPositionalParam("args", "call arguments", [])
1723
.setAction(async (taskArgs) => {
18-
const { unsigned, json, name, func } = taskArgs;
19-
const { contract, sender, unsignedTx } = await resolveFuncArgs(taskArgs);
24+
const { unsigned, json, name, func } = taskArgs;
25+
const { contract, sender, unsignedTx } = await resolveFuncArgs(taskArgs);
2026

21-
if (unsigned) {
22-
console.log(normalizeRpcResult(unsignedTx));
23-
return;
24-
}
27+
if (unsigned) {
28+
console.log(normalizeRpcResult(unsignedTx));
29+
return;
30+
}
2531

2632
const tx = await sender.sendTransaction(unsignedTx);
2733
if (!json) {
2834
process.stdout.write(`sent ${name}#${func} (tx: ${tx.hash})...`);
2935
}
3036

3137
const rc = await tx.wait();
32-
_.each(rc.logs, (log) => {
33-
const desc = contract.interface.parseLog(log);
34-
(log as any).eventName = desc.signature;
35-
(log as any).eventArgs = normalizeCallResult(desc.args);
38+
_.each(rc.logs, (log: ParsedLog) => {
39+
try {
40+
const desc = contract.interface.parseLog(log);
41+
log.eventName = desc.signature;
42+
log.eventArgs = normalizeCallResult(desc.args);
43+
} catch (e) {}
3644
});
3745

3846
if (json) {
3947
console.log(JSON.stringify(normalizeRpcResult(rc), null, 2));
4048
} else {
4149
const status = rc.status == 1 ? "ok" : `failed (status=${rc.status})`;
4250
console.log(`${status} (block ${rc.blockNumber}, gas used: ${rc.gasUsed.toString()})`);
43-
_.each(rc.logs, (log) => {
44-
console.log("emit", (log as any).eventName, JSON.stringify((log as any).eventArgs, null, 2));
51+
_.each(rc.logs, (log: ParsedLog) => {
52+
if (log.eventName) {
53+
console.log("emit", log.eventName, JSON.stringify(log.eventArgs, null, 2));
54+
}
4555
});
4656
}
4757
});

0 commit comments

Comments
 (0)