-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathupdate-policies.js
105 lines (84 loc) · 3.95 KB
/
update-policies.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
import { readFileSync, writeFileSync } from "fs";
import ts from "typescript";
const MANIFEST_PATHS = {
local: "../contracts/game/manifest_local.json",
slot: "../contracts/game/manifest_slot.json",
sepolia: "../contracts/game/manifest_sepolia.json",
mainnet: "../contracts/game/manifest_mainnet.json",
};
const policiesPath = "apps/game/src/hooks/context/policies.ts";
function updatePoliciesWithManifestAddresses(network, policiesPath) {
if (!Object.keys(MANIFEST_PATHS).includes(network)) {
throw new Error(`Invalid network. Must be one of: ${Object.keys(MANIFEST_PATHS).join(", ")}`);
}
const manifestPath = MANIFEST_PATHS[network];
const manifestData = JSON.parse(readFileSync(manifestPath, "utf8"));
const fileContent = readFileSync(policiesPath, "utf8");
const sourceFile = ts.createSourceFile(policiesPath, fileContent, ts.ScriptTarget.Latest, true);
function findAddressForSystem(systemName) {
for (const contract of manifestData.contracts) {
if (contract.systems && contract.systems.includes(systemName)) {
return contract.address;
}
}
return null;
}
let updatedContent = fileContent;
function updateAddressesInPolicies(node) {
if (ts.isObjectLiteralExpression(node)) {
node.properties.forEach((prop) => {
if (ts.isPropertyAssignment(prop) && ts.isStringLiteral(prop.name)) {
const currentAddress = prop.name.getText().replace(/"/g, "");
if (ts.isObjectLiteralExpression(prop.initializer)) {
const methodsProp = prop.initializer.properties.find(
(p) => ts.isPropertyAssignment(p) && ts.isIdentifier(p.name) && p.name.getText() === "methods",
);
if (methodsProp && ts.isPropertyAssignment(methodsProp)) {
if (ts.isArrayLiteralExpression(methodsProp.initializer)) {
// Find methods that are not 'dojo_name', 'world_dispatcher', or 'create'
const interestingMethods = methodsProp.initializer.elements.filter(
(elem) => ts.isObjectLiteralExpression(elem) && hasInterestingEntrypoint(elem),
);
interestingMethods.forEach((method) => {
const nameProperty = method.properties.find(
(p) => ts.isPropertyAssignment(p) && ts.isIdentifier(p.name) && p.name.getText() === "name",
);
if (nameProperty) {
const systemName = nameProperty.initializer.getText().replace(/"/g, "");
const newAddress = findAddressForSystem(systemName);
if (newAddress && newAddress !== currentAddress) {
console.log(`Updating address for system ${systemName}:`);
console.log(` Old address: ${currentAddress}`);
console.log(` New address: ${newAddress}`);
// Replace the old address with the new one
updatedContent = updatedContent.replace(`"${currentAddress}":`, `"${newAddress}":`);
}
}
});
}
}
}
}
});
}
ts.forEachChild(node, updateAddressesInPolicies);
}
function hasInterestingEntrypoint(methodNode) {
const nameProperty = methodNode.properties.find(
(p) => ts.isPropertyAssignment(p) && ts.isIdentifier(p.name) && p.name.getText() === "name",
);
return (
nameProperty &&
ts.isStringLiteral(nameProperty.initializer) &&
!["dojo_name", "world_dispatcher", "create"].includes(nameProperty.initializer.getText().replace(/"/g, ""))
);
}
updateAddressesInPolicies(sourceFile);
writeFileSync(policiesPath, updatedContent);
console.log(`Policies file updated successfully for ${network} network.`);
}
// If run directly, default to sepolia
if (import.meta.url === `file://${process.argv[1]}`) {
const network = process.argv[2];
updatePoliciesWithManifestAddresses(network, policiesPath);
}