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 a6fa74f commit 0a9df24
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 44 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
21 changes: 14 additions & 7 deletions test/governance/Comp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { deployCompFixture, reencryptCurrentVotes, reencryptPriorVotes } from ".
import { delegateBySig } from "./DelegateBySig";

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

const NULL_ADDRESS = "0x0000000000000000000000000000000000000000";

before(async function () {
await initSigners(3);
this.signers = await getSigners();
Expand All @@ -36,7 +41,7 @@ describe("Comp", function () {
encryptedTransferAmount.inputProof,
);

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

// Decrypt Alice's balance
expect(await reencryptBalance(this.signers, this.instances, "alice", this.comp, this.compAddress)).to.equal(
Expand All @@ -51,7 +56,7 @@ describe("Comp", function () {

it("can delegate tokens on-chain", async function () {
const tx = await this.comp.connect(this.signers.alice).delegate(this.signers.bob.address);
await tx.wait();
await expect(tx).to.emit(this.comp, "DelegateChanged").withArgs(this.signers.alice, NULL_ADDRESS, this.signers.bob);

const latestBlockNumber = await ethers.provider.getBlockNumber();
await waitNBlocks(1);
Expand All @@ -73,12 +78,13 @@ describe("Comp", function () {
let latestBlockNumber = await ethers.provider.getBlockNumber();
const block = await ethers.provider.getBlock(latestBlockNumber);
const expiry = block!.timestamp + 100;
const signature = await delegateBySig(delegator, delegatee.address, this.comp, nonce, expiry, false);
const signature = await delegateBySig(delegator, delegatee.address, this.comp, nonce, expiry);

const tx = await this.comp
.connect(this.signers.alice)
.delegateBySig(delegator, delegatee, nonce, expiry, signature);
await tx.wait();

await expect(tx).to.emit(this.comp, "DelegateChanged").withArgs(this.signers.alice, NULL_ADDRESS, this.signers.bob);

latestBlockNumber = await ethers.provider.getBlockNumber();
await waitNBlocks(1);
Expand Down Expand Up @@ -154,7 +160,8 @@ describe("Comp", function () {
const signature = await delegateBySig(delegator, delegatee.address, this.comp, nonce, expiry);

const tx = await this.comp.connect(delegator).incrementNonce();
await tx.wait();
// @dev the newNonce is 1
await expect(tx).to.emit(this.comp, "NonceIncremented").withArgs(delegator, BigInt("1"));

// Cannot reuse same nonce when delegating by sig
await expect(this.comp.delegateBySig(delegator, delegatee, nonce, expiry, signature)).to.be.revertedWithCustomError(
Expand Down Expand Up @@ -202,7 +209,7 @@ describe("Comp", function () {
);

const tx = await this.comp.connect(this.signers.alice).setGovernor(this.signers.bob);
await tx.wait();
await expect(tx).to.emit(this.comp, "NewGovernor").withArgs(this.signers.bob);

blockNumber = await ethers.provider.getBlockNumber();

Expand Down Expand Up @@ -393,7 +400,7 @@ describe("Comp", function () {
it("governor address can access votes for any account", async function () {
// Bob becomes the governor address.
let tx = await this.comp.connect(this.signers.alice).setGovernor(this.signers.bob.address);
await tx.wait();
await expect(tx).to.emit(this.comp, "NewGovernor").withArgs(this.signers.bob);

// Alice delegates her votes to Carol.
tx = await this.comp.connect(this.signers.alice).delegate(this.signers.carol.address);
Expand Down

0 comments on commit 0a9df24

Please sign in to comment.