test: 🚧 Added fixtures for new test schema

This commit is contained in:
PedroCailleret
2022-12-03 01:17:48 -03:00
parent 4403541660
commit c69d82ccee
49 changed files with 931 additions and 1126 deletions

View File

@@ -38,7 +38,7 @@
// const ownerKey = await p2pix._castAddrToKey(owner.address);
// const wallet2Key = await p2pix._castAddrToKey(wallet2.address);
// // Verify values at deployment
// expect(
// await p2pix.callStatic.validBacenSigners(ownerKey),

View File

@@ -286,7 +286,7 @@
// p2pix.unlockExpired([lockID]),
// ).to.be.revertedWithCustomError(
// p2pix, P2PixErrors.NotExpired);
// await network.provider.send("evm_mine");
// await network.provider.send("evm_mine");
// await network.provider.send("evm_mine");
@@ -302,4 +302,4 @@
// ).to.be.revertedWithCustomError(
// p2pix, P2PixErrors.NotExpired);
// });
// });
// });

149
test/p2pix.test.ts Normal file
View File

@@ -0,0 +1,149 @@
import "@nomicfoundation/hardhat-chai-matchers";
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { BigNumber, Wallet } from "ethers";
import { ethers, network } from "hardhat";
import { MockToken, P2PIX, Reputation } from "../src/types";
import { P2PixErrors } from "./utils/errors";
import { p2pixFixture } from "./utils/fixtures";
describe("P2PIX", () => {
type WalletWithAddress = Wallet & SignerWithAddress;
// contract deployer/admin
let owner: WalletWithAddress;
// extra EOAs
let acc01: WalletWithAddress;
let acc02: WalletWithAddress;
let acc03: WalletWithAddress;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let res: any;
let p2pix: P2PIX; // Contract instance
let erc20: MockToken; // Token instance
let reputation: Reputation; // Reputation Interface instance;
const fundAmount: BigNumber =
ethers.utils.parseEther("10000");
const price: BigNumber = ethers.utils.parseEther("100");
const zero = ethers.constants.AddressZero;
before("Set signers and reset network", async () => {
[owner, acc01, acc02, acc03] =
await // eslint-disable-next-line @typescript-eslint/no-explicit-any
(ethers as any).getSigners();
await network.provider.send("hardhat_reset");
});
beforeEach("Load deployment fixtures", async () => {
({ erc20, p2pix, reputation } = await loadFixture(
p2pixFixture,
));
});
describe("Init", async () => {
it("P2PIX, Reputation and ERC20 should initialize", async () => {
await p2pix.deployed();
await erc20.deployed();
await reputation.deployed();
expect(p2pix).to.be.ok;
expect(erc20).to.be.ok;
expect(reputation).to.be.ok;
const ownerKey = await p2pix._castAddrToKey(
owner.address,
);
const acc01Key = await p2pix._castAddrToKey(
acc01.address,
);
// storage checks
expect(
await p2pix.callStatic.defaultLockBlocks(),
).to.eq(4);
expect(await p2pix.callStatic.reputation()).to.eq(
reputation.address,
);
expect(await p2pix.callStatic.depositCount()).to.eq(0);
expect(
await p2pix.callStatic.validBacenSigners(ownerKey),
).to.eq(true);
expect(
await p2pix.callStatic.validBacenSigners(acc01Key),
).to.eq(true);
expect(
await p2pix.callStatic.allowedERC20s(erc20.address),
).to.eq(true);
// event emission
await expect(await p2pix.deployTransaction)
.to.emit(p2pix, "OwnerUpdated")
.withArgs(zero, owner.address)
.and.to.emit(p2pix, "LockBlocksUpdated")
.withArgs(4)
.and.to.emit(p2pix, "ReputationUpdated")
.withArgs(reputation.address)
.and.to.emit(p2pix, "ValidSignersUpdated")
.withArgs([owner.address, acc01.address])
.and.to.emit(p2pix, "AllowedERC20Updated")
.withArgs(erc20.address, true);
});
it("accounts have been funded", async () => {
// can't be eq to fundAmount due to contract deployment cost
res = await ethers.provider.getBalance(owner.address);
expect(res.toString()).to.have.lengthOf(22);
// console.log(res); // lengthOf = 22
// console.log(fundAmount); // lengthOf = 23
// those should eq to hardhat prefunded account's value
expect(
await ethers.provider.getBalance(acc01.address),
).to.eq(fundAmount);
expect(
await ethers.provider.getBalance(acc02.address),
).to.eq(fundAmount);
expect(
await ethers.provider.getBalance(acc03.address),
).to.eq(fundAmount);
});
});
// each describe tests a set of functionalities of the contract's behavior
describe("Owner Functions", async () => {
it("should allow contract's balance withdraw", async () => {
const oldBal = await p2pix.provider.getBalance(
p2pix.address,
);
const tx1 = await acc01.sendTransaction({
to: p2pix.address,
value: price,
});
const newBal = await p2pix.provider.getBalance(
p2pix.address,
);
expect(tx1).to.be.ok;
expect(oldBal).to.eq(0);
expect(newBal).to.eq(price);
await expect(
p2pix.withdrawBalance(),
).to.changeEtherBalances(
[owner.address, p2pix.address],
[price, "-100000000000000000000"],
);
await expect(
p2pix.connect(acc01).withdrawBalance(),
).to.be.revertedWith(
P2PixErrors.UNAUTHORIZED,
);
});
});
});

