This commit is contained in:
dexorder
2024-10-17 02:42:28 -04:00
commit 25def69c66
878 changed files with 112489 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
process.on('unhandledRejection', reason => {
throw new Error(reason);
});

View File

@@ -0,0 +1,29 @@
const { HardhatError } = require('hardhat/internal/core/errors');
function isExpectedError(e, suffix) {
// HH700: Artifact not found - from https://hardhat.org/hardhat-runner/docs/errors#HH700
return HardhatError.isHardhatError(e) && e.number === 700 && suffix !== '';
}
// Modifies the artifact require functions so that instead of X it loads the XUpgradeable contract.
// This allows us to run the same test suite on both the original and the transpiled and renamed Upgradeable contracts.
extendEnvironment(hre => {
const suffixes = ['UpgradeableWithInit', 'Upgradeable', ''];
// Ethers
const originalReadArtifact = hre.artifacts.readArtifact;
hre.artifacts.readArtifact = async function (name) {
for (const suffix of suffixes) {
try {
return await originalReadArtifact.call(this, name + suffix);
} catch (e) {
if (isExpectedError(e, suffix)) {
continue;
} else {
throw e;
}
}
}
throw new Error('Unreachable');
};
});

View File

@@ -0,0 +1,45 @@
// Warnings about unreachable code are emitted with a source location that corresponds to the unreachable code.
// We have some testing contracts that purposely cause unreachable code, but said code is in the library contracts, and
// with hardhat-ignore-warnings we are not able to selectively ignore them without potentially ignoring relevant
// warnings that we don't want to miss.
// Thus, we need to handle these warnings separately. We force Hardhat to compile them in a separate compilation job and
// then ignore the warnings about unreachable code that come from that compilation job.
const { task } = require('hardhat/config');
const {
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
TASK_COMPILE_SOLIDITY_COMPILE,
} = require('hardhat/builtin-tasks/task-names');
const marker = Symbol('unreachable');
const markedCache = new WeakMap();
task(TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE, async (params, _, runSuper) => {
const job = await runSuper(params);
// If the file is in the unreachable directory, we make a copy of the config and mark it, which will cause it to get
// compiled separately (along with the other marked files).
if (params.file.sourceName.startsWith('contracts/mocks/') && /\bunreachable\b/.test(params.file.sourceName)) {
const originalConfig = job.solidityConfig;
let markedConfig = markedCache.get(originalConfig);
if (markedConfig === undefined) {
markedConfig = { ...originalConfig, [marker]: true };
markedCache.set(originalConfig, markedConfig);
}
job.solidityConfig = markedConfig;
}
return job;
});
const W_UNREACHABLE_CODE = '5740';
task(TASK_COMPILE_SOLIDITY_COMPILE, async (params, _, runSuper) => {
const marked = params.compilationJob.solidityConfig[marker];
const result = await runSuper(params);
if (marked) {
result.output = {
...result.output,
errors: result.output.errors?.filter(e => e.severity !== 'warning' || e.errorCode !== W_UNREACHABLE_CODE),
};
}
return result;
});

View File

@@ -0,0 +1,18 @@
const fs = require('fs');
const { task } = require('hardhat/config');
const { TASK_COMPILE_GET_REMAPPINGS } = require('hardhat/builtin-tasks/task-names');
task(TASK_COMPILE_GET_REMAPPINGS).setAction((taskArgs, env, runSuper) =>
runSuper().then(remappings =>
Object.assign(
remappings,
Object.fromEntries(
fs
.readFileSync('remappings.txt', 'utf-8')
.split('\n')
.filter(Boolean)
.map(line => line.trim().split('=')),
),
),
),
);

View File

@@ -0,0 +1,6 @@
const { subtask } = require('hardhat/config');
const { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } = require('hardhat/builtin-tasks/task-names');
subtask(TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS).setAction(async (_, __, runSuper) =>
(await runSuper()).filter(path => !path.endsWith('.t.sol')),
);

View File

@@ -0,0 +1,25 @@
const { internalTask } = require('hardhat/config');
const { TASK_TEST_GET_TEST_FILES } = require('hardhat/builtin-tasks/task-names');
// Modifies `hardhat test` to skip the proxy tests after proxies are removed by the transpiler for upgradeability.
internalTask(TASK_TEST_GET_TEST_FILES).setAction(async (args, hre, runSuper) => {
const path = require('path');
const { promises: fs } = require('fs');
const hasProxies = await fs
.access(path.join(hre.config.paths.sources, 'proxy/Proxy.sol'))
.then(() => true)
.catch(() => false);
const ignoredIfProxy = [
'proxy/beacon/BeaconProxy.test.js',
'proxy/beacon/UpgradeableBeacon.test.js',
'proxy/ERC1967/ERC1967Proxy.test.js',
'proxy/transparent/ProxyAdmin.test.js',
'proxy/transparent/TransparentUpgradeableProxy.test.js',
'proxy/utils/UUPSUpgradeable.test.js',
].map(p => path.join(hre.config.paths.tests, p));
return (await runSuper(args)).filter(file => hasProxies || !ignoredIfProxy.includes(file));
});