-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add contract encode command (#118)
- Loading branch information
1 parent
242d0a2
commit fd019ae
Showing
7 changed files
with
227 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import chalk from "chalk"; | ||
import { ethers } from "ethers"; | ||
import inquirer from "inquirer"; | ||
|
||
import Program from "./command.js"; | ||
import { abiOption, argumentsOption, methodOption } from "./common/options.js"; | ||
import { encodeData, encodeParam, getFragmentFromSignature, getInputsFromSignature } from "./utils/formatters.js"; | ||
import { readAbiFromFile, askAbiMethod, formatMethodString } from "./utils/helpers.js"; | ||
import { logFullCommandFromOptions, optionNameToParam } from "../../utils/helpers.js"; | ||
import Logger from "../../utils/logger.js"; | ||
|
||
import type { ABI } from "./utils/helpers.js"; | ||
import type { Command } from "commander"; | ||
import type { DistinctQuestion } from "inquirer"; | ||
|
||
type EncodeOptions = { | ||
method?: string; | ||
arguments?: string[]; | ||
abi?: string; | ||
}; | ||
|
||
// ---------------- | ||
// prompts | ||
// ---------------- | ||
|
||
const askMethod = async (contractAbi: ABI | undefined, options: EncodeOptions) => { | ||
if (options.method) { | ||
return; | ||
} | ||
|
||
const methodByAbi = await askAbiMethod({ abi: contractAbi }); | ||
if (methodByAbi !== "manual") { | ||
const fullMethodName = methodByAbi.format(ethers.utils.FormatTypes.full); | ||
options.method = formatMethodString(fullMethodName); | ||
return; | ||
} | ||
|
||
const answers: Pick<EncodeOptions, "method"> = await inquirer.prompt( | ||
[ | ||
{ | ||
message: "Enter method to encode", | ||
name: optionNameToParam(methodOption.long!), | ||
type: "input", | ||
validate: (input: string) => { | ||
try { | ||
getFragmentFromSignature(input); // throws if invalid | ||
return true; | ||
} catch { | ||
return `Invalid method signature. Example: ${chalk.blueBright("balanceOf(address)")}`; | ||
} | ||
}, | ||
}, | ||
], | ||
options | ||
); | ||
|
||
options.method = answers.method; | ||
}; | ||
|
||
const askArguments = async (method: string, options: EncodeOptions) => { | ||
if (options.arguments) { | ||
return; | ||
} | ||
const inputs = getInputsFromSignature(method); | ||
if (!inputs.length) { | ||
options.arguments = []; | ||
return; | ||
} | ||
Logger.info(chalk.green("?") + chalk.bold(" Provide method arguments:")); | ||
const prompts: DistinctQuestion[] = []; | ||
|
||
inputs.forEach((input, index) => { | ||
let name = chalk.gray(`[${index + 1}/${inputs.length}]`); | ||
if (input.name) { | ||
name += ` ${input.name}`; | ||
name += chalk.gray(` (${input.type})`); | ||
} else { | ||
name += ` ${input.type}`; | ||
} | ||
|
||
prompts.push({ | ||
message: name, | ||
name: index.toString(), | ||
type: "input", | ||
validate: (value: string) => { | ||
try { | ||
encodeParam(input, value); // throws if invalid | ||
return true; | ||
} catch (error) { | ||
return `${chalk.redBright( | ||
"Failed to encode provided argument: " + (error instanceof Error ? error.message : error) | ||
)}`; | ||
} | ||
}, | ||
}); | ||
}); | ||
|
||
const answers = await inquirer.prompt(prompts); | ||
options.arguments = Object.values(answers); | ||
}; | ||
|
||
// ---------------- | ||
// request handler | ||
// ---------------- | ||
|
||
export const handler = async (options: EncodeOptions, context: Command) => { | ||
try { | ||
let abi: ABI | undefined; | ||
if (options.abi) { | ||
abi = readAbiFromFile(options.abi); | ||
Logger.info(chalk.gray("Using provided ABI file")); | ||
} | ||
await askMethod(abi, options); | ||
await askArguments(options.method!, options); | ||
|
||
const data = encodeData(options.method!, options.arguments!); | ||
Logger.info(""); | ||
Logger.info(chalk.greenBright("✔ Encoded data: ") + data); | ||
logFullCommandFromOptions(options, context, { emptyLine: true }); | ||
} catch (error) { | ||
Logger.error("There was an error while performing encoding"); | ||
Logger.error(error); | ||
} | ||
}; | ||
|
||
Program.command("encode") | ||
.addOption(methodOption) | ||
.addOption(argumentsOption) | ||
.addOption(abiOption) | ||
.description("Get calldata (e.g. 0x1234) from contract method signature and arguments") | ||
.action(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import "./read.js"; | ||
import "./write.js"; | ||
import "./encode.js"; | ||
|
||
import "./command.js"; // registers all the commands above |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters