Nonces.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. await this.nonces.$_useCheckedNonce(sender, currentNonce);
  33. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
  34. });
  35. it("increments only sender's nonce", async function () {
  36. const currentNonce = await this.nonces.nonces(sender);
  37. expect(currentNonce).to.be.bignumber.equal('0');
  38. expect(await this.nonces.nonces(other)).to.be.bignumber.equal('0');
  39. await this.nonces.$_useCheckedNonce(sender, currentNonce);
  40. expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
  41. expect(await this.nonces.nonces(other)).to.be.bignumber.equal('0');
  42. });
  43. it('reverts when nonce is not the expected', async function () {
  44. const currentNonce = await this.nonces.nonces(sender);
  45. await expectRevertCustomError(
  46. this.nonces.$_useCheckedNonce(sender, currentNonce.addn(1)),
  47. 'InvalidAccountNonce',
  48. [sender, currentNonce],
  49. );
  50. });
  51. });
  52. });