|
| 1 | +import { |
| 2 | + time, |
| 3 | + loadFixture, |
| 4 | +} from "@nomicfoundation/hardhat-toolbox/network-helpers"; |
| 5 | +import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs"; |
| 6 | +import { expect } from "chai"; |
| 7 | +import { ethers } from "hardhat"; |
| 8 | + |
| 9 | +describe("Lock", function () { |
| 10 | + // We define a fixture to reuse the same setup in every test. |
| 11 | + // We use loadFixture to run this setup once, snapshot that state, |
| 12 | + // and reset Hardhat Network to that snapshot in every test. |
| 13 | + async function deployOneYearLockFixture() { |
| 14 | + const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60; |
| 15 | + const ONE_GWEI = 1_000_000_000; |
| 16 | + |
| 17 | + const lockedAmount = ONE_GWEI; |
| 18 | + const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS; |
| 19 | + |
| 20 | + // Contracts are deployed using the first signer/account by default |
| 21 | + const [owner, otherAccount] = await ethers.getSigners(); |
| 22 | + |
| 23 | + const Lock = await ethers.getContractFactory("Lock"); |
| 24 | + const lock = await Lock.deploy(unlockTime, { value: lockedAmount }); |
| 25 | + |
| 26 | + return { lock, unlockTime, lockedAmount, owner, otherAccount }; |
| 27 | + } |
| 28 | + |
| 29 | + describe("Deployment", function () { |
| 30 | + it("Should set the right unlockTime", async function () { |
| 31 | + const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture); |
| 32 | + |
| 33 | + expect(await lock.unlockTime()).to.equal(unlockTime); |
| 34 | + }); |
| 35 | + |
| 36 | + it("Should set the right owner", async function () { |
| 37 | + const { lock, owner } = await loadFixture(deployOneYearLockFixture); |
| 38 | + |
| 39 | + expect(await lock.owner()).to.equal(owner.address); |
| 40 | + }); |
| 41 | + |
| 42 | + it("Should receive and store the funds to lock", async function () { |
| 43 | + const { lock, lockedAmount } = await loadFixture( |
| 44 | + deployOneYearLockFixture |
| 45 | + ); |
| 46 | + |
| 47 | + expect(await ethers.provider.getBalance(lock.target)).to.equal( |
| 48 | + lockedAmount |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it("Should fail if the unlockTime is not in the future", async function () { |
| 53 | + // We don't use the fixture here because we want a different deployment |
| 54 | + const latestTime = await time.latest(); |
| 55 | + const Lock = await ethers.getContractFactory("Lock"); |
| 56 | + await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith( |
| 57 | + "Unlock time should be in the future" |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("Withdrawals", function () { |
| 63 | + describe("Validations", function () { |
| 64 | + it("Should revert with the right error if called too soon", async function () { |
| 65 | + const { lock } = await loadFixture(deployOneYearLockFixture); |
| 66 | + |
| 67 | + await expect(lock.withdraw()).to.be.revertedWith( |
| 68 | + "You can't withdraw yet" |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("Should revert with the right error if called from another account", async function () { |
| 73 | + const { lock, unlockTime, otherAccount } = await loadFixture( |
| 74 | + deployOneYearLockFixture |
| 75 | + ); |
| 76 | + |
| 77 | + // We can increase the time in Hardhat Network |
| 78 | + await time.increaseTo(unlockTime); |
| 79 | + |
| 80 | + // We use lock.connect() to send a transaction from another account |
| 81 | + await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith( |
| 82 | + "You aren't the owner" |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () { |
| 87 | + const { lock, unlockTime } = await loadFixture( |
| 88 | + deployOneYearLockFixture |
| 89 | + ); |
| 90 | + |
| 91 | + // Transactions are sent using the first signer by default |
| 92 | + await time.increaseTo(unlockTime); |
| 93 | + |
| 94 | + await expect(lock.withdraw()).not.to.be.reverted; |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("Events", function () { |
| 99 | + it("Should emit an event on withdrawals", async function () { |
| 100 | + const { lock, unlockTime, lockedAmount } = await loadFixture( |
| 101 | + deployOneYearLockFixture |
| 102 | + ); |
| 103 | + |
| 104 | + await time.increaseTo(unlockTime); |
| 105 | + |
| 106 | + await expect(lock.withdraw()) |
| 107 | + .to.emit(lock, "Withdrawal") |
| 108 | + .withArgs(lockedAmount, anyValue); // We accept any value as `when` arg |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("Transfers", function () { |
| 113 | + it("Should transfer the funds to the owner", async function () { |
| 114 | + const { lock, unlockTime, lockedAmount, owner } = await loadFixture( |
| 115 | + deployOneYearLockFixture |
| 116 | + ); |
| 117 | + |
| 118 | + await time.increaseTo(unlockTime); |
| 119 | + |
| 120 | + await expect(lock.withdraw()).to.changeEtherBalances( |
| 121 | + [owner, lock], |
| 122 | + [lockedAmount, -lockedAmount] |
| 123 | + ); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments