Skip to content

Commit

Permalink
Release 17 Sep (#1826)
Browse files Browse the repository at this point in the history
* WIP Implement  allocatedGivbacks function

related to Giveth/giveth-dapps-v2#4678 Giveth/giveth-dapps-v2#4679

* allocatedGivbacks() endpoint implemented and works

related to Giveth/giveth-dapps-v2#4678 Giveth/giveth-dapps-v2#4679

* Fix allocatedGivbacksQuery test cases
  • Loading branch information
mohammadranjbarz authored Sep 17, 2024
1 parent 165d6b1 commit b8a47ba
Show file tree
Hide file tree
Showing 8 changed files with 694 additions and 6 deletions.
366 changes: 366 additions & 0 deletions src/repositories/donationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import {
findDonationsByTransactionId,
getPendingDonationsIds,
getProjectQfRoundStats,
getSumOfGivbackEligibleDonationsForSpecificRound,
isVerifiedDonationExistsInQfRound,
} from './donationRepository';
import { Donation, DONATION_STATUS } from '../entities/donation';
import { QfRound } from '../entities/qfRound';
import { Project } from '../entities/project';
import { refreshProjectEstimatedMatchingView } from '../services/projectViewsService';
import { calculateEstimateMatchingForProjectById } from '../utils/qfUtils';
import { setPowerRound } from './powerRoundRepository';

describe('createDonation test cases', createDonationTestCases);

Expand Down Expand Up @@ -61,6 +63,10 @@ describe(
isVerifiedDonationExistsInQfRoundTestCases,
);
describe('findDonationsToGiveth() test cases', findDonationsToGivethTestCases);
describe(
'getSumOfGivbackEligibleDonationsForSpecificRound() test cases',
getSumOfGivbackEligibleDonationsForSpecificRoundTestCases,
);

function fillQfRoundDonationsUserScoresTestCases() {
let qfRound: QfRound;
Expand Down Expand Up @@ -1456,3 +1462,363 @@ function findDonationsToGivethTestCases() {
await User.remove(user);
});
}

function getSumOfGivbackEligibleDonationsForSpecificRoundTestCases() {
it('should return correct value for specific round', async () => {
// 3 donations with 2 different donor
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

const valueUsd1 = 100;
const givbackFactor1 = 0.5;

const valueUsd2 = 200;
const givbackFactor2 = 0.65;

const valueUsd3 = 300;
const givbackFactor3 = 0.7;

const powerRound = 3232;

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd1,
powerRound,
givbackFactor: givbackFactor1,
isProjectVerified: true,
},
donor1.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd2,
powerRound,
givbackFactor: givbackFactor2,
isProjectVerified: true,
},
donor1.id,
project.id,
);
await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd3,
powerRound,
givbackFactor: givbackFactor3,
isProjectVerified: true,
},
donor2.id,
project.id,
);

const sumOfGivbackEligibleDonations =
await getSumOfGivbackEligibleDonationsForSpecificRound({
powerRound,
});

assert.equal(
sumOfGivbackEligibleDonations,
valueUsd1 * givbackFactor1 +
valueUsd2 * givbackFactor2 +
valueUsd3 * givbackFactor3,
);
});
it('should return correct value for specific round, exclude donations with isProjectVerified:false', async () => {
// 3 donations with 2 different donor
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

const valueUsd1 = 100;
const givbackFactor1 = 0.5;

const valueUsd2 = 200;
const givbackFactor2 = 0.65;

const valueUsd3 = 300;
const givbackFactor3 = 0.7;

const powerRound = 24234;

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd1,
powerRound,
givbackFactor: givbackFactor1,
isProjectVerified: true,
},
donor1.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd2,
powerRound,
givbackFactor: givbackFactor2,
isProjectVerified: true,
},
donor1.id,
project.id,
);
await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd3,
powerRound,
givbackFactor: givbackFactor3,
isProjectVerified: false,
},
donor2.id,
project.id,
);

const sumOfGivbackEligibleDonations =
await getSumOfGivbackEligibleDonationsForSpecificRound({
powerRound,
});

assert.equal(
sumOfGivbackEligibleDonations,
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
);
});
it('should return correct value for specific round, exclude donations of other power Rounds', async () => {
// 3 donations with 2 different donor
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

const valueUsd1 = 100;
const givbackFactor1 = 0.5;

const valueUsd2 = 200;
const givbackFactor2 = 0.65;

const valueUsd3 = 300;
const givbackFactor3 = 0.7;

const powerRound = 12321;

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd1,
powerRound,
givbackFactor: givbackFactor1,
isProjectVerified: true,
},
donor1.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd2,
powerRound,
givbackFactor: givbackFactor2,
isProjectVerified: true,
},
donor1.id,
project.id,
);
await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd3,
powerRound: 31234231,
givbackFactor: givbackFactor3,
isProjectVerified: true,
},
donor2.id,
project.id,
);

const sumOfGivbackEligibleDonations =
await getSumOfGivbackEligibleDonationsForSpecificRound({
powerRound,
});

assert.equal(
sumOfGivbackEligibleDonations,
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
);
});
it('should return correct value for specific round, exclude donations of purple list address', async () => {
// 3 donations with 2 different donor
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

const valueUsd1 = 100;
const givbackFactor1 = 0.5;

const valueUsd2 = 200;
const givbackFactor2 = 0.65;

const valueUsd3 = 300;
const givbackFactor3 = 0.7;

const powerRound = 1324123;

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd1,
powerRound,
givbackFactor: givbackFactor1,
isProjectVerified: true,
},
donor1.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd2,
powerRound,
givbackFactor: givbackFactor2,
isProjectVerified: true,
},
donor1.id,
project.id,
);

const verifiedProject = await saveProjectDirectlyToDb({
...createProjectData(),
verified: true,
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const donor3 = await saveUserDirectlyToDb(
verifiedProject!.walletAddress as string,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd3,
powerRound,
givbackFactor: givbackFactor3,
isProjectVerified: true,
},
donor3.id,
project.id,
);

const sumOfGivbackEligibleDonations =
await getSumOfGivbackEligibleDonationsForSpecificRound({
powerRound,
});

assert.equal(
sumOfGivbackEligibleDonations,
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
);
});
it('should return correct value for existing powerRound in DB if we dont pass it', async () => {
// 3 donations with 2 different donor
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

const valueUsd1 = 100;
const givbackFactor1 = 0.5;

const valueUsd2 = 200;
const givbackFactor2 = 0.65;

const valueUsd3 = 300;
const givbackFactor3 = 0.7;

const powerRound = 321425;
await setPowerRound(powerRound);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd1,
powerRound,
givbackFactor: givbackFactor1,
isProjectVerified: true,
},
donor1.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd2,
powerRound,
givbackFactor: givbackFactor2,
isProjectVerified: true,
},
donor1.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
valueUsd: valueUsd3,
powerRound: 1231,
givbackFactor: givbackFactor3,
isProjectVerified: true,
},
donor2.id,
project.id,
);

const sumOfGivbackEligibleDonations =
await getSumOfGivbackEligibleDonationsForSpecificRound({});

assert.equal(
sumOfGivbackEligibleDonations,
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
);
});
}
Loading

0 comments on commit b8a47ba

Please sign in to comment.