From 12a4d575383901e4baf861cbb7db55b4090d26f9 Mon Sep 17 00:00:00 2001 From: Evgenii Zaitsev <97302011+EvgeniiZaitsevCW@users.noreply.github.com> Date: Mon, 27 May 2024 20:49:14 +0700 Subject: [PATCH] test: smart contract tests improvements (#78) * test: use a simpler function to get the address of a contract * test: introduce special functions to work with block timestamps * test: import transaction-related classes from package "ethers" --- test-utils/eth.ts | 47 +++++++- test/base/CWToken.complex.test.ts | 94 ++++++++-------- test/base/ERC20Hookable.test.ts | 106 +++++++++--------- test/base/ERC20Mintable.test.ts | 45 ++++---- test/base/common/RescuableUpgradeable.test.ts | 10 +- 5 files changed, 170 insertions(+), 132 deletions(-) diff --git a/test-utils/eth.ts b/test-utils/eth.ts index c8c2c0ec..b53cbca7 100644 --- a/test-utils/eth.ts +++ b/test-utils/eth.ts @@ -1,12 +1,51 @@ -import { BaseContract, Contract } from "ethers"; +import { ethers, network } from "hardhat"; +import { BaseContract, BlockTag, Contract, TransactionReceipt, TransactionResponse } from "ethers"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; -import { TransactionReceipt, TransactionResponse } from "@ethersproject/abstract-provider"; +import { time } from "@nomicfoundation/hardhat-network-helpers"; export async function proveTx(txResponsePromise: Promise): Promise { - const txReceipt = await txResponsePromise; - return txReceipt.wait(); + const txResponse = await txResponsePromise; + const txReceipt = await txResponse.wait(); + if (!txReceipt) { + throw new Error("The transaction receipt is empty"); + } + return txReceipt; } export function connect(contract: BaseContract, signer: HardhatEthersSigner): Contract { return contract.connect(signer) as Contract; } + +export function getAddress(contract: Contract): string { + const address = contract.target; + if (typeof address !== "string" || address.length != 42 || !address.startsWith("0x")) { + throw new Error("The '.target' field of the contract is not an address string"); + } + return address; +} + +export async function getTxTimestamp(tx: Promise): Promise { + const receipt = await proveTx(tx); + const block = await ethers.provider.getBlock(receipt.blockNumber); + return Number(block?.timestamp ?? 0); +} + +export async function getBlockTimestamp(blockTag: BlockTag): Promise { + const block = await ethers.provider.getBlock(blockTag); + return block?.timestamp ?? 0; +} + +export async function getLatestBlockTimestamp(): Promise { + return getBlockTimestamp("latest"); +} + +export async function increaseBlockTimestampTo(target: number) { + if (network.name === "hardhat") { + await time.increaseTo(target); + } else if (network.name === "stratus") { + await ethers.provider.send("evm_setNextBlockTimestamp", [target]); + await ethers.provider.send("evm_mine", []); + } else { + throw new Error(`Setting block timestamp for the current blockchain is not supported: ${network.name}`); + } +} diff --git a/test/base/CWToken.complex.test.ts b/test/base/CWToken.complex.test.ts index 90e5e127..680939d6 100644 --- a/test/base/CWToken.complex.test.ts +++ b/test/base/CWToken.complex.test.ts @@ -2,8 +2,8 @@ import { ethers, network, upgrades } from "hardhat"; import { expect } from "chai"; import { Contract, ContractFactory } from "ethers"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; -import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers"; -import { proveTx, connect } from "../../test-utils/eth"; +import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; +import { connect, getLatestBlockTimestamp, increaseBlockTimestampTo, proveTx } from "../../test-utils/eth"; async function setUpFixture(func: () => Promise): Promise { if (network.name === "hardhat") { @@ -68,7 +68,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", }; } ) { - const timestamp = (await time.latest()) + 100; + const timestamp = (await getLatestBlockTimestamp()) + 100; const { token, amounts } = props; if (amounts.mint > 0) { await proveTx(token.mint(user.address, amounts.mint)); @@ -274,14 +274,14 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", describe("Frozen and premint balances", async () => { let timestamp: number; before(async () => { - timestamp = (await time.latest()) + 100; + timestamp = (await getLatestBlockTimestamp()) + 100; }); it("Transfer to purpose account - test 5 with release awaiting", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 5) ).to.changeTokenBalances( @@ -296,7 +296,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 10) ).to.changeTokenBalances( @@ -311,7 +311,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 15) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_FROZEN_AMOUNT); @@ -322,7 +322,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 20) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_FROZEN_AMOUNT); @@ -333,7 +333,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -344,7 +344,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 5) ).to.changeTokenBalances( @@ -359,7 +359,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 10) ).to.changeTokenBalances( @@ -374,7 +374,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 15) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_FROZEN_AMOUNT); @@ -385,7 +385,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 20) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_FROZEN_AMOUNT); @@ -396,7 +396,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.freeze(user.address, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -506,14 +506,14 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", describe("Premint and restricted balances", async () => { let timestamp: number; before(async () => { - timestamp = (await time.latest()) + 100; + timestamp = (await getLatestBlockTimestamp()) + 100; }); it("Transfer to purpose account - test 5 with release awaiting", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 5) ).to.changeTokenBalances( @@ -529,7 +529,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 10) ).to.changeTokenBalances( @@ -545,7 +545,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 15) ).to.changeTokenBalances( @@ -561,7 +561,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 20) ).to.changeTokenBalances( @@ -577,7 +577,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -589,7 +589,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 5) ).to.changeTokenBalances( @@ -605,7 +605,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 10) ).to.changeTokenBalances( @@ -621,7 +621,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 15) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_RESTRICTED_AMOUNT); @@ -632,7 +632,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 20) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_RESTRICTED_AMOUNT); @@ -643,7 +643,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.mint(user.address, 10)); await proveTx(token.premintIncrease(user.address, 10, timestamp)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 10)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -770,7 +770,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", describe("Frozen, restricted and premint balances", async () => { let timestamp: number; before(async () => { - timestamp = (await time.latest()) + 100; + timestamp = (await getLatestBlockTimestamp()) + 100; }); it("Transfer to purpose account - test 5 with release awaiting", async () => { const { token } = await setUpFixture(deployAndConfigureToken); @@ -778,7 +778,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 5) ).to.changeTokenBalances( @@ -795,7 +795,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 10) ).to.changeTokenBalances( @@ -812,7 +812,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 15) ).to.changeTokenBalances( @@ -829,7 +829,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 20) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_FROZEN_AMOUNT); @@ -841,7 +841,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -853,7 +853,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 5) ).to.changeTokenBalances( @@ -870,7 +870,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 10) ).to.changeTokenBalances( @@ -887,7 +887,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 15) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_RESTRICTED_AMOUNT); @@ -899,7 +899,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 20) ).to.be.revertedWithCustomError(token, REVERT_ERROR_TRANSFER_EXCEEDED_FROZEN_AMOUNT); @@ -911,7 +911,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", await proveTx(token.premintIncrease(user.address, 5, timestamp)); await proveTx(token.freeze(user.address, 5)); await proveTx(token.restrictionIncrease(user.address, PURPOSE, 5)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -1300,12 +1300,12 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", describe("Premint balance only, no frozen balance or restricted balance", async () => { let timestamp: number; before(async () => { - timestamp = await time.latest() + 100; + timestamp = await getLatestBlockTimestamp() + 100; }); it("Transfer to purpose account with release awaiting - test 5", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 5) ).to.changeTokenBalances( @@ -1318,7 +1318,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to purpose account with release awaiting - test 10", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 10) ).to.changeTokenBalances( @@ -1331,7 +1331,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to purpose account with release awaiting - test 15", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 15) ).to.changeTokenBalances( @@ -1344,7 +1344,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to purpose account with release awaiting - test 20", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 20) ).to.changeTokenBalances( @@ -1357,7 +1357,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to purpose account with release awaiting - test 25", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(purposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); @@ -1366,7 +1366,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to non-purpose account with release awaiting - test 5", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 5) ).to.changeTokenBalances( @@ -1379,7 +1379,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to non-purpose account with release awaiting - test 10", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 10) ).to.changeTokenBalances( @@ -1392,7 +1392,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to non-purpose account with release awaiting - test 15", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 15) ).to.changeTokenBalances( @@ -1405,7 +1405,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to non-purpose account with release awaiting - test 20", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 20) ).to.changeTokenBalances( @@ -1418,7 +1418,7 @@ describe("Contract 'CWToken' - Premintable, Freezable & Restrictable scenarios", it("Transfer to non-purpose account with release awaiting - test 25", async () => { const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(token.premintIncrease(user.address, 20, timestamp)); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); await expect( connect(token, user).transfer(nonPurposeAccount.address, 25) ).to.be.revertedWith(REVERT_MESSAGE_ERC20_TRANSFER_AMOUNT_EXCEEDS_BALANCE); diff --git a/test/base/ERC20Hookable.test.ts b/test/base/ERC20Hookable.test.ts index 9a30d19b..845b3675 100644 --- a/test/base/ERC20Hookable.test.ts +++ b/test/base/ERC20Hookable.test.ts @@ -3,7 +3,7 @@ import { expect } from "chai"; import { Contract, ContractFactory } from "ethers"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; -import { proveTx, connect } from "../../test-utils/eth"; +import { proveTx, connect, getAddress } from "../../test-utils/eth"; enum ErrorHandlingPolicy { Revert = 0, @@ -142,11 +142,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -161,11 +161,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -180,11 +180,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -199,11 +199,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -218,11 +218,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -239,11 +239,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -260,11 +260,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -283,11 +283,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Event } ]; @@ -300,23 +300,23 @@ describe("Contract 'ERC20Hookable'", async () => { await proveTx(hook2.setRevertWithPanic(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .not.to.emit(hook1, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithPanic(true)); await proveTx(hook2.setRevertWithPanic(false)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook2, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK) .not.to.emit(hook1, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithPanic(false)); await proveTx(hook2.setRevertWithPanic(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook1, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK); }); @@ -325,11 +325,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Event } ]; @@ -342,23 +342,23 @@ describe("Contract 'ERC20Hookable'", async () => { await proveTx(hook2.setRevertWithReasonMessage(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .not.to.emit(hook1, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithReasonMessage(true)); await proveTx(hook2.setRevertWithReasonMessage(false)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook2, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK) .not.to.emit(hook1, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithReasonMessage(false)); await proveTx(hook2.setRevertWithReasonMessage(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook1, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_BEFORE_TOKEN_TRANSFER_HOOK); }); @@ -367,11 +367,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Event } ]; @@ -385,14 +385,14 @@ describe("Contract 'ERC20Hookable'", async () => { await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) .withArgs( - await hook1.getAddress(), + getAddress(hook1), ZERO_REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, REVERT_LOW_LEVEL_DATA_BEFORE ) .to.emit(token, EVENT_NAME_BEFORE_TOKEN_TRANSFER_HOOK_FAILURE) .withArgs( - await hook2.getAddress(), + getAddress(hook2), ZERO_REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, REVERT_LOW_LEVEL_DATA_BEFORE @@ -407,11 +407,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -428,11 +428,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -449,11 +449,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Revert } ]; @@ -473,11 +473,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Event } ]; @@ -490,23 +490,23 @@ describe("Contract 'ERC20Hookable'", async () => { await proveTx(hook2.setRevertWithPanic(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .not.to.emit(hook1, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithPanic(true)); await proveTx(hook2.setRevertWithPanic(false)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook2, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK) .not.to.emit(hook1, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithPanic(false)); await proveTx(hook2.setRevertWithPanic(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), ZERO_REVERT_REASON_MESSAGE, PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook1, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK); }); @@ -515,11 +515,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Event } ]; @@ -532,23 +532,23 @@ describe("Contract 'ERC20Hookable'", async () => { await proveTx(hook2.setRevertWithReasonMessage(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .not.to.emit(hook1, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithReasonMessage(true)); await proveTx(hook2.setRevertWithReasonMessage(false)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook1.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook1), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook2, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK) .not.to.emit(hook1, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK); await proveTx(hook1.setRevertWithReasonMessage(false)); await proveTx(hook2.setRevertWithReasonMessage(true)); await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) - .withArgs(await hook2.getAddress(), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) + .withArgs(getAddress(hook2), REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, ZERO_REVERT_LOW_LEVEL_DATA) .to.emit(hook1, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK) .not.to.emit(hook2, EVENT_NAME_TEST_AFTER_TOKEN_TRANSFER_HOOK); }); @@ -557,11 +557,11 @@ describe("Contract 'ERC20Hookable'", async () => { const { token, hook1, hook2 } = await setUpFixture(deployTokenAndHooks); const hooks: HookConfig[] = [ { - account: await hook1.getAddress(), + account: getAddress(hook1), policy: ErrorHandlingPolicy.Event }, { - account: await hook2.getAddress(), + account: getAddress(hook2), policy: ErrorHandlingPolicy.Event } ]; @@ -575,14 +575,14 @@ describe("Contract 'ERC20Hookable'", async () => { await expect(connect(token, user).transfer(user.address, 0)) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) .withArgs( - await hook1.getAddress(), + getAddress(hook1), ZERO_REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, REVERT_LOW_LEVEL_DATA_AFTER ) .to.emit(token, EVENT_NAME_AFTER_TOKEN_TRANSFER_HOOK_FAILURE) .withArgs( - await hook2.getAddress(), + getAddress(hook2), ZERO_REVERT_REASON_MESSAGE, ZERO_PANIC_ERROR_CODE, REVERT_LOW_LEVEL_DATA_AFTER diff --git a/test/base/ERC20Mintable.test.ts b/test/base/ERC20Mintable.test.ts index bac943e7..b97f0290 100644 --- a/test/base/ERC20Mintable.test.ts +++ b/test/base/ERC20Mintable.test.ts @@ -1,10 +1,9 @@ import { ethers, network, upgrades } from "hardhat"; import { expect } from "chai"; -import { Contract, ContractFactory } from "ethers"; -import { TransactionResponse } from "@ethersproject/abstract-provider"; -import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers"; +import { Contract, ContractFactory, TransactionResponse } from "ethers"; +import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; -import { connect, proveTx } from "../../test-utils/eth"; +import { connect, getLatestBlockTimestamp, increaseBlockTimestampTo, proveTx } from "../../test-utils/eth"; async function setUpFixture(func: () => Promise): Promise { if (network.name === "hardhat") { @@ -355,7 +354,7 @@ describe("Contract 'ERC20Mintable'", async () => { describe("Premint functions", async () => { let timestamp: number; before(async () => { - timestamp = (await time.latest()) + 100; + timestamp = (await getLatestBlockTimestamp()) + 100; }); describe("Execute as expected and emits the correct events if", async () => { @@ -484,7 +483,7 @@ describe("Contract 'ERC20Mintable'", async () => { await proveTx(connect(token, minter).premintIncrease(user.address, TOKEN_AMOUNT, timestamp + i * 10)); } expect(await token.balanceOfPremint(user.address)).to.eq(TOKEN_AMOUNT * MAX_PENDING_PREMINTS_COUNT); - await time.increaseTo(timestamp + 1); + await increaseBlockTimestampTo(timestamp + 1); expect(await token.balanceOfPremint(user.address)).to.eq(TOKEN_AMOUNT * (MAX_PENDING_PREMINTS_COUNT - 1)); await executeAndCheckPremint(token, { amount: TOKEN_AMOUNT + 1, @@ -505,7 +504,7 @@ describe("Contract 'ERC20Mintable'", async () => { // set time to expire all premints const newTimestamp = timestamp + (i - 1) * 10 + 1; - await time.increaseTo(newTimestamp); + await increaseBlockTimestampTo(newTimestamp); await executeAndCheckPremint(token, { release: newTimestamp + 10, @@ -524,7 +523,7 @@ describe("Contract 'ERC20Mintable'", async () => { await proveTx(connect(token, minter).premintIncrease(user.address, TOKEN_AMOUNT, timestamps[i])); } // set time to expire premints in the beginning of array - await time.increaseTo(timestamps[1] + 1); + await increaseBlockTimestampTo(timestamps[1] + 1); await executeAndCheckPremint(token, { amount: 1, @@ -550,7 +549,7 @@ describe("Contract 'ERC20Mintable'", async () => { } // set time to expire premints in the middle of array - await time.increaseTo(timestamp + 3); + await increaseBlockTimestampTo(timestamp + 3); // update premint in the beginning of array before expired premints await executeAndCheckPremint(token, { @@ -578,7 +577,7 @@ describe("Contract 'ERC20Mintable'", async () => { } // set time to expire premints in the end of array - await time.increaseTo(timestamp + 3); + await increaseBlockTimestampTo(timestamp + 3); // update premint in array before expired premints await executeAndCheckPremint(token, { @@ -606,7 +605,7 @@ describe("Contract 'ERC20Mintable'", async () => { it("The provided release time has passed", async () => { const { token } = await setUpFixture(deployAndConfigureToken); - const timestamp = (await time.latest()) - 1; + const timestamp = (await getLatestBlockTimestamp()) - 1; await expect(connect(token, minter).premintIncrease(user.address, TOKEN_AMOUNT, timestamp)) .to.be.revertedWithCustomError(token, REVERT_ERROR_PREMINT_RELEASE_TIME_PASSED); await expect(connect(token, minter).premintDecrease(user.address, TOKEN_AMOUNT, timestamp)) @@ -714,7 +713,7 @@ describe("Contract 'ERC20Mintable'", async () => { describe("Function 'reschedulePremintRelease()'", async () => { let timestamp: number; before(async () => { - timestamp = (await time.latest()) + 100; + timestamp = (await getLatestBlockTimestamp()) + 100; }); async function checkPremints(token: Contract, expectedPremints: Premint[]) { @@ -792,7 +791,7 @@ describe("Contract 'ERC20Mintable'", async () => { const newPremint: Premint = ({ amount: TOKEN_AMOUNT, release: timestamp + 1000 }); // Shift the block time to the first original release timestamp - await time.increaseTo(originalReleaseTimestamps[0]); + await increaseBlockTimestampTo(originalReleaseTimestamps[0]); // Check that the premints are still here after adding and removing a new one await proveTx(connect(token, minter).premintIncrease(user.address, newPremint.amount, newPremint.release)); @@ -801,7 +800,7 @@ describe("Contract 'ERC20Mintable'", async () => { expect(await token.balanceOfPremint(user.address)).to.eq(expectedPremintBalance); // Shift the block time to the next original release timestamp - await time.increaseTo(originalReleaseTimestamps[1]); + await increaseBlockTimestampTo(originalReleaseTimestamps[1]); // Check that the premints are still here after adding and removing a new one await proveTx(connect(token, minter).premintIncrease(user.address, newPremint.amount, newPremint.release)); @@ -810,7 +809,7 @@ describe("Contract 'ERC20Mintable'", async () => { expect(await token.balanceOfPremint(user.address)).to.eq(expectedPremintBalance); // Shift the block time to the target release timestamp - await time.increaseTo(targetReleaseTimestamp); + await increaseBlockTimestampTo(targetReleaseTimestamp); // Check that the premints disappeared after adding a new one await proveTx(connect(token, minter).premintIncrease(user.address, newPremint.amount, newPremint.release)); @@ -840,7 +839,7 @@ describe("Contract 'ERC20Mintable'", async () => { const newPremint: Premint = ({ amount: TOKEN_AMOUNT, release: timestamp + 1000 }); // Shift the block time to the target release timestamp - await time.increaseTo(targetReleaseTimestamp); + await increaseBlockTimestampTo(targetReleaseTimestamp); // Check that the premints disappeared after adding a new one await proveTx(connect(token, minter).premintIncrease(user.address, newPremint.amount, newPremint.release)); @@ -849,7 +848,7 @@ describe("Contract 'ERC20Mintable'", async () => { expect(await token.balanceOfPremint(user.address)).to.eq(0); // Shift the block time to the original release timestamp - await time.increaseTo(originalReleaseTimestamp); + await increaseBlockTimestampTo(originalReleaseTimestamp); expect(await token.balanceOfPremint(user.address)).to.eq(0); }); @@ -916,7 +915,7 @@ describe("Contract 'ERC20Mintable'", async () => { it("The provided target release timestamp for the rescheduling is passed", async () => { const { token } = await setUpFixture(deployAndConfigureToken); const originalRelease = timestamp; - const targetRelease = await time.latest() - 1; + const targetRelease = await getLatestBlockTimestamp() - 1; await expect( connect(token, minter).reschedulePremintRelease(originalRelease, targetRelease) ).to.be.revertedWithCustomError(token, REVERT_ERROR_PREMINT_RESCHEDULING_TIME_PASSED); @@ -924,7 +923,7 @@ describe("Contract 'ERC20Mintable'", async () => { it("The provided original release time has passed", async () => { const { token } = await setUpFixture(deployAndConfigureToken); - const originalRelease = await time.latest() - 1; + const originalRelease = await getLatestBlockTimestamp() - 1; const targetRelease = originalRelease + 1000; await expect( connect(token, minter).reschedulePremintRelease(originalRelease, targetRelease) @@ -937,7 +936,7 @@ describe("Contract 'ERC20Mintable'", async () => { const targetRelease1 = originalRelease + 1; const targetRelease2 = originalRelease + 1000; await proveTx(connect(token, minter).reschedulePremintRelease(originalRelease, targetRelease1)); - await time.increaseTo(targetRelease1); + await increaseBlockTimestampTo(targetRelease1); await expect( connect(token, minter).reschedulePremintRelease(originalRelease, targetRelease2) ).to.be.revertedWithCustomError(token, REVERT_ERROR_PREMINT_RELEASE_TIME_PASSED); @@ -1017,17 +1016,17 @@ describe("Contract 'ERC20Mintable'", async () => { describe("Function 'balanceOfPremint()'", async () => { it("Returns the correct balance of premint", async () => { - const timestamp = (await time.latest()) + 100; + const timestamp = (await getLatestBlockTimestamp()) + 100; const { token } = await setUpFixture(deployAndConfigureToken); await proveTx(connect(token, minter).premintIncrease(user.address, TOKEN_AMOUNT, timestamp)); await proveTx(connect(token, minter).premintIncrease(user.address, TOKEN_AMOUNT + 1, timestamp + 50)); expect(await token.balanceOfPremint(user.address)).to.eq(TOKEN_AMOUNT * 2 + 1); - await time.increaseTo(timestamp); + await increaseBlockTimestampTo(timestamp); expect(await token.balanceOfPremint(user.address)).to.eq(TOKEN_AMOUNT + 1); - await time.increaseTo(timestamp + 50); + await increaseBlockTimestampTo(timestamp + 50); expect(await token.balanceOfPremint(user.address)).to.eq(0); }); }); diff --git a/test/base/common/RescuableUpgradeable.test.ts b/test/base/common/RescuableUpgradeable.test.ts index 65f5aa5e..dc260e96 100644 --- a/test/base/common/RescuableUpgradeable.test.ts +++ b/test/base/common/RescuableUpgradeable.test.ts @@ -3,7 +3,7 @@ import { expect } from "chai"; import { Contract, ContractFactory } from "ethers"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; -import { proveTx, connect } from "../../../test-utils/eth"; +import { connect, getAddress, proveTx } from "../../../test-utils/eth"; async function setUpFixture(func: () => Promise): Promise { if (network.name === "hardhat") { @@ -60,7 +60,7 @@ describe("Contract 'RescuableUpgradeable'", async () => { }> { const { rescuable } = await deployRescuable(); const { token } = await deployToken(); - await proveTx(token.mintForTest(await rescuable.getAddress(), TOKEN_AMOUNT)); + await proveTx(token.mintForTest(getAddress(rescuable), TOKEN_AMOUNT)); await proveTx(rescuable.setRescuer(rescuer.address)); return { rescuable, token }; } @@ -126,7 +126,7 @@ describe("Contract 'RescuableUpgradeable'", async () => { it("Executes as expected and emits the correct event", async () => { const { rescuable, token } = await setUpFixture(deployAndConfigure); const tx = connect(rescuable, rescuer).rescueERC20( - await token.getAddress(), + getAddress(token), deployer.address, TOKEN_AMOUNT ); @@ -137,13 +137,13 @@ describe("Contract 'RescuableUpgradeable'", async () => { ); await expect(tx) .to.emit(token, EVENT_NAME_TRANSFER) - .withArgs(await rescuable.getAddress(), deployer.address, TOKEN_AMOUNT); + .withArgs(getAddress(rescuable), deployer.address, TOKEN_AMOUNT); }); it("Is reverted if called not by the rescuer", async () => { const { rescuable, token } = await setUpFixture(deployAndConfigure); await expect( - connect(rescuable, user).rescueERC20(await token.getAddress(), deployer.address, TOKEN_AMOUNT) + connect(rescuable, user).rescueERC20(getAddress(token), deployer.address, TOKEN_AMOUNT) ).to.be.revertedWithCustomError(rescuable, REVERT_ERROR_UNAUTHORIZED_RESCUER); }); });