From 0dcf96e0c1f9cd00d261e14c6092270fb308b164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 31 Aug 2023 15:39:02 +0200 Subject: [PATCH] test() add utils to test with signers --- test/encryptedERC20/EncryptedERC20.fixture.ts | 8 +- test/encryptedERC20/EncryptedERC20.ts | 100 ++++++++---------- test/instance.ts | 3 +- test/signers.ts | 19 ++++ test/tfheOperations/tfheOperations.fixture.ts | 2 +- test/tfheOperations/tfheOperations.ts | 9 +- test/types.ts | 11 +- 7 files changed, 76 insertions(+), 76 deletions(-) create mode 100644 test/signers.ts diff --git a/test/encryptedERC20/EncryptedERC20.fixture.ts b/test/encryptedERC20/EncryptedERC20.fixture.ts index e37ac0d7..5055cd86 100644 --- a/test/encryptedERC20/EncryptedERC20.fixture.ts +++ b/test/encryptedERC20/EncryptedERC20.fixture.ts @@ -1,13 +1,13 @@ import { ethers } from 'hardhat'; -import type { EncryptedERC20 } from '../../types/contracts/EncryptedERC20'; +import type { EncryptedERC20 } from '../../types'; +import { getSigners } from '../signers'; export async function deployEncryptedERC20Fixture(): Promise { - const signers = await ethers.getSigners(); - const admin = signers[0]; + const signers = await getSigners(); const contractFactory = await ethers.getContractFactory('EncryptedERC20'); - const contract = await contractFactory.connect(admin).deploy(); + const contract = await contractFactory.connect(signers.alice).deploy(); await contract.waitForDeployment(); return contract; diff --git a/test/encryptedERC20/EncryptedERC20.ts b/test/encryptedERC20/EncryptedERC20.ts index 619a4b00..bd5dd5d9 100644 --- a/test/encryptedERC20/EncryptedERC20.ts +++ b/test/encryptedERC20/EncryptedERC20.ts @@ -2,76 +2,68 @@ import { expect } from 'chai'; import { ethers } from 'hardhat'; import { createInstances } from '../instance'; -import type { Signers } from '../types'; +import { getSigners } from '../signers'; import { deployEncryptedERC20Fixture } from './EncryptedERC20.fixture'; -describe('Unit tests', function () { +describe('EncryptedERC20', function () { before(async function () { - this.signers = {} as Signers; - const signers = await ethers.getSigners(); - this.signers.alice = signers[0]; - this.signers.bob = signers[1]; - this.signers.carol = signers[2]; - this.signers.dave = signers[3]; + this.signers = await getSigners(); }); - describe('EncryptedERC20', function () { - beforeEach(async function () { - const contract = await deployEncryptedERC20Fixture(); - this.contractAddress = await contract.getAddress(); - this.erc20 = contract; - const instances = await createInstances(this.contractAddress, ethers, this.signers); - this.instances = instances; - }); - - it('should mint the contract', async function () { - const encryptedAmount = this.instances.alice.encrypt32(1000); - const transaction = await this.erc20.mint(encryptedAmount); - await transaction.wait(); - // Call the method - const token = this.instances.alice.getTokenSignature(this.contractAddress) || { - signature: '', - publicKey: '', - }; - const encryptedBalance = await this.erc20.balanceOf(token.publicKey, token.signature); - // Decrypt the balance - const balance = this.instances.alice.decrypt(this.contractAddress, encryptedBalance); - expect(balance).to.equal(1000); + beforeEach(async function () { + const contract = await deployEncryptedERC20Fixture(); + this.contractAddress = await contract.getAddress(); + this.erc20 = contract; + this.instances = await createInstances(this.contractAddress, ethers, this.signers); + }); - const encryptedTotalSupply = await this.erc20.getTotalSupply(token.publicKey, token.signature); - // Decrypt the total supply - const totalSupply = this.instances.alice.decrypt(this.contractAddress, encryptedTotalSupply); - expect(totalSupply).to.equal(1000); - }); + it('should mint the contract', async function () { + const encryptedAmount = this.instances.alice.encrypt32(1000); + const transaction = await this.erc20.mint(encryptedAmount); + await transaction.wait(); + // Call the method + const token = this.instances.alice.getTokenSignature(this.contractAddress) || { + signature: '', + publicKey: '', + }; + const encryptedBalance = await this.erc20.balanceOf(token.publicKey, token.signature); + // Decrypt the balance + const balance = this.instances.alice.decrypt(this.contractAddress, encryptedBalance); + expect(balance).to.equal(1000); + + const encryptedTotalSupply = await this.erc20.getTotalSupply(token.publicKey, token.signature); + // Decrypt the total supply + const totalSupply = this.instances.alice.decrypt(this.contractAddress, encryptedTotalSupply); + expect(totalSupply).to.equal(1000); + }); - it('should transfer tokens between two users', async function () { - const encryptedAmount = this.instances.alice.encrypt32(10000); - const transaction = await this.erc20.mint(encryptedAmount); - await transaction.wait(); + it('should transfer tokens between two users', async function () { + const encryptedAmount = this.instances.alice.encrypt32(10000); + const transaction = await this.erc20.mint(encryptedAmount); + await transaction.wait(); - const encryptedTransferAmount = this.instances.alice.encrypt32(1337); - const tx = await this.erc20['transfer(address,bytes)'](this.signers.bob.address, encryptedTransferAmount); - await tx.wait(); + const encryptedTransferAmount = this.instances.alice.encrypt32(1337); + const tx = await this.erc20['transfer(address,bytes)'](this.signers.bob.address, encryptedTransferAmount); + await tx.wait(); - const tokenAlice = this.instances.alice.getTokenSignature(this.contractAddress)!; + const tokenAlice = this.instances.alice.getTokenSignature(this.contractAddress)!; - const encryptedBalanceAlice = await this.erc20.balanceOf(tokenAlice.publicKey, tokenAlice.signature); + const encryptedBalanceAlice = await this.erc20.balanceOf(tokenAlice.publicKey, tokenAlice.signature); - // Decrypt the balance - const balanceAlice = this.instances.alice.decrypt(this.contractAddress, encryptedBalanceAlice); + // Decrypt the balance + const balanceAlice = this.instances.alice.decrypt(this.contractAddress, encryptedBalanceAlice); - expect(balanceAlice).to.equal(10000 - 1337); + expect(balanceAlice).to.equal(10000 - 1337); - const bobErc20 = this.erc20.connect(this.signers.bob); + const bobErc20 = this.erc20.connect(this.signers.bob); - const tokenBob = this.instances.bob.getTokenSignature(this.contractAddress)!; + const tokenBob = this.instances.bob.getTokenSignature(this.contractAddress)!; - const encryptedBalanceBob = await bobErc20.balanceOf(tokenBob.publicKey, tokenBob.signature); + const encryptedBalanceBob = await bobErc20.balanceOf(tokenBob.publicKey, tokenBob.signature); - // Decrypt the balance - const balanceBob = this.instances.bob.decrypt(this.contractAddress, encryptedBalanceBob); + // Decrypt the balance + const balanceBob = this.instances.bob.decrypt(this.contractAddress, encryptedBalanceBob); - expect(balanceBob).to.equal(1337); - }); + expect(balanceBob).to.equal(1337); }); }); diff --git a/test/instance.ts b/test/instance.ts index 2f14e159..77a8c1f1 100644 --- a/test/instance.ts +++ b/test/instance.ts @@ -2,7 +2,8 @@ import { Signer } from 'ethers'; import fhevmjs, { FhevmInstance } from 'fhevmjs'; import { ethers as hethers } from 'hardhat'; -import { FhevmInstances, Signers } from './types'; +import type { Signers } from './signers'; +import { FhevmInstances } from './types'; let publicKey: string; let chainId: number; diff --git a/test/signers.ts b/test/signers.ts new file mode 100644 index 00000000..5b0f36bc --- /dev/null +++ b/test/signers.ts @@ -0,0 +1,19 @@ +import type { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/dist/src/signer-with-address'; +import { ethers } from 'hardhat'; + +export interface Signers { + alice: SignerWithAddress; + bob: SignerWithAddress; + carol: SignerWithAddress; + dave: SignerWithAddress; +} + +export const getSigners = async (): Promise => { + const signers = await ethers.getSigners(); + return { + alice: signers[0], + bob: signers[1], + carol: signers[2], + dave: signers[3], + }; +}; diff --git a/test/tfheOperations/tfheOperations.fixture.ts b/test/tfheOperations/tfheOperations.fixture.ts index fb142a22..fcd4ec81 100644 --- a/test/tfheOperations/tfheOperations.fixture.ts +++ b/test/tfheOperations/tfheOperations.fixture.ts @@ -1,6 +1,6 @@ import { ethers } from 'hardhat'; -import type { TFHETestSuite } from '../../types/contracts/TFHETestSuite'; +import type { TFHETestSuite } from '../../types'; export async function deployTfheTestFixture(): Promise { const signers = await ethers.getSigners(); diff --git a/test/tfheOperations/tfheOperations.ts b/test/tfheOperations/tfheOperations.ts index 0e9a5df8..0d0ee00b 100644 --- a/test/tfheOperations/tfheOperations.ts +++ b/test/tfheOperations/tfheOperations.ts @@ -2,17 +2,12 @@ import { expect } from 'chai'; import { ethers } from 'hardhat'; import { createInstances } from '../instance'; -import type { Signers } from '../types'; +import { getSigners } from '../signers'; import { deployTfheTestFixture } from './tfheOperations.fixture'; describe('TFHE operations', function () { before(async function () { - this.signers = {} as Signers; - const signers = await ethers.getSigners(); - this.signers.alice = signers[0]; - this.signers.bob = signers[1]; - this.signers.carol = signers[2]; - this.signers.dave = signers[3]; + this.signers = await getSigners(); const contract = await deployTfheTestFixture(); this.contractAddress = await contract.getAddress(); diff --git a/test/types.ts b/test/types.ts index cca9fe45..af7710d3 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,7 +1,7 @@ -import type { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/dist/src/signer-with-address'; import type { FhevmInstance } from 'fhevmjs'; -import type { EncryptedERC20 } from '../types/contracts/EncryptedERC20'; +import { EncryptedERC20 } from '../types'; +import type { Signers } from './signers'; declare module 'mocha' { export interface Context { @@ -12,13 +12,6 @@ declare module 'mocha' { } } -export interface Signers { - alice: SignerWithAddress; - bob: SignerWithAddress; - carol: SignerWithAddress; - dave: SignerWithAddress; -} - export interface FhevmInstances { alice: FhevmInstance; bob: FhevmInstance;