Skip to content

Commit

Permalink
Merge pull request #44 from keep-network/packages-update
Browse files Browse the repository at this point in the history
Packages update

### Why

Update of core packages like `ethers` and `hardhat` to make `hardhat-helpers`
package compatible with projects using new versions of these packages. 

### What

#### Packages updates:
- adding `.nvmrc` with specified node version `v20.10.0` as node older than LTS
  won't support new packages
- updating `typescript` to version `>5` (and related packages) to be able to
  compile these new packages
- updating `eslint` and related packages to correctly lint codebase
- updating `hardhat` packages to newest versions:
  - replacing deprecated `hardhat-etherscan` with `hardhat-verify`
- updating `ethers` package to version `>6`

#### Codebase updates:
- no more `BigNumbers` - as ethers v6 is not longer using `BigNumbers` they are
  replaced with native `bigints` calculations
- wherever possible types and utils are imported directly from `ethers`,
  sometimes it means that the function or type is slightly changed, moved out of
  `utils` or renamed
- some types like `SignerWithAddress` were removed completely and they were
  replaced with similar types
- some functions started to return default values like `null` or `undefined`
  which broke our code - optional chaining with default values has been used to
  fix these places

#### Remaining problems: 
- there are some problems with incompatible types
- some places has been changed more than others - see `upgrades` file - and we
  should make sure their test coverage was enough to be sure they are working
  correctly
  • Loading branch information
pdyraga authored Jan 17, 2024
2 parents 7a73c66 + 91cd53d commit f41f36f
Show file tree
Hide file tree
Showing 17 changed files with 2,037 additions and 1,315 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: "14.x"
cache: "yarn"

- name: Install dependencies
Expand All @@ -29,9 +28,8 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: "14.x"
cache: "yarn"

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ jobs:
with:
token: ${{ secrets.CI_GITHUB_TOKEN }}

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: "16"
cache: "yarn"
registry-url: "https://registry.npmjs.org"
scope: "@keep-network"
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/iron
38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,37 @@
],
"devDependencies": {
"@keep-network/prettier-config-keep": "github:keep-network/prettier-config-keep",
"@nomiclabs/hardhat-ethers": "^2.1.0",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@openzeppelin/hardhat-upgrades": "^1.22.0",
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomicfoundation/hardhat-network-helpers": "^1.0.10",
"@nomicfoundation/hardhat-verify": "^2.0.3",
"@openzeppelin/hardhat-upgrades": "^3.0.2",
"@types/chai": "^4.2.21",
"@types/chai-as-promised": "^7.1.4",
"@types/debug": "^4.1.12",
"@types/fs-extra": "^9.0.13",
"@types/mocha": "^8.2.3",
"@types/node": "^16.3.1",
"@typescript-eslint/eslint-plugin": "^4.28.3",
"@typescript-eslint/parser": "^4.28.3",
"@types/node": "^20.11.2",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"eslint": "^7.30.0",
"eslint": "^8.56.0",
"eslint-config-keep": "github:keep-network/eslint-config-keep",
"ethers": "^5.6.9",
"ethers": "^6.10.0",
"fs-extra": "^10.1.0",
"hardhat": "^2.10.0",
"hardhat-deploy": "^0.11.11",
"hardhat": "^2.19.4",
"hardhat-deploy": "^0.11.45",
"mocha": "^9.0.2",
"prettier": "^2.3.2",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@nomiclabs/hardhat-ethers": "^2.1.0",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@openzeppelin/hardhat-upgrades": "^1.22.0",
"ethers": "^5.6.9",
"hardhat": "^2.10.0",
"hardhat-deploy": "^0.11.11"
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomicfoundation/hardhat-verify": "^2.0.3",
"@openzeppelin/hardhat-upgrades": "^3.0.2",
"ethers": "^6.10.0",
"hardhat": "^2.19.4",
"hardhat-deploy": "^0.11.45"
}
}
19 changes: 7 additions & 12 deletions src/account.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "@nomiclabs/hardhat-ethers"
import "@nomicfoundation/hardhat-ethers"

import type { BigNumberish, BigNumber, Signer } from "ethers"
import { parseUnits, parseEther, Signer } from "ethers"
import type { HardhatRuntimeEnvironment } from "hardhat/types"
import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"

export type FundOptions = {
from: Signer
value?: BigNumberish
value?: bigint
unit?: string
}

