Skip to content

Commit

Permalink
Separate Optimism donations from other chain donations
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadranjbarz committed May 17, 2023
1 parent 8d21611 commit b7eeb1d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
27 changes: 20 additions & 7 deletions src/givethIoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
filterDonationsWithPurpleList,
purpleListDonations
} from './commonServices'
import {calculateReferralRewardFromRemainingAmount, calculateReferralReward, getNetworkNameById} from "./utils";
import {
calculateReferralRewardFromRemainingAmount,
calculateReferralReward,
getNetworkNameById,
filterRawDonationsByChain
} from "./utils";

const givethiobaseurl = process.env.GIVETHIO_BASE_URL

Expand All @@ -28,7 +33,9 @@ export const getEligibleDonations = async (
niceProjectSlugs?: string[],
eligible?: boolean,
disablePurpleList?: boolean,
justCountListed?: boolean
justCountListed?: boolean,
chain ?: "all-other-chains" |"optimism"

}): Promise<FormattedDonation[]> => {
try {
const {
Expand All @@ -38,6 +45,7 @@ export const getEligibleDonations = async (
niceProjectSlugs,
disablePurpleList,
justCountListed,
chain
} = params
const eligible = params.eligible === undefined ? true : params.eligible
const timeFormat = 'YYYY/MM/DD-HH:mm:ss';
Expand Down Expand Up @@ -92,7 +100,8 @@ export const getEligibleDonations = async (
`;

const result = await request(`${givethiobaseurl}/graphql`, query)
let donationsToVerifiedProjects: GivethIoDonation[] = result.donations
const rawDonationsFilterByChain = filterRawDonationsByChain(result, chain)
let donationsToVerifiedProjects: GivethIoDonation[] = rawDonationsFilterByChain
.filter(
(donation: GivethIoDonation) =>
moment(donation.createdAt) < secondDate
Expand All @@ -102,7 +111,7 @@ export const getEligibleDonations = async (
&& donation.status === 'verified'
)

let donationsToNotVerifiedProjects: GivethIoDonation[] = result.donations
let donationsToNotVerifiedProjects: GivethIoDonation[] = rawDonationsFilterByChain
.filter(
(donation: GivethIoDonation) =>
moment(donation.createdAt) < secondDate
Expand Down Expand Up @@ -264,7 +273,8 @@ export const getVerifiedPurpleListDonations = async (beginDate: string, endDate:
`;

const result = await request(`${givethiobaseurl}/graphql`, query)
let donationsToVerifiedProjects = result.donations
const rawDonations = result.donations
let donationsToVerifiedProjects = rawDonations
.filter(
(donation: GivethIoDonation) =>
moment(donation.createdAt) < secondDate
Expand Down Expand Up @@ -310,14 +320,16 @@ export const getDonationsReport = async (params: {

niceWhitelistTokens?: string[],
niceProjectSlugs?: string[],
applyChainvineReferral?: boolean
applyChainvineReferral?: boolean,
chain ?: "all-other-chains" |"optimism"
}): Promise<MinimalDonation[]> => {
const {
beginDate,
endDate,
niceWhitelistTokens,
niceProjectSlugs,
applyChainvineReferral
applyChainvineReferral,
chain
} = params
try {
const response = await getEligibleDonations(
Expand All @@ -326,6 +338,7 @@ export const getDonationsReport = async (params: {
niceWhitelistTokens,
niceProjectSlugs,
disablePurpleList: Boolean(niceWhitelistTokens),
chain
})


Expand Down
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ app.get(`/calculate`,
givRelayerAddress,
niceWhitelistTokens,
niceProjectSlugs, nicePerDollar,
chain
} = req.query;
const givAvailable = Number(req.query.givAvailable)
const givPrice = Number(req.query.givPrice)
Expand All @@ -64,7 +65,7 @@ app.get(`/calculate`,
beginDate: startDate as string,
endDate:endDate as string,
niceWhitelistTokens:tokens,
niceProjectSlugs: slugs
niceProjectSlugs: slugs,
})

const niceDonationsGroupByGiverAddress = _.groupBy(givethDonationsForNice, 'giverAddress')
Expand Down Expand Up @@ -103,7 +104,8 @@ app.get(`/calculate`,
const givethDonations = await getDonationsReport({
beginDate: startDate as string,
endDate: endDate as string,
applyChainvineReferral: true
applyChainvineReferral: true,
chain: chain as "all-other-chains" |"optimism"
});

const givethioDonationsAmount = givethDonations.reduce((previousValue: number, currentValue: MinimalDonation) => {
Expand Down Expand Up @@ -246,7 +248,8 @@ app.get(`/calculate`,
const getEligibleAndNonEligibleDonations = async (req: Request, res: Response, eligible = true) => {
try {
const {
endDate, startDate, download, justCountListed
endDate, startDate, download, justCountListed,
chain
} = req.query;

const givethIoDonations = await getEligibleDonations(
Expand All @@ -255,6 +258,8 @@ const getEligibleAndNonEligibleDonations = async (req: Request, res: Response, e
endDate: endDate as string,
eligible,
justCountListed: justCountListed === 'yes',
chain: chain as "all-other-chains" |"optimism"

});
const donations =
givethIoDonations.sort((a: FormattedDonation, b: FormattedDonation) => {
Expand Down Expand Up @@ -282,7 +287,7 @@ const getEligibleDonationsForNiceToken = async (req: Request, res: Response, eli
try {
const {
endDate, startDate, download, justCountListed, niceWhitelistTokens,
niceProjectSlugs, nicePerDollar,
niceProjectSlugs, nicePerDollar
} = req.query;

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

});
const donations =
allDonations.sort((a: FormattedDonation, b: FormattedDonation) => {
Expand Down
24 changes: 24 additions & 0 deletions src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"get": {
"description": "It return both givback and NICE results",
"parameters": [
{
"name": "chain",
"type": "string",
"description": "For Optimism we need to distribute on Optimism chain so we should have different response",
"default": "all-other-chains",
"enum": ["all-other-chains","optimism"],
"in": "query"
},
{
"name": "startDate",
"type": "string",
Expand Down Expand Up @@ -84,6 +92,14 @@
"get": {
"description": "Return all donations to verifed projects/campaigns and purpleList addresses are excluded",
"parameters": [
{
"name": "chain",
"type": "string",
"description": "For Optimism we need to distribute on Optimism chain so we should have different response",
"default": "all-other-chains",
"enum": ["all-other-chains","optimism"],
"in": "query"
},
{
"name": "startDate",
"type": "string",
Expand Down Expand Up @@ -179,6 +195,14 @@
"get": {
"description": "Return all donations to unVerified projects/campaigns and donations to verified projects/campains from purple list addresses",
"parameters": [
{
"name": "chain",
"type": "string",
"description": "For Optimism we need to distribute on Optimism chain so we should have different response",
"default": "all-other-chains",
"enum": ["all-other-chains","optimism"],
"in": "query"
},
{
"name": "startDate",
"type": "string",
Expand Down
14 changes: 13 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DonationResponse} from "./types/general";
import {DonationResponse, GivethIoDonation} from "./types/general";

const {ethers} = require("ethers");
const Web3 = require('web3');
Expand Down Expand Up @@ -142,6 +142,18 @@ export const getNetworkNameById = (networkId: number): string => {
}
}

export const filterRawDonationsByChain = (gqlResult :{donations :GivethIoDonation[]}, chain ?: "all-other-chains" |"optimism"): GivethIoDonation[]=>{
if (chain === 'optimism'){
return gqlResult.donations.filter(donation => donation.transactionNetworkId === 10)
}else if (chain === "all-other-chains") {
// Exclude Optimism donations and return all other donations
return gqlResult.donations.filter(donation => donation.transactionNetworkId !== 10)
}else{
return gqlResult.donations
}


}
const referralSharePercentage = Number(process.env.REFERRAL_SHARE_PERCENTAGE) ||10

export const calculateReferralReward = (valueUsd: number) :number=>{
Expand Down

0 comments on commit b7eeb1d

Please sign in to comment.