-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-upgraded-test-contracts.ts
More file actions
66 lines (54 loc) · 2.4 KB
/
Copy pathgenerate-upgraded-test-contracts.ts
File metadata and controls
66 lines (54 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// See README.md for more details
import * as fs from "fs";
import * as path from "path";
const inputDirectory = "./contracts"; // path to your contracts directory
const outputDirectory = "./contracts/test"; // path to your output directory
fs.readdir(inputDirectory, (err, files) => {
if (err) {
return console.log("Unable to scan directory: " + err);
}
// First pass: Generate all the upgraded contract names
const upgradedNames = files
.filter((file) => path.extname(file) === ".sol")
.map((file) => "TestUpgraded" + path.basename(file, ".sol"));
console.log(
"To add to UTT.test.ts (remove lines for non-upgradable contracts such as Operator.sol, UTTProxy.sol):"
);
// Second pass: Replace the imports and contract names in all files
files.forEach((file) => {
if (path.extname(file) === ".sol") {
const filePath = path.join(inputDirectory, file);
const data = fs.readFileSync(filePath, "utf8");
const name = path.basename(file, ".sol");
const upgradedName = "TestUpgraded" + name;
let upgradedData = data;
// Replace all import statements
upgradedNames.forEach((upgradedName) => {
const originalName = upgradedName.replace("TestUpgraded", "");
upgradedData = upgradedData.replace(
new RegExp(`import ".*/${originalName}.sol";`, "g"),
`import "./${upgradedName}.sol";`
);
upgradedData = upgradedData.replace(
new RegExp(`\\b${originalName}\\b`, "g"),
upgradedName
);
});
const newVarName = `new${upgradedName}Var`;
const gapMatch = upgradedData.match(/uint256\[(\d+)\] private __gap;/);
if (gapMatch) {
const gapLength = Number(gapMatch[1]); // The first matched group is at index 1
const newGapLength = gapLength - 1;
upgradedData = upgradedData.replace(
gapMatch[0],
`uint256 ${newVarName};\n uint256[${newGapLength}] private __gap;\n\n function increment${newVarName}() public {\n ${newVarName} += 1;\n }\n\n function get${newVarName}() public view returns (uint256) {\n return ${newVarName};\n }`
);
}
console.log(`\n upgradedContract.increment${newVarName}();`);
console.log(
` expect(await upgradedContract.get${newVarName}()).to.be.eq(1);`
);
fs.writeFileSync(`${outputDirectory}/${upgradedName}.sol`, upgradedData);
}
});
});