Skip to content

Commit

Permalink
Fix Actual Matching Cap (#1508)
Browse files Browse the repository at this point in the history
* Fix projectActualserviceView

* fix stream balance depleted issue (#1496)

Co-authored-by: mohammadranjbarz <[email protected]>

* rebuild

* refresh and fetch user address separately (#1499)

* Added pg_trgm extension migration (#1502)

* fix actual matching cap (#1507)

---------

Co-authored-by: Mohammad Ranjbar Z <[email protected]>
Co-authored-by: Ramin <[email protected]>
Co-authored-by: Amin Latifi <[email protected]>
  • Loading branch information
4 people authored Apr 25, 2024
1 parent 4fae7b7 commit e74f203
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
11 changes: 11 additions & 0 deletions migration/1713859866338-enable_pg_trgm_extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class EnablePgTrgmExtension1713859866338 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('CREATE EXTENSION IF NOT EXISTS pg_trgm');
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP EXTENSION IF EXISTS pg_trgm');
}
}
21 changes: 13 additions & 8 deletions src/services/projectViewsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const getQfRoundActualDonationDetails = async (
SELECT *
FROM project_actual_matching_view
WHERE "qfRoundId" = ${qfRoundId}
ORDER BY "donationsSqrtRootSumSquared" DESC NULLS LAST
`)) as ProjectActualMatchingView[];
const totalReward = qfRound!.allocatedFund;
const maxRewardShare = Number(qfRound?.maximumReward || 0.2);
Expand All @@ -55,25 +56,29 @@ export const getQfRoundActualDonationDetails = async (
}, 0);
const weightCap = totalWeight * maxRewardShare;
const fundingCap = totalReward * maxRewardShare;
const countOfProjectsWithMaxShare = rows.filter(currentRow => {
return currentRow.donationsSqrtRootSumSquared >= weightCap;
}).length;
let remainingWeight = totalWeight;
const remainingFunds =
totalReward - countOfProjectsWithMaxShare * fundingCap;
let remainingFunds = totalReward;

const result = [] as ProjectActualMatchingView[];
// Fill rows for those wight are more than maxRewardShare
// Fill rows for those weight are more than maxRewardShare
for (const row of rows) {
if (row.donationsSqrtRootSumSquared / totalWeight >= maxRewardShare) {
const matchingFund =
(row.donationsSqrtRootSumSquared / remainingWeight) * remainingFunds;
if (matchingFund >= fundingCap) {
remainingWeight -= row.donationsSqrtRootSumSquared;
remainingFunds -= fundingCap;
row.actualMatching = fundingCap;
row.donationsSqrtRootSumSquared = weightCap;
result.push(row);
}
}

for (const row of rows) {
if (row.donationsSqrtRootSumSquared / totalWeight < maxRewardShare) {
const matchingFund =
(row.donationsSqrtRootSumSquared / remainingWeight) * remainingFunds;

// Avoid matching the same over the cap rows
if (matchingFund < fundingCap) {
row.actualMatching =
(row.donationsSqrtRootSumSquared / remainingWeight) * remainingFunds;
result.push(row);
Expand Down

0 comments on commit e74f203

Please sign in to comment.