Nonces.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const [sender, other] = await ethers.getSigners();
  6. const mock = await ethers.deployContract('$Nonces');
  7. return { sender, other, mock };
  8. }
  9. describe('Nonces', function () {
  10. beforeEach(async function () {
  11. Object.assign(this, await loadFixture(fixture));
  12. });
  13. it('gets a nonce', async function () {
  14. expect(await this.mock.nonces(this.sender)).to.equal(0n);
  15. });
  16. describe('_useNonce', function () {
  17. it('increments a nonce', async function () {
  18. expect(await this.mock.nonces(this.sender)).to.equal(0n);
  19. await expect(await this.mock.$_useNonce(this.sender))
  20. .to.emit(this.mock, 'return$_useNonce')
  21. .withArgs(0n);
  22. expect(await this.mock.nonces(this.sender)).to.equal(1n);
  23. });
  24. it("increments only sender's nonce", async function () {
  25. expect(await this.mock.nonces(this.sender)).to.equal(0n);
  26. expect(await this.mock.nonces(this.other)).to.equal(0n);
  27. await this.mock.$_useNonce(this.sender);
  28. expect(await this.mock.nonces(this.sender)).to.equal(1n);
  29. expect(await this.mock.nonces(this.other)).to.equal(0n);
  30. });
  31. });
  32. describe('_useCheckedNonce', function () {
  33. it('increments a nonce', async function () {
  34. const currentNonce = await this.mock.nonces(this.sender);
  35. expect(currentNonce).to.equal(0n);
  36. await this.mock.$_useCheckedNonce(this.sender, currentNonce);
  37. expect(await this.mock.nonces(this.sender)).to.equal(1n);
  38. });
  39. it("increments only sender's nonce", async function () {
  40. const currentNonce = await this.mock.nonces(this.sender);
  41. expect(currentNonce).to.equal(0n);
  42. expect(await this.mock.nonces(this.other)).to.equal(0n);
  43. await this.mock.$_useCheckedNonce(this.sender, currentNonce);
  44. expect(await this.mock.nonces(this.sender)).to.equal(1n);
  45. expect(await this.mock.nonces(this.other)).to.equal(0n);
  46. });
  47. it('reverts when nonce is not the expected', async function () {
  48. const currentNonce = await this.mock.nonces(this.sender);
  49. await expect(this.mock.$_useCheckedNonce(this.sender, currentNonce + 1n))
  50. .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce')
  51. .withArgs(this.sender, currentNonce);
  52. });
  53. });
  54. });