Skip to content

Commit

Permalink
Merge pull request #91 from GeneralMagicio/fixBugsInSyncWithReport
Browse files Browse the repository at this point in the history
fix some bugs in script
  • Loading branch information
aminlatifi authored Oct 10, 2024
2 parents f78a19d + f28a591 commit f76682f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/scripts/syncDataWithInverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { syncDonationsWithBlockchainData } from './syncDataWithInverter';
import { InverterAdapter } from '../adapters/inverter/inverterAdapter';
import { EarlyAccessRound } from '../entities/earlyAccessRound';

describe('Sync Donations Script Test Cases', () => {
describe.skip('Sync Donations Script Test Cases', () => {
let existingProjectIds: number[] = [];
let existingDonationIds: number[] = [];
beforeEach(async () => {
Expand Down
115 changes: 64 additions & 51 deletions src/scripts/syncDataWithJsonReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function processReportForDonations(
try {
const rewardInfo: StreamingPaymentProcessorResponse =
await adapter.getProjectRewardInfo(projectOrchestratorAddress);
const participants = reportData.batch.participants;
const participants = reportData.batch.data.participants;
const lowerCasedParticipants = Object.keys(participants).reduce(
(acc, key) => {
acc[key.toLowerCase()] = participants[key];
Expand All @@ -68,47 +68,57 @@ async function processReportForDonations(
const totalValidContribution = ethers.BigNumber.from(
participantData.validContribution,
);
const issuanceAllocation = ethers.BigNumber.from(
participantData.issuanceAllocation,
);

const donationTransaction = participantData.transactions.find(
(tx: any) =>
tx.txHash.toLowerCase() === donation.transactionId.toLowerCase(),
);
// if issuance allocation is not exist, that mean this user has not any valid contributions
let rewardAmount = 0;
if (participantData.issuanceAllocation) {
const issuanceAllocationRow = ethers.BigNumber.from(
participantData.issuanceAllocation,
);
const issuanceAllocation = parseFloat(
ethers.utils.formatUnits(issuanceAllocationRow, 18),
); // Assuming 18 decimal places

const donationTransaction = participantData.transactions.find(
(tx: any) =>
tx.transactionHash.toLowerCase() ===
donation.transactionId.toLowerCase(),
);

if (!donationTransaction) {
console.error(`No transaction data found for donation ${donation.id}`);
continue;
}
if (!donationTransaction) {
console.error(
`No transaction data found for donation ${donation.id}`,
);
continue;
}

const donationValidContribution = ethers.BigNumber.from(
donationTransaction.validContribution,
);
const contributionPercentage = donationValidContribution.div(
totalValidContribution,
);
const donationValidContribution = ethers.BigNumber.from(
donationTransaction.validContribution,
);
const contributionPercentage =
parseFloat(ethers.utils.formatUnits(donationValidContribution, 18)) /
parseFloat(ethers.utils.formatUnits(totalValidContribution, 18));

// Calculate the reward proportionally based on the valid contribution
const rewardAmount = issuanceAllocation.mul(contributionPercentage);
donation.rewardTokenAmount = parseFloat(
ethers.utils.formatUnits(rewardAmount, 18),
); // Assuming 18 decimal places
// Calculate the reward proportionally based on the valid contribution
rewardAmount = issuanceAllocation * contributionPercentage;
}
donation.rewardTokenAmount = rewardAmount;

// Fetch the cliff, reward start, and end dates from the InverterAdapter
const vestingInfo = rewardInfo[0]?.vestings.find(
v => v.recipient === donation.fromWalletAddress,
);

if (vestingInfo) {
// Calculate cliff proportionally in the same way as rewardAmount
const totalCliff = parseFloat(vestingInfo.cliff);
donation.cliff = totalCliff * contributionPercentage.toNumber();
donation.cliff = parseFloat(vestingInfo.cliff);
donation.rewardStreamStart = new Date(parseInt(vestingInfo.start));
donation.rewardStreamEnd = new Date(parseInt(vestingInfo.end));
if (String(vestingInfo.amountRaw) !== String(issuanceAllocation)) {
if (
String(vestingInfo.amountRaw) !== '0' &&
String(vestingInfo.amountRaw) !==
String(participantData.issuanceAllocation)
) {
console.warn(`The reward amount and issuance allocation for project ${donation.projectId} is not match!\n
the reward raw amount is: ${vestingInfo.amountRaw} and the issuance allocation in report is: ${issuanceAllocation}`);
the reward raw amount is: ${vestingInfo.amountRaw} and the issuance allocation in report is: ${participantData.issuanceAllocation}`);
}
} else {
console.error(
Expand All @@ -128,25 +138,26 @@ async function processReportForDonations(
}
}

function getRoundNumberByDonations(donations: Donation[]): number {
if (!donations.length) {
return 0; // Return 0 if there are no donations
}

const firstDonation = donations[0]; // Assuming all donations belong to the same round

// Check if the project is in an Early Access Round or QF Round
if (firstDonation.earlyAccessRound) {
return firstDonation.earlyAccessRound.roundNumber; // Return the round number directly for Early Access
} else if (firstDonation.qfRound.roundNumber) {
return firstDonation.qfRound.roundNumber + 4; // Add 4 to the round number for QF Rounds
} else {
console.error(
`No round information found for donation ${firstDonation.id}`,
);
return 0; // Return 0 if no round information is found
}
}
// function getRoundNumberByDonations(donations: Donation[]): number {
// // todo: we need to find round number in a better way, because maybe there left some donations from previous rounds
// if (!donations.length) {
// return 0; // Return 0 if there are no donations
// }
//
// const firstDonation = donations[0]; // Assuming all donations belong to the same round
//
// // Check if the project is in an Early Access Round or QF Round
// if (firstDonation.earlyAccessRound) {
// return firstDonation.earlyAccessRound.roundNumber; // Return the round number directly for Early Access
// } else if (firstDonation.qfRound.roundNumber) {
// return firstDonation.qfRound.roundNumber + 4; // Add 4 to the round number for QF Rounds
// } else {
// console.error(
// `No round information found for donation ${firstDonation.id}`,
// );
// return 0; // Return 0 if no round information is found
// }
// }

export async function updateRewardsForDonations(
donationFilter: FindOptionsWhere<Donation>,
Expand Down Expand Up @@ -181,9 +192,11 @@ export async function updateRewardsForDonations(
continue;
}

const roundNumber = getRoundNumberByDonations(
donationsByProjectId[projectId],
);
// const roundNumber = getRoundNumberByDonations(
// donationsByProjectId[projectId],
// );
const roundNumber = Number(process.argv[2]);

// Look for matching report files based on orchestrator address
let matchedReportFile = null;
for (const reportFilePath of allReportFiles) {
Expand Down

0 comments on commit f76682f

Please sign in to comment.