Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Nov 29, 2024
1 parent 36841f2 commit 0e60f43
Show file tree
Hide file tree
Showing 5 changed files with 612 additions and 6 deletions.
13 changes: 7 additions & 6 deletions contracts/token/ERC20/EncryptedWETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { EncryptedERC20 } from "./EncryptedERC20.sol";
import "fhevm/lib/TFHE.sol";
import "fhevm/gateway/GatewayCaller.sol";

import "hardhat/console.sol";

/**
* @title EncryptedWETH
* @notice This contract allows users to wrap/unwrap trustlessly
Expand Down Expand Up @@ -63,6 +61,13 @@ abstract contract EncryptedWETH is EncryptedERC20, GatewayCaller {
wrap();
}

/**
* @notice Receive function calls wrap().
*/
receive() external payable {
wrap();
}

/**
* @notice Unwrap EncryptedERC20 tokens to ether.
* @param amount Amount to unwrap.
Expand Down Expand Up @@ -115,8 +120,6 @@ abstract contract EncryptedWETH is EncryptedERC20, GatewayCaller {
UnwrapRequest memory unwrapRequest = unwrapRequests[requestId];
delete unwrapRequests[requestId];

console.log(canUnwrap);

if (canUnwrap) {
_unsafeBurn(unwrapRequest.account, TFHE.asEuint64(unwrapRequest.amount));
_totalSupply -= unwrapRequest.amount;
Expand All @@ -133,10 +136,8 @@ abstract contract EncryptedWETH is EncryptedERC20, GatewayCaller {
}

emit Unwrap(unwrapRequest.account, unwrapRequest.amount);
console.log("YES");
} else {
emit UnwrapFail(unwrapRequest.account, unwrapRequest.amount);
console.log("FAIL");
}

delete isAccountRestricted[unwrapRequest.account];
Expand Down
43 changes: 43 additions & 0 deletions test/encryptedERC20/EncryptedERC20Wrapped.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ethers } from "hardhat";

import type { ERC20Mintable, EncryptedERC20Wrapped, TestEncryptedERC20Wrapped } from "../../types";
import { Signers } from "../signers";

export async function deployERC20AndEncryptedERC20WrappedFixture(
signers: Signers,
name: string,
symbol: string,
decimals: number,
): Promise<[ERC20Mintable, TestEncryptedERC20Wrapped]> {
const contractFactoryERC20Mintable = await ethers.getContractFactory("ERC20Mintable");
const contractERC20 = await contractFactoryERC20Mintable
.connect(signers.alice)
.deploy(name, symbol, decimals, signers.alice.address);
await contractERC20.waitForDeployment();

const contractFactoryEncryptedERC20Wrapped = await ethers.getContractFactory("TestEncryptedERC20Wrapped");
const contractEncryptedERC20Wrapped = await contractFactoryEncryptedERC20Wrapped
.connect(signers.alice)
.deploy(contractERC20.getAddress());
await contractEncryptedERC20Wrapped.waitForDeployment();

return [contractERC20, contractEncryptedERC20Wrapped];
}

export async function mintAndWrap(
signers: Signers,
user: string,
plainToken: ERC20Mintable,
token: EncryptedERC20Wrapped,
tokenAddress: string,
amount: bigint,
): Promise<void> {
let tx = await plainToken.connect(signers[user as keyof Signers]).mint(amount);
await tx.wait();

tx = await plainToken.connect(signers[user as keyof Signers]).approve(tokenAddress, amount);
await tx.wait();

tx = await token.connect(signers[user as keyof Signers]).wrap(amount);
await tx.wait();
}
Loading

0 comments on commit 0e60f43

Please sign in to comment.