Skip to content

Commit

Permalink
test: EncryptedERC20WithErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Oct 29, 2024
1 parent 6742fa1 commit b095982
Show file tree
Hide file tree
Showing 4 changed files with 534 additions and 6 deletions.
10 changes: 4 additions & 6 deletions test/encryptedERC20/EncryptedERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe("EncryptedERC20", function () {
expect(await this.encryptedERC20.totalSupply()).to.equal(0);
expect(await this.encryptedERC20.name()).to.equal("Naraggara");
expect(await this.encryptedERC20.symbol()).to.equal("NARA");
expect(await this.encryptedERC20.decimals()).to.be.eq(BigInt(6));
});

it("should mint the contract", async function () {
Expand All @@ -40,11 +41,9 @@ describe("EncryptedERC20", function () {
const transferAmount = 1337;

let tx = await this.encryptedERC20.connect(this.signers.alice).mint(mintAmount);
const t1 = await tx.wait();
expect(t1?.status).to.eq(1);
await tx.wait();

const input = this.instances.alice.createEncryptedInput(this.encryptedERC20Address, this.signers.alice.address);

input.add64(transferAmount);
const encryptedTransferAmount = await input.encrypt();

Expand All @@ -54,8 +53,7 @@ describe("EncryptedERC20", function () {
encryptedTransferAmount.inputProof,
);

const t2 = await tx.wait();
expect(t2?.status).to.eq(1);
await tx.wait();

// Decrypt Alice's balance
expect(
Expand All @@ -74,7 +72,7 @@ describe("EncryptedERC20", function () {
const mintAmount = 1000;
const transferAmount = 1337;

let tx = await this.encryptedERC20.mint(mintAmount);
let tx = await this.encryptedERC20.connect(this.signers.alice).mint(mintAmount);
await tx.wait();

const input = this.instances.alice.createEncryptedInput(this.encryptedERC20Address, this.signers.alice.address);
Expand Down
46 changes: 46 additions & 0 deletions test/encryptedERC20/EncryptedERC20WithErrors.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ethers } from "hardhat";

import type { EncryptedERC20WithErrorsMintable } from "../../types";
import { reencryptEuint8 } from "../reencrypt";
import { Signers } from "../signers";
import { FhevmInstances } from "../types";

export async function deployEncryptedERC20WithErrorsFixture(
signers: Signers,
name: string,
symbol: string,
owner: string,
): Promise<EncryptedERC20WithErrorsMintable> {
const contractFactory = await ethers.getContractFactory("EncryptedERC20WithErrorsMintable");
const contract = await contractFactory
.connect(signers[owner as keyof Signers])
.deploy(name, symbol, signers[owner as keyof Signers].address);
await contract.waitForDeployment();
return contract;
}

export async function checkErrorCode(
signers: Signers,
instances: FhevmInstances,
user: string,
transferId: bigint,
token: EncryptedERC20WithErrorsMintable,
tokenAddress: string,
): Promise<string> {
const errorCodeHandle = await token.getErrorCodeForTransferId(transferId);
const errorCode = await reencryptEuint8(signers, instances, user, errorCodeHandle, tokenAddress);
switch (errorCode) {
case BigInt(0): {
return "NO_ERROR";
}
case BigInt(1): {
return "UNSUFFICIENT_BALANCE";
}
case BigInt(2): {
return "UNSUFFICIENT_APPROVAL";
}
default: {
throw "Error code is invalid";
}
}
}
Loading

0 comments on commit b095982

Please sign in to comment.