From 557627a19de9717ef87b5507842f88dff82ef982 Mon Sep 17 00:00:00 2001 From: Ajand Mardalizad Date: Fri, 29 Oct 2021 14:40:09 +0330 Subject: [PATCH 1/4] fixed #443 --- app/src/utils/data/contributions.ts | 56 ++++++++++++++++++----------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/app/src/utils/data/contributions.ts b/app/src/utils/data/contributions.ts index a3a936d2..6a1a00b3 100644 --- a/app/src/utils/data/contributions.ts +++ b/app/src/utils/data/contributions.ts @@ -53,27 +53,43 @@ export async function getContributions( if (SUBGRAPH_URL) { try { // make the request - const res = await fetch(SUBGRAPH_URL, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - query: `{ - grantDonations(where: {lastUpdatedBlockNumber_gte: ${fromBlock}, lastUpdatedBlockNumber_lte: ${blockNumber}}) { - grantId - tokenIn - donationAmount - from - hash - rounds - lastUpdatedBlockNumber - } - }`, - }), - }); - // resolve the json - const json = await res.json(); + + const limit = 100; + + const fetchUntilAll = async (SUBGRAPH_URL: string, before = [], skip = 0): Promise => { + const res = await fetch(SUBGRAPH_URL, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + query: `{ + grantDonations(first: ${limit}, skip: ${ + skip * limit + }, where: {lastUpdatedBlockNumber_gte: ${fromBlock}, lastUpdatedBlockNumber_lte: ${blockNumber}}) { + grantId + tokenIn + donationAmount + from + hash + rounds + lastUpdatedBlockNumber + } + }`, + }), + }); + // resolve the json + const json = await res.json(); + + if (json.data.grantDonations.length) { + return await fetchUntilAll(SUBGRAPH_URL, before, skip + 1); + } else { + return [...before]; + } + }; + + const donationData = await fetchUntilAll(SUBGRAPH_URL); + // update each of the grants - json.data.grantDonations.forEach((contribution: ContributionSubgraph) => { + donationData.forEach((contribution: ContributionSubgraph) => { // update to most recent block collected fromBlock = Math.max(fromBlock, contribution.lastUpdatedBlockNumber); const grantId = BigNumber.from(contribution.grantId).toNumber(); From 63e13890ca431d487de43b8c71dfb1a1acdb9679 Mon Sep 17 00:00:00 2001 From: Ajand Mardalizad Date: Fri, 29 Oct 2021 21:23:39 +0330 Subject: [PATCH 2/4] before merged with json --- app/src/utils/constants.ts | 2 +- app/src/utils/data/contributions.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/utils/constants.ts b/app/src/utils/constants.ts index 9b030f77..c1e74a08 100644 --- a/app/src/utils/constants.ts +++ b/app/src/utils/constants.ts @@ -41,7 +41,7 @@ export const DefaultForageConfig: LocalForageConfig = { // this store should be used for any on-chain/off-chain data but never user data (as we might clear it without notice) name: 'dGrants', // we can bump this version number to bust the users cache - version: 1, + version: 3, }; // LocalForage keys diff --git a/app/src/utils/data/contributions.ts b/app/src/utils/data/contributions.ts index 6a1a00b3..20d7019b 100644 --- a/app/src/utils/data/contributions.ts +++ b/app/src/utils/data/contributions.ts @@ -56,7 +56,7 @@ export async function getContributions( const limit = 100; - const fetchUntilAll = async (SUBGRAPH_URL: string, before = [], skip = 0): Promise => { + const fetchUntilAll = async (SUBGRAPH_URL: string, before: any[] = [], skip = 0): Promise => { const res = await fetch(SUBGRAPH_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -80,7 +80,8 @@ export async function getContributions( const json = await res.json(); if (json.data.grantDonations.length) { - return await fetchUntilAll(SUBGRAPH_URL, before, skip + 1); + const current = [...before, ...json.data.grantDonation]; + return await fetchUntilAll(SUBGRAPH_URL, current, skip + 1); } else { return [...before]; } From 346f72575e5bfb8d9927c16b70231a560652c2e8 Mon Sep 17 00:00:00 2001 From: Ajand Mardalizad Date: Sun, 31 Oct 2021 20:07:38 +0330 Subject: [PATCH 3/4] type and version fixedfor #469 --- app/src/utils/constants.ts | 2 +- app/src/utils/data/contributions.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/utils/constants.ts b/app/src/utils/constants.ts index c1e74a08..693eee10 100644 --- a/app/src/utils/constants.ts +++ b/app/src/utils/constants.ts @@ -41,7 +41,7 @@ export const DefaultForageConfig: LocalForageConfig = { // this store should be used for any on-chain/off-chain data but never user data (as we might clear it without notice) name: 'dGrants', // we can bump this version number to bust the users cache - version: 3, + version: 2, }; // LocalForage keys diff --git a/app/src/utils/data/contributions.ts b/app/src/utils/data/contributions.ts index 20d7019b..123f13ae 100644 --- a/app/src/utils/data/contributions.ts +++ b/app/src/utils/data/contributions.ts @@ -56,7 +56,11 @@ export async function getContributions( const limit = 100; - const fetchUntilAll = async (SUBGRAPH_URL: string, before: any[] = [], skip = 0): Promise => { + const fetchUntilAll = async ( + SUBGRAPH_URL: string, + before: ContributionSubgraph[] = [], + skip = 0 + ): Promise => { const res = await fetch(SUBGRAPH_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, From 749f2ae63df5876d5f0a716d1f67bee3b5f626b8 Mon Sep 17 00:00:00 2001 From: Ajand Mardalizad Date: Mon, 1 Nov 2021 12:46:43 +0330 Subject: [PATCH 4/4] fixed #470 --- app/src/utils/data/contributions.ts | 31 ++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/app/src/utils/data/contributions.ts b/app/src/utils/data/contributions.ts index 123f13ae..3f3424f7 100644 --- a/app/src/utils/data/contributions.ts +++ b/app/src/utils/data/contributions.ts @@ -285,6 +285,10 @@ export function grantDonationListener( }, refs: Record ) { + let timoutManager = 0; + const eachSaveTime = 400; + const processingSet = new Set(); + const listener = async ( grantId: BigNumberish, tokenIn: string, @@ -292,16 +296,26 @@ export function grantDonationListener( grantRounds: string[], event: Event ) => { - // console.log(name, grantId, tokenIn, donationAmount, rounds); + const tx = await event.getTransaction(); + + const wait = (timeout: number) => { + return new Promise((resolve) => { + setTimeout(() => { + return resolve(`Waited: ${timeout}ms`); + }, timeout); + }); + }; + timoutManager += eachSaveTime; + await wait(timoutManager - eachSaveTime + Math.floor(Math.random() * (eachSaveTime / 2))); + if (processingSet.has(`${tx.hash}-${grantId}`)) { + return; + } + processingSet.add(`${tx.hash}-${grantId}`); + const blockNumber = await provider.value.getBlockNumber(); // get tx details to pull contributor details from - const tx = await event.getTransaction(); // log the new contribution - console.log('New contribution: ', { - grantId: BigNumber.from(grantId).toNumber(), - donationAmount: parseFloat(formatUnits(donationAmount, args.donationToken.decimals)), - from: tx.from, - }); + // store the new contribution const contributions = await syncStorage( contributionsKey, @@ -337,6 +351,9 @@ export function grantDonationListener( (a?.blockNumber || 0) > (b?.blockNumber || 0) ? -1 : a?.blockNumber == b?.blockNumber ? 0 : 1 ) as Contribution[]; + timoutManager -= eachSaveTime; + processingSet.delete(`${tx.hash}-${grantId}`); + return { contributions: refs.contributions.value, };