StorageSlot.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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: generators.address.zero },
  19. { type: 'Bytes32', value: generators.bytes32(), zero: generators.bytes32.zero },
  20. { type: 'Uint256', value: generators.uint256(), zero: generators.uint256.zero },
  21. { type: 'Int256', value: generators.int256(), zero: generators.int256.zero },
  22. { type: 'Bytes', value: generators.hexBytes(128), zero: generators.hexBytes.zero },
  23. { type: 'String', value: 'lorem ipsum', zero: '' },
  24. ]) {
  25. describe(`${type} storage slot`, function () {
  26. it('set', async function () {
  27. await this.mock.getFunction(`set${type}Slot`)(slot, value);
  28. });
  29. describe('get', function () {
  30. beforeEach(async function () {
  31. await this.mock.getFunction(`set${type}Slot`)(slot, value);
  32. });
  33. it('from right slot', async function () {
  34. expect(await this.mock.getFunction(`get${type}Slot`)(slot)).to.equal(value);
  35. });
  36. it('from other slot', async function () {
  37. expect(await this.mock.getFunction(`get${type}Slot`)(otherSlot)).to.equal(zero);
  38. });
  39. });
  40. });
  41. }
  42. for (const { type, value, zero } of [
  43. { type: 'String', value: 'lorem ipsum', zero: '' },
  44. { type: 'Bytes', value: generators.hexBytes(128), zero: '0x' },
  45. ]) {
  46. describe(`${type} storage pointer`, function () {
  47. it('set', async function () {
  48. await this.mock.getFunction(`set${type}Storage`)(slot, value);
  49. });
  50. describe('get', function () {
  51. beforeEach(async function () {
  52. await this.mock.getFunction(`set${type}Storage`)(slot, value);
  53. });
  54. it('from right slot', async function () {
  55. expect(await this.mock.getFunction(`${type.toLowerCase()}Map`)(slot)).to.equal(value);
  56. expect(await this.mock.getFunction(`get${type}Storage`)(slot)).to.equal(value);
  57. });
  58. it('from other slot', async function () {
  59. expect(await this.mock.getFunction(`${type.toLowerCase()}Map`)(otherSlot)).to.equal(zero);
  60. expect(await this.mock.getFunction(`get${type}Storage`)(otherSlot)).to.equal(zero);
  61. });
  62. });
  63. });
  64. }
  65. });