Skip to content

Commit

Permalink
test: events for EncryptedERC20 contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Nov 29, 2024
1 parent 2238f15 commit d1a19b4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 37 deletions.
36 changes: 26 additions & 10 deletions test/encryptedERC20/EncryptedERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { getSigners, initSigners } from "../signers";
import { deployEncryptedERC20Fixture, reencryptAllowance, reencryptBalance } from "./EncryptedERC20.fixture";

describe("EncryptedERC20", function () {
// @dev The placeholder is type(uint256).max --> 2**256 - 1.
const PLACEHOLDER = BigInt(2) ** BigInt(256) - BigInt(1);

before(async function () {
await initSigners(2);
this.signers = await getSigners();
Expand All @@ -27,7 +30,7 @@ describe("EncryptedERC20", function () {
it("should mint the contract", async function () {
const mintAmount = 1000;
const tx = await this.encryptedERC20.connect(this.signers.alice).mint(mintAmount);
await tx.wait();
await expect(tx).to.emit(this.encryptedERC20, "Mint").withArgs(this.signers.alice, mintAmount);

expect(
await reencryptBalance(this.signers, this.instances, "alice", this.encryptedERC20, this.encryptedERC20Address),
Expand All @@ -47,13 +50,15 @@ describe("EncryptedERC20", function () {
input.add64(transferAmount);
const encryptedTransferAmount = await input.encrypt();

tx = await this.encryptedERC20["transfer(address,bytes32,bytes)"](
this.signers.bob.address,
encryptedTransferAmount.handles[0],
encryptedTransferAmount.inputProof,
);
tx = await this.encryptedERC20
.connect(this.signers.alice)
[
"transfer(address,bytes32,bytes)"
](this.signers.bob.address, encryptedTransferAmount.handles[0], encryptedTransferAmount.inputProof);

await tx.wait();
await expect(tx)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, PLACEHOLDER);

// Decrypt Alice's balance
expect(
Expand All @@ -78,12 +83,17 @@ describe("EncryptedERC20", function () {
const input = this.instances.alice.createEncryptedInput(this.encryptedERC20Address, this.signers.alice.address);
input.add64(transferAmount);
const encryptedTransferAmount = await input.encrypt();

tx = await this.encryptedERC20["transfer(address,bytes32,bytes)"](
this.signers.bob.address,
encryptedTransferAmount.handles[0],
encryptedTransferAmount.inputProof,
);
await tx.wait();

// @dev There is no error-handling in this version of EncryptedERC20.
await expect(tx)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, PLACEHOLDER);

// Decrypt Alice's balance
expect(
Expand Down Expand Up @@ -117,7 +127,10 @@ describe("EncryptedERC20", function () {
encryptedAllowanceAmount.handles[0],
encryptedAllowanceAmount.inputProof,
);
await tx.wait();

await expect(tx)
.to.emit(this.encryptedERC20, "Approval")
.withArgs(this.signers.alice, this.signers.bob, PLACEHOLDER);

// @dev The allowance amount is set to be equal to the transfer amount.
expect(
Expand All @@ -142,7 +155,10 @@ describe("EncryptedERC20", function () {
encryptedTransferAmount.handles[0],
encryptedTransferAmount.inputProof,
);
await tx2.wait();

await expect(tx2)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, PLACEHOLDER);

// Decrypt Alice's balance
expect(
Expand Down
78 changes: 51 additions & 27 deletions test/encryptedERC20/EncryptedERC20WithErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { reencryptAllowance, reencryptBalance } from "./EncryptedERC20.fixture";
import { checkErrorCode, deployEncryptedERC20WithErrorsFixture } from "./EncryptedERC20WithErrors.fixture";

describe("EncryptedERC20WithErrors", function () {
const DEFAULT_TRANSFER_ID = BigInt(0);
const DEFAULT_SECOND_TRANSFER_ID = BigInt(1);
// @dev The placeholder is type(uint256).max --> 2**256 - 1.
const PLACEHOLDER = BigInt(2) ** BigInt(256) - BigInt(1);

before(async function () {
await initSigners(2);
Expand All @@ -31,7 +31,7 @@ describe("EncryptedERC20WithErrors", function () {
it("should mint the contract", async function () {
const mintAmount = 1000;
const tx = await this.encryptedERC20.connect(this.signers.alice).mint(mintAmount);
await tx.wait();
await expect(tx).to.emit(this.encryptedERC20, "Mint").withArgs(this.signers.alice, mintAmount);

expect(
await reencryptBalance(this.signers, this.instances, "alice", this.encryptedERC20, this.encryptedERC20Address),
Expand All @@ -43,6 +43,7 @@ describe("EncryptedERC20WithErrors", function () {
it("should transfer tokens between two users", async function () {
const mintAmount = 10_000;
const transferAmount = 1337;
const expectedTransferId = BigInt(0);

let tx = await this.encryptedERC20.connect(this.signers.alice).mint(mintAmount);
await tx.wait();
Expand All @@ -57,7 +58,9 @@ describe("EncryptedERC20WithErrors", function () {
"transfer(address,bytes32,bytes)"
](this.signers.bob.address, encryptedTransferAmount.handles[0], encryptedTransferAmount.inputProof);

await tx.wait();
await expect(tx)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, expectedTransferId);

// Decrypt Alice's balance
expect(
Expand All @@ -75,7 +78,7 @@ describe("EncryptedERC20WithErrors", function () {
this.signers,
this.instances,
"alice",
DEFAULT_TRANSFER_ID,
expectedTransferId,
this.encryptedERC20,
this.encryptedERC20Address,
),
Expand All @@ -87,7 +90,7 @@ describe("EncryptedERC20WithErrors", function () {
this.signers,
this.instances,
"bob",
DEFAULT_TRANSFER_ID,
expectedTransferId,
this.encryptedERC20,
this.encryptedERC20Address,
),
Expand All @@ -99,19 +102,24 @@ describe("EncryptedERC20WithErrors", function () {
// amount.
const mintAmount = 1000;
const transferAmount = 1337;
const expectedTransferId = BigInt(0);

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);
input.add64(transferAmount);
const encryptedTransferAmount = await input.encrypt();

tx = await this.encryptedERC20["transfer(address,bytes32,bytes)"](
this.signers.bob.address,
encryptedTransferAmount.handles[0],
encryptedTransferAmount.inputProof,
);
await tx.wait();

await expect(tx)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, expectedTransferId);

// Decrypt Alice's balance
expect(
Expand All @@ -129,7 +137,7 @@ describe("EncryptedERC20WithErrors", function () {
this.signers,
this.instances,
"bob",
DEFAULT_TRANSFER_ID,
expectedTransferId,
this.encryptedERC20,
this.encryptedERC20Address,
),
Expand Down Expand Up @@ -157,7 +165,10 @@ describe("EncryptedERC20WithErrors", function () {
encryptedAllowanceAmount.handles[0],
encryptedAllowanceAmount.inputProof,
);
await tx.wait();

await expect(tx)
.to.emit(this.encryptedERC20, "Approval")
.withArgs(this.signers.alice, this.signers.bob, PLACEHOLDER);

// @dev The allowance amount is set to be equal to the transfer amount.
expect(
Expand All @@ -171,18 +182,21 @@ describe("EncryptedERC20WithErrors", function () {
),
).to.equal(transferAmount);

const bobErc20 = this.encryptedERC20.connect(this.signers.bob);
const expectedTransferId1 = BigInt(0);

const inputBob1 = this.instances.bob.createEncryptedInput(this.encryptedERC20Address, this.signers.bob.address);
inputBob1.add64(transferAmount + 1); // above allowance so next tx should actually not send any token
const encryptedTransferAmount = await inputBob1.encrypt();

const tx2 = await bobErc20["transferFrom(address,address,bytes32,bytes)"](
this.signers.alice.address,
this.signers.bob.address,
encryptedTransferAmount.handles[0],
encryptedTransferAmount.inputProof,
);
await tx2.wait();
const tx2 = await this.encryptedERC20
.connect(this.signers.bob)
[
"transferFrom(address,address,bytes32,bytes)"
](this.signers.alice.address, this.signers.bob.address, encryptedTransferAmount.handles[0], encryptedTransferAmount.inputProof);

await expect(tx2)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, expectedTransferId1);

// Decrypt Alice's balance
expect(
Expand All @@ -200,23 +214,27 @@ describe("EncryptedERC20WithErrors", function () {
this.signers,
this.instances,
"bob",
DEFAULT_TRANSFER_ID,
expectedTransferId1,
this.encryptedERC20,
this.encryptedERC20Address,
),
).to.equal("UNSUFFICIENT_APPROVAL");

const expectedTransferId2 = BigInt(1);

const inputBob2 = this.instances.bob.createEncryptedInput(this.encryptedERC20Address, this.signers.bob.address);
inputBob2.add64(transferAmount); // below allowance so next tx should send token
const encryptedTransferAmount2 = await inputBob2.encrypt();

const tx3 = await bobErc20["transferFrom(address,address,bytes32,bytes)"](
this.signers.alice.address,
this.signers.bob.address,
encryptedTransferAmount2.handles[0],
encryptedTransferAmount2.inputProof,
);
await tx3.wait();
const tx3 = await await this.encryptedERC20
.connect(this.signers.bob)
[
"transferFrom(address,address,bytes32,bytes)"
](this.signers.alice.address, this.signers.bob.address, encryptedTransferAmount2.handles[0], encryptedTransferAmount2.inputProof);

await expect(tx3)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, expectedTransferId2);

// Decrypt Alice's balance
expect(
Expand Down Expand Up @@ -246,7 +264,7 @@ describe("EncryptedERC20WithErrors", function () {
this.signers,
this.instances,
"bob",
DEFAULT_SECOND_TRANSFER_ID,
expectedTransferId2,
this.encryptedERC20,
this.encryptedERC20Address,
),
Expand Down Expand Up @@ -439,6 +457,8 @@ describe("EncryptedERC20WithErrors", function () {
it("cannot reencrypt errors if the account is not a participant of the transfer", async function () {
const mintAmount = 10_000;
const transferAmount = 1337;
const expectedTransferId = 0;

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

Expand All @@ -452,7 +472,11 @@ describe("EncryptedERC20WithErrors", function () {
"transfer(address,bytes32,bytes)"
](this.signers.bob.address, encryptedTransferAmount.handles[0], encryptedTransferAmount.inputProof);

const errorCodeHandle = await this.encryptedERC20.getErrorCodeForTransferId(DEFAULT_TRANSFER_ID);
await expect(tx)
.to.emit(this.encryptedERC20, "Transfer")
.withArgs(this.signers.alice, this.signers.bob, expectedTransferId);

const errorCodeHandle = await this.encryptedERC20.getErrorCodeForTransferId(expectedTransferId);

const { publicKey: publicKeyCarol, privateKey: privateKeyCarol } = this.instances.carol.generateKeypair();
const eip712Carol = this.instances.carol.createEIP712(publicKeyCarol, this.encryptedERC20Address);
Expand Down

0 comments on commit d1a19b4

Please sign in to comment.