Skip to content

Commit 100bfb3

Browse files
gravityblastvacekj
authored andcommitted
WIP: extract _calculate logic
1 parent caa1b9b commit 100bfb3

File tree

1 file changed

+19
-151
lines changed

1 file changed

+19
-151
lines changed

src/calculator/index.ts

+19-151
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ export default class Calculator {
169169
"votes",
170170
`${this.chainId}/rounds/${this.roundId}/votes.json`
171171
);
172+
173+
return this._calculate(votes);
174+
}
175+
176+
private async _calculate(votes: Vote[]): Promise<Array<AugmentedResult>> {
172177
const applications = await this.parseJSONFile<Application>(
173178
"applications",
174179
`${this.chainId}/rounds/${this.roundId}/applications.json`
@@ -277,175 +282,38 @@ export default class Calculator {
277282
* @param potentialVotes
278283
*/
279284
async estimateMatching(
280-
potentialVotes: PotentialVote[]
285+
potentialVotes: Vote[],
286+
roundId: string,
287+
roundToken: string
281288
): Promise<MatchingEstimateResult[]> {
282289
const votes = await this.parseJSONFile<Vote>(
283290
"votes",
284291
`${this.chainId}/rounds/${this.roundId}/votes.json`
285292
);
286-
const applications = await this.parseJSONFile<Application>(
287-
"applications",
288-
`${this.chainId}/rounds/${this.roundId}/applications.json`
289-
);
290-
291-
const rounds = await this.parseJSONFile<Round>(
292-
"rounds",
293-
`${this.chainId}/rounds.json`
294-
);
295-
296-
const round = rounds.find((r: Round) => r.id === this.roundId);
297-
298-
if (round === undefined) {
299-
throw new ResourceNotFoundError("round");
300-
}
301-
302-
if (round.matchAmount === undefined) {
303-
throw new ResourceNotFoundError("round match amount");
304-
}
305-
306-
if (round.token === undefined) {
307-
throw new ResourceNotFoundError("round token");
308-
}
309-
310-
const matchAmount = BigInt(round.matchAmount);
311-
const matchTokenDecimals = BigInt(
312-
getDecimalsForToken(this.chainId, round.token)
313-
);
314-
let matchingCapAmount;
315293

316-
if (round.metadata?.quadraticFundingConfig?.matchingCap) {
317-
// round.metadata.quadraticFundingConfig.matchingCapAmount is a percentage, 0 to 100, could contain decimals
318-
matchingCapAmount =
319-
(matchAmount *
320-
BigInt(
321-
Math.trunc(
322-
Number(
323-
round.metadata?.quadraticFundingConfig?.matchingCapAmount ?? 0
324-
) * 100
325-
)
326-
)) /
327-
10000n;
328-
}
329-
330-
const votesWithCoefficients = await getVotesWithCoefficients(
331-
this.chain,
332-
round,
333-
applications,
334-
votes,
335-
this.passportProvider,
336-
{
337-
minimumAmountUSD: this.minimumAmountUSD,
338-
enablePassport: this.enablePassport,
339-
passportThreshold: this.passportThreshold,
340-
}
341-
);
342-
343-
const potentialVotesAugmented: Vote[] = await Promise.all(
344-
potentialVotes.map(async (vote) => {
345-
const { amount: amountUSD } = await this.priceProvider.convertToUSD(
346-
this.chainId,
347-
vote.token,
348-
vote.amount
349-
);
350-
351-
const { amount: amountRoundToken } =
352-
await this.priceProvider.convertFromUSD(
353-
this.chainId,
354-
round.token,
355-
amountUSD
356-
);
357-
358-
/* Find the latest approved application */
359-
const application = applications
360-
.filter(
361-
(application) =>
362-
application.metadata?.application.recipient === vote.recipient
363-
)
364-
.filter((application) => application.status === "APPROVED")
365-
.sort((a, b) => a.statusUpdatedAtBlock - b.statusUpdatedAtBlock)[0];
366-
if (!application) {
367-
throw "Couldn't find application for project";
368-
}
369-
return {
370-
amount: vote.amount.toString(),
371-
amountRoundToken: amountRoundToken.toString(),
372-
amountUSD,
373-
token: vote.token,
374-
roundId: this.roundId,
375-
voter: vote.contributor,
376-
grantAddress: vote.recipient,
377-
projectId: application.projectId,
378-
id: "",
379-
applicationId: application.id,
380-
};
381-
})
382-
);
383-
384-
const potentialVotesWithCoefficients = await getVotesWithCoefficients(
385-
this.chain,
386-
round,
387-
applications,
388-
potentialVotesAugmented,
389-
this.passportProvider,
390-
{
391-
minimumAmountUSD: this.minimumAmountUSD,
392-
enablePassport: this.enablePassport,
393-
passportThreshold: this.passportThreshold,
394-
}
395-
);
396-
397-
const contributions = this.#votesWithCoefficientToContribution(
398-
votesWithCoefficients
399-
);
400-
401-
const potentialContributions = this.#votesWithCoefficientToContribution(
402-
potentialVotesWithCoefficients
403-
);
404-
405-
const contributionsWithPotentialVotes = [
406-
...potentialContributions,
407-
...contributions,
408-
];
409-
410-
const potentialResults = linearQF(
411-
contributionsWithPotentialVotes,
412-
matchAmount,
413-
matchTokenDecimals,
414-
{
415-
minimumAmount: BigInt(this.minimumAmountUSD ?? 0),
416-
matchingCapAmount,
417-
ignoreSaturation: this.ignoreSaturation ?? false,
418-
}
419-
);
420-
421-
const currentResults = linearQF(
422-
contributions,
423-
matchAmount,
424-
matchTokenDecimals,
425-
{
426-
minimumAmount: BigInt(this.minimumAmountUSD ?? 0),
427-
matchingCapAmount,
428-
ignoreSaturation: this.ignoreSaturation ?? false,
429-
}
430-
);
294+
const currentResults = await this._calculate(votes);
295+
const potentialResults = await this._calculate([
296+
...votes,
297+
...potentialVotes,
298+
]);
431299

432300
const finalResults: MatchingEstimateResult[] = [];
433301

434-
for (const key of Object.keys(potentialResults)) {
435-
const potentialResult = potentialResults[key] ?? {};
436-
const currentResult = currentResults[key] ?? {};
302+
for (const key in potentialResults) {
303+
const potentialResult = potentialResults[key];
304+
const currentResult = currentResults[key];
437305

438306
/* Subtracting undefined from a bigint would fail,
439307
* so we explicitly subtract 0 if it's undefined */
440308
const difference =
441309
potentialResult.matched - (currentResult.matched ?? 0n);
442310
const differenceInUSD = await this.priceProvider.convertToUSD(
443311
this.chainId,
444-
round.token,
312+
roundToken,
445313
difference
446314
);
447315

448-
const recipient = potentialVotesWithCoefficients.find(
316+
const recipient = potentialVotes.find(
449317
(vote) => vote.applicationId == key
450318
)?.grantAddress;
451319

@@ -454,7 +322,7 @@ export default class Calculator {
454322
...currentResult,
455323
...potentialResult,
456324
difference,
457-
roundId: round.id,
325+
roundId,
458326
chainId: this.chainId,
459327
recipient: recipient ?? ethers.constants.AddressZero,
460328
differenceInUSD: differenceInUSD.amount,

0 commit comments

Comments
 (0)