Token.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // This is an example test file. Hardhat will run every *.js file in `test/`,
  2. // so feel free to add new ones.
  3. // Hardhat tests are normally written with Mocha and Chai.
  4. // We import Chai to use its asserting functions here.
  5. const { expect } = require("chai");
  6. // We use `loadFixture` to share common setups (or fixtures) between tests.
  7. // Using this simplifies your tests and makes them run faster, by taking
  8. // advantage or Hardhat Network's snapshot functionality.
  9. const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers");
  10. // `describe` is a Mocha function that allows you to organize your tests.
  11. // Having your tests organized makes debugging them easier. All Mocha
  12. // functions are available in the global scope.
  13. //
  14. // `describe` receives the name of a section of your test suite, and a
  15. // callback. The callback must define the tests of that section. This callback
  16. // can't be an async function.
  17. describe("Token contract", function () {
  18. // We define a fixture to reuse the same setup in every test. We use
  19. // loadFixture to run this setup once, snapshot that state, and reset Hardhat
  20. // Network to that snapshot in every test.
  21. async function deployTokenFixture() {
  22. // Get the ContractFactory and Signers here.
  23. const Token = await ethers.getContractFactory("Token");
  24. const [owner, addr1, addr2] = await ethers.getSigners();
  25. // To deploy our contract, we just have to call Token.deploy() and await
  26. // for it to be deployed(), which happens onces its transaction has been
  27. // mined.
  28. const hardhatToken = await Token.deploy();
  29. await hardhatToken.deployed();
  30. // Fixtures can return anything you consider useful for your tests
  31. return { Token, hardhatToken, owner, addr1, addr2 };
  32. }
  33. // You can nest describe calls to create subsections.
  34. describe("Deployment", function () {
  35. // `it` is another Mocha function. This is the one you use to define your
  36. // tests. It receives the test name, and a callback function.
  37. //
  38. // If the callback function is async, Mocha will `await` it.
  39. it("Should set the right owner", async function () {
  40. // We use loadFixture to setup our environment, and then assert that
  41. // things went well
  42. const { hardhatToken, owner } = await loadFixture(deployTokenFixture);
  43. // Expect receives a value and wraps it in an assertion object. These
  44. // objects have a lot of utility methods to assert values.
  45. // This test expects the owner variable stored in the contract to be
  46. // equal to our Signer's owner.
  47. expect(await hardhatToken.owner()).to.equal(owner.address);
  48. });
  49. it("Should assign the total supply of tokens to the owner", async function () {
  50. const { hardhatToken, owner } = await loadFixture(deployTokenFixture);
  51. const ownerBalance = await hardhatToken.balanceOf(owner.address);
  52. expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
  53. });
  54. });
  55. describe("Transactions", function () {
  56. it("Should transfer tokens between accounts", async function () {
  57. const { hardhatToken, owner, addr1, addr2 } = await loadFixture(deployTokenFixture);
  58. // Transfer 50 tokens from owner to addr1
  59. await expect(hardhatToken.transfer(addr1.address, 50))
  60. .to.changeTokenBalances(hardhatToken, [owner, addr1], [-50, 50]);
  61. // Transfer 50 tokens from addr1 to addr2
  62. // We use .connect(signer) to send a transaction from another account
  63. await expect(hardhatToken.connect(addr1).transfer(addr2.address, 50))
  64. .to.changeTokenBalances(hardhatToken, [addr1, addr2], [-50, 50]);
  65. });
  66. it("should emit Transfer events", async function () {
  67. const { hardhatToken, owner, addr1, addr2 } = await loadFixture(deployTokenFixture);
  68. // Transfer 50 tokens from owner to addr1
  69. await expect(hardhatToken.transfer(addr1.address, 50))
  70. .to.emit(hardhatToken, "Transfer").withArgs(owner.address, addr1.address, 50)
  71. // Transfer 50 tokens from addr1 to addr2
  72. // We use .connect(signer) to send a transaction from another account
  73. await expect(hardhatToken.connect(addr1).transfer(addr2.address, 50))
  74. .to.emit(hardhatToken, "Transfer").withArgs(addr1.address, addr2.address, 50)
  75. });
  76. it("Should fail if sender doesn't have enough tokens", async function () {
  77. const { hardhatToken, owner, addr1 } = await loadFixture(deployTokenFixture);
  78. const initialOwnerBalance = await hardhatToken.balanceOf(
  79. owner.address
  80. );
  81. // Try to send 1 token from addr1 (0 tokens) to owner (1000 tokens).
  82. // `require` will evaluate false and revert the transaction.
  83. await expect(
  84. hardhatToken.connect(addr1).transfer(owner.address, 1)
  85. ).to.be.revertedWith("Not enough tokens");
  86. // Owner balance shouldn't have changed.
  87. expect(await hardhatToken.balanceOf(owner.address)).to.equal(
  88. initialOwnerBalance
  89. );
  90. });
  91. });
  92. });