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

staging -> main #93

Merged
merged 5 commits into from
Oct 11, 2024
Merged
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 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
4 changes: 2 additions & 2 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ export async function bootstrap() {
try {
if (req) {
const { headers } = req;
const authVersion = headers.authversion || '1';
if (headers.authorization) {
const authVersion = headers?.authversion || '1';
if (headers?.authorization) {
token = headers.authorization.split(' ')[1].toString();
user = await authorizationHandler(authVersion as string, token);
}
Expand Down
Loading