Skip to content

Commit

Permalink
fix: parse array args correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
itsacoyote committed Aug 14, 2024
1 parent 18e855c commit dedf75f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
15 changes: 3 additions & 12 deletions src/commands/contract/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 { encodeData, formatArgs, 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";
Expand All @@ -15,7 +15,7 @@ import type { DistinctQuestion } from "inquirer";

type EncodeOptions = {
method?: string;
arguments?: string[];
arguments?: Array<string | string[]>;
abi?: string;
};

Expand Down Expand Up @@ -82,16 +82,6 @@ const askArguments = async (method: string, options: EncodeOptions) => {
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)
)}`;
}
},
});
});

Expand All @@ -113,6 +103,7 @@ export const handler = async (options: EncodeOptions, context: Command) => {
await askMethod(abi, options);
await askArguments(options.method!, options);

options.arguments = formatArgs(options.method!, options.arguments!);
const data = encodeData(options.method!, options.arguments!);
Logger.info("");
Logger.info(chalk.greenBright("✔ Encoded data: ") + data);
Expand Down
15 changes: 3 additions & 12 deletions src/commands/contract/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import {
decodeData,
encodeData,
encodeParam,
formatArgs,
getFragmentFromSignature,
getInputValues,
getInputsFromSignature,
Expand Down Expand Up @@ -48,7 +48,7 @@ const decodeSkipOption = new Option("--decode-skip", "Skip decoding response");
type CallOptions = DefaultTransactionOptions & {
contract?: string;
method?: string;
arguments?: string[];
arguments?: Array<string[] | string>;
data?: string;
outputTypes: string[];
from?: string;
Expand Down Expand Up @@ -123,16 +123,6 @@ const askArguments = async (method: string, options: CallOptions) => {
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)
)}`;
}
},
});
});

Expand Down Expand Up @@ -230,6 +220,7 @@ export const handler = async (options: CallOptions, context: Command) => {
if (!options.data) {
await askArguments(options.method!, options);
}
options.arguments = formatArgs(options.method!, options.arguments!);

const transaction: TransactionRequest = {
to: contractInfo.address,
Expand Down
21 changes: 21 additions & 0 deletions src/commands/contract/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,24 @@ export const getMethodsFromAbi = (abi: ABI, type: "read" | "write"): ethers.util
return contractInterface.fragments as ethers.utils.FunctionFragment[];
}
};

/**
* Format method args based on the method signature into a valid
* encodable format.
*
* @example "42,77" => [42,77]
*/
export const formatArgs = (method: string, args: Array<string[] | string>) => {
const inputs = getInputsFromSignature(method);

return args.map((arg, index) => {
const input = inputs[index];
if (input.baseType === "array") {
return (arg as string)
.replace(/\[|\]/g, "")
.split(",")
.map((element) => element.trim());
}
return arg;
});
};
16 changes: 3 additions & 13 deletions src/commands/contract/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
methodOption,
showTransactionInfoOption,
} from "./common/options.js";
import { encodeData, encodeParam, getFragmentFromSignature, getInputsFromSignature } from "./utils/formatters.js";
import { encodeData, formatArgs, getFragmentFromSignature, getInputsFromSignature } from "./utils/formatters.js";
import {
checkIfMethodExists,
getContractInfoWithLoader,
Expand All @@ -39,7 +39,7 @@ const valueOption = new Option("--value <Ether amount>", "Ether value to send wi
type WriteOptions = DefaultTransactionOptions & {
contract?: string;
method?: string;
arguments?: string[];
arguments?: Array<string[] | string>;
value?: string;
data?: string;
abi?: string;
Expand Down Expand Up @@ -109,16 +109,6 @@ const askArguments = async (method: string, options: WriteOptions) => {
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)
)}`;
}
},
});
});

Expand Down Expand Up @@ -183,6 +173,7 @@ export const handler = async (options: WriteOptions, context: Command) => {
if (!options.data) {
await askArguments(options.method!, options);
}
options.arguments = formatArgs(options.method!, options.arguments!);

const { privateKey }: { privateKey: string } = await inquirer.prompt(
[
Expand All @@ -202,7 +193,6 @@ export const handler = async (options: WriteOptions, context: Command) => {
options
);
const senderWallet = getL2Wallet(options.privateKey || privateKey, provider);

const transaction: TransactionRequest = {
from: senderWallet.address,
to: contractInfo.address,
Expand Down

0 comments on commit dedf75f

Please sign in to comment.