Update docs
This commit is contained in:
@ -1,210 +1,73 @@
|
||||
const { constants, BN } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { generators } = require('../helpers/random');
|
||||
|
||||
const StorageSlotMock = artifacts.require('StorageSlotMock');
|
||||
const slot = ethers.id('some.storage.slot');
|
||||
const otherSlot = ethers.id('some.other.storage.slot');
|
||||
|
||||
const slot = web3.utils.keccak256('some.storage.slot');
|
||||
const otherSlot = web3.utils.keccak256('some.other.storage.slot');
|
||||
const TYPES = [
|
||||
{ name: 'Boolean', type: 'bool', value: true, isValueType: true, zero: false },
|
||||
{ name: 'Address', type: 'address', value: generators.address(), isValueType: true, zero: generators.address.zero },
|
||||
{ name: 'Bytes32', type: 'bytes32', value: generators.bytes32(), isValueType: true, zero: generators.bytes32.zero },
|
||||
{ name: 'Uint256', type: 'uint256', value: generators.uint256(), isValueType: true, zero: generators.uint256.zero },
|
||||
{ name: 'Int256', type: 'int256', value: generators.int256(), isValueType: true, zero: generators.int256.zero },
|
||||
{ name: 'Bytes', type: 'bytes', value: generators.hexBytes(128), isValueType: false, zero: generators.hexBytes.zero },
|
||||
{ name: 'String', type: 'string', value: 'lorem ipsum', isValueType: false, zero: '' },
|
||||
];
|
||||
|
||||
contract('StorageSlot', function (accounts) {
|
||||
async function fixture() {
|
||||
return { mock: await ethers.deployContract('StorageSlotMock') };
|
||||
}
|
||||
|
||||
describe('StorageSlot', function () {
|
||||
beforeEach(async function () {
|
||||
this.store = await StorageSlotMock.new();
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('boolean storage slot', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = true;
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setBoolean(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setBoolean(slot, this.value);
|
||||
for (const { name, type, value, zero } of TYPES) {
|
||||
describe(`${type} storage slot`, function () {
|
||||
it('set', async function () {
|
||||
await this.mock.getFunction(`set${name}Slot`)(slot, value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.getBoolean(slot)).to.be.equal(this.value);
|
||||
});
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.mock.getFunction(`set${name}Slot`)(slot, value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.getBoolean(otherSlot)).to.be.equal(false);
|
||||
it('from right slot', async function () {
|
||||
expect(await this.mock.getFunction(`get${name}Slot`)(slot)).to.equal(value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.mock.getFunction(`get${name}Slot`)(otherSlot)).to.equal(zero);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('address storage slot', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = accounts[1];
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setAddress(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setAddress(slot, this.value);
|
||||
for (const { name, type, value, zero } of TYPES.filter(type => !type.isValueType)) {
|
||||
describe(`${type} storage pointer`, function () {
|
||||
it('set', async function () {
|
||||
await this.mock.getFunction(`set${name}Storage`)(slot, value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.getAddress(slot)).to.be.equal(this.value);
|
||||
});
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.mock.getFunction(`set${name}Storage`)(slot, value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.getAddress(otherSlot)).to.be.equal(constants.ZERO_ADDRESS);
|
||||
it('from right slot', async function () {
|
||||
expect(await this.mock.getFunction(`${type}Map`)(slot)).to.equal(value);
|
||||
expect(await this.mock.getFunction(`get${name}Storage`)(slot)).to.equal(value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.mock.getFunction(`${type}Map`)(otherSlot)).to.equal(zero);
|
||||
expect(await this.mock.getFunction(`get${name}Storage`)(otherSlot)).to.equal(zero);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bytes32 storage slot', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = web3.utils.keccak256('some byte32 value');
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setBytes32(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setBytes32(slot, this.value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.getBytes32(slot)).to.be.equal(this.value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.getBytes32(otherSlot)).to.be.equal(constants.ZERO_BYTES32);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint256 storage slot', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = new BN(1742);
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setUint256(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setUint256(slot, this.value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.getUint256(slot)).to.be.bignumber.equal(this.value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.getUint256(otherSlot)).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('string storage slot', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = 'lorem ipsum';
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setString(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setString(slot, this.value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.getString(slot)).to.be.equal(this.value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.getString(otherSlot)).to.be.equal('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('string storage pointer', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = 'lorem ipsum';
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setStringStorage(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setStringStorage(slot, this.value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.stringMap(slot)).to.be.equal(this.value);
|
||||
expect(await this.store.getStringStorage(slot)).to.be.equal(this.value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.stringMap(otherSlot)).to.be.equal('');
|
||||
expect(await this.store.getStringStorage(otherSlot)).to.be.equal('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bytes storage slot', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = web3.utils.randomHex(128);
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setBytes(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setBytes(slot, this.value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.getBytes(slot)).to.be.equal(this.value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.getBytes(otherSlot)).to.be.equal(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bytes storage pointer', function () {
|
||||
beforeEach(async function () {
|
||||
this.value = web3.utils.randomHex(128);
|
||||
});
|
||||
|
||||
it('set', async function () {
|
||||
await this.store.setBytesStorage(slot, this.value);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
beforeEach(async function () {
|
||||
await this.store.setBytesStorage(slot, this.value);
|
||||
});
|
||||
|
||||
it('from right slot', async function () {
|
||||
expect(await this.store.bytesMap(slot)).to.be.equal(this.value);
|
||||
expect(await this.store.getBytesStorage(slot)).to.be.equal(this.value);
|
||||
});
|
||||
|
||||
it('from other slot', async function () {
|
||||
expect(await this.store.bytesMap(otherSlot)).to.be.equal(null);
|
||||
expect(await this.store.getBytesStorage(otherSlot)).to.be.equal(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user