Lock.ts 4.2 KB

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