TransientSlot.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { generators } = require('../helpers/random');
  5. const slot = ethers.id('some.storage.slot');
  6. const otherSlot = ethers.id('some.other.storage.slot');
  7. // Non-value types are not supported by the `TransientSlot` library.
  8. const TYPES = [
  9. { name: 'Boolean', type: 'bool', value: true, zero: false },
  10. { name: 'Address', type: 'address', value: generators.address(), zero: generators.address.zero },
  11. { name: 'Bytes32', type: 'bytes32', value: generators.bytes32(), zero: generators.bytes32.zero },
  12. { name: 'Uint256', type: 'uint256', value: generators.uint256(), zero: generators.uint256.zero },
  13. { name: 'Int256', type: 'int256', value: generators.int256(), zero: generators.int256.zero },
  14. ];
  15. async function fixture() {
  16. return { mock: await ethers.deployContract('TransientSlotMock') };
  17. }
  18. describe('TransientSlot', function () {
  19. beforeEach(async function () {
  20. Object.assign(this, await loadFixture(fixture));
  21. });
  22. for (const { name, type, value, zero } of TYPES) {
  23. describe(`${type} transient slot`, function () {
  24. const load = `tload${name}(bytes32)`;
  25. const store = `tstore(bytes32,${type})`;
  26. const event = `${name}Value`;
  27. it('load', async function () {
  28. await expect(this.mock[load](slot)).to.emit(this.mock, event).withArgs(slot, zero);
  29. });
  30. it('store and load (2 txs)', async function () {
  31. await this.mock[store](slot, value);
  32. await expect(this.mock[load](slot)).to.emit(this.mock, event).withArgs(slot, zero);
  33. });
  34. it('store and load (batched)', async function () {
  35. await expect(
  36. this.mock.multicall([
  37. this.mock.interface.encodeFunctionData(store, [slot, value]),
  38. this.mock.interface.encodeFunctionData(load, [slot]),
  39. this.mock.interface.encodeFunctionData(load, [otherSlot]),
  40. ]),
  41. )
  42. .to.emit(this.mock, event)
  43. .withArgs(slot, value)
  44. .to.emit(this.mock, event)
  45. .withArgs(otherSlot, zero);
  46. await expect(this.mock[load](slot)).to.emit(this.mock, event).withArgs(slot, zero);
  47. });
  48. });
  49. }
  50. });