-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2766 from NomicFoundation/francovictorio/hh-688/c…
…hainid-zero-view-functions Fix `block.chainid` returning 0 in view functions
- Loading branch information
Showing
6 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"hardhat": patch | ||
--- | ||
|
||
Fixed `block.chainid` returning 0 in view functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/hardhat-core/test/internal/hardhat-network/provider/modules/eth/chainId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { assert } from "chai"; | ||
import { BN, toBuffer } from "ethereumjs-util"; | ||
|
||
import { workaroundWindowsCiFailures } from "../../../../../utils/workaround-windows-ci-failures"; | ||
import { EXAMPLE_CHAIN_ID_CONTRACT } from "../../../helpers/contracts"; | ||
import { setCWD } from "../../../helpers/cwd"; | ||
import { | ||
DEFAULT_ACCOUNTS_ADDRESSES, | ||
DEFAULT_CHAIN_ID, | ||
PROVIDERS, | ||
} from "../../../helpers/providers"; | ||
import { deployContract } from "../../../helpers/transactions"; | ||
|
||
describe("Eth module", function () { | ||
PROVIDERS.forEach(({ name, useProvider, isFork }) => { | ||
if (isFork) { | ||
this.timeout(50000); | ||
} | ||
|
||
workaroundWindowsCiFailures.call(this, { isFork }); | ||
|
||
describe(`${name} provider`, function () { | ||
setCWD(); | ||
useProvider(); | ||
|
||
describe("chain id", function () { | ||
it("should read the right chain id in the constructor", async function () { | ||
const contractAddress = await deployContract( | ||
this.provider, | ||
`0x${EXAMPLE_CHAIN_ID_CONTRACT.bytecode.object}` | ||
); | ||
|
||
const chainIdHex = await this.provider.send("eth_call", [ | ||
{ | ||
to: contractAddress, | ||
from: DEFAULT_ACCOUNTS_ADDRESSES[0], | ||
data: `${EXAMPLE_CHAIN_ID_CONTRACT.selectors.chainId}`, | ||
}, | ||
]); | ||
|
||
const chainId = new BN(toBuffer(chainIdHex)).toNumber(); | ||
|
||
assert.equal(chainId, DEFAULT_CHAIN_ID); | ||
}); | ||
|
||
it("should read the right chain id in a write function", async function () { | ||
const contractAddress = await deployContract( | ||
this.provider, | ||
`0x${EXAMPLE_CHAIN_ID_CONTRACT.bytecode.object}` | ||
); | ||
|
||
await this.provider.send("eth_sendTransaction", [ | ||
{ | ||
to: contractAddress, | ||
from: DEFAULT_ACCOUNTS_ADDRESSES[0], | ||
data: `${EXAMPLE_CHAIN_ID_CONTRACT.selectors.setChainId}`, | ||
}, | ||
]); | ||
|
||
const chainIdHex = await this.provider.send("eth_call", [ | ||
{ | ||
to: contractAddress, | ||
from: DEFAULT_ACCOUNTS_ADDRESSES[0], | ||
data: `${EXAMPLE_CHAIN_ID_CONTRACT.selectors.chainId}`, | ||
}, | ||
]); | ||
|
||
const chainId = new BN(toBuffer(chainIdHex)).toNumber(); | ||
|
||
assert.equal(chainId, DEFAULT_CHAIN_ID); | ||
}); | ||
|
||
it("should read the right chain id in a view function", async function () { | ||
const contractAddress = await deployContract( | ||
this.provider, | ||
`0x${EXAMPLE_CHAIN_ID_CONTRACT.bytecode.object}` | ||
); | ||
|
||
const chainIdHex = await this.provider.send("eth_call", [ | ||
{ | ||
to: contractAddress, | ||
from: DEFAULT_ACCOUNTS_ADDRESSES[0], | ||
data: `${EXAMPLE_CHAIN_ID_CONTRACT.selectors.getChainId}`, | ||
}, | ||
]); | ||
|
||
const chainId = new BN(toBuffer(chainIdHex)).toNumber(); | ||
|
||
assert.equal(chainId, DEFAULT_CHAIN_ID); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters