Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contribution Count Shows Different Numbers - fixed #443 #460

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: 2,
};

// LocalForage keys
Expand Down
92 changes: 65 additions & 27 deletions app/src/utils/data/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,48 @@ 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: ContributionSubgraph[] = [],
skip = 0
): Promise<any[]> => {
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) {
const current = [...before, ...json.data.grantDonation];
return await fetchUntilAll(SUBGRAPH_URL, current, 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();
Expand Down Expand Up @@ -264,23 +285,37 @@ export function grantDonationListener(
},
refs: Record<string, Ref>
) {
let timoutManager = 0;
const eachSaveTime = 400;
const processingSet = new Set();

const listener = async (
grantId: BigNumberish,
tokenIn: string,
donationAmount: BigNumberish,
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,
Expand Down Expand Up @@ -316,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,
};
Expand Down