Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed blockTag bug on contract lookup #4494

Merged
merged 5 commits into from Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-pears-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/hardhat-ethers": patch
---

Added support for passing bigints as block tags
Original file line number Diff line number Diff line change
@@ -597,6 +597,15 @@ export class HardhatEthersProvider implements ethers.Provider {
return this.getBlockNumber().then((b) => toQuantity(b + blockTag));
}

if (typeof blockTag === "bigint") {
if (blockTag >= 0n) {
return toQuantity(blockTag);
}
return this.getBlockNumber().then((b) =>
toQuantity(b + Number(blockTag))
);
}

throw new HardhatEthersError(`Invalid blockTag: ${blockTag}`);
}

37 changes: 37 additions & 0 deletions packages/hardhat-ethers/test/hardhat-ethers-provider.ts
Original file line number Diff line number Diff line change
@@ -688,6 +688,43 @@ describe("hardhat ethers provider", function () {
);
});

it("should accept a block number as a bigint", async function () {
const signer = await this.env.ethers.provider.getSigner(0);
const factory = new this.env.ethers.ContractFactory<[], ExampleContract>(
EXAMPLE_CONTRACT.abi,
EXAMPLE_CONTRACT.deploymentBytecode,
signer
);
const contract = await factory.deploy();
await contract.inc();

const blockNumber = await this.env.ethers.provider.getBlockNumber();

const resultAfter = await this.env.ethers.provider.call({
from: signer.address,
to: contract,
data: "0x3fa4f245", // value()
blockTag: "latest",
});

assert.strictEqual(
resultAfter,
"0x0000000000000000000000000000000000000000000000000000000000000001"
);

const resultBefore = await this.env.ethers.provider.call({
from: signer.address,
to: contract,
data: "0x3fa4f245", // value()
blockTag: BigInt(blockNumber - 1),
});

assert.strictEqual(
resultBefore,
"0x0000000000000000000000000000000000000000000000000000000000000000"
);
});

it("should accept a block hash", async function () {
const signer = await this.env.ethers.provider.getSigner(0);
const factory = new this.env.ethers.ContractFactory<[], ExampleContract>(