-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (140 loc) · 4.25 KB
/
index.js
File metadata and controls
160 lines (140 loc) · 4.25 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const fs = require("fs");
const path = require("path");
const fetch = require("isomorphic-fetch");
const { BLOCK_TIMES_MS, VAULTS } = require("./constants");
const { BigNumber, ethers } = require("ethers");
const { parseUnits } = require("ethers/lib/utils");
const CHAIN_THRESHOLD_BP = 850;
Object.filter = (obj, predicate) =>
Object.keys(obj)
.filter((key) => predicate(obj[key]))
.reduce((res, key) => ((res[key] = obj[key]), res), {});
const buildMap = (keys, values) => {
const map = new Map();
for (let i = 0; i < keys.length; i++) {
map.set(keys[i], values[i]);
}
return map;
};
const QI_PER_POLYGON_BLOCK = BigNumber.from("650000000000000000");
async function main() {
const args = process.argv.slice(2);
if (args.length > 0) {
const res = await fetch("https://hub.snapshot.org/graphql", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
query: `{
proposal(id: "${args[0]}") {
choices
scores
}
}`,
}),
});
const {
data: {
proposal: { choices, scores },
},
} = await res.json();
const choiceScoreMap = buildMap(choices, scores);
const scoreSum = parseUnits(
scores.reduce((prev, curr) => (prev += curr), 0).toString()
);
let includedChainIds = [];
let chainIdScoreSumMap = new Map();
for (chainId of [
...new Set(
choices.map((choice) =>
VAULTS[choice] ? VAULTS[choice].chainId : console.log(choice)
)
),
]) {
const chainIdVaults = Object.filter(
VAULTS,
(choice) => choice.chainId == chainId
);
let chainIdSum = 0;
for (let i = 0; i < Object.keys(chainIdVaults).length; i++) {
const vault = Object.keys(chainIdVaults)[i];
chainIdSum += choiceScoreMap.get(vault);
}
let chainIdScoreSum = parseUnits(chainIdSum.toString());
chainIdScoreSumMap.set(chainId, chainIdScoreSum);
if (
parseFloat(chainIdScoreSum.toString()) /
parseFloat(scoreSum.toString()) >
CHAIN_THRESHOLD_BP / 10000
) {
includedChainIds.push(chainId);
}
}
let includedChoices = [];
for (let i = 0; i < choices.length; i++) {
const choice = choices[i];
const score = scores[i];
try {
if (includedChainIds.includes(VAULTS[choice].chainId)) {
includedChoices.push({
name: choice,
score: score,
});
}
} catch (e) {
if (
e.message == "Cannot read properties of undefined (reading 'chainId')"
) {
console.error(`Snapshot option ${choice} not found in constants.js`);
}
}
}
let includedChoicesScoreSum = includedChoices.reduce(
(prev, curr) => (prev += curr.score),
0
);
let borrowIncentives = [];
for (let i = 0; i < includedChoices.length; i++) {
const choice = includedChoices[i];
const { name, score } = choice;
const meta = VAULTS[name];
if (score > 0) {
const reward = QI_PER_POLYGON_BLOCK.mul(
BigNumber.from(BLOCK_TIMES_MS[meta.chainId])
)
.mul(parseUnits(score.toString()))
.div(parseUnits(includedChoicesScoreSum.toString()))
.div(BLOCK_TIMES_MS[137]);
const minCdr = meta.minCdr / 100 + 0.25;
const maxCdr = meta.minCdr / 100 + 2.7;
borrowIncentives.push({
name,
vaultAddress: meta.address,
minCdr,
maxCdr,
rewardPerBlock: reward.toString(),
collateralDecimals: meta.collateralDecimals,
startBlock: 18840162,
endBlock: 99999999,
chainId: meta.chainId.toString(),
});
}
}
let values = {};
[...new Set(borrowIncentives.map((b) => b.chainId))].forEach(
(chainId) =>
(values[chainId] = borrowIncentives.filter(
(incentive) => incentive.chainId == chainId
))
);
const fileName = path.join(__dirname, `/results/${args[0]}.json`);
const output = JSON.stringify({
...values,
});
fs.writeFileSync(fileName, output);
} else {
console.log("Usage: node index.js <ProposalId>");
}
}
main();