-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #3572 from NomicFoundation/add-reset-helper
Add a new 'reset' network helper
- Loading branch information
Showing
10 changed files
with
166 additions
and
21 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 @@ | ||
--- | ||
"@nomicfoundation/hardhat-network-helpers": patch | ||
--- | ||
|
||
Added a new 'reset' network helper |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { NumberLike } from "../types"; | ||
import { getHardhatProvider, toNumber } from "../utils"; | ||
|
||
export async function reset( | ||
url?: string, | ||
blockNumber?: NumberLike | ||
): Promise<void> { | ||
const provider = await getHardhatProvider(); | ||
|
||
if (url === undefined) { | ||
await provider.request({ | ||
method: "hardhat_reset", | ||
params: [], | ||
}); | ||
} else if (blockNumber === undefined) { | ||
await provider.request({ | ||
method: "hardhat_reset", | ||
params: [ | ||
{ | ||
forking: { | ||
jsonRpcUrl: url, | ||
}, | ||
}, | ||
], | ||
}); | ||
} else { | ||
await provider.request({ | ||
method: "hardhat_reset", | ||
params: [ | ||
{ | ||
forking: { | ||
jsonRpcUrl: url, | ||
blockNumber: toNumber(blockNumber), | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/hardhat-network-helpers/test/fixture-projects/.gitignore
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 @@ | ||
cache/ |
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,58 @@ | ||
import { assert } from "chai"; | ||
|
||
import * as hh from "../../src"; | ||
import { INFURA_URL } from "../setup"; | ||
import { useEnvironment } from "../test-utils"; | ||
|
||
describe("resetWithoutFork", function () { | ||
useEnvironment("simple"); | ||
|
||
it("should reset the non-forked network", async function () { | ||
assert.equal(await hh.time.latestBlock(), 0); | ||
await hh.mine(); | ||
assert.equal(await hh.time.latestBlock(), 1); | ||
await hh.reset(); | ||
assert.equal(await hh.time.latestBlock(), 0); | ||
}); | ||
|
||
it("should reset with a url", async function () { | ||
if (INFURA_URL === undefined) { | ||
this.skip(); | ||
} | ||
this.timeout(60000); | ||
|
||
// fork mainnet | ||
await hh.reset(INFURA_URL); | ||
|
||
const mainnetBlockNumber = await hh.time.latestBlock(); | ||
|
||
// fork goerli | ||
await hh.reset(INFURA_URL.replace("mainnet", "goerli")); | ||
|
||
const goerliBlockNumber = await hh.time.latestBlock(); | ||
|
||
const blockNumberDelta = Math.abs(mainnetBlockNumber - goerliBlockNumber); | ||
|
||
// check that there is a significative difference between the latest | ||
// block numbers of each chain | ||
assert.isAbove(blockNumberDelta, 100); | ||
}); | ||
|
||
it("should reset with a url and block number", async function () { | ||
if (INFURA_URL === undefined) { | ||
this.skip(); | ||
} | ||
this.timeout(60000); | ||
|
||
// fork mainnet | ||
await hh.reset(INFURA_URL); | ||
|
||
const mainnetBlockNumber = await hh.time.latestBlock(); | ||
assert.isAbove(mainnetBlockNumber, 1_000_000); | ||
|
||
// fork an older block number | ||
await hh.reset(INFURA_URL, mainnetBlockNumber - 1000); | ||
const olderMainnetBlockNumber = await hh.time.latestBlock(); | ||
assert.equal(olderMainnetBlockNumber, mainnetBlockNumber - 1000); | ||
}); | ||
}); |
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