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

Improve test fixtures #64

Merged
merged 29 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
32b0f4d
Handling ERC4626 vaults in AcreRouter
dimpar Dec 4, 2023
3246552
Adding pnpm-lock.yaml
dimpar Dec 4, 2023
a3ba20f
Replacing OZ upgradable lib to a non-upgradable one
dimpar Dec 6, 2023
072f854
Drafting tests for AcreRouter
dimpar Dec 6, 2023
cd91d07
Add helpers to get deployed contract
nkuba Dec 5, 2023
4bf5442
Add helpers for hardhat signers
nkuba Dec 5, 2023
16676e9
Improve test fixture used in Acre test
nkuba Dec 5, 2023
07ff2a0
Merge remote-tracking branch 'origin/test-fixtures' into vaults-acre-…
dimpar Dec 7, 2023
9a9c513
Adding AcreRouter to deployed context
dimpar Dec 7, 2023
2ae92ec
Adding AcreRouter deployment scripts
dimpar Dec 7, 2023
fd4a287
Refactoring tests adjusting for helper methods
dimpar Dec 7, 2023
1c85545
Adding func-visibility rule
dimpar Dec 7, 2023
2bbf761
Referencing correct vault names in tests
dimpar Dec 7, 2023
cc54053
Merge remote-tracking branch 'origin/main' into test-fixtures
nkuba Dec 8, 2023
3a05a45
Replace realpath with another resolution
nkuba Dec 8, 2023
aa133de
Removing a rule that already exist in parent config
dimpar Dec 11, 2023
9e0ffd9
Renaming AcreRouter -> Dispatcher
dimpar Dec 11, 2023
b56c0ec
Changing a comment for vaults array
dimpar Dec 11, 2023
d7e8ac6
Renaming Vault -> VaultInfo struct
dimpar Dec 11, 2023
01086fc
Approve->authorize for vaults and functions around it
dimpar Dec 11, 2023
f477e45
Replacing require err messages with custom errors
dimpar Dec 11, 2023
d0bd5dd
Renaming events and returning the entire vaults array
dimpar Dec 11, 2023
cf0d47d
Adding func.dependencies for Dispatcher ownership transfer
dimpar Dec 11, 2023
b131f82
Refactoring Dispatcher tests:
dimpar Dec 11, 2023
e94c5fc
Simplifying tests
dimpar Dec 11, 2023
5cdfff9
Handling ERC4626 vaults in AcreRouter (#62)
nkuba Dec 13, 2023
e21a50b
Improve signers mapping
nkuba Dec 13, 2023
2831ef6
Don't return awaited promise in getUnnamedSigner
nkuba Dec 13, 2023
790c768
Merge remote-tracking branch 'origin/main' into test-fixtures
nkuba Dec 13, 2023
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: 4 additions & 1 deletion core/scripts/fetch_external_artifacts.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#! /bin/bash
set -eou pipefail

ROOT_DIR="$(realpath "$(dirname $0)/../")"
ROOT_DIR=$(
cd "$(dirname $0)/../"
pwd -P
)
TMP_DIR=${ROOT_DIR}/tmp/external-artifacts
EXTERNAL_ARTIFACTS_DIR=${ROOT_DIR}/external

Expand Down
24 changes: 12 additions & 12 deletions core/test/Acre.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {
SnapshotRestorer,
loadFixture,
takeSnapshot,
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers"
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"
import { ethers } from "hardhat"
import { expect } from "chai"
import { ContractTransactionResponse, ZeroAddress } from "ethers"
import type { TestERC20, Acre } from "../typechain"

import type { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"
import type { SnapshotRestorer } from "@nomicfoundation/hardhat-toolbox/network-helpers"
import { deployment } from "./helpers/context"
import { getUnnamedSigner } from "./helpers/signer"

import { to1e18 } from "./utils"

async function acreFixture() {
const [staker1, staker2] = await ethers.getSigners()
import type { Acre, TestERC20 } from "../typechain"

const TestERC20 = await ethers.getContractFactory("TestERC20")
const tbtc = await TestERC20.deploy()
async function fixture() {
const { tbtc, acre } = await deployment()

const Acre = await ethers.getContractFactory("Acre")
const acre = await Acre.deploy(await tbtc.getAddress())
const [staker1, staker2] = await getUnnamedSigner()

const amountToMint = to1e18(100000)
tbtc.mint(staker1, amountToMint)
Expand All @@ -33,7 +33,7 @@ describe("Acre", () => {
let staker2: HardhatEthersSigner

before(async () => {
;({ acre, tbtc, staker1, staker2 } = await loadFixture(acreFixture))
;({ acre, tbtc, staker1, staker2 } = await loadFixture(fixture))
})

describe("stake", () => {
Expand Down
15 changes: 15 additions & 0 deletions core/test/helpers/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { deployments } from "hardhat"

import { getDeployedContract } from "./contract"

import type { Acre, TestERC20 } from "../../typechain"

// eslint-disable-next-line import/prefer-default-export
export async function deployment() {
await deployments.fixture()

const tbtc: TestERC20 = await getDeployedContract("TBTC")
const acre: Acre = await getDeployedContract("Acre")

return { tbtc, acre }
}
22 changes: 22 additions & 0 deletions core/test/helpers/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ethers } from "ethers"
import { deployments } from "hardhat"

import type { BaseContract } from "ethers"
import { getUnnamedSigner } from "./signer"

/**
* Get instance of a contract from Hardhat Deployments.
* @param deploymentName Name of the contract deployment.
* @returns Deployed Ethers contract instance.
*/
// eslint-disable-next-line import/prefer-default-export
export async function getDeployedContract<T extends BaseContract>(
deploymentName: string,
): Promise<T> {
const { address, abi } = await deployments.get(deploymentName)

// Use default unnamed signer from index 0 to initialize the contract runner.
const [defaultSigner] = await getUnnamedSigner()

return new ethers.BaseContract(address, abi, defaultSigner) as T
}
3 changes: 3 additions & 0 deletions core/test/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./context"
export * from "./contract"
export * from "./signer"
37 changes: 37 additions & 0 deletions core/test/helpers/signer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ethers, getNamedAccounts, getUnnamedAccounts } from "hardhat"

import type { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"

/**
* Get named Hardhat Ethers Signers.
* @returns Map of named Hardhat Ethers Signers.
*/
export async function getNamedSigner(): Promise<{
[name: string]: HardhatEthersSigner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define type eg. type NamedSigners = { [name: string]: HardhatEthersSigner } and we can use it in the line below:

  const namedSigners: NamedSigners = {}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a straightforward mapping holding broadly used HardhatEthersSigner properties.

When you use the function, you see what to expect:
Screen Shot 2023-12-13 at 08 54 08

With NamedSigners type, you don't see right away what the function will return:
Screen Shot 2023-12-13 at 08 58 04

IMHO, it will be more convenient for development to define this helper function using the current approach, HardhatEthersSigner, rather than a custom type.

}> {
const namedSigners: { [name: string]: HardhatEthersSigner } = {}

await Promise.all(
Object.entries(await getNamedAccounts()).map(async ([name, address]) => {
namedSigners[name] = await ethers.getSigner(address)
}),
)

return namedSigners
}

/**
* Get unnamed Hardhat Ethers Signers.
* @returns Array of unnamed Hardhat Ethers Signers.
*/
export async function getUnnamedSigner(): Promise<HardhatEthersSigner[]> {
const unnamedSigners: HardhatEthersSigner[] = []

await Promise.all(
(await getUnnamedAccounts()).map(async (address) => {
unnamedSigners.push(await ethers.getSigner(address))
}),
)

return unnamedSigners
nkuba marked this conversation as resolved.
Show resolved Hide resolved
}