Lock.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
  2. import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers";
  3. import { expect } from "chai";
  4. import { ethers } from "hardhat";
  5. import type { Signers } from "../types";
  6. import { deployLockFixture } from "./Lock.fixture";
  7. describe("Lock", function () {
  8. before(async function () {
  9. this.signers = {} as Signers;
  10. const signers = await ethers.getSigners();
  11. this.signers.admin = signers[0];
  12. this.loadFixture = loadFixture;
  13. });
  14. describe("Deployment", function () {
  15. beforeEach(async function () {
  16. const { lock, lock_address, unlockTime, owner, lockedAmount } = await this.loadFixture(deployLockFixture);
  17. this.lock = lock;
  18. this.lock_address = lock_address;
  19. this.unlockTime = unlockTime;
  20. this.owner = owner;
  21. this.lockedAmount = lockedAmount;
  22. });
  23. it("Should fail if the unlockTime is not in the future", async function () {
  24. // We don't use the fixture here because we want a different deployment
  25. const latestTime = await time.latest();
  26. const Lock = await ethers.getContractFactory("Lock");
  27. await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWithCustomError(Lock, "InvalidUnlockTime");
  28. });
  29. it("Should set the right unlockTime", async function () {
  30. expect(await this.lock.unlockTime()).to.equal(this.unlockTime);
  31. });
  32. it("Should set the right owner", async function () {
  33. expect(await this.lock.owner()).to.equal(this.owner.address);
  34. });
  35. it("Should receive and store the funds to lock", async function () {
  36. expect(await ethers.provider.getBalance(this.lock_address)).to.equal(this.lockedAmount);
  37. });
  38. });
  39. describe("Withdrawals", function () {
  40. beforeEach(async function () {
  41. const { lock, unlockTime, owner, lockedAmount, otherAccount } = await this.loadFixture(deployLockFixture);
  42. this.lock = lock;
  43. this.unlockTime = unlockTime;
  44. this.owner = owner;
  45. this.lockedAmount = lockedAmount;
  46. this.otherAccount = otherAccount;
  47. });
  48. describe("Validations", function () {
  49. it("Should revert with the right error if called too soon", async function () {
  50. await expect(this.lock.withdraw()).to.be.revertedWithCustomError(this.lock, "UnlockTimeNotReached");
  51. });
  52. it("Should revert with the right error if called from another account", async function () {
  53. // We can increase the time in Hardhat Network
  54. await time.increaseTo(this.unlockTime);
  55. // We use lock.connect() to send a transaction from another account
  56. await expect(this.lock.connect(this.otherAccount).withdraw()).to.be.revertedWithCustomError(
  57. this.lock,
  58. "NotOwner",
  59. );
  60. });
  61. it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
  62. // Transactions are sent using the first signer by default
  63. await time.increaseTo(this.unlockTime);
  64. await expect(this.lock.withdraw()).not.to.be.reverted;
  65. });
  66. });
  67. describe("Events", function () {
  68. it("Should emit an event on withdrawals", async function () {
  69. await time.increaseTo(this.unlockTime);
  70. await expect(this.lock.withdraw()).to.emit(this.lock, "Withdrawal").withArgs(this.lockedAmount, anyValue); // We accept any value as `when` arg
  71. });
  72. });
  73. describe("Transfers", function () {
  74. it("Should transfer the funds to the owner", async function () {
  75. await time.increaseTo(this.unlockTime);
  76. await expect(this.lock.withdraw()).to.changeEtherBalances(
  77. [this.owner, this.lock],
  78. [this.lockedAmount, -this.lockedAmount],
  79. );
  80. });
  81. });
  82. });
  83. });