Use solidity mapper to get value for mapping and nested mapping (balance and allowance) (#48)

* Implement getting value for basic mapping type.

* Add test for basic mapping type.

* Implement getting value for nested mapping type.

Co-authored-by: nikugogoi <95nikass@gmail.com>
This commit is contained in:
Ashwin Phatak
2021-06-09 10:18:19 +05:30
committed by GitHub
co-authored by nikugogoi
parent 84e1927402
commit fc44617db3
11 changed files with 2901 additions and 94 deletions
@@ -309,4 +309,33 @@ describe('Get value from storage', () => {
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testFixedArrays.address, 'uintArray'));
expect(value).to.eql(expectedValue.map(el => BigInt(el)));
});
describe('mapping type', () => {
let testMappingTypes: Contract, storageLayout: StorageLayout;
before(async () => {
const TestMappingTypes = await ethers.getContractFactory('TestMappingTypes');
testMappingTypes = await TestMappingTypes.deploy();
await testMappingTypes.deployed();
storageLayout = await getStorageLayout('TestMappingTypes');
});
it('get value for basic mapping type', async () => {
const expectedValue = 123;
const [, signer1] = await ethers.getSigners();
await testMappingTypes.connect(signer1).setAddressUintMap(expectedValue);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'addressUintMap', signer1.address);
expect(value).to.equal(BigInt(expectedValue));
});
it('get value for nested mapping type', async () => {
const expectedValue = 123;
const [, signer1, signer2] = await ethers.getSigners();
await testMappingTypes.connect(signer1).setNestedAddressUintMap(signer2.address, expectedValue);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'addressUintMap', signer1.address, signer2.address);
expect(value).to.equal(BigInt(expectedValue));
});
});
});
+37 -5
View File
@@ -13,6 +13,7 @@ interface Types {
numberOfBytes: string;
label: string;
base?: string;
value?: string;
};
}
@@ -57,10 +58,10 @@ export const getStorageInfo = (storageLayout: StorageLayout, variableName: strin
* @param variableName
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
export const getStorageValue = async (storageLayout: StorageLayout, getStorageAt: GetStorageAt, blockHash: string, address: string, variableName: string): Promise<{ value: any, proof: { data: string } }> => {
export const getStorageValue = async (storageLayout: StorageLayout, getStorageAt: GetStorageAt, blockHash: string, address: string, variableName: string, ...mappingKeys: Array<string>): Promise<{ value: any, proof: { data: string } }> => {
const { slot, offset, type, types } = getStorageInfo(storageLayout, variableName);
return getDecodedValue(getStorageAt, blockHash, address, types, { slot, offset, type });
return getDecodedValue(getStorageAt, blockHash, address, types, { slot, offset, type }, mappingKeys);
};
/**
@@ -96,9 +97,9 @@ export const getValueByType = (storageValue: string, typeLabel: string): bigint
* @param storageInfo
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
const getDecodedValue = async (getStorageAt: GetStorageAt, blockHash: string, address: string, types: Types, storageInfo: { slot: string, offset: number, type: string }): Promise<{ value: any, proof: { data: string } }> => {
const getDecodedValue = async (getStorageAt: GetStorageAt, blockHash: string, address: string, types: Types, storageInfo: { slot: string, offset: number, type: string }, mappingKeys: Array<string>): Promise<{ value: any, proof: { data: string } }> => {
const { slot, offset, type } = storageInfo;
const { encoding, numberOfBytes, label: typeLabel, base } = types[type];
const { encoding, numberOfBytes, label: typeLabel, base, value: mappingValueType } = types[type];
const [isArray, arraySize] = typeLabel.match(/\[([0-9]*)\]/) || [false];
let value: string, proof: { data: string };
@@ -114,7 +115,7 @@ const getDecodedValue = async (getStorageAt: GetStorageAt, blockHash: string, ad
for (let i = 0; i < Number(baseNumberOfBytes) * Number(arraySize); i = i + Number(baseNumberOfBytes)) {
const arraySlot = BigNumber.from(slot).add(Math.floor(i / 32)).toHexString();
const slotOffset = i % 32;
const { value, proof } = await getDecodedValue(getStorageAt, blockHash, address, types, { slot: arraySlot, offset: slotOffset, type: base });
({ value, proof } = await getDecodedValue(getStorageAt, blockHash, address, types, { slot: arraySlot, offset: slotOffset, type: base }, []));
resultArray.push(value);
proofs.push(JSON.parse(proof.data));
}
@@ -140,6 +141,18 @@ const getDecodedValue = async (getStorageAt: GetStorageAt, blockHash: string, ad
({ value, proof } = await getBytesValue(blockHash, address, slot, getStorageAt));
break;
case 'mapping': {
const mappingSlot = await getMappingSlot(slot, mappingKeys[0]);
if (mappingValueType) {
({ value, proof } = await getDecodedValue(getStorageAt, blockHash, address, types, { slot: mappingSlot, offset: 0, type: mappingValueType }, mappingKeys.slice(1)));
} else {
throw new Error(`Mapping value type not specified for ${mappingKeys[0]}`);
}
break;
}
default:
throw new Error(`Encoding ${encoding} not implemented.`);
}
@@ -150,6 +163,25 @@ const getDecodedValue = async (getStorageAt: GetStorageAt, blockHash: string, ad
};
};
/**
* Function to get slot for mapping types.
* @param mappingSlot
* @param key
*/
export const getMappingSlot = (mappingSlot: string, key: string): string => {
// https://github.com/ethers-io/ethers.js/issues/1079#issuecomment-703056242
const mappingSlotPadded = utils.hexZeroPad(BigNumber.from(mappingSlot).toHexString(), 32);
const keyPadded = utils.hexZeroPad(key, 32);
const fullKey = utils.concat([
keyPadded,
mappingSlotPadded
]);
const slot = utils.keccak256(fullKey);
return slot;
};
/**
* Function to get value for inplace encoding.
* @param address
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract TestMappingTypes {
// Mapping type variable occupies one single slot but the actual elements are stored at a different storage slot that is computed using a Keccak-256 hash.
// https://docs.soliditylang.org/en/v0.8.4/internals/layout_in_storage.html#mappings-and-dynamic-arrays
mapping (address => mapping (address => uint)) private nestedAddressUintMap;
// Mapping type variable occupies the next single slot.
mapping(address => uint) public addressUintMap;
// Set variable addressUintMap.
function setAddressUintMap(uint value) external {
addressUintMap[msg.sender] = value;
}
// Set variable nestedAddressUintMap.
function setNestedAddressUintMap(address addressValue, uint uintValue) external {
nestedAddressUintMap[msg.sender][addressValue] = uintValue;
}
}