-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateMerkle.js
828 lines (688 loc) · 23.4 KB
/
generateMerkle.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
const { MerkleTree } = require("merkletreejs");
const keccak256 = require("keccak256");
const fs = require('fs');
const orderBy = require("lodash/orderBy");
const { gql, request } = require("graphql-request");
const axios = require('axios').default;
const { utils, BigNumber } = require("ethers");
const { formatUnits, parseEther } = require("viem");
const { parse } = require("csv-parse/sync");
const { parseAbi, encodeFunctionData } = require("viem");
const MERKLE_ADDRESS = "0x03E34b085C52985F6a5D27243F20C84bDdc01Db4";
const STASH_CONTROLLER_ADDRESS = "0x2f18e001B44DCc1a1968553A2F32ab8d45B12195";
const SNAPSHOT_ENDPOINT = "https://hub.snapshot.org/graphql";
const ENDPOINT_DELEGATORS = "https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot";
const DELEGATION_ADDRESS = "0x52ea58f4FC3CEd48fa18E909226c1f8A0EF887DC";
const AGNOSTIC_ENDPOINT = "https://proxy.eu-02.agnostic.engineering/query";
const AGNOSTIC_API_KEY = "Fr2LXSVvKCfmXse8JQJiJBLHY9ujU3YZf8Kr6TDDh4Sw";
const SDCRV_SPACE = "sdcrv.eth";
const SDBAL_SPACE = "sdbal.eth";
const SDFXS_SPACE = "sdfxs.eth";
const SDANGLE_SPACE = "sdangle.eth";
const SPACES = [SDCRV_SPACE, SDBAL_SPACE, SDFXS_SPACE, SDANGLE_SPACE];
const LABELS_TO_SPACE = {
"frax": SDFXS_SPACE,
"curve": SDCRV_SPACE,
"balancer": SDBAL_SPACE,
"angle": SDANGLE_SPACE
};
const SPACES_TOKENS = {
[SDCRV_SPACE]: "0xD1b5651E55D4CeeD36251c61c50C889B36F6abB5",
[SDBAL_SPACE]: "0xF24d8651578a55b0C119B9910759a351A3458895",
[SDFXS_SPACE]: "0x402F878BDd1f5C66FdAF0fabaBcF74741B68ac36",
[SDANGLE_SPACE]: "0x752B4c6e92d96467fE9b9a2522EF07228E00F87c"
};
const SPACES_SYMBOL = {
[SDCRV_SPACE]: "sdCRV",
[SDBAL_SPACE]: "sdBAL",
[SDFXS_SPACE]: "sdFXS",
[SDANGLE_SPACE]: "sdANGLE"
};
const SPACES_IMAGE = {
[SDCRV_SPACE]: "https://assets.coingecko.com/coins/images/27756/small/scCRV-2.png?1665654580",
[SDBAL_SPACE]: "https://assets.coingecko.com/coins/images/11683/small/Balancer.png?1592792958",
[SDFXS_SPACE]: "https://assets.coingecko.com/coins/images/13423/small/Frax_Shares_icon.png?1679886947",
[SDANGLE_SPACE]: "https://assets.coingecko.com/coins/images/19060/small/ANGLE_Token-light.png?1666774221"
};
const SPACES_UNDERLYING_TOKEN = {
[SDCRV_SPACE]: "0xd533a949740bb3306d119cc777fa900ba034cd52",
[SDBAL_SPACE]: "0x5c6ee304399dbdb9c8ef030ab642b10820db8f56", //80BAL instead of bal
[SDFXS_SPACE]: "0x3432b6a60d23ca0dfca7761b7ab56459d9c964d0",
[SDANGLE_SPACE]: "0x31429d1856ad1377a8a0079410b297e1a9e214c2"
};
const abi = parseAbi([
'function multiFreeze(address[] tokens) public',
'function multiSet(address[] tokens, bytes32[] roots) public',
]);
const main = async () => {
const csvResult = extractCSV();
// Fetch last merkle
const { data: lastMerkles } = await axios.get("https://raw.githubusercontent.com/StakeDAO/bribes/main/merkle.json");
// Fetch last proposal ids
const proposalIdPerSpace = await fetchLastProposalsIds();
const newMerkles = [];
const delegationAPRs = {};
const toFreeze = [];
const toSet = [];
for (const space of Object.keys(proposalIdPerSpace)) {
checkSpace(space);
// If no bribe to distribute to this space => skip
if(!csvResult[space]) {
continue;
}
const tokenPrice = await getTokenPrice(space);
const id = proposalIdPerSpace[space];
// Get the proposal to find the create timestamp
const proposal = await getProposal(id);
// Here, we should have delegation voter + all other voters
// Object with vp property
let voters = await getVoters(id);
voters = await getVoterVotingPower(proposal, voters);
// Get all delegator addresses
const delegators = await getAllDelegators(proposal.created, space);
// Get voting power for all delegator
// Map of address => VotingPower
const delegatorsVotingPower = await getDelegationVotingPower(proposal, delegators.concat([DELEGATION_ADDRESS]));
// Reduce delegator voting power if some guys voted directly
for (const delegatorAddress of Object.keys(delegatorsVotingPower)) {
const da = delegatorAddress.toLowerCase();
for (const vote of voters) {
if (vote.voter.toLowerCase() === da) {
delegatorsVotingPower[delegatorAddress] -= vote.vp;
if (delegatorsVotingPower[delegatorAddress] <= 0) {
delegatorsVotingPower[delegatorAddress] = 0;
}
break;
}
}
}
const delegatorSumVotingPower = Object.values(delegatorsVotingPower).reduce((acc, vp) => acc + vp, 0.0);
let delegationVote = voters.find((v) => v.voter.toLowerCase() === DELEGATION_ADDRESS.toLowerCase());
//const delegatorSumVotingPower = delegationVote;
if (!delegationVote) {
throw new Error("No delegation vote for " + space + " - " + id);
}
/*if(delegationVote.vp !== delegatorSumVotingPower) {
throw new Error("Voting power delegation !== voting power sum all delegators - " + delegationVote.vp + " vs " + delegatorSumVotingPower + " - " + space + " - " + id);
}*/
// We can have sum delegation lower than delegation vp
//delegationVote.vp = delegatorSumVotingPower;
delegationVote.totalRewards = 0;
// Extract address per choice
// The address will be only the strat of the address
let addressesPerChoice = extractProposalChoices(proposal);
// Get only choices where we have a bribe reward
// Now, the address is the complete address
// Map -> gauge address => {index : choice index, amount: sdTKN }
addressesPerChoice = getChoiceWhereExistsBribe(addressesPerChoice, csvResult[space]);
for (const gaugeAddress of Object.keys(addressesPerChoice)) {
const index = addressesPerChoice[gaugeAddress].index;
const sdTknRewardAmount = addressesPerChoice[gaugeAddress].amount;
// Calculate the total VP used to vote for this gauge across all voters
let totalVP = 0;
for (const voter of voters) {
let vpChoiceSum = 0;
let currentChoiceIndex = 0;
for (const choiceIndex of Object.keys(voter.choice)) {
if (index === parseInt(choiceIndex)) {
currentChoiceIndex = voter.choice[choiceIndex];
}
vpChoiceSum += voter.choice[choiceIndex];
}
if (currentChoiceIndex === 0) {
continue;
}
const ratio = currentChoiceIndex * 100 / vpChoiceSum;
totalVP += voter.vp * ratio / 100;
}
// We split the bribe reward amount amount between voters
for (const voter of voters) {
// Sum choice votes
let vpChoiceSum = 0;
let currentChoiceIndex = 0;
for (const choiceIndex of Object.keys(voter.choice)) {
if (index === parseInt(choiceIndex)) {
currentChoiceIndex = voter.choice[choiceIndex];
}
vpChoiceSum += voter.choice[choiceIndex];
}
if (currentChoiceIndex === 0) {
// User not voted for this gauge
continue;
}
// User voted for this gauge address
// We calculate his vp associated
const ratio = currentChoiceIndex * 100 / vpChoiceSum;
const vpUsed = voter.vp * ratio / 100;
const totalVPRatio = vpUsed * 100 / totalVP;
const amountEarned = totalVPRatio * sdTknRewardAmount / 100;
if (voter.totalRewards === undefined) {
voter.totalRewards = 0;
}
voter.totalRewards += amountEarned;
}
}
// Now we have all rewards across all voters
// But one voter is the delegation, we have to split his rewards across the delegation
delegationVote = voters.find((v) => v.voter.toLowerCase() === DELEGATION_ADDRESS.toLowerCase());
delegationVote.delegation = {};
for (const delegatorAddress of Object.keys(delegatorsVotingPower)) {
const vp = delegatorsVotingPower[delegatorAddress];
const ratioVp = vp * 100 / delegatorSumVotingPower;
// This user should receive ratioVp% of all rewards
if(space === SDCRV_SPACE && delegatorAddress.toLowerCase() === "0x1c0d72a330f2768daf718def8a19bab019eead09".toLowerCase()) {
console.log("Concentrator vp : ", vp);
console.log("Delegation vp : ", delegatorSumVotingPower)
console.log("Total rewards : ", delegationVote.totalRewards);
console.log("Ratio % : ", ratioVp)
console.log("Ratio rewards : ", ratioVp * delegationVote.totalRewards / 100)
console.log("Ratio rewards new : ",vp * delegationVote.totalRewards / delegatorSumVotingPower)
}
delegationVote.delegation[delegatorAddress.toLowerCase()] = ratioVp * delegationVote.totalRewards / 100;
}
// Check split
/*const totalSplitDelegationRewards = Object.values(delegationVote.delegation).reduce((acc, d) => acc + d, 0.0) || 0;
if (parseInt(delegationVote.totalRewards) !== parseInt(totalSplitDelegationRewards)) {
throw new Error("Delegation " + space + " - " + id + " split rewards wrong " + delegationVote.totalRewards + " - " + totalSplitDelegationRewards);
}*/
// Calculate delegation apr
let delegationAPR = ((Number(delegationVote.totalRewards) * 26 * tokenPrice) / delegationVote.vp) * 100 / tokenPrice;
if (space === SDFXS_SPACE) {
delegationAPR *= 4;
}
delegationAPRs[space] = delegationAPR;
// Create a map with userAddress => reward amount
const userRewards = {};
for (const voter of voters) {
if (!voter.totalRewards) {
continue
}
if (voter.delegation) {
for (const delegatorAddress of Object.keys(voter.delegation)) {
const amount = voter.delegation[delegatorAddress.toLowerCase()];
if (userRewards[delegatorAddress.toLowerCase()]) {
userRewards[delegatorAddress.toLowerCase()] += amount;
} else {
userRewards[delegatorAddress.toLowerCase()] = amount;
}
}
} else {
if (userRewards[voter.voter.toLowerCase()]) {
userRewards[voter.voter.toLowerCase()] += voter.totalRewards;
} else {
userRewards[voter.voter.toLowerCase()] = voter.totalRewards;
}
}
}
// New distribution is split here
// We have to sum with old distribution if users don't claim
const tokenToDistribute = SPACES_TOKENS[space];
const lastMerkle = lastMerkles.find((m) => m.address.toLowerCase() === tokenToDistribute.toLowerCase());
if (lastMerkle) {
const usersClaimedAddress = await getAllAccountClaimedSinceLastFreezeWithAgnostic(tokenToDistribute);
const userAddressesLastMerkle = Object.keys(lastMerkle.merkle);
for (const userAddress of userAddressesLastMerkle) {
const userAddressLowerCase = userAddress.toLowerCase();
const isClaimed = usersClaimedAddress[userAddressLowerCase];
// If user didn't claim, we add the previous rewards to new one
if (!isClaimed) {
const leaf = lastMerkle.merkle[userAddress];
const amount = parseFloat(formatUnits(BigNumber.from(leaf.amount), 18));
if (userRewards[userAddressLowerCase]) {
userRewards[userAddressLowerCase] += amount;
} else {
userRewards[userAddressLowerCase] = amount;
}
}
}
}
// Since this point, userRewards map contains the new reward amount for each user
// We have to generate the merkle
const userRewardAddresses = Object.keys(userRewards);
const elements = [];
for (let i = 0; i < userRewardAddresses.length; i++) {
const userAddress = userRewardAddresses[i];
const amount = parseEther(userRewards[userAddress.toLowerCase()].toString());
elements.push(utils.solidityKeccak256(["uint256", "address", "uint256"], [i, userAddress.toLowerCase(), BigNumber.from(amount)]));
}
const merkleTree = new MerkleTree(elements, keccak256, { sort: true });
const merkle = {};
let totalAmount = BigNumber.from(0);
for (let i = 0; i < userRewardAddresses.length; i++) {
const userAddress = userRewardAddresses[i];
const amount = BigNumber.from(parseEther(userRewards[userAddress.toLowerCase()].toString()));
totalAmount = totalAmount.add(amount);
merkle[userAddress.toLowerCase()] = {
index: i,
amount,
proof: merkleTree.getHexProof(elements[i]),
};
}
newMerkles.push({
"symbol": SPACES_SYMBOL[space],
"address": tokenToDistribute,
"image": SPACES_IMAGE[space],
"merkle": merkle,
root: merkleTree.getHexRoot(),
"total": totalAmount
});
toFreeze.push(tokenToDistribute);
toSet.push(merkleTree.getHexRoot());
}
const freezeData = encodeFunctionData({
abi,
functionName: 'multiFreeze',
args: [toFreeze],
});
const multiSetData = encodeFunctionData({
abi,
functionName: 'multiSet',
args: [toFreeze, toSet],
})
console.log("To freeze :");
console.log("Contract : " + STASH_CONTROLLER_ADDRESS);
console.log("Data : ");
console.log(freezeData);
console.log("----------");
console.log("New roots :");
console.log("Contract : " + STASH_CONTROLLER_ADDRESS);
console.log("Data : ");
console.log(multiSetData);
for (const lastMerkle of lastMerkles) {
let found = false;
for (const newMerkle of newMerkles) {
if (newMerkle.address.toLowerCase() === lastMerkle.address.toLowerCase()) {
found = true;
break;
}
}
if (!found) {
newMerkles.push(lastMerkle);
}
}
fs.writeFileSync(`./merkle.json`, JSON.stringify(newMerkles));
fs.writeFileSync(`./delegationsAPRs.json`, JSON.stringify(delegationAPRs));
}
const extractCSV = () => {
const cvsFile = fs.readFileSync("./report.csv");
let records = parse(cvsFile, {
columns: true,
skip_empty_lines: true,
delimiter: ";",
});
const newRecords = [];
for(const row of records) {
let obj = {};
for(const key of Object.keys(row)) {
obj[key.toLowerCase()] = row[key];
}
newRecords.push(obj);
}
records = newRecords;
const response = {};
for (const row of records) {
const space = LABELS_TO_SPACE[row["protocol"]];
if (!space) {
throw new Error("can't find space for " + row["protocol"]);
}
if (!response[space]) {
response[space] = {};
}
const gaugeAddress = row["Gauge Address".toLowerCase()];
if (!gaugeAddress) {
throw new Error("can't find gauge address for " + row["protocol"]);
}
if (!response[space][gaugeAddress]) {
response[space][gaugeAddress] = 0;
}
response[space][gaugeAddress] += parseFloat(row["Reward sd Value".toLowerCase()])
}
return response;
};
/**
* Fetch last proposal id for all spaces
*/
const fetchLastProposalsIds = async () => {
const query = gql`
query Proposals {
proposals(
first: 1000
skip: 0
orderBy: "created",
orderDirection: desc,
where: {
space_in: [${SPACES.map((space) => '"' + space + '"').join(",")}]
type: "weighted"
}
) {
id
title
space {
id
}
}
}
`;
const result = await request(SNAPSHOT_ENDPOINT, query);
const proposals = result.proposals.filter((proposal) => proposal.title.indexOf("Gauge vote") > -1);
const proposalIdPerSpace = {};
for (const space of SPACES) {
let firstFound = false;
for (const proposal of proposals) {
if (proposal.space.id !== space) {
continue;
}
if (firstFound) {
proposalIdPerSpace[space] = proposal.id;
break;
}
firstFound = true;
}
}
return proposalIdPerSpace;
}
/**
* Get all voters for a proposal
*/
const getVoters = async (proposalId) => {
const QUERY_VOTES = gql`
query Proposal(
$proposal: String!
$orderBy: String
$orderDirection: OrderDirection
$created: Int
) {
votes(
first: 1000
where: { proposal: $proposal, vp_gt: 0, created_lt: $created }
orderBy: $orderBy
orderDirection: $orderDirection
) {
id
ipfs
voter
created
choice
vp
vp_by_strategy
}
}
`;
let votes = [];
let run = true;
let created = null
// Fetch all data
do {
let params = {
proposal: proposalId,
orderBy: "created",
orderDirection: "desc",
};
if (created) {
params["created"] = created;
}
const result = await request(SNAPSHOT_ENDPOINT, QUERY_VOTES, params);
if (result.votes?.length > 0) {
votes = votes.concat(result.votes);
created = result.votes[result.votes.length - 1].created;
}
else {
run = false;
}
} while (run);
return votes;
}
const getVoterVotingPower = async (proposal, votes) => {
const votersAddresses = votes.map((v) => v.voter);
const { data } = await axios.post(
"https://score.snapshot.org/api/scores",
{
params: {
network: "1",
snapshot: parseInt(proposal.snapshot),
strategies: proposal.strategies,
space: proposal.space.id,
addresses: votersAddresses
},
},
);
const scores = votes.map((vote) => {
let vp = 0;
if (vote.vp > 0) {
vp = vote.vp;
} else {
vp = data?.result?.scores?.[0]?.[vote.voter] || data?.result?.scores?.[1]?.[vote.voter];
}
return { ...vote, vp };
});
return orderBy(scores, "vp", "desc");
}
/**
* Fetch the proposal
*/
const getProposal = async (idProposal) => {
const QUERY_PROPOSAL = gql`
query Proposal(
$id: String!
) {
proposal(id: $id) {
id
ipfs
title
body
start
end
state
author
created
choices
snapshot
type
strategies {
name
params
}
space {
id
name
members
avatar
symbol
}
scores_state
scores_total
scores
votes
}
}
`;
const result = await request(SNAPSHOT_ENDPOINT, QUERY_PROPOSAL, {
id: idProposal,
});
return result.proposal;
}
const getAllDelegators = async (proposalCreatedTimestamp, space) => {
let delegatorAddresses = [];
let run = true;
let skip = 0;
const DELEGATIONS_QUERY = gql`
query Proposal(
$skip: Int
$timestamp: Int
$space: String
) {
delegations(first: 1000 skip: $skip where: {
space: $space
delegate:"${DELEGATION_ADDRESS}"
timestamp_lte: $timestamp
}) {
delegator
space
delegate
}
}
`;
// Fetch all data
do {
const result = await request(ENDPOINT_DELEGATORS, DELEGATIONS_QUERY, { space, skip, timestamp: proposalCreatedTimestamp });
if (result.delegations?.length > 0) {
delegatorAddresses = delegatorAddresses.concat(result.delegations.map((d) => d.delegator));
skip += 1000;
}
else {
run = false;
}
} while (run);
return delegatorAddresses;
};
const getDelegationVotingPower = async (proposal, delegatorAddresses) => {
const { data } = await axios.post(
"https://score.snapshot.org/api/scores",
{
params: {
network: "1",
snapshot: parseInt(proposal.snapshot),
strategies: proposal.strategies,
space: proposal.space.id,
addresses: delegatorAddresses
},
},
);
return { ...data?.result?.scores[0], ...data?.result?.scores[1] };
}
/**
* For each proposal choice, extract his gauge address with his index
*/
const extractProposalChoices = (proposal) => {
const SEP = " - 0x";
const addressesPerChoice = {};
for (let i = 0; i < proposal.choices.length; i++) {
const choice = proposal.choices[i];
if(choice.indexOf("Current Weights") > -1 || choice.indexOf("Paste") > -1 || choice.indexOf("Total Percentage") > -1) {
continue;
}
const start = choice.indexOf(" - 0x");
if (start === -1) {
throw new Error("Impossible to parse choice : " + choice);
}
const end = choice.indexOf("…", start);
if (end === -1) {
throw new Error("Impossible to parse choice : " + choice);
}
const address = choice.substring(start + SEP.length - 2, end);
addressesPerChoice[address] = i + 1;
}
return addressesPerChoice;
};
const getChoiceWhereExistsBribe = (addressesPerChoice, cvsResult) => {
const newAddressesPerChoice = {};
if (!cvsResult) {
return newAddressesPerChoice;
}
const cvsResultLowerCase = {};
for(const key of Object.keys(cvsResult)) {
cvsResultLowerCase[key.toLowerCase()] = cvsResult[key];
}
const addresses = Object.keys(cvsResultLowerCase).map((addr) => addr.toLowerCase());
for (const key of Object.keys(addressesPerChoice)) {
const k = key.toLowerCase();
for (const addr of addresses) {
if (addr.indexOf(k) === -1) {
continue;
}
newAddressesPerChoice[addr] = {
index: addressesPerChoice[key],
amount: cvsResultLowerCase[addr]
};
break;
}
}
if (Object.keys(newAddressesPerChoice).length !== addresses.length) {
throw new Error("Error when get complete gauge address");
}
return newAddressesPerChoice;
};
const DATE_LAST_CLAIM_QUERY = `
SELECT
timestamp
FROM evm_events_ethereum_mainnet
WHERE
address = '${MERKLE_ADDRESS}' and
signature = 'Claimed(address,uint256,uint256,address,uint256)'
ORDER BY timestamp DESC
LIMIT 1
`;
const DATE_LAST_UPDATE_QUERY = (timestamp, tokenAddress) => `
SELECT
timestamp
FROM evm_events_ethereum_mainnet
WHERE
address = '${MERKLE_ADDRESS}' and
timestamp < '${timestamp}' and
input_0_value_address = '${tokenAddress}' and
signature = 'MerkleRootUpdated(address,bytes32,uint256)'
ORDER BY timestamp DESC
LIMIT 1
`;
const ALL_CLAIMED_QUERY = (since, end, tokenAddress) => `
SELECT
input_3_value_address as user
FROM evm_events_ethereum_mainnet
WHERE
address = '${MERKLE_ADDRESS}' and
timestamp > '${since}' and
timestamp <= '${end}' and
input_0_value_address = '${tokenAddress}' and
signature = 'Claimed(address,uint256,uint256,address,uint256)'
ORDER BY timestamp DESC
`;
const getAllAccountClaimedSinceLastFreezeWithAgnostic = async (tokenAddress) => {
const resp = {};
const lastClaim = await agnosticFetch(DATE_LAST_CLAIM_QUERY);
const lastUpdate = await agnosticFetch(DATE_LAST_UPDATE_QUERY(lastClaim[0][0], tokenAddress));
const lastClaimTimestamp = lastClaim[0][0];
const lastUpdateTimestamp = lastUpdate[0][0];
const allClaimed = await agnosticFetch(ALL_CLAIMED_QUERY(lastUpdateTimestamp, lastClaimTimestamp, tokenAddress));
if (!allClaimed) {
return resp;
}
for (const row of allClaimed) {
resp[row[0].toLowerCase()] = true;
}
return resp
}
const agnosticFetch = async (query) => {
try {
const response = await axios.post(AGNOSTIC_ENDPOINT, query, {
headers: {
'Authorization': `${AGNOSTIC_API_KEY}`,
"Cache-Control": "max-age=300"
}
});
return response.data.rows;
}
catch (e) {
console.error(e);
return [];
}
}
const getTokenPrice = async (space) => {
const key = `ethereum:${SPACES_UNDERLYING_TOKEN[space]}`;
const resp = await axios.get(`https://coins.llama.fi/prices/current/${key}`);
return resp.data.coins[key].price;
}
const checkSpace = (space) => {
if (!SPACES_SYMBOL[space]) {
throw new Error("No symbol defined for space " + space);
}
if (!SPACES_IMAGE[space]) {
throw new Error("No image defined for space " + space);
}
if (!SPACES_UNDERLYING_TOKEN[space]) {
throw new Error("No underlying token defined for space " + space);
}
if (!SPACES_TOKENS[space]) {
throw new Error("No sdToken defined for space " + space);
}
}
main();