Expand All @@ -33,16 +33,11 @@ async function impersonateAccount(
// In case the account represents a contract, keep in mind the contract must
// have a receive or fallback method to be funded successfully.

let fundingValue: BigNumber
let fundingValue: bigint
if (fundOptions.unit) {
fundingValue = hre.ethers.utils.parseUnits(
fundOptions.value!.toString(),
fundOptions.unit
)
fundingValue = parseUnits(fundOptions.value!.toString(), fundOptions.unit)
} else {
fundingValue = hre.ethers.utils.parseEther(
fundOptions.value?.toString() || "1"
)
fundingValue = parseEther(fundOptions.value?.toString() || "1")
}

await fundOptions.from.sendTransaction({
Expand Down
8 changes: 4 additions & 4 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ export interface HardhatAddressHelpers {
* @throws {HardhatPluginError} Throws an error if address is a zero address.
*/
export function validate(address: string): string {
if (!ethers.utils.isAddress(address)) {
if (!ethers.isAddress(address)) {
throw new HardhatPluginError(
"@keep-network/hardhat-helpers",
`address ${address} is not a valid address`
)
}

if (ethers.utils.getAddress(address) === ethers.constants.AddressZero) {
if (ethers.getAddress(address) === ethers.ZeroAddress) {
throw new HardhatPluginError(
"@keep-network/hardhat-helpers",
`address is a zero address`
)
}

return ethers.utils.getAddress(address)
return ethers.getAddress(address)
}

/**
Expand Down Expand Up @@ -65,5 +65,5 @@ export function equal(address1: string, address2: string): boolean {
validate(address1)
validate(address2)

return ethers.utils.getAddress(address1) === ethers.utils.getAddress(address2)
return ethers.getAddress(address1) === ethers.getAddress(address2)
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ extendEnvironment((hre) => {
return ownable(hre)
}),
time: lazyObject(() => {
return time(hre)
return time()
}),
signers: lazyObject(() => {
return signers(hre)
}),
snapshot: lazyObject(() => {
return snapshot(hre)
return snapshot()
}),
upgrades: lazyObject(() => {
return upgrades(hre)
Expand Down
30 changes: 15 additions & 15 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { formatFixed } from "@ethersproject/bignumber"
import { BigNumber } from "ethers"
import { BigNumberish } from "ethers"

export interface HardhatNumberHelpers {
to1e18(n: any): BigNumber
to1ePrecision(n: any, precision: number): BigNumber
from1e18(n: any): string
from1ePrecision(n: any, precision: number): string
to1e18(n: BigNumberish): bigint
to1ePrecision(n: BigNumberish, precision: number): bigint
from1e18(n: BigNumberish): string
from1ePrecision(n: BigNumberish, precision: number): string
}

export function to1e18(n: any): BigNumber {
export function to1e18(n: BigNumberish): bigint {
return to1ePrecision(n, 18)
}

export function to1ePrecision(n: any, precision: number): BigNumber {
const decimalMultiplier = BigNumber.from(10).pow(precision)
return BigNumber.from(n).mul(decimalMultiplier)
export function to1ePrecision(n: BigNumberish, precision: number): bigint {
const decimalMultiplier = 10n ** BigInt(precision)
return BigInt(n) * decimalMultiplier
}

export function from1e18(n: any): string {
export function from1e18(n: BigNumberish): string {
return from1ePrecision(n, 18)
}

export function from1ePrecision(n: any, precision: number): string {
const value: BigNumber = BigNumber.from(n)
const decimalMultiplier: BigNumber = BigNumber.from(10).pow(precision)
export function from1ePrecision(n: BigNumberish, precision: number): string {
const value = BigInt(n)
const decimalMultiplier = 10n ** BigInt(precision)

return value.gte(decimalMultiplier) && value.mod(decimalMultiplier).isZero()
? value.div(decimalMultiplier).toString()
return value >= decimalMultiplier && value % decimalMultiplier === 0n
? (value / decimalMultiplier).toString()
: formatFixed(n, precision)
}
2 changes: 1 addition & 1 deletion src/signers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
import type { HardhatRuntimeEnvironment } from "hardhat/types"
import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"

type NamedSigners = {
[name: string]: SignerWithAddress
Expand Down
34 changes: 14 additions & 20 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { JsonRpcProvider } from "@ethersproject/providers"
import { HardhatRuntimeEnvironment } from "hardhat/types"
import {
SnapshotRestorer,
takeSnapshot,
} from "@nomicfoundation/hardhat-network-helpers"

const snapshotIdsStack: any[] = []
const snapshotStack: SnapshotRestorer[] = []

export interface HardhatSnapshotHelpers {
createSnapshot(): Promise<void>
Expand All @@ -10,31 +12,23 @@ export interface HardhatSnapshotHelpers {

/**
* Snapshot the state of the blockchain at the current.
* @param {JsonRpcProvider} provider Ethers provider
*/
export async function createSnapshot(provider: JsonRpcProvider): Promise<void> {
const snapshotId = await provider.send("evm_snapshot", [])
snapshotIdsStack.push(snapshotId)
export async function createSnapshot(): Promise<void> {
const snapshot = await takeSnapshot()
snapshotStack.push(snapshot)
}

/**
* Restores the chain to a latest snapshot.
* @param {JsonRpcProvider} provider Ethers provider
*/
export async function restoreSnapshot(
provider: JsonRpcProvider
): Promise<void> {
const snapshotId = snapshotIdsStack.pop()
await provider.send("evm_revert", [snapshotId])
export async function restoreSnapshot(): Promise<void> {
const snapshot = snapshotStack.pop()
await snapshot?.restore()
}

export default function (
hre: HardhatRuntimeEnvironment
): HardhatSnapshotHelpers {
const provider = hre.ethers.provider

export default function (): HardhatSnapshotHelpers {
return {
createSnapshot: () => createSnapshot(provider),
restoreSnapshot: () => restoreSnapshot(provider),
createSnapshot,
restoreSnapshot,
}
}
Loading

0 comments on commit f41f36f

Please sign in to comment.