Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement allocatedGivbacks function #1808

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading