2021-06-02 05:53:33 +00:00
|
|
|
import { ContractInterface } from '@ethersproject/contracts';
|
|
|
|
import { artifacts, ethers } from 'hardhat';
|
2021-05-31 05:37:11 +00:00
|
|
|
import { CompilerOutput, CompilerOutputBytecode } from 'hardhat/types';
|
|
|
|
|
|
|
|
import { StorageLayout, GetStorageAt } from '../src';
|
|
|
|
|
|
|
|
// storageLayout doesnt exist in type CompilerOutput doesnt.
|
|
|
|
// Extending CompilerOutput type to include storageLayout property.
|
|
|
|
interface StorageCompilerOutput extends CompilerOutput {
|
|
|
|
contracts: {
|
|
|
|
[sourceName: string]: {
|
|
|
|
[contractName: string]: {
|
2021-06-02 05:53:33 +00:00
|
|
|
abi: ContractInterface;
|
2021-05-31 05:37:11 +00:00
|
|
|
evm: {
|
|
|
|
bytecode: CompilerOutputBytecode;
|
|
|
|
deployedBytecode: CompilerOutputBytecode;
|
|
|
|
methodIdentifiers: {
|
|
|
|
[methodSignature: string]: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
storageLayout?: StorageLayout;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get storage layout of specified contract.
|
|
|
|
* @param contractName
|
|
|
|
*/
|
2021-06-02 05:53:33 +00:00
|
|
|
export const getStorageLayout = async (contractName: string): Promise<StorageLayout> => {
|
2021-05-31 05:37:11 +00:00
|
|
|
const artifact = await artifacts.readArtifact(contractName);
|
2021-06-02 05:53:33 +00:00
|
|
|
const buildInfo = await artifacts.getBuildInfo(`${artifact.sourceName}:${artifact.contractName}`);
|
2021-05-31 05:37:11 +00:00
|
|
|
|
|
|
|
if (!buildInfo) {
|
|
|
|
throw new Error('storageLayout not present in compiler output.');
|
|
|
|
}
|
|
|
|
|
2021-06-02 05:53:33 +00:00
|
|
|
const output: StorageCompilerOutput = buildInfo.output;
|
2021-05-31 05:37:11 +00:00
|
|
|
const { storageLayout } = output.contracts[artifact.sourceName][artifact.contractName];
|
|
|
|
|
|
|
|
if (!storageLayout) {
|
2021-06-02 05:53:33 +00:00
|
|
|
throw new Error('Contract hasn\'t been compiled.');
|
2021-05-31 05:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return storageLayout;
|
2021-06-02 05:53:33 +00:00
|
|
|
};
|
2021-05-31 05:37:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get storage value in hardhat environment using ethers.
|
|
|
|
* @param address
|
|
|
|
* @param position
|
|
|
|
*/
|
2021-06-03 06:22:23 +00:00
|
|
|
export const getStorageAt: GetStorageAt = async ({ blockHash, contract, slot }) => {
|
|
|
|
// TODO: Fix use of blockHash as hex string in getStorageAt.
|
|
|
|
// Using blockHash in getStorageAt throws error.
|
|
|
|
// https://github.com/ethers-io/ethers.js/pull/1550#issuecomment-841746994
|
2021-06-03 09:33:39 +00:00
|
|
|
// Using latest tag for temporary fix in test scenario.
|
2021-06-03 06:22:23 +00:00
|
|
|
blockHash = 'latest';
|
|
|
|
const value = await ethers.provider.getStorageAt(contract, slot, blockHash);
|
2021-05-31 05:37:11 +00:00
|
|
|
|
2021-06-03 06:22:23 +00:00
|
|
|
return {
|
|
|
|
value,
|
|
|
|
proof: {
|
2021-06-14 10:42:42 +00:00
|
|
|
// Returning null value as proof, since ethers library getStorageAt method doesnt return proof.
|
|
|
|
// This function is used in tests to mock the getStorageAt method of ipld-eth-client which returns proof along with value.
|
2021-06-03 06:22:23 +00:00
|
|
|
data: JSON.stringify(null)
|
|
|
|
}
|
|
|
|
};
|
2021-06-02 05:53:33 +00:00
|
|
|
};
|