diff --git a/codegen/testgen.ts b/codegen/testgen.ts index bd1d3b2a..034bdf75 100644 --- a/codegen/testgen.ts +++ b/codegen/testgen.ts @@ -64,7 +64,7 @@ export function generateTestCode(shards: OverloadShard[]): string { import { expect } from 'chai'; import { ethers } from 'hardhat'; import { createInstances } from '../instance'; - import type { Signers } from '../types'; + import { getSigners } from '../signers'; `); @@ -92,12 +92,7 @@ async function deployTfheTestFixture${os.shardNumber}(): 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.ts b/test/tfheOperations/tfheOperations.ts index fc59978a..67aba1eb 100644 --- a/test/tfheOperations/tfheOperations.ts +++ b/test/tfheOperations/tfheOperations.ts @@ -4,7 +4,7 @@ import { ethers } from 'hardhat'; import type { TFHETestSuite1 } from '../../types/contracts/tests/TFHETestSuite1'; import type { TFHETestSuite2 } from '../../types/contracts/tests/TFHETestSuite2'; import { createInstances } from '../instance'; -import type { Signers } from '../types'; +import { getSigners } from '../signers'; async function deployTfheTestFixture1(): Promise { const signers = await ethers.getSigners(); @@ -30,12 +30,7 @@ async function deployTfheTestFixture2(): Promise { 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 contract1 = await deployTfheTestFixture1(); this.contract1Address = await contract1.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;