Skip to content

Commit

Permalink
Return anonymous field in donations, merge recurring donation streams
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadranjbarz committed Sep 22, 2024
1 parent 8d6c00d commit 900df1e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
48 changes: 47 additions & 1 deletion src/commonServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// List of peoples who should not give givbacks
import {FormattedDonation, GivbackFactorParams} from "./types/general";
import {FormattedDonation, GivbackFactorParams, GivethIoDonation} from "./types/general";

const {gql, request} = require("graphql-request");
const givethiobaseurl = process.env.GIVETHIO_BASE_URL
Expand Down Expand Up @@ -57,4 +57,50 @@ export const donationValueAfterGivFactor = (params: {
(usdValue * givFactor).toFixed(7)
)
}
export const groupDonationsByParentRecurringId = (
donations: GivethIoDonation[]
): GivethIoDonation[] => {
const groupedDonations: GivethIoDonation[] = [];

// Create a map to group donations by parentRecurringDonationId
const donationMap = donations.reduce((map, donation) => {
const parentRecurringDonationId = donation.recurringDonation?.id;
if (parentRecurringDonationId) {
if (!map[parentRecurringDonationId]) {
map[parentRecurringDonationId] = [];
}
map[parentRecurringDonationId].push(donation);
} else {
// If there is no parentRecurringDonationId, add directly to the grouped donations
groupedDonations.push({
...donation,
amount: donation.amount, // Convert amount to number
});
}
return map;
}, {} as Record<string, GivethIoDonation[]>);

// Iterate through the map to create grouped donations
for (const parentId in donationMap) {
const donationsGroup = donationMap[parentId];

// Use the data of the first donation in the group
const firstDonation = donationsGroup[0];

// Sum the amounts, valueUsd, and valueUsdAfterGivbackFactor
const totalAmount = donationsGroup.reduce((sum, donation) => sum + parseFloat(donation.amount), 0);
const totalValueUsd = donationsGroup.reduce((sum, donation) => sum + donation.valueUsd, 0);

// Create a new grouped donation object
const groupedDonation: GivethIoDonation = {
...firstDonation,
amount: String(totalAmount),
valueUsd: totalValueUsd,
numberOfStreamedDonations: donationsGroup.length,
};

groupedDonations.push(groupedDonation);
}

return groupedDonations;
};
4 changes: 4 additions & 0 deletions src/givethIoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const getEligibleDonations = async (
amount
givbackFactor
chainType
anonymous
isProjectGivbackEligible
projectRank
powerRound
Expand Down Expand Up @@ -221,6 +222,7 @@ export const getEligibleDonations = async (
currency: item.currency,
createdAt: moment(item.createdAt).format('YYYY-MM-DD-hh:mm:ss'),
valueUsd: item.valueUsd,
anonymous: item.anonymous,
bottomRankInRound: item.bottomRankInRound,
givbacksRound: item.powerRound,
projectRank: item.projectRank,
Expand All @@ -240,6 +242,7 @@ export const getEligibleDonations = async (
isReferrerGivbackEligible: item.isReferrerGivbackEligible,
referrerWallet: item.referrerWallet,

numberOfStreamedDonations: item.numberOfStreamedDonations,
parentRecurringDonationId: item?.recurringDonation?.id,
parentRecurringDonationTxHash: item?.recurringDonation?.txHash
}
Expand All @@ -249,6 +252,7 @@ export const getEligibleDonations = async (
const givbackFactor = item.givbackFactor || 0.5;
return {
amount: item.amount,
anonymous: item.anonymous,
currency: item.currency,
createdAt: moment(item.createdAt).format('YYYY-MM-DD-hh:mm:ss'),
valueUsd: item.valueUsd,
Expand Down
3 changes: 3 additions & 0 deletions src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface FormattedDonation {
referrerWallet?: string
referrer?: boolean,
referred?: boolean,
anonymous: boolean,
parentRecurringDonationId?: string,
parentRecurringDonationTxHash?: string,

Expand Down Expand Up @@ -65,9 +66,11 @@ export interface GivethIoDonation {
// giverName: string
// giverEmail: string,
status: string,
anonymous: boolean,
isProjectGivbackEligible: boolean,
isReferrerGivbackEligible?: boolean,
referrerWallet?: string
numberOfStreamedDonations ?: number
}

export interface DonationResponse {
Expand Down
11 changes: 6 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {DonationResponse, GivethIoDonation, MinimalDonation} from "./types/gener
import {hexlify, ethers} from "ethers";
import { keccak256 } from "@ethersproject/keccak256";
import { toUtf8Bytes } from "@ethersproject/strings";
import {groupDonationsByParentRecurringId} from "./commonServices";


const Web3 = require('web3');
Expand Down Expand Up @@ -284,16 +285,16 @@ export const getNetworkNameById = (networkId: number): string => {
}

export const filterRawDonationsByChain = (gqlResult: { donations: GivethIoDonation[] }, chain ?: "all-other-chains" | "gnosis" | "zkEVM"): GivethIoDonation[] => {

const donations = groupDonationsByParentRecurringId(gqlResult.donations)
if (chain === 'gnosis') {
return gqlResult.donations.filter(donation => donation.transactionNetworkId === 100)
return donations.filter(donation => donation.transactionNetworkId === 100)
} else if (chain === 'zkEVM') {
return gqlResult.donations.filter(donation => donation.transactionNetworkId === 1101)
return donations.filter(donation => donation.transactionNetworkId === 1101)
} else if (chain === "all-other-chains") {
// Exclude Optimism donations and return all other donations
return gqlResult.donations.filter(donation => donation.transactionNetworkId !== 100 && donation.transactionNetworkId !== 1101)
return donations.filter(donation => donation.transactionNetworkId !== 100 && donation.transactionNetworkId !== 1101)
} else {
return gqlResult.donations
return donations
}


Expand Down

0 comments on commit 900df1e

Please sign in to comment.