-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate-erd.js
executable file
·62 lines (56 loc) · 1.73 KB
/
generate-erd.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
const { writeFile, writeFileSync } = require("fs");
const IDL = require("./sdk/idl/solana_nft_programs_rewards_center_idl");
const snakeCase = (str) =>
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
const links = [
["stake_entry.pool", "stake_pool.address"],
["stake_authorization_record.pool", "stake_pool.address"],
["reward_distributor.stake_pool", "stake_pool.address"],
["reward_entry.reward_distributor", "reward_distributor.address"],
["stake_booster.stake_pool", "stake_pool.address"],
["receipt_manager.stake_pool", "stake_pool.address"],
["receipt_entry.stake_entry", "stake_entry.address"],
["reward_receipt.receipt_entry", "receipt_entry.address"],
["reward_receipt.receipt_manager", "receipt_manager.address"],
];
const renderValue = (v) => {
if (typeof v === "string") {
return v;
}
if (typeof v === "object" && "option" in v) {
return `Opt(${v.option})`;
}
if (typeof v === "object" && "vec" in v) {
return `${v.vec}[]`;
}
return v.toString();
};
const main = () => {
const outPath = `./DIAGRAM.txt`;
writeFile(outPath, "", function () {
console.log(`[reset] ${outPath}`);
});
for (let i = 0; i < IDL.accounts.length; i++) {
const { name: accountName, type } = IDL.accounts[i];
const t = `
Table ${snakeCase(accountName).slice(1)} {
address publicKey [pk]
${type.fields
.map(({ name: field, type }) => {
const link = links.find(
([src]) =>
src === `${snakeCase(accountName).slice(1)}.${snakeCase(field)}`
);
return `${snakeCase(field)} ${renderValue(type)} ${
link ? `[ref: > ${link[1].toString()}]` : ""
}`;
})
.join("\n ")}
}
`;
writeFileSync(outPath, t, {
flag: "a+",
});
}
};
main();