-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate-rewards.ts
More file actions
107 lines (89 loc) · 3.07 KB
/
Copy pathvalidate-rewards.ts
File metadata and controls
107 lines (89 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
import * as fs from "fs/promises";
import { Address } from "viem";
import { createClient, getChain } from "./blockchain";
import * as github from "./github";
import * as messages from "./messages";
import { Entity } from "./validate-fs";
export type RewardsContract = {
address: string;
type: string;
};
export type MetadataWithRewards = {
rewards?: RewardsContract[];
[key: string]: unknown;
};
const isEntityAbi = [
{
inputs: [{ internalType: "address", name: "entity_", type: "address" }],
name: "isEntity",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "view",
type: "function",
},
] as const;
const vaultAbi = [
{
inputs: [],
name: "VAULT",
outputs: [{ internalType: "address", name: "", type: "address" }],
stateMutability: "view",
type: "function",
},
] as const;
export const validateRewards = async ({
entityId: vaultAddress,
metadata: metadataPath,
entityType,
}: Entity) => {
if (!metadataPath || entityType !== "vaults") {
return;
}
const chain = getChain();
const client = createClient();
const rewardsFactory = github.getInput("rewards-factory", {
required: false,
});
if (!rewardsFactory) {
return;
}
const metadataContent = await fs.readFile(metadataPath, "utf8");
const metadata: MetadataWithRewards = JSON.parse(metadataContent);
if (!metadata.rewards || metadata.rewards.length === 0) {
return;
}
for (const reward of metadata.rewards) {
if (reward.type !== "defaultStakingRewardsV2") {
await github.addComment(messages.invalidRewardsType(reward.address, reward.type));
throw new Error(
`Rewards contract \`${reward.address}\` has invalid type \`${reward.type}\`. Expected: defaultStakingRewardsV2`,
);
}
const isEntity = await client.readContract({
address: rewardsFactory as Address,
abi: isEntityAbi,
functionName: "isEntity",
args: [reward.address as Address],
});
if (!isEntity) {
await github.addComment(
messages.rewardsNotFromFactory(reward.address, rewardsFactory, chain.name),
);
throw new Error(
`Rewards contract \`${reward.address}\` is not deployed by the rewards factory \`${rewardsFactory}\` on ${chain.name} network`,
);
}
const rewardsVault = await client.readContract({
address: reward.address as Address,
abi: vaultAbi,
functionName: "VAULT",
});
if (rewardsVault.toLowerCase() !== vaultAddress.toLowerCase()) {
await github.addComment(
messages.rewardsVaultMismatch(reward.address, rewardsVault, vaultAddress),
);
throw new Error(
`Rewards contract \`${reward.address}\` is associated with vault \`${rewardsVault}\`, but expected \`${vaultAddress}\``,
);
}
}
};