Skip to content

Commit

Permalink
Exclude donations less than certain amount
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadranjbarz committed Sep 2, 2024
1 parent 9235597 commit 4078ede
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 22 deletions.
45 changes: 32 additions & 13 deletions src/givethIoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "./types/general";
import {GIVETH_TOKEN_DISTRO_ADDRESS} from "./subgraphService";
import TokenDistroJSON from '../abi/TokenDistroV2.json'

const Ethers = require("ethers");
const {isAddress} = require("ethers");

Expand All @@ -26,7 +27,7 @@ import {
calculateReferralRewardFromRemainingAmount,
calculateReferralReward,
getNetworkNameById,
filterRawDonationsByChain
filterRawDonationsByChain, isDonationAmountValid
} from "./utils";

const givethiobaseurl = process.env.GIVETHIO_BASE_URL
Expand All @@ -44,7 +45,7 @@ export const isEvmAddress = (address: string): boolean => {

const isStellarDonationAndUserLoggedInWithEvmAddress = (donation: GivethIoDonation): boolean => {
console.log('isStellarDonationAndUserLoggedIn', donation)
return Boolean (donation.transactionNetworkId === 1500 && donation?.user?.walletAddress && isEvmAddress(donation?.user?.walletAddress))
return Boolean(donation.transactionNetworkId === 1500 && donation?.user?.walletAddress && isEvmAddress(donation?.user?.walletAddress))
}

const donationGiverAddress = (donation: GivethIoDonation): string => {
Expand All @@ -62,6 +63,8 @@ export const getEligibleDonations = async (
params: {
beginDate: string,
endDate: string,
minEligibleValueUsd: number,
givethCommunityProjectSlug: string,
niceWhitelistTokens?: string[],
niceProjectSlugs?: string[],
eligible?: boolean,
Expand All @@ -78,7 +81,9 @@ export const getEligibleDonations = async (
niceProjectSlugs,
disablePurpleList,
justCountListed,
chain
chain,
minEligibleValueUsd,
givethCommunityProjectSlug
} = params
const eligible = params.eligible === undefined ? true : params.eligible
const timeFormat = 'YYYY/MM/DD-HH:mm:ss';
Expand Down Expand Up @@ -146,20 +151,28 @@ export const getEligibleDonations = async (
moment(donation.createdAt) < secondDate
&& moment(donation.createdAt) > firstDate
&& donation.valueUsd
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation) )
&& isDonationAmountValid({
donation,
minEligibleValueUsd,
givethCommunityProjectSlug,
} )
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation))
&& donation.isProjectVerified
&& donation.status === 'verified'
)

let donationsToNotVerifiedProjects: GivethIoDonation[] = rawDonationsFilterByChain
.filter(
(donation: GivethIoDonation) =>
moment(donation.createdAt) < secondDate
&& moment(donation.createdAt) > firstDate
&& donation.valueUsd
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation) )
&& !donation.isProjectVerified
&& donation.status === 'verified'
(
moment(donation.createdAt) < secondDate
&& moment(donation.createdAt) > firstDate
&& donation.valueUsd
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation))
&& (!donation.isProjectVerified || donation.valueUsd >= minEligibleValueUsd)
&& donation.status === 'verified'
)

)

