Nonces.test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const expectEvent = require('@openzeppelin/test-helpers/src/expectEvent');
  2. const { expectRevertCustomError } = require('../helpers/customError');
  3. require('@openzeppelin/test-helpers');
  4. const Nonces = artifacts.require('$Nonces');
  5. contract('Nonces', function (accounts) {
  6. const [sender, other] = accounts;
  7. beforeEach(async function () {
  8. this.nonces = await Nonces.new();
  9. });
  10. it('gets a nonce', async function () {
  11. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('0');
  12. });
  13. describe('_useNonce', function () {
  14. it('increments a nonce', async function () {
  15. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('0');
  16. const { receipt } = await this.nonces.$_useNonce(sender);
  17. expectEvent(receipt, 'return$_useNonce', ['0']);
  18. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
  19. });
  20. it("increments only sender's nonce", async function () {
  21. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('0');
  22. expect(await this.nonces.nonces(other)).to.be.bignumber.equal('0');
  23. await this.nonces.$_useNonce(sender);
  24. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
  25. expect(await this.nonces.nonces(other)).to.be.bignumber.equal('0');
  26. });
  27. });
  28. describe('_useCheckedNonce', function () {
  29. it('increments a nonce', async function () {
  30. const currentNonce = await this.nonces.nonces(sender);
  31. expect(currentNonce).to.be.bignumber.equal('0');
  32. const { receipt } = await this.nonces.$_useCheckedNonce(sender, currentNonce);
  33. expectEvent(receipt, 'return$_useCheckedNonce', [currentNonce]);
  34. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
  35. });
  36. it("increments only sender's nonce", async function () {
  37. const currentNonce = await this.nonces.nonces(sender);
  38. expect(currentNonce).to.be.bignumber.equal('0');
  39. expect(await this.nonces.nonces(other)).to.be.bignumber.equal('0');
  40. await this.nonces.$_useCheckedNonce(sender, currentNonce);
  41. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
  42. expect(await this.nonces.nonces(other)).to.be.bignumber.equal('0');
  43. });
  44. it('reverts when nonce is not the expected', async function () {
  45. const currentNonce = await this.nonces.nonces(sender);
  46. await expectRevertCustomError(
  47. this.nonces.$_useCheckedNonce(sender, currentNonce.addn(1)),
  48. 'InvalidAccountNonce',
  49. [sender, currentNonce],
  50. );
  51. });
  52. });
  53. });