Skip to content

Commit

Permalink
Merge branch 'main' into feature/test-codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zk committed Sep 1, 2023
2 parents 9884ed3 + dcefd56 commit 994cfb8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 82 deletions.
9 changes: 2 additions & 7 deletions codegen/testgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
`);

Expand Down Expand Up @@ -92,12 +92,7 @@ async function deployTfheTestFixture${os.shardNumber}(): Promise<TFHETestSuite${
res.push(`
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();
`);

Expand Down
8 changes: 4 additions & 4 deletions test/encryptedERC20/EncryptedERC20.fixture.ts
Original file line number Diff line number Diff line change
@@ -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<EncryptedERC20> {
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;
Expand Down
100 changes: 46 additions & 54 deletions test/encryptedERC20/EncryptedERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
3 changes: 2 additions & 1 deletion test/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions test/signers.ts
Original file line number Diff line number Diff line change
@@ -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<Signers> => {
const signers = await ethers.getSigners();
return {
alice: signers[0],
bob: signers[1],
carol: signers[2],
dave: signers[3],
};
};
9 changes: 2 additions & 7 deletions test/tfheOperations/tfheOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TFHETestSuite1> {
const signers = await ethers.getSigners();
Expand All @@ -30,12 +30,7 @@ async function deployTfheTestFixture2(): Promise<TFHETestSuite2> {

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();
Expand Down
11 changes: 2 additions & 9 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 994cfb8

Please sign in to comment.