StorageSlot.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. async function fixture() {
  8. const [account] = await ethers.getSigners();
  9. const mock = await ethers.deployContract('StorageSlotMock');
  10. return { mock, account };
  11. }
  12. describe('StorageSlot', function () {
  13. beforeEach(async function () {
  14. Object.assign(this, await loadFixture(fixture));
  15. });
  16. for (const { type, value, zero } of [
  17. { type: 'Boolean', value: true, zero: false },
  18. { type: 'Address', value: generators.address(), zero: ethers.ZeroAddress },
  19. { type: 'Bytes32', value: generators.bytes32(), zero: ethers.ZeroHash },
  20. { type: 'String', value: 'lorem ipsum', zero: '' },
  21. { type: 'Bytes', value: generators.hexBytes(128), zero: '0x' },
  22. ]) {
  23. describe(`${type} storage slot`, function () {
  24. it('set', async function () {
  25. await this.mock.getFunction(`set${type}Slot`)(slot, value);
  26. });
  27. describe('get', function () {
  28. beforeEach(async function () {
  29. await this.mock.getFunction(`set${type}Slot`)(slot, value);
  30. });
  31. it('from right slot', async function () {
  32. expect(await this.mock.getFunction(`get${type}Slot`)(slot)).to.equal(value);
  33. });
  34. it('from other slot', async function () {
  35. expect(await this.mock.getFunction(`get${type}Slot`)(otherSlot)).to.equal(zero);
  36. });
  37. });
  38. });
  39. }
  40. for (const { type, value, zero } of [
  41. { type: 'String', value: 'lorem ipsum', zero: '' },
  42. { type: 'Bytes', value: generators.hexBytes(128), zero: '0x' },
  43. ]) {
  44. describe(`${type} storage pointer`, function () {
  45. it('set', async function () {
  46. await this.mock.getFunction(`set${type}Storage`)(slot, value);
  47. });
  48. describe('get', function () {
  49. beforeEach(async function () {
  50. await this.mock.getFunction(`set${type}Storage`)(slot, value);
  51. });
  52. it('from right slot', async function () {
  53. expect(await this.mock.getFunction(`${type.toLowerCase()}Map`)(slot)).to.equal(value);
  54. expect(await this.mock.getFunction(`get${type}Storage`)(slot)).to.equal(value);
  55. });
  56. it('from other slot', async function () {
  57. expect(await this.mock.getFunction(`${type.toLowerCase()}Map`)(otherSlot)).to.equal(zero);
  58. expect(await this.mock.getFunction(`get${type}Storage`)(otherSlot)).to.equal(zero);
  59. });
  60. });
  61. });
  62. }
  63. });