Skip to content

Commit

Permalink
Merge pull request #59 from HrithikSampson/zkevm-givback-calculation
Browse files Browse the repository at this point in the history
zkEVM added to download or chain option to retrieve only zkEVM givBac…
  • Loading branch information
mohammadranjbarz authored Sep 1, 2024
2 parents 7a9851a + c6ef67f commit 9235597
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/givethIoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const getEligibleDonations = async (
eligible?: boolean,
disablePurpleList?: boolean,
justCountListed?: boolean,
chain?: "all-other-chains" | "gnosis"
chain?: "all-other-chains" | "gnosis" | "zkEVM"

}): Promise<FormattedDonation[]> => {
try {
Expand Down Expand Up @@ -370,7 +370,7 @@ export const getDonationsReport = async (params: {
niceWhitelistTokens?: string[],
niceProjectSlugs?: string[],
applyChainvineReferral?: boolean,
chain?: "all-other-chains" | "gnosis"
chain?: "all-other-chains" | "gnosis" | "zkEVM"
}): Promise<MinimalDonation[]> => {
const {
beginDate,
Expand Down
95 changes: 93 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ app.get(`/calculate`,
chain: "all-other-chains"
});

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

const totalDonations = await getDonationsReport({
beginDate: startDate as string,
endDate: endDate as string,
Expand All @@ -151,6 +158,7 @@ app.get(`/calculate`,
const groupByGiverAddressForTotalDonations = _.groupBy(totalDonations, 'giverAddress')
const groupByGiverAddressForOptimismDonations = _.groupBy(gnosisDonations, 'giverAddress')
const groupByGiverAddressForAllOtherChainsDonations = _.groupBy(otherChainDonations, 'giverAddress')
const groupByGiverAddressForzkEVMDonations = _.groupBy(zkEVMDonations, 'giverAddress')


const optimismMinimalDonations = getDonationsForSmartContractParams({
Expand All @@ -168,6 +176,11 @@ app.get(`/calculate`,
groupByGiverAddress: groupByGiverAddressForTotalDonations
})

const zkEVMChainMinimalDonations = getDonationsForSmartContractParams({
maxGivbackFactorPercentage,
groupByGiverAddress: groupByGiverAddressForzkEVMDonations
})

const totalMinimalDonationsSortedByUsdValue = totalMinimalDonations.sort((a, b) => {
return b.totalDonationsUsdValueAfterGivFactor - a.totalDonationsUsdValueAfterGivFactor
});
Expand All @@ -190,6 +203,11 @@ app.get(`/calculate`,
raisedValueSum
})

const zkEVMChainMinimalDonationsWithShare = convertMinimalDonationToDonationResponse({
minimalDonationsArray: zkEVMChainMinimalDonations,
givPrice,
raisedValueSum
})

const allOtherChainsDonationsWithShare = convertMinimalDonationToDonationResponse({
minimalDonationsArray: allOtherChainsMinimalDonations,
Expand All @@ -215,6 +233,7 @@ app.get(`/calculate`,
// https://github.com/Giveth/givback-calculation/issues/35#issuecomment-1716106403
const optimismRelayerAddress = '0xf13e93af5e706ab3073e393e77bb2d7ce7bec01f'
const gnosisRelayerAddress = '0xd0e81E3EE863318D0121501ff48C6C3e3Fd6cbc7'
const zkEVMRelayerAddress = '0x0000000000000000000000000000000000000000'
const response = {
raisedValueSumExcludedPurpleList: Math.ceil(raisedValueSum),
givDistributed,
Expand Down Expand Up @@ -243,6 +262,18 @@ app.get(`/calculate`,
),
givbacks: allOtherChainsDonationsWithShare
},
zkEVM: {
smartContractParams: await createSmartContractCallAddBatchParams(
{
nrGIVAddress,
donationsWithShare: zkEVMChainMinimalDonationsWithShare.filter(givback => givback.givback > 0),
givRelayerAddress: zkEVMRelayerAddress,
network:'zkEVM'
},
Number(maxAddressesPerFunctionCall) || 200
),
givbacks: zkEVMChainMinimalDonationsWithShare
},
niceTokens: niceDonationsWithShareFormatted,
// niceRaisedValueSumExcludedPurpleList: Math.ceil(raisedValueForGivethioDonationsSum),
// niceGivethioDonationsAmountForNice: Math.ceil(givethioDonationsAmountForNice),
Expand Down Expand Up @@ -275,6 +306,20 @@ app.get(`/calculate`,
res.setHeader('Content-disposition', "attachment; filename=" + fileName);
res.setHeader('Content-type', 'application/json');
res.send(csv)
} else if(download === 'zkEVM'){
console.log('zkEVM response',response.zkEVM);
const csv = parse(response.zkEVM.givbacks.map((item: DonationResponse) => {
return {
givDistributed,
givPrice,
givbackUsdValue: givPrice * item.givback,
...item
}
}));
const fileName = `givbackReport_zkEVM_${startDate}-${endDate}.csv`;
res.setHeader('Content-disposition', "attachment; filename=" + fileName);
res.setHeader('Content-type', 'application/json');
res.send(csv)
} else if (download === 'NICE') {
const csv = parse(response.niceTokens);
const fileName = `givbackReport_NICE_${startDate}-${endDate}.csv`;
Expand Down Expand Up @@ -305,8 +350,7 @@ const getEligibleAndNonEligibleDonations = async (req: Request, res: Response, e
endDate: endDate as string,
eligible,
justCountListed: justCountListed === 'yes',
chain: chain as "all-other-chains" | "gnosis"

chain: chain as "all-other-chains" | "gnosis" | "zkEVM"
});
const donations =
givethIoDonations.sort((a: FormattedDonation, b: FormattedDonation) => {
Expand Down Expand Up @@ -637,6 +681,13 @@ app.get(`/calculate-updated`,
chain: "all-other-chains"
});

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

console.log('***new webservice donations*** new', {
otherChainDonations: otherChainDonations.length,
start,
Expand Down Expand Up @@ -672,6 +723,7 @@ app.get(`/calculate-updated`,
const groupByGiverAddressForTotalDonations = _.groupBy(totalDonations, 'giverAddress')
const groupByGiverAddressForOptimismDonations = _.groupBy(gnosisDonations, 'giverAddress')
const groupByGiverAddressForAllOtherChainsDonations = _.groupBy(otherChainDonations, 'giverAddress')
const groupByGiverAddressForzkEVMDonations = _.groupBy(zkEVMDonations, 'giverAddress')


const optimismMinimalDonations = getDonationsForSmartContractParams({
Expand All @@ -684,6 +736,11 @@ app.get(`/calculate-updated`,
groupByGiverAddress: groupByGiverAddressForAllOtherChainsDonations
})

const zkEVMChainMinimalDonations = getDonationsForSmartContractParams({
maxGivbackFactorPercentage,
groupByGiverAddress: groupByGiverAddressForzkEVMDonations
})

const totalMinimalDonations = getDonationsForSmartContractParams({
maxGivbackFactorPercentage,
groupByGiverAddress: groupByGiverAddressForTotalDonations
Expand Down Expand Up @@ -717,6 +774,13 @@ app.get(`/calculate-updated`,
givPrice,
raisedValueSum
})

const zkEVMDonationsWithShare = convertMinimalDonationToDonationResponse({
minimalDonationsArray: zkEVMChainMinimalDonations,
givPrice,
raisedValueSum
})

console.log('**allOtherChainsDonationsWithShare**', allOtherChainsDonationsWithShare.length)
console.log('**allOtherChainsMinimalDonations**', allOtherChainsMinimalDonations.length)

Expand All @@ -736,13 +800,27 @@ app.get(`/calculate-updated`,
const givDistributed = Math.ceil(raisedValueSumAfterGivFactor / givPrice);
const optimismRelayerAddress = '0xf13e93af5e706ab3073e393e77bb2d7ce7bec01f'
const gnosisRelayerAddress = '0xd0e81E3EE863318D0121501ff48C6C3e3Fd6cbc7'
// TODO : Set the relayer address for zkEVM.
const zkEVMRelayerAddress = '0x0000000000000000000000000000000000000000'

const response = {
start,
end,
raisedValueSumExcludedPurpleList: Math.ceil(raisedValueSum),
givDistributed,
givethioDonationsAmount: Math.ceil(totalDonationsAmount),
zkEVM: {
smartContractParams: await createSmartContractCallAddBatchParams(
{
nrGIVAddress,
donationsWithShare: zkEVMDonationsWithShare.filter(givback => givback.givback > 0),
givRelayerAddress: zkEVMRelayerAddress,
network:'gnosis'
},
Number(maxAddressesPerFunctionCall) || 200
),
givbacks: zkEVMDonationsWithShare
},
gnosis: {
smartContractParams: await createSmartContractCallAddBatchParams(
{
Expand Down Expand Up @@ -799,6 +877,19 @@ app.get(`/calculate-updated`,
res.setHeader('Content-disposition', "attachment; filename=" + fileName);
res.setHeader('Content-type', 'application/json');
res.send(csv)
} else if (download === "zkEVM") {
const csv = parse(response.zkEVM.givbacks.map((item: DonationResponse) => {
return {
givDistributed,
givPrice,
givbackUsdValue: givPrice * item.givback,
...item
}
}));
const fileName = `givbackReport_zkEVM_${start}-${end}.csv`;
res.setHeader('Content-disposition', "attachment; filename=" + fileName);
res.setHeader('Content-type', 'application/json');
res.send(csv)
} else if (download === 'NICE') {
const csv = parse(response.niceTokens);
const fileName = `givbackReport_NICE_${start}-${end}.csv`;
Expand Down
8 changes: 4 additions & 4 deletions src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"type": "string",
"description": "If you want to see json here set no, but if you want to download a file set yes",
"default": "no",
"enum": ["no", "gnosis","all-other-chains", "NICE" ],
"enum": ["no", "gnosis","all-other-chains", "NICE", "zkEVM" ],
"in": "query"
}
],
Expand All @@ -84,7 +84,7 @@
"type": "string",
"description": "For Gnosis we need to distribute on Gnosis chain so we should have different response",
"default": "all-other-chains",
"enum": ["all-other-chains","gnosis"],
"enum": ["all-other-chains","gnosis","zkEVM"],
"in": "query"
},
{
Expand Down Expand Up @@ -187,7 +187,7 @@
"type": "string",
"description": "For Gnosis we need to distribute on Gnosis chain so we should have different response",
"default": "all-other-chains",
"enum": ["all-other-chains","gnosis"],
"enum": ["all-other-chains","gnosis","zkEVM"],
"in": "query"
},
{
Expand Down Expand Up @@ -400,7 +400,7 @@
"type": "string",
"description": "If you want to see json here set no, but if you want to download a file set yes",
"default": "no",
"enum": ["no", "gnosis","all-other-chains", "NICE" ],
"enum": ["no", "gnosis","all-other-chains", "NICE", "zkEVM"],
"in": "query"
}
],
Expand Down
30 changes: 25 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const createSmartContractCallAddBatchParams = async (params: {
nrGIVAddress: string,
donationsWithShare: DonationResponse[],
givRelayerAddress: string,
network : 'gnosis' | 'optimism'
network : 'gnosis' | 'optimism' | 'zkEVM'
}, maxAddressesPerFunctionCall: number): Promise<{
result: string,
hashParams: string
Expand Down Expand Up @@ -210,8 +210,11 @@ const xdaiWeb3 = new Web3(xdaiWeb3NodeUrl);
const optimismWeb3NodeUrl = process.env.OPTIMISM_NODE_HTTP_URL
const optimismWeb3 = new Web3(optimismWeb3NodeUrl);

export const getLastNonceForWalletAddress = async (walletAddress: string, chain: 'gnosis' | 'optimism'): Promise<number> => {
const web3Provider = chain === 'optimism' ? optimismWeb3 : xdaiWeb3
const zkEVMWeb3NodeUrl = process.env.ZKEVM_NODE_HTTP_URL
const zkEVMWeb3 = new Web3(zkEVMWeb3NodeUrl);

export const getLastNonceForWalletAddress = async (walletAddress: string, chain: 'gnosis' | 'optimism' | 'zkEVM'): Promise<number> => {
const web3Provider = getWeb3Provider(chain);
const userTransactionsCount = await web3Provider.eth.getTransactionCount(
walletAddress
);
Expand All @@ -224,6 +227,20 @@ export const getLastNonceForWalletAddress = async (walletAddress: string, chain:
return Math.max(userTransactionsCount - 1, 0)
}

const getWeb3Provider = (chain: 'gnosis' | 'optimism' | 'zkEVM'): any => {
let web3Provider = xdaiWeb3;
switch(chain) {
case 'optimism':
web3Provider = optimismWeb3
break;
case 'zkEVM':
web3Provider = zkEVMWeb3;
break;
default:
web3Provider = xdaiWeb3;
}
return web3Provider;
}

export const getNetworkNameById = (networkId: number): string => {
switch (networkId) {
Expand Down Expand Up @@ -266,12 +283,15 @@ export const getNetworkNameById = (networkId: number): string => {
}
}

export const filterRawDonationsByChain = (gqlResult: { donations: GivethIoDonation[] }, chain ?: "all-other-chains" | "gnosis"): GivethIoDonation[] => {
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') {
return gqlResult.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)
return gqlResult.donations.filter(donation => donation.transactionNetworkId !== 100 && donation.transactionNetworkId !== 1101)
} else {
return gqlResult.donations
}
Expand Down

0 comments on commit 9235597

Please sign in to comment.