Skip to content

Commit 93008c9

Browse files
committed
add docs and update pragmas
1 parent 801edd1 commit 93008c9

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

contracts/interfaces/IERC1271.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
// OpenZeppelin Contracts (last updated v5.3.0) (interfaces/IERC1271.sol)
33

4-
pragma solidity >=0.6.9;
4+
pragma solidity >=0.5.0;
55

66
/**
77
* @dev Interface of the ERC-1271 standard signature validation method for
@@ -13,5 +13,5 @@ interface IERC1271 {
1313
* @param hash Hash of the data to be signed
1414
* @param signature Signature byte array associated with `hash`
1515
*/
16-
function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue);
16+
function isValidSignature(bytes32 hash, bytes calldata signature) external view returns (bytes4 magicValue);
1717
}

contracts/interfaces/IERC3156.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC3156.sol)
33

4-
pragma solidity >=0.8.20;
4+
pragma solidity >=0.5.0;
55

66
import {IERC3156FlashBorrower} from "./IERC3156FlashBorrower.sol";
77
import {IERC3156FlashLender} from "./IERC3156FlashLender.sol";

scripts/minimize-pragma.js

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { exec } = require('child_process');
22
const fs = require('fs');
33

44
let solcVersionsMaxPatch = ['0.5.16', '0.6.12', '0.7.6', '0.8.29'];
5+
6+
// Generate an array of all the solc versions.
57
const allSolcVersions = solcVersionsMaxPatch.flatMap(minorVersion => {
68
let patchVersions = [];
79
const maxPatchVersion = parseInt(minorVersion.split('.')[2]);
@@ -12,25 +14,40 @@ const allSolcVersions = solcVersionsMaxPatch.flatMap(minorVersion => {
1214
return patchVersions;
1315
});
1416

17+
// Files that have been finalized and should not be modified again.
1518
let finalizedFiles = [];
1619

1720
minimizeAllInterfacePragmas();
1821

19-
async function getApplicablePragmas(file) {
20-
const pragmas = await Promise.all(allSolcVersions.map(version => compileWithVersion(file, version)));
21-
return pragmas;
22+
/**
23+
* Main entry point that minimizes the pragma for all interface files. Draft interfaces are ignored.
24+
*/
25+
async function minimizeAllInterfacePragmas() {
26+
const dirPath = 'contracts/interfaces';
27+
const files = fs.readdirSync(dirPath);
28+
for (const file of files) {
29+
if (!file.endsWith('.sol') || file.startsWith('draft')) {
30+
continue;
31+
}
32+
await minimizePragma(`${dirPath}/${file}`);
33+
}
2234
}
2335

36+
/**
37+
* Minimize the pragma for a give file and all its dependencies.
38+
* @param {*} file Absolute path to the file to minimize.
39+
*/
2440
async function minimizePragma(file) {
2541
if (finalizedFiles.includes(file)) {
2642
return;
2743
}
2844

45+
console.log(`Minimizing pragma in ${file}`);
46+
2947
await updatePragmaWithDependencies(file);
3048

3149
const sources = getFileSources(file);
3250
for (const source of sources) {
33-
console.log(source);
3451
await minimizePragma(source);
3552
}
3653

@@ -56,6 +73,19 @@ async function minimizePragma(file) {
5673
finalizedFiles.push(file);
5774
}
5875

76+
/**
77+
* Get the applicable pragmas for a given file by compiling it with all solc versions.
78+
*/
79+
async function getApplicablePragmas(file) {
80+
const pragmas = await Promise.all(allSolcVersions.map(version => compileWithVersion(file, version)));
81+
return pragmas;
82+
}
83+
84+
/**
85+
* Get the applicable pragmas for all parents of a given file. Each parent's applicability is merged.
86+
* @param {*} parents
87+
* @returns An array of applicable pragmas for all parents.
88+
*/
5989
async function getParentApplicablePragmas(parents) {
6090
let pragmas;
6191
for (const parent of parents) {
@@ -68,9 +98,15 @@ async function getParentApplicablePragmas(parents) {
6898
return pragmas;
6999
}
70100

101+
/**
102+
* Compile the given file with the specified solidity version using forge.
103+
* @param {*} file Absolute path to the file to compile.
104+
* @param {*} solcVersion Solc version to use for compilation. (ex: 0.8.4)
105+
* @returns {Promise<{solcVersion: string, success: boolean}>} Compilation result.
106+
*/
71107
async function compileWithVersion(file, solcVersion) {
72108
return new Promise(resolve => {
73-
exec(`forge build ${file} --ast --use ${solcVersion} --out out/out-solc${solcVersion}`, error => {
109+
exec(`forge build ${file} --use ${solcVersion} --out out/out-solc${solcVersion}`, error => {
74110
if (error !== null) {
75111
return resolve({ solcVersion, success: false });
76112
}
@@ -79,17 +115,11 @@ async function compileWithVersion(file, solcVersion) {
79115
});
80116
}
81117

82-
async function minimizeAllInterfacePragmas() {
83-
const dirPath = 'contracts/interfaces';
84-
const files = fs.readdirSync(dirPath);
85-
for (const file of files) {
86-
if (!file.endsWith('.sol') || file.startsWith('draft')) {
87-
continue;
88-
}
89-
await minimizePragma(`${dirPath}/${file}`);
90-
}
91-
}
92-
118+
/**
119+
* Gets the sources of a given file from the AST output. Note, must already be compiled with AST.
120+
* @param {*} file Absolute path to the file to get sources from.
121+
* @returns An array of sources for the file excluding the file itself.
122+
*/
93123
function getFileSources(file) {
94124
const contractName = file.split('/').at(-1);
95125

@@ -104,6 +134,11 @@ function getFileSources(file) {
104134
return sources.filter(source => source !== file);
105135
}
106136

137+
/**
138+
* Updates the pragma in the given file to the newPragma version.
139+
* @param {*} file Absolute path to the file to update.
140+
* @param {*} newPragma New pragma version to set. (ex: >=0.8.4)
141+
*/
107142
function updatePragma(file, newPragma) {
108143
if (finalizedFiles.includes(file)) return;
109144

@@ -115,6 +150,12 @@ function updatePragma(file, newPragma) {
115150
console.log(`Updated pragma in ${file} to ${newPragma}`);
116151
}
117152

153+
/**
154+
* Updates the pragma in the given file and all its dependencies to the newPragma version.
155+
* This is a recursive function that will update all dependencies of the file.
156+
* @param {*} file Absolute path to the file to update.
157+
* @param {*} newPragma New pragma version to set. (ex: >=0.8.4). Defaults to >=0.5.0.
158+
*/
118159
async function updatePragmaWithDependencies(file, newPragma = '>=0.5.0') {
119160
updatePragma(file, newPragma);
120161

@@ -127,6 +168,9 @@ async function updatePragmaWithDependencies(file, newPragma = '>=0.5.0') {
127168
}
128169
}
129170

171+
/**
172+
* Merge two lists of pragma compatibility results.
173+
*/
130174
function mergePragmaLists(pragmaList1, pragmaList2) {
131175
if (pragmaList1 === undefined || pragmaList2 === undefined) return pragmaList1 ?? pragmaList2;
132176

0 commit comments

Comments
 (0)