Skip to content

Commit

Permalink
Boilerplate created
Browse files Browse the repository at this point in the history
  • Loading branch information
dacarva committed Dec 16, 2024
1 parent bf1146d commit f6b6783
Showing 1 changed file with 1 addition and 71 deletions.
72 changes: 1 addition & 71 deletions packages/hardhat/test/PiggyBank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,123 +10,53 @@ describe("PiggyBank Contract", function () {
let lockUpPeriod: number;

Check failure on line 10 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

'lockUpPeriod' is defined but never used

beforeEach(async () => {

Check warning on line 12 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎⏎··`
// Get the signers (owner and a user)
[owner, user] = await ethers.getSigners();

// Deploy the PiggyBank contract with a lock-up period of 1 week
lockUpPeriod = 7 * 24 * 60 * 60; // 7 days in seconds
const PiggyBankFactory = await ethers.getContractFactory("PiggyBank");
piggyBank = await PiggyBankFactory.deploy(lockUpPeriod);
await piggyBank.waitForDeployment();

});

describe("Deployment", function () {
it("Should deploy with the correct owner and lock-up period", async function () {

Check warning on line 17 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
expect(await piggyBank.owner()).to.equal(owner.address);
expect(await piggyBank.lockUpPeriod()).to.equal(lockUpPeriod);
});
});

describe("Deposit", function () {
it("Should allow a user to deposit Ether", async function () {

Check warning on line 22 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
const depositAmount = ethers.parseEther("1.0");

// Use the deposit function instead of direct transfer
await piggyBank.connect(user).deposit({ value: depositAmount });

const depositBalance = await piggyBank.getDepositBalance(user.address);
expect(depositBalance).to.equal(depositAmount);
});

it("Should revert when trying to deposit 0 Ether", async function () {

Check warning on line 25 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
await expect(piggyBank.connect(user).deposit({ value: 0 }))
.to.be.revertedWith("Must send Ether to deposit");
});

it("Should emit a Deposited event when Ether is deposited", async function () {

Check warning on line 28 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
const depositAmount = ethers.parseEther("0.5");

await expect(piggyBank.connect(user).deposit({ value: depositAmount }))
.to.emit(piggyBank, "Deposited")
.withArgs(user.address, depositAmount);
});

it("Should update deposit time only on first deposit", async function () {

Check warning on line 31 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
const firstDeposit = ethers.parseEther("0.5");
const secondDeposit = ethers.parseEther("0.5");

// First deposit
await piggyBank.connect(user).deposit({ value: firstDeposit });
const firstDepositTime = (await ethers.provider.getBlock('latest'))!.timestamp;

// Second deposit
await piggyBank.connect(user).deposit({ value: secondDeposit });

const deposit = await piggyBank.deposits(user.address);
expect(deposit.depositTime).to.equal(firstDepositTime);
expect(deposit.amount).to.equal(firstDeposit + secondDeposit);
});
});

describe("Withdraw", function () {
beforeEach(async function () {

Check warning on line 36 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
// Setup: User deposits some Ether
await piggyBank.connect(user).deposit({ value: ethers.parseEther("1.0") });
});

it("Should allow withdrawal after lock-up period", async function () {

Check warning on line 39 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
// Fast-forward time
await ethers.provider.send("evm_increaseTime", [lockUpPeriod]);
await ethers.provider.send("evm_mine", []);

await expect(piggyBank.connect(user).withdraw())
.to.changeEtherBalance(user, ethers.parseEther("1.0"));
});

it("Should revert withdrawal before lock-up period", async function () {

Check warning on line 42 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
await expect(piggyBank.connect(user).withdraw())
.to.be.revertedWith("Funds are still locked");
});

it("Should revert withdrawal with no deposit", async function () {

Check warning on line 45 in packages/hardhat/test/PiggyBank.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, lts/*)

Delete `⏎····`
const newUser = (await ethers.getSigners())[2];
await expect(piggyBank.connect(newUser).withdraw())
.to.be.revertedWith("No funds to withdraw");
});

it("Should correctly report unlock time", async function () {
const depositTime = (await ethers.provider.getBlock('latest'))!.timestamp;
const expectedUnlockTime = depositTime + lockUpPeriod;
expect(await piggyBank.connect(user).getUnlockTime())
.to.equal(expectedUnlockTime);
});
});

describe("Owner Functions", function () {
it("Should allow only owner to change lock-up period", async function () {
const newPeriod = 30 * 24 * 60 * 60; // 30 days
await expect(piggyBank.connect(user).setLockUpPeriod(newPeriod))
.to.be.revertedWith("Not the contract owner");

await piggyBank.connect(owner).setLockUpPeriod(newPeriod);
expect(await piggyBank.lockUpPeriod()).to.equal(newPeriod);
});

it("Should allow only owner to withdraw funds", async function () {
const amount = ethers.parseEther("1.0");
await piggyBank.connect(user).deposit({ value: amount });

await expect(piggyBank.connect(user).ownerWithdraw(amount))
.to.be.revertedWith("Not the contract owner");

await expect(piggyBank.connect(owner).ownerWithdraw(amount))
.to.changeEtherBalance(owner, amount);
});

it("Should revert owner withdrawal if insufficient balance", async function () {
const amount = ethers.parseEther("1.0");
await expect(piggyBank.connect(owner).ownerWithdraw(amount))
.to.be.revertedWith("Insufficient balance in the contract");
});
});
});

0 comments on commit f6b6783

Please sign in to comment.