StorageSlot.test.js 2.9 KB

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