Skip to content

Commit f85d8b4

Browse files
Implement allocatedGivbacks function (#1808)
* 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
1 parent bebce89 commit f85d8b4

8 files changed

+694
-8
lines changed

src/repositories/donationRepository.test.ts

+366
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import {
2121
findDonationsByTransactionId,
2222
getPendingDonationsIds,
2323
getProjectQfRoundStats,
24+
getSumOfGivbackEligibleDonationsForSpecificRound,
2425
isVerifiedDonationExistsInQfRound,
2526
} from './donationRepository';
2627
import { Donation, DONATION_STATUS } from '../entities/donation';
2728
import { QfRound } from '../entities/qfRound';
2829
import { Project } from '../entities/project';
2930
import { refreshProjectEstimatedMatchingView } from '../services/projectViewsService';
3031
import { calculateEstimateMatchingForProjectById } from '../utils/qfUtils';
32+
import { setPowerRound } from './powerRoundRepository';
3133

3234
describe('createDonation test cases', createDonationTestCases);
3335

@@ -61,6 +63,10 @@ describe(
6163
isVerifiedDonationExistsInQfRoundTestCases,
6264
);
6365
describe('findDonationsToGiveth() test cases', findDonationsToGivethTestCases);
66+
describe(
67+
'getSumOfGivbackEligibleDonationsForSpecificRound() test cases',
68+
getSumOfGivbackEligibleDonationsForSpecificRoundTestCases,
69+
);
6470

6571
function fillQfRoundDonationsUserScoresTestCases() {
6672
let qfRound: QfRound;
@@ -1456,3 +1462,363 @@ function findDonationsToGivethTestCases() {
14561462
await User.remove(user);
14571463
});
14581464
}
1465+
1466+
function getSumOfGivbackEligibleDonationsForSpecificRoundTestCases() {
1467+
it('should return correct value for specific round', async () => {
1468+
// 3 donations with 2 different donor
1469+
const project = await saveProjectDirectlyToDb({
1470+
...createProjectData(),
1471+
title: String(new Date().getTime()),
1472+
slug: String(new Date().getTime()),
1473+
});
1474+
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1475+
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1476+
1477+
const valueUsd1 = 100;
1478+
const givbackFactor1 = 0.5;
1479+
1480+
const valueUsd2 = 200;
1481+
const givbackFactor2 = 0.65;
1482+
1483+
const valueUsd3 = 300;
1484+
const givbackFactor3 = 0.7;
1485+
1486+
const powerRound = 3232;
1487+
1488+
await saveDonationDirectlyToDb(
1489+
{
1490+
...createDonationData(),
1491+
status: 'verified',
1492+
valueUsd: valueUsd1,
1493+
powerRound,
1494+
givbackFactor: givbackFactor1,
1495+
isProjectVerified: true,
1496+
},
1497+
donor1.id,
1498+
project.id,
1499+
);
1500+
1501+
await saveDonationDirectlyToDb(
1502+
{
1503+
...createDonationData(),
1504+
status: 'verified',
1505+
valueUsd: valueUsd2,
1506+
powerRound,
1507+
givbackFactor: givbackFactor2,
1508+
isProjectVerified: true,
1509+
},
1510+
donor1.id,
1511+
project.id,
1512+
);
1513+
await saveDonationDirectlyToDb(
1514+
{
1515+
...createDonationData(),
1516+
status: 'verified',
1517+
valueUsd: valueUsd3,
1518+
powerRound,
1519+
givbackFactor: givbackFactor3,
1520+
isProjectVerified: true,
1521+
},
1522+
donor2.id,
1523+
project.id,
1524+
);
1525+
1526+
const sumOfGivbackEligibleDonations =
1527+
await getSumOfGivbackEligibleDonationsForSpecificRound({
1528+
powerRound,
1529+
});
1530+
1531+
assert.equal(
1532+
sumOfGivbackEligibleDonations,
1533+
valueUsd1 * givbackFactor1 +
1534+
valueUsd2 * givbackFactor2 +
1535+
valueUsd3 * givbackFactor3,
1536+
);
1537+
});
1538+
it('should return correct value for specific round, exclude donations with isProjectVerified:false', async () => {
1539+
// 3 donations with 2 different donor
1540+
const project = await saveProjectDirectlyToDb({
1541+
...createProjectData(),
1542+
title: String(new Date().getTime()),
1543+
slug: String(new Date().getTime()),
1544+
});
1545+
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1546+
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1547+
1548+
const valueUsd1 = 100;
1549+
const givbackFactor1 = 0.5;
1550+
1551+
const valueUsd2 = 200;
1552+
const givbackFactor2 = 0.65;
1553+
1554+
const valueUsd3 = 300;
1555+
const givbackFactor3 = 0.7;
1556+
1557+
const powerRound = 24234;
1558+
1559+
await saveDonationDirectlyToDb(
1560+
{
1561+
...createDonationData(),
1562+
status: 'verified',
1563+
valueUsd: valueUsd1,
1564+
powerRound,
1565+
givbackFactor: givbackFactor1,
1566+
isProjectVerified: true,
1567+
},
1568+
donor1.id,
1569+
project.id,
1570+
);
1571+
1572+
await saveDonationDirectlyToDb(
1573+
{
1574+
...createDonationData(),
1575+
status: 'verified',
1576+
valueUsd: valueUsd2,
1577+
powerRound,
1578+
givbackFactor: givbackFactor2,
1579+
isProjectVerified: true,
1580+
},
1581+
donor1.id,
1582+
project.id,
1583+
);
1584+
await saveDonationDirectlyToDb(
1585+
{
1586+
...createDonationData(),
1587+
status: 'verified',
1588+
valueUsd: valueUsd3,
1589+
powerRound,
1590+
givbackFactor: givbackFactor3,
1591+
isProjectVerified: false,
1592+
},
1593+
donor2.id,
1594+
project.id,
1595+
);
1596+
1597+
const sumOfGivbackEligibleDonations =
1598+
await getSumOfGivbackEligibleDonationsForSpecificRound({
1599+
powerRound,
1600+
});
1601+
1602+
assert.equal(
1603+
sumOfGivbackEligibleDonations,
1604+
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
1605+
);
1606+
});
1607+
it('should return correct value for specific round, exclude donations of other power Rounds', async () => {
1608+
// 3 donations with 2 different donor
1609+
const project = await saveProjectDirectlyToDb({
1610+
...createProjectData(),
1611+
title: String(new Date().getTime()),
1612+
slug: String(new Date().getTime()),
1613+
});
1614+
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1615+
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1616+
1617+
const valueUsd1 = 100;
1618+
const givbackFactor1 = 0.5;
1619+
1620+
const valueUsd2 = 200;
1621+
const givbackFactor2 = 0.65;
1622+
1623+
const valueUsd3 = 300;
1624+
const givbackFactor3 = 0.7;
1625+
1626+
const powerRound = 12321;
1627+
1628+
await saveDonationDirectlyToDb(
1629+
{
1630+
...createDonationData(),
1631+
status: 'verified',
1632+
valueUsd: valueUsd1,
1633+
powerRound,
1634+
givbackFactor: givbackFactor1,
1635+
isProjectVerified: true,
1636+
},
1637+
donor1.id,
1638+
project.id,
1639+
);
1640+
1641+
await saveDonationDirectlyToDb(
1642+
{
1643+
...createDonationData(),
1644+
status: 'verified',
1645+
valueUsd: valueUsd2,
1646+
powerRound,
1647+
givbackFactor: givbackFactor2,
1648+
isProjectVerified: true,
1649+
},
1650+
donor1.id,
1651+
project.id,
1652+
);
1653+
await saveDonationDirectlyToDb(
1654+
{
1655+
...createDonationData(),
1656+
status: 'verified',
1657+
valueUsd: valueUsd3,
1658+
powerRound: 31234231,
1659+
givbackFactor: givbackFactor3,
1660+
isProjectVerified: true,
1661+
},
1662+
donor2.id,
1663+
project.id,
1664+
);
1665+
1666+
const sumOfGivbackEligibleDonations =
1667+
await getSumOfGivbackEligibleDonationsForSpecificRound({
1668+
powerRound,
1669+
});
1670+
1671+
assert.equal(
1672+
sumOfGivbackEligibleDonations,
1673+
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
1674+
);
1675+
});
1676+
it('should return correct value for specific round, exclude donations of purple list address', async () => {
1677+
// 3 donations with 2 different donor
1678+
const project = await saveProjectDirectlyToDb({
1679+
...createProjectData(),
1680+
title: String(new Date().getTime()),
1681+
slug: String(new Date().getTime()),
1682+
});
1683+
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1684+
1685+
const valueUsd1 = 100;
1686+
const givbackFactor1 = 0.5;
1687+
1688+
const valueUsd2 = 200;
1689+
const givbackFactor2 = 0.65;
1690+
1691+
const valueUsd3 = 300;
1692+
const givbackFactor3 = 0.7;
1693+
1694+
const powerRound = 1324123;
1695+
1696+
await saveDonationDirectlyToDb(
1697+
{
1698+
...createDonationData(),
1699+
status: 'verified',
1700+
valueUsd: valueUsd1,
1701+
powerRound,
1702+
givbackFactor: givbackFactor1,
1703+
isProjectVerified: true,
1704+
},
1705+
donor1.id,
1706+
project.id,
1707+
);
1708+
1709+
await saveDonationDirectlyToDb(
1710+
{
1711+
...createDonationData(),
1712+
status: 'verified',
1713+
valueUsd: valueUsd2,
1714+
powerRound,
1715+
givbackFactor: givbackFactor2,
1716+
isProjectVerified: true,
1717+
},
1718+
donor1.id,
1719+
project.id,
1720+
);
1721+
1722+
const verifiedProject = await saveProjectDirectlyToDb({
1723+
...createProjectData(),
1724+
verified: true,
1725+
title: String(new Date().getTime()),
1726+
slug: String(new Date().getTime()),
1727+
});
1728+
const donor3 = await saveUserDirectlyToDb(
1729+
verifiedProject!.walletAddress as string,
1730+
);
1731+
1732+
await saveDonationDirectlyToDb(
1733+
{
1734+
...createDonationData(),
1735+
status: 'verified',
1736+
valueUsd: valueUsd3,
1737+
powerRound,
1738+
givbackFactor: givbackFactor3,
1739+
isProjectVerified: true,
1740+
},
1741+
donor3.id,
1742+
project.id,
1743+
);
1744+
1745+
const sumOfGivbackEligibleDonations =
1746+
await getSumOfGivbackEligibleDonationsForSpecificRound({
1747+
powerRound,
1748+
});
1749+
1750+
assert.equal(
1751+
sumOfGivbackEligibleDonations,
1752+
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
1753+
);
1754+
});
1755+
it('should return correct value for existing powerRound in DB if we dont pass it', async () => {
1756+
// 3 donations with 2 different donor
1757+
const project = await saveProjectDirectlyToDb({
1758+
...createProjectData(),
1759+
title: String(new Date().getTime()),
1760+
slug: String(new Date().getTime()),
1761+
});
1762+
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1763+
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
1764+
1765+
const valueUsd1 = 100;
1766+
const givbackFactor1 = 0.5;
1767+
1768+
const valueUsd2 = 200;
1769+
const givbackFactor2 = 0.65;
1770+
1771+
const valueUsd3 = 300;
1772+
const givbackFactor3 = 0.7;
1773+
1774+
const powerRound = 321425;
1775+
await setPowerRound(powerRound);
1776+
1777+
await saveDonationDirectlyToDb(
1778+
{
1779+
...createDonationData(),
1780+
status: 'verified',
1781+
valueUsd: valueUsd1,
1782+
powerRound,
1783+
givbackFactor: givbackFactor1,
1784+
isProjectVerified: true,
1785+
},
1786+
donor1.id,
1787+
project.id,
1788+
);
1789+
1790+
await saveDonationDirectlyToDb(
1791+
{
1792+
...createDonationData(),
1793+
status: 'verified',
1794+
valueUsd: valueUsd2,
1795+
powerRound,
1796+
givbackFactor: givbackFactor2,
1797+
isProjectVerified: true,
1798+
},
1799+
donor1.id,
1800+
project.id,
1801+
);
1802+
1803+
await saveDonationDirectlyToDb(
1804+
{
1805+
...createDonationData(),
1806+
status: 'verified',
1807+
valueUsd: valueUsd3,
1808+
powerRound: 1231,
1809+
givbackFactor: givbackFactor3,
1810+
isProjectVerified: true,
1811+
},
1812+
donor2.id,
1813+
project.id,
1814+
);
1815+
1816+
const sumOfGivbackEligibleDonations =
1817+
await getSumOfGivbackEligibleDonationsForSpecificRound({});
1818+
1819+
assert.equal(
1820+
sumOfGivbackEligibleDonations,
1821+
valueUsd1 * givbackFactor1 + valueUsd2 * givbackFactor2,
1822+
);
1823+
});
1824+
}

0 commit comments

Comments
 (0)