View File

@@ -8,4 +8,5 @@ export enum P2PixErrors {
AlreadyReleased = "AlreadyReleased",
TxAlreadyUsed = "TxAlreadyUsed",
InvalidSigner = "InvalidSigner",
}
UNAUTHORIZED = "UNAUTHORIZED",
}

82
test/utils/fixtures.ts Normal file
View File

@@ -0,0 +1,82 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { Signer } from "ethers";
import { ethers } from "hardhat";
// import keccak256 from "keccak256";
// import { MerkleTree } from "merkletreejs";
import {
MockToken,
P2PIX,
Reputation,
} from "../../src/types";
// exported interfaces
export interface P2pixFixture {
p2pix: P2PIX;
reputation: Reputation;
erc20: MockToken;
// proof: string[];
// wrongProof: string[];
// merkleRoot: string;
}
// exported constants
export const getSignerAddrs = (
amount: number,
addrs: SignerWithAddress[],
): string[] => {
const signers: string[] = [];
const buffr = addrs.slice(0, amount);
for (let i = 0; i < amount; i++) {
signers.push(buffr[i].address);
}
return signers;
};
export const randomSigners = (amount: number): Signer[] => {
const signers: Signer[] = [];
for (let i = 0; i < amount; i++) {
signers.push(ethers.Wallet.createRandom());
}
return signers;
};
export const getError = (Error: string) =>
ethers.utils
.keccak256(ethers.utils.toUtf8Bytes(Error))
.slice(0, 10);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const padBuffer = (addr: any) => {
return Buffer.from(
addr.substr(2).padStart(32 * 2, 0),
"hex",
);
};
// exported async functions
export async function p2pixFixture(): Promise<P2pixFixture> {
const validSigners = getSignerAddrs(
2,
await ethers.getSigners(),
);
const Reputation = await ethers.getContractFactory(
"Reputation",
);
const reputation =
(await Reputation.deploy()) as Reputation;
const ERC20 = await ethers.getContractFactory("MockToken");
const erc20 = (await ERC20.deploy(
ethers.utils.parseEther("20000000"), // 20M
)) as MockToken;
const P2PIX = await ethers.getContractFactory("P2PIX");
const p2pix = (await P2PIX.deploy(
4,
validSigners,
reputation.address,
[erc20.address],
[true],
)) as P2PIX;
return { reputation, erc20, p2pix };
}