-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-contracts.js
More file actions
88 lines (72 loc) · 2.79 KB
/
sync-contracts.js
File metadata and controls
88 lines (72 loc) · 2.79 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
const fs = require('fs');
const path = require('path');
// Configuration
// Use DeployGaia.s.sol latest local run
const BROADCAST_DIR = path.join(__dirname, 'contracts/broadcast/SetupDemo.s.sol/31337');
const OUTPUT_FILE = path.join(__dirname, 'frontend/src/lib/contracts.ts');
function findLatestRun() {
const runLatestPath = path.join(BROADCAST_DIR, 'run-latest.json');
if (fs.existsSync(runLatestPath)) {
return runLatestPath;
}
return null;
}
function generateConfig() {
const runFile = findLatestRun();
if (!runFile) {
console.error('❌ No run-latest.json found in', BROADCAST_DIR);
process.exit(1);
}
console.log('Reading from:', runFile);
const data = JSON.parse(fs.readFileSync(runFile, 'utf8'));
// Extract Contracts
const contracts = {};
const transactions = data.transactions || [];
// Map contract names to addresses
// We prioritize the *last* deployment of a contract with a given name
transactions.forEach(tx => {
if (tx.transactionType === 'CREATE' && tx.contractName && tx.contractAddress) {
contracts[tx.contractName] = tx.contractAddress;
}
});
console.log('Found contracts:', contracts);
const fileContent = `// This file is automatically generated by sync-contracts.js
// Do not edit manually
export const CONTRACTS = {
${Object.entries(contracts).map(([name, address]) => ` ${name}: "${address}",`).join('\n')}
} as const;
export const CHAIN_ID = ${data.chain || 31337};
`;
fs.writeFileSync(OUTPUT_FILE, fileContent);
console.log('✅ Generated frontend config at:', OUTPUT_FILE);
// Update Python Agent .env
const ENV_FILE = path.join(__dirname, 'python_agent/.env');
if (fs.existsSync(ENV_FILE)) {
let envContent = fs.readFileSync(ENV_FILE, 'utf8');
const updates = {
'PROPOSAL_MANAGER_ADDRESS': contracts['GaiaProposalManager'],
'CHARITY_REGISTRY_ADDRESS': contracts['GaiaCharityRegistry'],
'USDC_TOKEN_ADDRESS': contracts['MockERC20']
};
let updated = false;
for (const [key, value] of Object.entries(updates)) {
if (value) {
const regex = new RegExp(`^${key}=.*`, 'm');
if (regex.test(envContent)) {
envContent = envContent.replace(regex, `${key}=${value}`);
updated = true;
} else {
envContent += `\n${key}=${value}`;
updated = true;
}
}
}
if (updated) {
fs.writeFileSync(ENV_FILE, envContent);
console.log('✅ Updated Python Agent .env at:', ENV_FILE);
}
} else {
console.warn('⚠️ python_agent/.env not found, skipping update.');
}
}
generateConfig();