/
index.ts
69 lines (62 loc) · 1.83 KB
/
index.ts
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
import { getDelegations } from '../../utils/delegation';
import { getScoresDirect } from '../../utils';
export const author = '0xButterfield';
export const version = '0.1.0';
export const dependOnOtherAddress = true;
export async function strategy(
space,
network,
provider,
addresses,
options,
snapshot
) {
const invalidStrategies = [
'{"name":"erc20-balance-of","params":{"symbol":"HOP","address":"0xed8Bdb5895B8B7f9Fdb3C087628FD8410E853D48","decimals":18}}' //https://snapshot.org/#/hop.eth/proposal/0x603f0f6e54c7be8d5db7e16ae7145e6df4b439b8aac49654cdfd6b0c03eb6492
];
if (
options.strategies.some((s) =>
invalidStrategies.includes(JSON.stringify(s))
)
)
return {};
const delegationSpace = options.delegationSpace || space;
const delegations = await getDelegations(
delegationSpace,
network,
addresses,
snapshot
);
if (Object.keys(delegations).length === 0) return {};
const scores = (
await getScoresDirect(
space,
options.strategies,
network,
provider,
Object.values(delegations).reduce((a: string[], b: string[]) =>
a.concat(b)
),
snapshot
)
).filter((score) => Object.keys(score).length !== 0);
const addressScores = Object.fromEntries(
addresses.map((address) => {
const addressScore = delegations[address]
? delegations[address].reduce(
(a, b) => a + scores.reduce((x, y) => (y[b] ? x + y[b] : x), 0),
0
)
: 0;
return [address, addressScore];
})
);
const totalScore = Object.values(addressScores).reduce((a, b) => a + b, 0);
const maxScore = (options.capPercentage * totalScore) / 100;
return Object.fromEntries(
Object.entries(addressScores).map(([address, addressScore]) => [
address,
Math.min(addressScore, maxScore)
])
);
}