This repository was archived by the owner on May 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodegen.js
More file actions
110 lines (94 loc) · 3.07 KB
/
Copy pathcodegen.js
File metadata and controls
110 lines (94 loc) · 3.07 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
#!/usr/bin/env node
const path = require('node:path');
const fs = require('node:fs/promises');
const ABI_WHITELIST = {
CoreProxy: {
getAccountOwner: true,
createAccount: true,
AccountCreated: true,
getApprovedPools: true,
getPreferredPool: true,
getPoolName: true,
},
AccountProxy: {
balanceOf: true,
tokenOfOwnerByIndex: true,
},
};
async function codegen() {
const deploymentsDir = path.dirname(require.resolve('@synthetixio/v3-contracts'));
const supportedDeployments = (await fs.readdir(deploymentsDir, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => {
const [chainIdString, preset] = dirent.name.split('-');
const chainId = chainIdString && parseInt(chainIdString);
if (chainId && preset) {
return { chainId, preset };
}
return null;
})
.filter(Boolean);
console.log({ supportedDeployments });
await fs.rm('lib/deployments', { force: true, recursive: true });
await fs.mkdir('lib/deployments', { recursive: true });
const index = [
//
'const deployments = {};',
];
const AllErrors = new Set();
for (const { chainId, preset } of supportedDeployments) {
await fs.mkdir(`./lib/deployments/${chainId}-${preset}`, { recursive: true });
const meta = JSON.parse(
await fs.readFile(
require.resolve(`@synthetixio/v3-contracts/${chainId}-${preset}/meta.json`),
'utf8'
)
);
index.push('');
index.push(`deployments["${chainId}"] = {};`);
index.push(`deployments["${chainId}"]["${preset}"] = {};`);
for (const [contractName, address] of Object.entries(meta.contracts)) {
console.log({ chainId, preset, contractName, address });
const fullAbi = JSON.parse(
await fs.readFile(
require.resolve(`@synthetixio/v3-contracts/${chainId}-${preset}/${contractName}.json`),
'utf8'
)
);
const abi = fullAbi.filter(({ name }) => ABI_WHITELIST?.[contractName]?.[name] === true);
await fs.writeFile(
`./lib/deployments/${chainId}-${preset}/${contractName}.js`,
`
exports.address = ${JSON.stringify(address)};
exports.abi = ${JSON.stringify(abi)};
`,
'utf8'
);
index.push(
`deployments["${chainId}"]["${preset}"]["${contractName}"] = require('./${chainId}-${preset}/${contractName}')`
);
}
// Generate AllErrors
const abi = JSON.parse(
await fs.readFile(
require.resolve(`@synthetixio/v3-contracts/${chainId}-${preset}/AllErrors.readable.json`),
'utf8'
)
);
for (const e of abi) {
AllErrors.add(e);
}
}
const { Interface } = require('@ethersproject/abi');
await fs.writeFile(
'./lib/deployments/AllErrors.js',
`
exports.abi = ${new Interface(Array.from(AllErrors)).format('json')};
`,
'utf8'
);
index.push(`deployments["AllErrors"] = require('./AllErrors')`);
index.push('exports.deployments = deployments;');
await fs.writeFile('./lib/deployments/index.js', index.join('\n'), 'utf8');
}
codegen();