Skip to content

Commit 9a7fb3a

Browse files
authored
Merge pull request #4494 from kaliubuntu0206/blocktag
Fixed blockTag bug on contract lookup
2 parents 87a83d9 + ebe5a5f commit 9a7fb3a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.changeset/eleven-pears-accept.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-ethers": patch
3+
---
4+
5+
Added support for passing bigints as block tags

packages/hardhat-ethers/src/internal/hardhat-ethers-provider.ts

+9
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,15 @@ export class HardhatEthersProvider implements ethers.Provider {
597597
return this.getBlockNumber().then((b) => toQuantity(b + blockTag));
598598
}
599599

600+
if (typeof blockTag === "bigint") {
601+
if (blockTag >= 0n) {
602+
return toQuantity(blockTag);
603+
}
604+
return this.getBlockNumber().then((b) =>
605+
toQuantity(b + Number(blockTag))
606+
);
607+
}
608+
600609
throw new HardhatEthersError(`Invalid blockTag: ${blockTag}`);
601610
}
602611

packages/hardhat-ethers/test/hardhat-ethers-provider.ts

+37
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,43 @@ describe("hardhat ethers provider", function () {
688688
);
689689
});
690690

691+
it("should accept a block number as a bigint", async function () {
692+
const signer = await this.env.ethers.provider.getSigner(0);
693+
const factory = new this.env.ethers.ContractFactory<[], ExampleContract>(
694+
EXAMPLE_CONTRACT.abi,
695+
EXAMPLE_CONTRACT.deploymentBytecode,
696+
signer
697+
);
698+
const contract = await factory.deploy();
699+
await contract.inc();
700+
701+
const blockNumber = await this.env.ethers.provider.getBlockNumber();
702+
703+
const resultAfter = await this.env.ethers.provider.call({
704+
from: signer.address,
705+
to: contract,
706+
data: "0x3fa4f245", // value()
707+
blockTag: "latest",
708+
});
709+
710+
assert.strictEqual(
711+
resultAfter,
712+
"0x0000000000000000000000000000000000000000000000000000000000000001"
713+
);
714+
715+
const resultBefore = await this.env.ethers.provider.call({
716+
from: signer.address,
717+
to: contract,
718+
data: "0x3fa4f245", // value()
719+
blockTag: BigInt(blockNumber - 1),
720+
});
721+
722+
assert.strictEqual(
723+
resultBefore,
724+
"0x0000000000000000000000000000000000000000000000000000000000000000"
725+
);
726+
});
727+
691728
it("should accept a block hash", async function () {
692729
const signer = await this.env.ethers.provider.getSigner(0);
693730
const factory = new this.env.ethers.ContractFactory<[], ExampleContract>(

0 commit comments

Comments
 (0)