@@ -169,6 +169,11 @@ export default class Calculator {
169
169
"votes" ,
170
170
`${ this . chainId } /rounds/${ this . roundId } /votes.json`
171
171
) ;
172
+
173
+ return this . _calculate ( votes ) ;
174
+ }
175
+
176
+ private async _calculate ( votes : Vote [ ] ) : Promise < Array < AugmentedResult > > {
172
177
const applications = await this . parseJSONFile < Application > (
173
178
"applications" ,
174
179
`${ this . chainId } /rounds/${ this . roundId } /applications.json`
@@ -277,175 +282,38 @@ export default class Calculator {
277
282
* @param potentialVotes
278
283
*/
279
284
async estimateMatching (
280
- potentialVotes : PotentialVote [ ]
285
+ potentialVotes : Vote [ ] ,
286
+ roundId : string ,
287
+ roundToken : string
281
288
) : Promise < MatchingEstimateResult [ ] > {
282
289
const votes = await this . parseJSONFile < Vote > (
283
290
"votes" ,
284
291
`${ this . chainId } /rounds/${ this . roundId } /votes.json`
285
292
) ;
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 ;
315
293
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
+ ] ) ;
431
299
432
300
const finalResults : MatchingEstimateResult [ ] = [ ] ;
433
301
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 ] ;
437
305
438
306
/* Subtracting undefined from a bigint would fail,
439
307
* so we explicitly subtract 0 if it's undefined */
440
308
const difference =
441
309
potentialResult . matched - ( currentResult . matched ?? 0n ) ;
442
310
const differenceInUSD = await this . priceProvider . convertToUSD (
443
311
this . chainId ,
444
- round . token ,
312
+ roundToken ,
445
313
difference
446
314
) ;
447
315
448
- const recipient = potentialVotesWithCoefficients . find (
316
+ const recipient = potentialVotes . find (
449
317
( vote ) => vote . applicationId == key
450
318
) ?. grantAddress ;
451
319
@@ -454,7 +322,7 @@ export default class Calculator {
454
322
...currentResult ,
455
323
...potentialResult ,
456
324
difference,
457
- roundId : round . id ,
325
+ roundId,
458
326
chainId : this . chainId ,
459
327
recipient : recipient ?? ethers . constants . AddressZero ,
460
328
differenceInUSD : differenceInUSD . amount ,
0 commit comments