@klaytn/hardhat-utils
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -24,2 +24,3 @@ import type ethers from "ethers"; | ||
export declare function resolveFuncArgs(taskArgs: FuncTaskCommonArgs): Promise<ResolvedFuncArgs>; | ||
export declare function adjustFuncArgsType(frag: ethers.utils.FunctionFragment, args: any[]): any[]; | ||
export interface NormalizeOpts { | ||
@@ -26,0 +27,0 @@ raw?: boolean; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.normalizeRpcResult = exports.normalizeCallResult = exports.resolveFuncArgs = exports.AmountArgType = exports.FromArgType = void 0; | ||
exports.normalizeRpcResult = exports.normalizeCallResult = exports.adjustFuncArgsType = exports.resolveFuncArgs = exports.AmountArgType = exports.FromArgType = void 0; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
@@ -39,16 +39,54 @@ require("../type-extensions"); | ||
let contract; | ||
if (!to || to == "") { | ||
if (!to || to == "") { // 'to' address unspecified; lookup in deployments | ||
const deployment = await hre.deployments.get(name); | ||
contract = new hre.ethers.Contract(deployment.address, deployment.abi); | ||
} | ||
else { | ||
else { // 'to' address specified | ||
contract = await hre.ethers.getContractAt(name, to); | ||
} | ||
const sender = await hre.ethers.getSigner(from); | ||
const frag = contract.interface.getFunction(func); | ||
const adjustedArgs = adjustFuncArgsType(frag, args); | ||
const unsignedTx = await contract | ||
.connect(sender) | ||
.populateTransaction[func](...args); | ||
.populateTransaction[func](...adjustedArgs); | ||
return { contract, sender, unsignedTx }; | ||
} | ||
exports.resolveFuncArgs = resolveFuncArgs; | ||
function adjustFuncArgsType(frag, args) { | ||
if (frag.inputs.length != args.length) { | ||
throw new Error(`Argument count mismatch for ${frag.format()}: want ${frag.inputs.length}, have ${args.length}`); | ||
} | ||
for (let i = 0; i < args.length; i++) { | ||
const ty = frag.inputs[i]; | ||
const arg = args[i]; | ||
// If an array is expected, try to split argument using comma. | ||
if (ty.baseType == "array") { | ||
args[i] = lodash_1.default.split(arg, ","); | ||
} | ||
// If a bool is expected, try to interpret the input as bool. | ||
if (ty.baseType == "bool") { | ||
args[i] = parseBool(arg); | ||
} | ||
} | ||
return args; | ||
} | ||
exports.adjustFuncArgsType = adjustFuncArgsType; | ||
function parseBool(arg) { | ||
if (lodash_1.default.isNumber(arg)) { | ||
return !!arg; | ||
} | ||
if (lodash_1.default.isString(arg)) { | ||
// Explicit string "false" and "true" | ||
if (arg.toLowerCase() == "false") { | ||
return false; | ||
} | ||
if (arg.toLowerCase() == "true") { | ||
return true; | ||
} | ||
// Otherwise it must be a number | ||
return !!Number(arg); | ||
} | ||
throw new Error(`Argument not boolean: '${arg}'`); | ||
} | ||
// Prettify function call result in array | ||
@@ -55,0 +93,0 @@ function normalizeCallResult(res, opts) { |
@@ -22,4 +22,5 @@ "use strict"; | ||
.addOptionalParam("index", "Bundler account index under the derivation path", "0") | ||
.addOptionalParam("privkey", "Bundler account private key without hex prefix. Has priority over mnemonic.") | ||
.setAction(async (taskArgs) => { | ||
const { host, port, dockerImageId, attachRemote, entrypoints, mnemonic, derivationPath, index } = taskArgs; | ||
const { host, port, dockerImageId, attachRemote, entrypoints, mnemonic, derivationPath, index, privkey } = taskArgs; | ||
const dir = path_1.default.resolve(__dirname, "../fixtures/bundler"); | ||
@@ -34,3 +35,3 @@ process_1.default.chdir(dir); | ||
}); | ||
const privateKey = accounts[0].privateKey.substring(2); | ||
const privateKey = privkey ?? accounts[0].privateKey.substring(2); | ||
const extraEnvs = { | ||
@@ -37,0 +38,0 @@ "DOCKER_IMAGE": dockerImageId, |
@@ -33,5 +33,8 @@ "use strict"; | ||
lodash_1.default.each(rc.logs, (log) => { | ||
const desc = contract.interface.parseLog(log); | ||
log.eventName = desc.signature; | ||
log.eventArgs = (0, helpers_1.normalizeCallResult)(desc.args); | ||
try { | ||
const desc = contract.interface.parseLog(log); | ||
log.eventName = desc.signature; | ||
log.eventArgs = (0, helpers_1.normalizeCallResult)(desc.args); | ||
} | ||
catch (e) { } | ||
}); | ||
@@ -45,3 +48,5 @@ if (json) { | ||
lodash_1.default.each(rc.logs, (log) => { | ||
console.log("emit", log.eventName, JSON.stringify(log.eventArgs, null, 2)); | ||
if (log.eventName) { | ||
console.log("emit", log.eventName, JSON.stringify(log.eventArgs, null, 2)); | ||
} | ||
}); | ||
@@ -48,0 +53,0 @@ } |
{ | ||
"name": "@klaytn/hardhat-utils", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "Hardhat utility tasks", | ||
@@ -5,0 +5,0 @@ "repository": "github:klaytn/hardhat-utils", |
@@ -47,6 +47,6 @@ import type ethers from "ethers"; | ||
let contract: ethers.Contract; | ||
if (!to || to == "") { | ||
if (!to || to == "") { // 'to' address unspecified; lookup in deployments | ||
const deployment = await hre.deployments.get(name); | ||
contract = new hre.ethers.Contract(deployment.address, deployment.abi); | ||
} else { | ||
} else { // 'to' address specified | ||
contract = await hre.ethers.getContractAt(name, to); | ||
@@ -57,5 +57,8 @@ } | ||
const frag = contract.interface.getFunction(func); | ||
const adjustedArgs = adjustFuncArgsType(frag, args); | ||
const unsignedTx = await contract | ||
.connect(sender) | ||
.populateTransaction[func](...args); | ||
.populateTransaction[func](...adjustedArgs); | ||
@@ -65,2 +68,40 @@ return { contract, sender, unsignedTx }; | ||
export function adjustFuncArgsType(frag: ethers.utils.FunctionFragment, args: any[]): any[] { | ||
if (frag.inputs.length != args.length) { | ||
throw new Error(`Argument count mismatch for ${frag.format()}: want ${frag.inputs.length}, have ${args.length}`); | ||
} | ||
for (let i = 0; i < args.length; i++) { | ||
const ty = frag.inputs[i]; | ||
const arg = args[i]; | ||
// If an array is expected, try to split argument using comma. | ||
if (ty.baseType == "array") { | ||
args[i] = _.split(arg, ","); | ||
} | ||
// If a bool is expected, try to interpret the input as bool. | ||
if (ty.baseType == "bool") { | ||
args[i] = parseBool(arg); | ||
} | ||
} | ||
return args; | ||
} | ||
function parseBool(arg: any): boolean { | ||
if (_.isNumber(arg)) { | ||
return !!arg; | ||
} | ||
if (_.isString(arg)) { | ||
// Explicit string "false" and "true" | ||
if (arg.toLowerCase() == "false") { return false; } | ||
if (arg.toLowerCase() == "true") { return true; } | ||
// Otherwise it must be a number | ||
return !!Number(arg); | ||
} | ||
throw new Error(`Argument not boolean: '${arg}'`); | ||
} | ||
export interface NormalizeOpts { | ||
@@ -67,0 +108,0 @@ raw?: boolean; |
@@ -18,4 +18,5 @@ import _ from "lodash"; | ||
.addOptionalParam("index", "Bundler account index under the derivation path", "0") | ||
.addOptionalParam("privkey", "Bundler account private key without hex prefix. Has priority over mnemonic.") | ||
.setAction(async (taskArgs) => { | ||
const { host, port, dockerImageId, attachRemote, entrypoints, mnemonic, derivationPath, index } = taskArgs; | ||
const { host, port, dockerImageId, attachRemote, entrypoints, mnemonic, derivationPath, index, privkey} = taskArgs; | ||
@@ -32,3 +33,3 @@ const dir = path.resolve(__dirname, "../fixtures/bundler"); | ||
}) | ||
const privateKey = accounts[0].privateKey.substring(2); | ||
const privateKey = privkey ?? accounts[0].privateKey.substring(2); | ||
@@ -35,0 +36,0 @@ const extraEnvs = { |
import { task } from "hardhat/config"; | ||
import { Log } from "@ethersproject/abstract-provider"; | ||
import _ from "lodash"; | ||
@@ -9,2 +10,7 @@ | ||
interface ParsedLog extends Log { | ||
eventName?: string; | ||
eventArgs?: string; | ||
} | ||
task(TASK_SEND, "Send a transaction to a contract") | ||
@@ -19,9 +25,9 @@ .addOptionalParam("from", "Caller address or index", 0, FromArgType) | ||
.setAction(async (taskArgs) => { | ||
const { unsigned, json, name, func } = taskArgs; | ||
const { contract, sender, unsignedTx } = await resolveFuncArgs(taskArgs); | ||
const { unsigned, json, name, func } = taskArgs; | ||
const { contract, sender, unsignedTx } = await resolveFuncArgs(taskArgs); | ||
if (unsigned) { | ||
console.log(normalizeRpcResult(unsignedTx)); | ||
return; | ||
} | ||
if (unsigned) { | ||
console.log(normalizeRpcResult(unsignedTx)); | ||
return; | ||
} | ||
@@ -34,6 +40,8 @@ const tx = await sender.sendTransaction(unsignedTx); | ||
const rc = await tx.wait(); | ||
_.each(rc.logs, (log) => { | ||
const desc = contract.interface.parseLog(log); | ||
(log as any).eventName = desc.signature; | ||
(log as any).eventArgs = normalizeCallResult(desc.args); | ||
_.each(rc.logs, (log: ParsedLog) => { | ||
try { | ||
const desc = contract.interface.parseLog(log); | ||
log.eventName = desc.signature; | ||
log.eventArgs = normalizeCallResult(desc.args); | ||
} catch (e) {} | ||
}); | ||
@@ -46,6 +54,8 @@ | ||
console.log(`${status} (block ${rc.blockNumber}, gas used: ${rc.gasUsed.toString()})`); | ||
_.each(rc.logs, (log) => { | ||
console.log("emit", (log as any).eventName, JSON.stringify((log as any).eventArgs, null, 2)); | ||
_.each(rc.logs, (log: ParsedLog) => { | ||
if (log.eventName) { | ||
console.log("emit", log.eventName, JSON.stringify(log.eventArgs, null, 2)); | ||
} | ||
}); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
204955
2751