Skip to content

Commit

Permalink
test: refactor tests for fhevmjs06
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Dec 16, 2024
1 parent 7432d16 commit a1cbe5d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 80 deletions.
19 changes: 9 additions & 10 deletions test/confidentialERC20/ConfidentialERC20Wrapped.fixture.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,33 +13,32 @@ 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();

return [contractERC20, contractConfidentialERC20Wrapped];
}

export async function mintAndWrap(
signers: Signers,
user: string,
account: Signer,
plainToken: ERC20Mintable,
token: ConfidentialERC20Wrapped,
tokenAddress: string,
amount: bigint,
): Promise<void> {
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();
}
51 changes: 20 additions & 31 deletions test/confidentialERC20/ConfidentialERC20Wrapped.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 () {
Expand All @@ -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,
),
Expand Down Expand Up @@ -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,
),
Expand All @@ -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();

Expand Down Expand Up @@ -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,
),
Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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 () {
Expand All @@ -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,
),
Expand Down Expand Up @@ -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,
),
Expand Down
6 changes: 3 additions & 3 deletions test/confidentialERC20/ConfidentialWETH.fixture.ts
Original file line number Diff line number Diff line change
@@ -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<TestConfidentialWETH> {
export async function deployConfidentialWETHFixture(account: Signer): Promise<TestConfidentialWETH> {
// @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;
Expand Down
47 changes: 11 additions & 36 deletions test/confidentialERC20/ConfidentialWETH.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Expand All @@ -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);
});

Expand All @@ -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
Expand 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"));
});

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
});

Expand All @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit a1cbe5d

Please sign in to comment.