Skip to content

Commit

Permalink
Added more information to some invalid argument errors (#1130).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 5, 2021
1 parent bee76a4 commit f3c6d81
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
7 changes: 6 additions & 1 deletion packages/contracts/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ async function resolveAddresses(resolver: Signer | Provider, value: any, paramTy
}

if (paramType.baseType === "array") {
if (!Array.isArray(value)) { return Promise.reject(new Error("invalid value for array")); }
if (!Array.isArray(value)) {
return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, {
argument: "value",
value
}));
}
return await Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren)));
}

Expand Down
13 changes: 7 additions & 6 deletions packages/solidity/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"author": "Richard Moore <me@ricmoo.com>",
"dependencies": {
"@ethersproject/bignumber": "^5.4.0",
"@ethersproject/bytes": "^5.4.0",
"@ethersproject/keccak256": "^5.4.0",
"@ethersproject/sha2": "^5.4.0",
"@ethersproject/strings": "^5.4.0"
"@ethersproject/bignumber": "^5.5.0",
"@ethersproject/bytes": "^5.5.0",
"@ethersproject/keccak256": "^5.5.0",
"@ethersproject/logger": "^5.5.0",
"@ethersproject/sha2": "^5.5.0",
"@ethersproject/strings": "^5.5.0"
},
"description": "Solidity coder for non-standard (tight) packing.",
"ethereum": "donations.ethers.eth",
Expand Down Expand Up @@ -41,5 +42,5 @@
"sideEffects": false,
"tarballHash": "0x44154ee5d7130c1d61b72b000f1c5acb7bfa7b0870dde55ea8dec7b6045a465d",
"types": "./lib/index.d.ts",
"version": "5.4.0"
"version": "5.5.0"
}
23 changes: 17 additions & 6 deletions packages/solidity/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");

const Zeros = "0000000000000000000000000000000000000000000000000000000000000000";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);


function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
switch(type) {
case "address":
Expand All @@ -33,7 +38,7 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
let size = parseInt(match[2] || "256")

if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) {
throw new Error("invalid number type - " + type);
logger.throwArgumentError("invalid number type", "type", type)
}

if (isArray) { size = 256; }
Expand All @@ -48,9 +53,11 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
const size = parseInt(match[1]);

if (String(size) !== match[1] || size === 0 || size > 32) {
throw new Error("invalid bytes type - " + type);
logger.throwArgumentError("invalid bytes type", "type", type)
}
if (arrayify(value).byteLength !== size) {
logger.throwArgumentError(`invalid value for ${ type }`, "value", value)
}
if (arrayify(value).byteLength !== size) { throw new Error("invalid value for " + type); }
if (isArray) { return arrayify((value + Zeros).substring(0, 66)); }
return value;
}
Expand All @@ -59,21 +66,25 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
if (match && Array.isArray(value)) {
const baseType = match[1];
const count = parseInt(match[2] || String(value.length));
if (count != value.length) { throw new Error("invalid value for " + type); }
if (count != value.length) {
logger.throwArgumentError(`invalid array length for ${ type }`, "value", value)
}
const result: Array<Uint8Array> = [];
value.forEach(function(value) {
result.push(_pack(baseType, value, true));
});
return concat(result);
}

throw new Error("invalid type - " + type);
return logger.throwArgumentError("invalid type", "type", type)
}

// @TODO: Array Enum

export function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
if (types.length != values.length) { throw new Error("type/value count mismatch"); }
if (types.length != values.length) {
logger.throwArgumentError("wrong number of values; expected ${ types.length }", "values", values)
}
const tight: Array<Uint8Array> = [];
types.forEach(function(type, index) {
tight.push(_pack(type, values[index]));
Expand Down

0 comments on commit f3c6d81

Please sign in to comment.