-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfix.js
179 lines (155 loc) · 4.82 KB
/
fix.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const fs = require("node:fs");
const { getAddress } = require("ethers");
// Get all chain directories
const chainDirs = fs.readdirSync(".").filter((dir) => /^\d+$/.test(dir));
function fixAddress(address) {
try {
return getAddress(address);
} catch (error) {
throw Error(`Invalid address ${address}: ${error.message}`);
}
}
function fixAddressesInArray(addresses, context) {
return addresses.map((address) => {
const fixedAddress = fixAddress(address);
if (fixedAddress !== address) {
return {
changed: true,
message: `Fixing ${context}: ${address} -> ${fixedAddress}`,
value: fixedAddress,
};
}
return { changed: false, value: address };
});
}
function fixAddressesInObject(obj, context) {
const result = {};
let changes = false;
const changesList = [];
for (const [key, value] of Object.entries(obj)) {
const fixedKey = fixAddress(key);
if (fixedKey !== key) {
changes = true;
const message = `Fixing ${context}: ${key} -> ${fixedKey}`;
console.log(message);
changesList.push(message);
}
result[fixedKey] = value;
}
return { result, changes, changesList };
}
function fixBiomeFormatting(content) {
// Remove trailing commas in arrays and objects
let fixed = content.replace(/,(\s*[}\]])/g, "$1");
// Ensure consistent spacing around colons in objects, but not in URLs
fixed = fixed.replace(/([^"\s]):(?=\s*[{"])/g, "$1 :");
// Ensure consistent spacing around commas
fixed = fixed.replace(/,(\S)/g, ", $1");
return fixed;
}
function fixChain(chainId) {
console.log(`\nProcessing chain ${chainId}...`);
// Read all JSON files
const files = {
entities: JSON.parse(fs.readFileSync(`${chainId}/entities.json`, "utf8")),
vaults: JSON.parse(fs.readFileSync(`${chainId}/vaults.json`, "utf8")),
points: JSON.parse(fs.readFileSync(`${chainId}/points.json`, "utf8")),
products: JSON.parse(fs.readFileSync(`${chainId}/products.json`, "utf8")),
};
let changes = false;
const changesList = [];
// Fix entity addresses
for (const [entityId, entity] of Object.entries(files.entities)) {
if (entity.addresses) {
const {
result,
changes: entityChanges,
changesList: entityChangesList,
} = fixAddressesInObject(entity.addresses, `entities.${entityId}`);
if (entityChanges) {
changes = true;
changesList.push(...entityChangesList);
entity.addresses = result;
}
}
}
// Fix vault addresses
const {
result: fixedVaults,
changes: vaultChanges,
changesList: vaultChangesList,
} = fixAddressesInObject(files.vaults, "vault");
if (vaultChanges) {
changes = true;
changesList.push(...vaultChangesList);
files.vaults = fixedVaults;
}
// Fix product vault addresses
for (const [productId, product] of Object.entries(files.products)) {
if (product.vaults) {
const fixedAddresses = fixAddressesInArray(
product.vaults,
`vault address in products.${productId}`,
);
const productChanges = fixedAddresses.filter((a) => a.changed);
if (productChanges.length > 0) {
changes = true;
changesList.push(...productChanges.map((a) => a.message));
product.vaults = fixedAddresses.map((a) => a.value);
}
}
}
// Fix points addresses
for (const point of files.points) {
if (point.skipValidation) continue;
if (point.token) {
const fixedToken = fixAddress(point.token);
if (fixedToken !== point.token) {
changes = true;
const message = `Fixing token address in points.${point.name}: ${point.token} -> ${fixedToken}`;
console.log(message);
changesList.push(message);
point.token = fixedToken;
}
}
for (const field of ["collateralVaults", "liabilityVaults"]) {
if (point[field]) {
const fixedAddresses = fixAddressesInArray(
point[field],
`${field} address in points.${point.name}`,
);
const pointChanges = fixedAddresses.filter((a) => a.changed);
if (pointChanges.length > 0) {
changes = true;
changesList.push(...pointChanges.map((a) => a.message));
point[field] = fixedAddresses.map((a) => a.value);
}
}
}
}
// Write back changes if any were made
if (changes) {
console.log(`\nWriting changes for chain ${chainId}:`);
console.log(`Found ${changesList.length} addresses to fix`);
// Write all files with Biome formatting
for (const [filename, data] of Object.entries(files)) {
const content = JSON.stringify(data, null, 2);
const biomeFixed = fixBiomeFormatting(content);
fs.writeFileSync(`${chainId}/${filename}.json`, biomeFixed);
console.log(`- Updated ${filename}.json`);
}
console.log(`\nAll changes saved for chain ${chainId}`);
} else {
console.log(`No malformed addresses found in chain ${chainId}`);
}
}
// Process all chains
for (const chainId of chainDirs) {
try {
fixChain(chainId);
} catch (error) {
console.error(`Error processing chain ${chainId}:`, error);
process.exit(1);
}
}
console.log("\nAddress fixing complete!");