-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend.ts
57 lines (49 loc) · 2.01 KB
/
send.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { task } from "hardhat/config";
import { Log } from "@ethersproject/abstract-provider";
import _ from "lodash";
import { FromArgType, resolveFuncArgs, normalizeCallResult, normalizeRpcResult } from "../helpers";
import "../type-extensions";
export const TASK_SEND = "send";
interface ParsedLog extends Log {
eventName?: string;
eventArgs?: string;
}
task(TASK_SEND, "Send a transaction to a contract")
.addOptionalParam("from", "Caller address or index", 0, FromArgType)
.addOptionalParam("to", "Target address. Loaded from deployments if empty.", "")
.addFlag("unsigned", "Print unsigned tx and exit")
.addFlag("json", "Print receipt in json")
.addPositionalParam("name", "Contract name (example: 'Counter', 'src/Lock.sol:Lock')")
.addPositionalParam("func", "Function name or signature (example: 'number()', 'balanceOf(address)')")
.addVariadicPositionalParam("args", "call arguments", [])
.setAction(async (taskArgs) => {
const { unsigned, json, name, func } = taskArgs;
const { contract, sender, unsignedTx } = await resolveFuncArgs(taskArgs);
if (unsigned) {
console.log(normalizeRpcResult(unsignedTx));
return;
}
const tx = await sender.sendTransaction(unsignedTx);
if (!json) {
process.stdout.write(`sent ${name}#${func} (tx: ${tx.hash})...`);
}
const rc = await tx.wait();
_.each(rc.logs, (log: ParsedLog) => {
try {
const desc = contract.interface.parseLog(log);
log.eventName = desc.signature;
log.eventArgs = normalizeCallResult(desc.args);
} catch (e) {}
});
if (json) {
console.log(JSON.stringify(normalizeRpcResult(rc), null, 2));
} else {
const status = rc.status == 1 ? "ok" : `failed (status=${rc.status})`;
console.log(`${status} (block ${rc.blockNumber}, gas used: ${rc.gasUsed.toString()})`);
_.each(rc.logs, (log: ParsedLog) => {
if (log.eventName) {
console.log("emit", log.eventName, JSON.stringify(log.eventArgs, null, 2));
}
});
}
});