diff --git a/test/encryptedERC20/EncryptedERC20.test.ts b/test/encryptedERC20/EncryptedERC20.test.ts index 608d0ef..927acf2 100644 --- a/test/encryptedERC20/EncryptedERC20.test.ts +++ b/test/encryptedERC20/EncryptedERC20.test.ts @@ -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(); @@ -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), @@ -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( @@ -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( @@ -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( @@ -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( diff --git a/test/encryptedERC20/EncryptedERC20WithErrors.test.ts b/test/encryptedERC20/EncryptedERC20WithErrors.test.ts index 6ebb43c..50088d0 100644 --- a/test/encryptedERC20/EncryptedERC20WithErrors.test.ts +++ b/test/encryptedERC20/EncryptedERC20WithErrors.test.ts @@ -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); @@ -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), @@ -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(); @@ -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( @@ -75,7 +78,7 @@ describe("EncryptedERC20WithErrors", function () { this.signers, this.instances, "alice", - DEFAULT_TRANSFER_ID, + expectedTransferId, this.encryptedERC20, this.encryptedERC20Address, ), @@ -87,7 +90,7 @@ describe("EncryptedERC20WithErrors", function () { this.signers, this.instances, "bob", - DEFAULT_TRANSFER_ID, + expectedTransferId, this.encryptedERC20, this.encryptedERC20Address, ), @@ -99,6 +102,7 @@ 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(); @@ -106,12 +110,16 @@ describe("EncryptedERC20WithErrors", 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(); + + await expect(tx) + .to.emit(this.encryptedERC20, "Transfer") + .withArgs(this.signers.alice, this.signers.bob, expectedTransferId); // Decrypt Alice's balance expect( @@ -129,7 +137,7 @@ describe("EncryptedERC20WithErrors", function () { this.signers, this.instances, "bob", - DEFAULT_TRANSFER_ID, + expectedTransferId, this.encryptedERC20, this.encryptedERC20Address, ), @@ -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( @@ -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( @@ -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( @@ -246,7 +264,7 @@ describe("EncryptedERC20WithErrors", function () { this.signers, this.instances, "bob", - DEFAULT_SECOND_TRANSFER_ID, + expectedTransferId2, this.encryptedERC20, this.encryptedERC20Address, ), @@ -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(); @@ -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); diff --git a/test/governance/Comp.test.ts b/test/governance/Comp.test.ts index cc5def5..eb06c19 100644 --- a/test/governance/Comp.test.ts +++ b/test/governance/Comp.test.ts @@ -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(); @@ -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( @@ -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); @@ -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); @@ -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( @@ -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(); @@ -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);