From a1cbe5d858295a645037197bb12230e9a023d1e7 Mon Sep 17 00:00:00 2001 From: PacificYield <173040337+PacificYield@users.noreply.github.com> Date: Tue, 10 Dec 2024 07:40:13 +0100 Subject: [PATCH] test: refactor tests for fhevmjs06 --- .../ConfidentialERC20Wrapped.fixture.ts | 19 ++++--- .../ConfidentialERC20Wrapped.test.ts | 51 ++++++++----------- .../ConfidentialWETH.fixture.ts | 6 +-- .../ConfidentialWETH.test.ts | 47 ++++------------- 4 files changed, 43 insertions(+), 80 deletions(-) diff --git a/test/confidentialERC20/ConfidentialERC20Wrapped.fixture.ts b/test/confidentialERC20/ConfidentialERC20Wrapped.fixture.ts index 5da82d6..4038638 100644 --- a/test/confidentialERC20/ConfidentialERC20Wrapped.fixture.ts +++ b/test/confidentialERC20/ConfidentialERC20Wrapped.fixture.ts @@ -1,10 +1,10 @@ +import { Signer } from "ethers"; import { ethers } from "hardhat"; import type { ConfidentialERC20Wrapped, ERC20Mintable, TestConfidentialERC20Wrapped } from "../../types"; -import { Signers } from "../signers"; export async function deployERC20AndConfidentialERC20WrappedFixture( - signers: Signers, + account: Signer, name: string, symbol: string, decimals: number, @@ -13,13 +13,13 @@ export async function deployERC20AndConfidentialERC20WrappedFixture( const maxDecryptionDelay = 60 * 5; const contractFactoryERC20Mintable = await ethers.getContractFactory("ERC20Mintable"); const contractERC20 = await contractFactoryERC20Mintable - .connect(signers.alice) - .deploy(name, symbol, decimals, signers.alice.address); + .connect(account) + .deploy(name, symbol, decimals, await account.getAddress()); await contractERC20.waitForDeployment(); const contractFactory = await ethers.getContractFactory("TestConfidentialERC20Wrapped"); const contractConfidentialERC20Wrapped = await contractFactory - .connect(signers.alice) + .connect(account) .deploy(contractERC20.getAddress(), maxDecryptionDelay); await contractConfidentialERC20Wrapped.waitForDeployment(); @@ -27,19 +27,18 @@ export async function deployERC20AndConfidentialERC20WrappedFixture( } export async function mintAndWrap( - signers: Signers, - user: string, + account: Signer, plainToken: ERC20Mintable, token: ConfidentialERC20Wrapped, tokenAddress: string, amount: bigint, ): Promise { - let tx = await plainToken.connect(signers[user as keyof Signers]).mint(amount); + let tx = await plainToken.connect(account).mint(amount); await tx.wait(); - tx = await plainToken.connect(signers[user as keyof Signers]).approve(tokenAddress, amount); + tx = await plainToken.connect(account).approve(tokenAddress, amount); await tx.wait(); - tx = await token.connect(signers[user as keyof Signers]).wrap(amount); + tx = await token.connect(account).wrap(amount); await tx.wait(); } diff --git a/test/confidentialERC20/ConfidentialERC20Wrapped.test.ts b/test/confidentialERC20/ConfidentialERC20Wrapped.test.ts index 8d422b7..44f8ae5 100644 --- a/test/confidentialERC20/ConfidentialERC20Wrapped.test.ts +++ b/test/confidentialERC20/ConfidentialERC20Wrapped.test.ts @@ -2,20 +2,21 @@ import { expect } from "chai"; import { ethers } from "hardhat"; import { awaitAllDecryptionResults } from "../asyncDecrypt"; -import { createInstances } from "../instance"; +import { createInstance } from "../instance"; import { getSigners, initSigners } from "../signers"; import { reencryptBalance } from "./ConfidentialERC20.fixture"; import { deployERC20AndConfidentialERC20WrappedFixture } from "./ConfidentialERC20Wrapped.fixture"; describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { before(async function () { - await initSigners(2); + await initSigners(); this.signers = await getSigners(); + this.instance = await createInstance(); }); beforeEach(async function () { const [erc20, confidentialERC20Wrapped] = await deployERC20AndConfidentialERC20WrappedFixture( - this.signers, + this.signers.alice, "Naraggara", "NARA", 6, @@ -25,7 +26,6 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { this.confidentialERC20Wrapped = confidentialERC20Wrapped; this.erc20ContractAddress = await erc20.getAddress(); this.confidentialERC20WrappedAddress = await confidentialERC20Wrapped.getAddress(); - this.instances = await createInstances(this.signers); }); it("name/symbol are automatically set", async function () { @@ -51,9 +51,8 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { // Check encrypted balance expect( await reencryptBalance( - this.signers, - this.instances, - "alice", + this.signers.alice, + this.instance, this.confidentialERC20Wrapped, this.confidentialERC20WrappedAddress, ), @@ -81,9 +80,8 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { expect( await reencryptBalance( - this.signers, - this.instances, - "alice", + this.signers.alice, + this.instance, this.confidentialERC20Wrapped, this.confidentialERC20WrappedAddress, ), @@ -105,10 +103,7 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { tx = await this.confidentialERC20Wrapped.connect(this.signers.alice).unwrap(amountToUnwrap); await tx.wait(); - const input = this.instances.alice.createEncryptedInput( - this.confidentialERC20WrappedAddress, - this.signers.alice.address, - ); + const input = this.instance.createEncryptedInput(this.confidentialERC20WrappedAddress, this.signers.alice.address); input.add64(transferAmount); const encryptedTransferAmount = await input.encrypt(); @@ -160,9 +155,8 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { expect(await this.confidentialERC20Wrapped.totalSupply()).to.equal(amountToWrap); expect( await reencryptBalance( - this.signers, - this.instances, - "alice", + this.signers.alice, + this.instance, this.confidentialERC20Wrapped, this.confidentialERC20WrappedAddress, ), @@ -181,10 +175,7 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { await tx.wait(); let transferAmount = ethers.parseUnits("3000", 6); - let input = this.instances.alice.createEncryptedInput( - this.confidentialERC20WrappedAddress, - this.signers.alice.address, - ); + let input = this.instance.createEncryptedInput(this.confidentialERC20WrappedAddress, this.signers.alice.address); input.add64(transferAmount); let encryptedTransferAmount = await input.encrypt(); @@ -200,7 +191,7 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { await awaitAllDecryptionResults(); transferAmount = ethers.parseUnits("1000", 6); - input = this.instances.bob.createEncryptedInput(this.confidentialERC20WrappedAddress, this.signers.bob.address); + input = this.instance.createEncryptedInput(this.confidentialERC20WrappedAddress, this.signers.bob.address); input.add64(transferAmount); encryptedTransferAmount = await input.encrypt(); @@ -242,13 +233,14 @@ describe("ConfidentialERC20Wrapped using ERC20 with 6 decimals", function () { describe("ConfidentialERC20Wrapped using ERC20 with 18 decimals", function () { before(async function () { - await initSigners(2); + await initSigners(); this.signers = await getSigners(); + this.instance = await createInstance(); }); beforeEach(async function () { const [erc20, confidentialERC20Wrapped] = await deployERC20AndConfidentialERC20WrappedFixture( - this.signers, + this.signers.alice, "Naraggara", "NARA", 18, @@ -257,7 +249,6 @@ describe("ConfidentialERC20Wrapped using ERC20 with 18 decimals", function () { this.confidentialERC20Wrapped = confidentialERC20Wrapped; this.erc20ContractAddress = await erc20.getAddress(); this.confidentialERC20WrappedAddress = await confidentialERC20Wrapped.getAddress(); - this.instances = await createInstances(this.signers); }); it("can wrap", async function () { @@ -282,9 +273,8 @@ describe("ConfidentialERC20Wrapped using ERC20 with 18 decimals", function () { // Check encrypted balance expect( await reencryptBalance( - this.signers, - this.instances, - "alice", + this.signers.alice, + this.instance, this.confidentialERC20Wrapped, this.confidentialERC20WrappedAddress, ), @@ -319,9 +309,8 @@ describe("ConfidentialERC20Wrapped using ERC20 with 18 decimals", function () { // Check encrypted balance expect( await reencryptBalance( - this.signers, - this.instances, - "alice", + this.signers.alice, + this.instance, this.confidentialERC20Wrapped, this.confidentialERC20WrappedAddress, ), diff --git a/test/confidentialERC20/ConfidentialWETH.fixture.ts b/test/confidentialERC20/ConfidentialWETH.fixture.ts index 0385169..5973db5 100644 --- a/test/confidentialERC20/ConfidentialWETH.fixture.ts +++ b/test/confidentialERC20/ConfidentialWETH.fixture.ts @@ -1,13 +1,13 @@ +import { Signer } from "ethers"; import { ethers } from "hardhat"; import type { TestConfidentialWETH } from "../../types"; -import { Signers } from "../signers"; -export async function deployConfidentialWETHFixture(signers: Signers): Promise { +export async function deployConfidentialWETHFixture(account: Signer): Promise { // @dev We use 5 minutes for the maximum decryption delay (from the Gateway). const maxDecryptionDelay = 60 * 5; const contractFactory = await ethers.getContractFactory("TestConfidentialWETH"); - const confidentialWETH = await contractFactory.connect(signers.alice).deploy(maxDecryptionDelay); + const confidentialWETH = await contractFactory.connect(account).deploy(maxDecryptionDelay); await confidentialWETH.waitForDeployment(); return confidentialWETH; diff --git a/test/confidentialERC20/ConfidentialWETH.test.ts b/test/confidentialERC20/ConfidentialWETH.test.ts index 40ecdfb..839deff 100644 --- a/test/confidentialERC20/ConfidentialWETH.test.ts +++ b/test/confidentialERC20/ConfidentialWETH.test.ts @@ -1,22 +1,21 @@ import { expect } from "chai"; -import { parseUnits } from "ethers"; import { ethers } from "hardhat"; import { awaitAllDecryptionResults } from "../asyncDecrypt"; -import { createInstances } from "../instance"; +import { createInstance } from "../instance"; import { getSigners, initSigners } from "../signers"; import { reencryptBalance } from "./ConfidentialERC20.fixture"; import { deployConfidentialWETHFixture } from "./ConfidentialWETH.fixture"; describe("ConfidentialWETH", function () { before(async function () { - await initSigners(3); + await initSigners(); this.signers = await getSigners(); - this.instances = await createInstances(this.signers); + this.instance = await createInstance(); }); beforeEach(async function () { - const confidentialWETH = await deployConfidentialWETHFixture(this.signers); + const confidentialWETH = await deployConfidentialWETHFixture(this.signers.alice); this.confidentialWETH = confidentialWETH; this.confidentialWETHAddress = await confidentialWETH.getAddress(); }); @@ -40,13 +39,7 @@ describe("ConfidentialWETH", function () { // Check encrypted balance expect( - await reencryptBalance( - this.signers, - this.instances, - "alice", - this.confidentialWETH, - this.confidentialWETHAddress, - ), + await reencryptBalance(this.signers.alice, this.instance, this.confidentialWETH, this.confidentialWETHAddress), ).to.equal(amountToWrap6Decimals); }); @@ -70,13 +63,7 @@ describe("ConfidentialWETH", function () { // Check encrypted balance expect( - await reencryptBalance( - this.signers, - this.instances, - "alice", - this.confidentialWETH, - this.confidentialWETHAddress, - ), + await reencryptBalance(this.signers.alice, this.instance, this.confidentialWETH, this.confidentialWETHAddress), ).to.equal(amountToWrap6Decimals - amountToUnwrap6Decimals); // Unwrap all @@ -85,13 +72,7 @@ describe("ConfidentialWETH", function () { await awaitAllDecryptionResults(); expect( - await reencryptBalance( - this.signers, - this.instances, - "alice", - this.confidentialWETH, - this.confidentialWETHAddress, - ), + await reencryptBalance(this.signers.alice, this.instance, this.confidentialWETH, this.confidentialWETHAddress), ).to.equal(BigInt("0")); }); @@ -136,7 +117,7 @@ describe("ConfidentialWETH", function () { tx = await this.confidentialWETH.connect(this.signers.alice).unwrap(amountToUnwrap); await tx.wait(); - const input = this.instances.alice.createEncryptedInput(this.confidentialWETHAddress, this.signers.alice.address); + const input = this.instance.createEncryptedInput(this.confidentialWETHAddress, this.signers.alice.address); input.add64(transferAmount); const encryptedTransferAmount = await input.encrypt(); @@ -187,13 +168,7 @@ describe("ConfidentialWETH", function () { expect(await ethers.provider.getBalance(this.confidentialWETHAddress)).to.equal(amountToWrap18Decimals); expect(await this.confidentialWETH.totalSupply()).to.equal(amountToWrap6Decimals); expect( - await reencryptBalance( - this.signers, - this.instances, - "alice", - this.confidentialWETH, - this.confidentialWETHAddress, - ), + await reencryptBalance(this.signers.alice, this.instance, this.confidentialWETH, this.confidentialWETHAddress), ).to.equal(amountToWrap6Decimals); }); @@ -208,7 +183,7 @@ describe("ConfidentialWETH", function () { await tx.wait(); let transferAmount = ethers.parseUnits("3000", 6); - let input = this.instances.alice.createEncryptedInput(this.confidentialWETHAddress, this.signers.alice.address); + let input = this.instance.createEncryptedInput(this.confidentialWETHAddress, this.signers.alice.address); input.add64(transferAmount); let encryptedTransferAmount = await input.encrypt(); @@ -223,7 +198,7 @@ describe("ConfidentialWETH", function () { await awaitAllDecryptionResults(); transferAmount = ethers.parseUnits("1000", 6); - input = this.instances.bob.createEncryptedInput(this.confidentialWETHAddress, this.signers.bob.address); + input = this.instance.createEncryptedInput(this.confidentialWETHAddress, this.signers.bob.address); input.add64(transferAmount); encryptedTransferAmount = await input.encrypt();