-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathfix.js
More file actions
266 lines (233 loc) · 7.37 KB
/
fix.js
File metadata and controls
266 lines (233 loc) · 7.37 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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 (assets.json is optional per-chain)
const files = {
entities: JSON.parse(fs.readFileSync(`${chainId}/entities.json`, "utf8")),
points: JSON.parse(fs.readFileSync(`${chainId}/points.json`, "utf8")),
products: JSON.parse(fs.readFileSync(`${chainId}/products.json`, "utf8")),
};
const assetsPath = `${chainId}/assets.json`;
if (fs.existsSync(assetsPath)) {
files.assets = JSON.parse(fs.readFileSync(assetsPath, "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 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);
}
}
if (product.deprecatedVaults) {
const fixedAddresses = fixAddressesInArray(
product.deprecatedVaults,
`deprecated 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.deprecatedVaults = fixedAddresses.map((a) => a.value);
}
}
if (product.featuredVaults) {
const fixedAddresses = fixAddressesInArray(
product.featuredVaults,
`featured 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.featuredVaults = fixedAddresses.map((a) => a.value);
}
}
if (product.vaultOverrides) {
const {
result: fixedOverrides,
changes: overrideChanges,
changesList: overrideChangesList,
} = fixAddressesInObject(
product.vaultOverrides,
`vaultOverrides in products.${productId}`,
);
if (overrideChanges) {
changes = true;
changesList.push(...overrideChangesList);
product.vaultOverrides = fixedOverrides;
}
}
}
// 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);
}
}
}
}
// Fix assets.json entry addresses. The `address` field is optional —
// pattern-only entries (symbols / names / regex) have nothing to fix.
if (files.assets) {
for (const entry of files.assets) {
if (!entry || typeof entry.address !== "string" || !entry.address)
continue;
const fixed = fixAddress(entry.address);
if (fixed !== entry.address) {
changes = true;
const message = `Fixing asset address in assets: ${entry.address} -> ${fixed}`;
console.log(message);
changesList.push(message);
entry.address = fixed;
}
}
}
// 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}`);
}
}
function fixGlobal() {
const assetsPath = "all/assets.json";
if (!fs.existsSync(assetsPath)) return;
console.log("\nProcessing global scope all/...");
const assets = JSON.parse(fs.readFileSync(assetsPath, "utf8"));
let changes = false;
for (const entry of assets) {
if (!entry || typeof entry.address !== "string" || !entry.address) continue;
const fixed = fixAddress(entry.address);
if (fixed !== entry.address) {
changes = true;
console.log(
`Fixing asset address in all/assets.json: ${entry.address} -> ${fixed}`,
);
entry.address = fixed;
}
}
if (changes) {
const content = JSON.stringify(assets, null, 2);
fs.writeFileSync(assetsPath, fixBiomeFormatting(content));
console.log(`- Updated ${assetsPath}`);
} else {
console.log("No malformed addresses found in all/assets.json");
}
}
// Process all chains
for (const chainId of chainDirs) {
try {
fixChain(chainId);
} catch (error) {
console.error(`Error processing chain ${chainId}:`, error);
process.exit(1);
}
}
try {
fixGlobal();
} catch (error) {
console.error("Error processing global scope:", error);
process.exit(1);
}
console.log("\nAddress fixing complete!");