Skip to content

Commit

Permalink
Merge pull request #64 from Giveth/staging
Browse files Browse the repository at this point in the history
October release, compatible with decentralized verificaiton
  • Loading branch information
mohammadranjbarz authored Oct 6, 2024
2 parents c56dd70 + 41e4754 commit 076a23c
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ GIVETHIO_BASE_URL=https://mainnet.serve.giveth.io
TRACE_BASE_URL=https://feathers.beta.giveth.io
PURPLE_LIST=0x1ad91ee08f21be3de0ba2ba6918e714da6b45836,0x63ecc058D97ED7bAb75616B77C96734D0B823f87
MAINNET_NODE_URL=https://mainnet.infura.io/v3/infuraApiKey
ENVIRONMENT=staging
ZKEVM_NODE_HTTP_URL=
ZKEVM_CARDONA_HTTP_URL=
XDAI_NODE_HTTP_URL=

PINATA_API_KEY=
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ You just need to run docker-compose-develop.yml, and you dont need any dependenc
## Preview
It's the environment for calculate givback from aggregate donations fo https://giveth.io and https://trace.giveth.io
#### Production
https://givback.develop.giveth.io/api-docs
https://givbacks.giveth.io/api-docs
#### Staging
https://givback.develop.giveth.io/staging/api-docs
https://givback.develop.giveth.io/api-docs
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;
};
80 changes: 59 additions & 21 deletions src/givethIoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ 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");

require('dotenv').config()

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

const givethiobaseurl = process.env.GIVETHIO_BASE_URL
Expand All @@ -37,6 +39,19 @@ const gnosisProvider = new Ethers.JsonRpcProvider(xdaiNodeHttpUrl);
const tokenDistroGC = new Ethers.Contract(GIVETH_TOKEN_DISTRO_ADDRESS, TokenDistroJSON.abi, gnosisProvider);


export const isEvmAddress = (address: string): boolean => {
return isAddress(address);
};

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

const donationGiverAddress = (donation: GivethIoDonation): string => {
return isStellarDonationAndUserLoggedInWithEvmAddress(donation) ? donation.user.walletAddress : donation.fromWalletAddress
}

/**
*
* @returns {Promise<[{amount:400, currency:"GIV",createdAt:"",
Expand All @@ -48,12 +63,14 @@ export const getEligibleDonations = async (
params: {
beginDate: string,
endDate: string,
minEligibleValueUsd: number,
givethCommunityProjectSlug: string,
niceWhitelistTokens?: string[],
niceProjectSlugs?: string[],
eligible?: boolean,
disablePurpleList?: boolean,
justCountListed?: boolean,
chain?: "all-other-chains" | "gnosis"
chain?: "all-other-chains" | "gnosis" | "zkEVM"

}): Promise<FormattedDonation[]> => {
try {
Expand All @@ -64,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 @@ -95,7 +114,8 @@ export const getEligibleDonations = async (
amount
givbackFactor
chainType
isProjectVerified
anonymous
isProjectGivbackEligible
projectRank
powerRound
bottomRankInRound
Expand All @@ -116,6 +136,7 @@ export const getEligibleDonations = async (
user {
name
email
walletAddress
}
fromWalletAddress
status
Expand All @@ -131,20 +152,28 @@ export const getEligibleDonations = async (
moment(donation.createdAt) < secondDate
&& moment(donation.createdAt) > firstDate
&& donation.valueUsd
&& donation.chainType == 'EVM'
&& donation.isProjectVerified
&& isDonationAmountValid({
donation,
minEligibleValueUsd,
givethCommunityProjectSlug,
} )
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation))
&& donation.isProjectGivbackEligible
&& donation.status === 'verified'
)

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

)

if (niceWhitelistTokens) {
Expand Down Expand Up @@ -193,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 @@ -201,7 +231,7 @@ export const getEligibleDonations = async (
usdValue: item.valueUsd,
givFactor: item.givbackFactor
}),
giverAddress: item.fromWalletAddress,
giverAddress: donationGiverAddress(item),
txHash: item.transactionId,
network: getNetworkNameById(item.transactionNetworkId),
source: 'giveth.io',
Expand All @@ -212,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 @@ -221,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 All @@ -232,7 +264,7 @@ export const getEligibleDonations = async (
projectRank: item.projectRank,
bottomRankInRound: item.powerRound,
givbacksRound: item.powerRound,
giverAddress: item.fromWalletAddress,
giverAddress: donationGiverAddress(item),
txHash: item.transactionId,
network: getNetworkNameById(item.transactionNetworkId),
source: 'giveth.io',
Expand Down Expand Up @@ -290,7 +322,7 @@ export const getVerifiedPurpleListDonations = async (beginDate: string, endDate:
transactionNetworkId
amount
chainType
isProjectVerified
isProjectGivbackEligible
project {
slug
verified
Expand All @@ -313,8 +345,8 @@ export const getVerifiedPurpleListDonations = async (beginDate: string, endDate:
moment(donation.createdAt) < secondDate
&& moment(donation.createdAt) > firstDate
&& donation.valueUsd
&& donation.chainType == 'EVM'
&& donation.isProjectVerified
&& (donation.chainType == 'EVM' || isStellarDonationAndUserLoggedInWithEvmAddress(donation))
&& donation.isProjectGivbackEligible
&& donation.status === 'verified'
)

Expand All @@ -326,7 +358,7 @@ export const getVerifiedPurpleListDonations = async (beginDate: string, endDate:
createdAt: moment(item.createdAt).format('YYYY-MM-DD-hh:mm:ss'),
valueUsd: item.valueUsd,
givbackFactor: item.givbackFactor,
giverAddress: item.fromWalletAddress,
giverAddress: donationGiverAddress(item),
txHash: item.transactionId,
network: getNetworkNameById(item.transactionNetworkId),
source: 'giveth.io',
Expand All @@ -351,27 +383,33 @@ 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,
chain?: "all-other-chains" | "gnosis"
chain?: "all-other-chains" | "gnosis" | "zkEVM"
}): Promise<MinimalDonation[]> => {
const {
beginDate,
endDate,
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
Loading

0 comments on commit 076a23c

Please sign in to comment.