Skip to content

Commit 89a907a

Browse files
authored
Merge pull request #5153 from ItsNickBarry/task-type-bigint
Add `BigInt` task argument type
2 parents e54f4f7 + 583bdc4 commit 89a907a

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

.changeset/clever-peaches-obey.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": minor
3+
---
4+
5+
Added BigInt task argument type

packages/hardhat-core/src/internal/core/params/argumentTypes.ts

+44
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,50 @@ export const int: CLIArgumentType<number> = {
121121
},
122122
};
123123

124+
/**
125+
* BigInt type.
126+
* Accepts either a decimal string integer or hexadecimal string integer.
127+
* @throws HH301
128+
*/
129+
export const bigint: CLIArgumentType<bigint> = {
130+
name: "bigint",
131+
parse: (argName, strValue) => {
132+
const decimalPattern = /^\d+(?:n)?$/;
133+
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
134+
135+
if (
136+
strValue.match(decimalPattern) === null &&
137+
strValue.match(hexPattern) === null
138+
) {
139+
throw new HardhatError(ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
140+
value: strValue,
141+
name: argName,
142+
type: bigint.name,
143+
});
144+
}
145+
146+
return BigInt(strValue.replace("n", ""));
147+
},
148+
/**
149+
* Check if argument value is of type "bigint".
150+
*
151+
* @param argName {string} argument's name - used for context in case of error.
152+
* @param value {any} argument's value to validate.
153+
*
154+
* @throws HH301 if value is not of type "bigint"
155+
*/
156+
validate: (argName: string, value: any): void => {
157+
const isBigInt = typeof value === "bigint";
158+
if (!isBigInt) {
159+
throw new HardhatError(ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
160+
value,
161+
name: argName,
162+
type: bigint.name,
163+
});
164+
}
165+
},
166+
};
167+
124168
/**
125169
* Float type.
126170
* Accepts either a decimal string number or hexadecimal string number.

packages/hardhat-core/test/internal/core/params/argumentTypes.ts

+76
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,82 @@ describe("argumentTypes", () => {
127127
});
128128
});
129129

130+
describe("bigint type", () => {
131+
it("should work with decimal values", () => {
132+
assert.equal(types.bigint.parse("arg", "0"), 0n);
133+
assert.equal(types.bigint.parse("arg", "1"), 1n);
134+
assert.equal(types.bigint.parse("arg", "1123"), 1123n);
135+
assert.equal(types.bigint.parse("arg", "05678"), 5678n);
136+
assert.equal(
137+
types.bigint.parse("arg", "9007199254740992"),
138+
BigInt("9007199254740992")
139+
);
140+
});
141+
142+
it("should work with hex values", () => {
143+
assert.equal(types.bigint.parse("arg", "0x0"), BigInt(0));
144+
assert.equal(types.bigint.parse("arg", "0x1"), BigInt(1));
145+
assert.equal(types.bigint.parse("arg", "0xA"), BigInt(0xa));
146+
assert.equal(types.bigint.parse("arg", "0xa"), BigInt(0xa));
147+
assert.equal(types.bigint.parse("arg", "0x0a"), BigInt(0x0a));
148+
assert.equal(
149+
types.bigint.parse("arg", "0x20000000000000"),
150+
BigInt("0x20000000000000")
151+
);
152+
});
153+
154+
it("should work with bigint values with 'n' suffix", () => {
155+
assert.equal(types.bigint.parse("arg", "0n"), BigInt(0));
156+
});
157+
158+
it("should fail with incorrect values", () => {
159+
expectHardhatError(
160+
() => types.bigint.parse("arg", ""),
161+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
162+
);
163+
expectHardhatError(
164+
() => types.bigint.parse("arg", "1."),
165+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
166+
);
167+
expectHardhatError(
168+
() => types.bigint.parse("arg", ".1"),
169+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
170+
);
171+
expectHardhatError(
172+
() => types.bigint.parse("arg", "0.1"),
173+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
174+
);
175+
expectHardhatError(
176+
() => types.bigint.parse("arg", "asdas"),
177+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
178+
);
179+
expectHardhatError(
180+
() => types.bigint.parse("arg", "a1"),
181+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
182+
);
183+
expectHardhatError(
184+
() => types.bigint.parse("arg", "1a"),
185+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
186+
);
187+
expectHardhatError(
188+
() => types.bigint.parse("arg", "1 1"),
189+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
190+
);
191+
expectHardhatError(
192+
() => types.bigint.parse("arg", "x123"),
193+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
194+
);
195+
expectHardhatError(
196+
() => types.bigint.parse("arg", "1e0"),
197+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
198+
);
199+
expectHardhatError(
200+
() => types.bigint.parse("arg", "0x0n"),
201+
ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE
202+
);
203+
});
204+
});
205+
130206
describe("float type", () => {
131207
it("should work with integer decimal values", () => {
132208
assert.equal(types.float.parse("arg", "0"), 0);

0 commit comments

Comments
 (0)