if (niceWhitelistTokens) {
Expand Down Expand Up @@ -328,7 +341,7 @@ export const getVerifiedPurpleListDonations = async (beginDate: string, endDate:
moment(donation.createdAt) < secondDate
&& moment(donation.createdAt) > firstDate
&& donation.valueUsd
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation) )
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation))
&& donation.isProjectVerified
&& donation.status === 'verified'
)
Expand Down Expand Up @@ -366,7 +379,8 @@ export const getDonationsReport = async (params: {
// example: 2021/07/01-00:00:00
beginDate: string,
endDate: string,

minEligibleValueUsd: number,
givethCommunityProjectSlug:string,
niceWhitelistTokens?: string[],
niceProjectSlugs?: string[],
applyChainvineReferral?: boolean,
Expand All @@ -378,15 +392,20 @@ export const getDonationsReport = async (params: {
niceWhitelistTokens,
niceProjectSlugs,
applyChainvineReferral,
chain
chain,
givethCommunityProjectSlug,
minEligibleValueUsd
} = params
try {

const response = await getEligibleDonations(
{
beginDate, endDate,
niceWhitelistTokens,
niceProjectSlugs,
disablePurpleList: Boolean(niceWhitelistTokens),
minEligibleValueUsd,
givethCommunityProjectSlug,
chain
})

Expand Down
49 changes: 41 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ app.get(`/calculate`,
maxAddressesPerFunctionCall,
niceWhitelistTokens,
niceProjectSlugs, nicePerDollar,
givethCommunityProjectSlug
} = req.query;
const givAvailable = Number(req.query.givAvailable)
const givPrice = Number(req.query.givPrice)
const minEligibleValueUsd = Number(req.query.minEligibleValueUsd)
const givWorth = givAvailable * givPrice

const tokens = (niceWhitelistTokens as string).split(',')
Expand All @@ -76,6 +78,8 @@ app.get(`/calculate`,
endDate: endDate as string,
niceWhitelistTokens: tokens,
niceProjectSlugs: slugs,
minEligibleValueUsd,
givethCommunityProjectSlug: givethCommunityProjectSlug as string
})

const niceDonationsGroupByGiverAddress = _.groupBy(givethDonationsForNice, 'giverAddress')
Expand Down Expand Up @@ -122,26 +126,35 @@ app.get(`/calculate`,
beginDate: startDate as string,
endDate: endDate as string,
applyChainvineReferral: true,
chain: "gnosis"
chain: "gnosis",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd

});
const otherChainDonations = await getDonationsReport({
beginDate: startDate as string,
endDate: endDate as string,
applyChainvineReferral: true,
chain: "all-other-chains"
chain: "all-other-chains",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});

const zkEVMDonations = await getDonationsReport({
beginDate: startDate as string,
endDate: endDate as string,
applyChainvineReferral: true,
chain: "zkEVM"
chain: "zkEVM",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});

const totalDonations = await getDonationsReport({
beginDate: startDate as string,
endDate: endDate as string,
applyChainvineReferral: true,
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});

const totalDonationsAmount = totalDonations.reduce((previousValue: number, currentValue: MinimalDonation) => {
Expand Down Expand Up @@ -343,14 +356,18 @@ const getEligibleAndNonEligibleDonations = async (req: Request, res: Response, e
endDate, startDate, download, justCountListed,
chain
} = req.query;
const minEligibleValueUsd = Number(req.query.minEligibleValueUsd)
const givethCommunityProjectSlug = req.query.givethCommunityProjectSlug

const givethIoDonations = await getEligibleDonations(
{
beginDate: startDate as string,
endDate: endDate as string,
eligible,
justCountListed: justCountListed === 'yes',
chain: chain as "all-other-chains" | "gnosis" | "zkEVM"
chain: chain as "all-other-chains" | "gnosis" | "zkEVM",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});
const donations =
givethIoDonations.sort((a: FormattedDonation, b: FormattedDonation) => {
Expand Down Expand Up @@ -378,8 +395,9 @@ const getEligibleDonationsForNiceToken = async (req: Request, res: Response, eli
try {
const {
endDate, startDate, download, justCountListed, niceWhitelistTokens,
niceProjectSlugs, nicePerDollar
niceProjectSlugs, nicePerDollar, givethCommunityProjectSlug
} = req.query;
const minEligibleValueUsd = Number(req.query.minEligibleValueUsd)

const tokens = (niceWhitelistTokens as string).split(',')
const slugs = (niceProjectSlugs as string).split(',')
Expand All @@ -391,6 +409,8 @@ const getEligibleDonationsForNiceToken = async (req: Request, res: Response, eli
endDate: endDate as string,
eligible: true,
justCountListed: justCountListed === 'yes',
minEligibleValueUsd,
givethCommunityProjectSlug: givethCommunityProjectSlug as string

});
const donations =
Expand Down Expand Up @@ -596,9 +616,12 @@ app.get(`/calculate-updated`,
maxAddressesPerFunctionCall,
niceWhitelistTokens,
niceProjectSlugs, nicePerDollar,
givethCommunityProjectSlug,
} = req.query;


const givAvailable = Number(req.query.givAvailable)
const minEligibleValueUsd = Number(req.query.minEligibleValueUsd)
const {start, end} = await getGIVbacksRound(Number(roundNumber))
const endDate = moment(end, 'YYYY/MM/DD-HH:mm:ss')
const endDateTimestamp = endDate.unix()
Expand Down Expand Up @@ -626,6 +649,8 @@ app.get(`/calculate-updated`,
endDate: end,
niceWhitelistTokens: tokens,
niceProjectSlugs: slugs,
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
})

const niceDonationsGroupByGiverAddress = _.groupBy(givethDonationsForNice, 'giverAddress')
Expand Down Expand Up @@ -672,20 +697,26 @@ app.get(`/calculate-updated`,
beginDate: start,
endDate: end,
applyChainvineReferral: true,
chain: "gnosis"
chain: "gnosis",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});
const otherChainDonations = await getDonationsReport({
beginDate: start,
endDate: end,
applyChainvineReferral: true,
chain: "all-other-chains"
chain: "all-other-chains",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});

const zkEVMDonations = await getDonationsReport({
beginDate: start,
endDate: end,
applyChainvineReferral: true,
chain: "zkEVM"
chain: "zkEVM",
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});

console.log('***new webservice donations*** new', {
Expand All @@ -699,6 +730,8 @@ app.get(`/calculate-updated`,
beginDate: start,
endDate: end,
applyChainvineReferral: true,
givethCommunityProjectSlug:givethCommunityProjectSlug as string,
minEligibleValueUsd
});

const totalDonationsAmount = totalDonations.reduce((previousValue: number, currentValue: MinimalDonation) => {
Expand Down
50 changes: 50 additions & 0 deletions src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
"default": "the-giveth-community-of-makers",
"in": "query"
},
{
"name": "minEligibleValueUsd",
"type": "number",
"default": "5",
"in": "query"
},
{
"name": "givethCommunityProjectSlug",
"type": "string",
"default": "the-giveth-community-of-makers",
"in": "query"
},
{
"name": "download",
"type": "string",
Expand Down Expand Up @@ -99,6 +111,19 @@
"default": "2022/01/21-15:59:59",
"in": "query"
},

{
"name": "minEligibleValueUsd",
"type": "number",
"default": "5",
"in": "query"
},
{
"name": "givethCommunityProjectSlug",
"type": "string",
"default": "the-giveth-community-of-makers",
"in": "query"
},
{
"name": "download",
"type": "string",
Expand Down Expand Up @@ -202,6 +227,19 @@
"default": "2022/01/21-15:59:59",
"in": "query"
},

{
"name": "minEligibleValueUsd",
"type": "number",
"default": "5",
"in": "query"
},
{
"name": "givethCommunityProjectSlug",
"type": "string",
"default": "the-giveth-community-of-makers",
"in": "query"
},
{
"name": "download",
"type": "string",
Expand Down Expand Up @@ -395,6 +433,18 @@
"default": "the-giveth-community-of-makers",
"in": "query"
},
{
"name": "minEligibleValueUsd",
"type": "number",
"default": "5",
"in": "query"
},
{
"name": "givethCommunityProjectSlug",
"type": "string",
"default": "the-giveth-community-of-makers",
"in": "query"
},
{
"name": "download",
"type": "string",
Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export const getNetworkNameById = (networkId: number): string => {
}

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

if (chain === 'gnosis') {
return gqlResult.donations.filter(donation => donation.transactionNetworkId === 100)
} else if (chain === 'zkEVM') {
Expand Down Expand Up @@ -319,3 +319,10 @@ export const getBlockByTimestamp = async (timestamp: number, chainId: number) :P
return 0
}
}

export const isDonationAmountValid = (params: {
donation: GivethIoDonation, minEligibleValueUsd: number, givethCommunityProjectSlug:string
}): boolean => {
const { donation, minEligibleValueUsd, givethCommunityProjectSlug } = params
return donation.valueUsd >= minEligibleValueUsd || donation.project.slug === givethCommunityProjectSlug
}

0 comments on commit 4078ede

Please sign in to comment.