Skip to content

Commit

Permalink
Merge pull request #113 from GeneralMagicio/feat/import-missed-donations
Browse files Browse the repository at this point in the history
Accept donations don't match with rounds
  • Loading branch information
aminlatifi authored Oct 29, 2024
2 parents 93c9072 + d0f7851 commit 4d70d3a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
58 changes: 33 additions & 25 deletions src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from 'chai';
import axios, { AxiosResponse } from 'axios';
import { In, Not } from 'typeorm';
import sinon from 'sinon';
import { ExecutionResult, GraphQLError } from 'graphql';
import { ExecutionResult } from 'graphql';
import qAccService from '../services/qAccService';
import {
generateTestAccessToken,
Expand Down Expand Up @@ -4942,7 +4942,7 @@ function qAccLimitTestCases() {
assert.equal(donation?.earlyAccessRoundId, earlyAccessRound1.id);
});

it('should throw exceed user limit error in an active early access round', async () => {
it('should not associate to round when user limit exceed in an active early access round', async () => {
const tokenPrice = 0.1;
const roundUSDCapPerUserPerProject = 50000;
earlyAccessRound1 = await EarlyAccessRound.create({
Expand All @@ -4956,32 +4956,40 @@ function qAccLimitTestCases() {

const amount = roundUSDCapPerUserPerProject / tokenPrice + 1;
// send create donation request
const donationsResponse: AxiosResponse<
ExecutionResult<{ createDonation: number }>
> = await axios.post(
graphqlUrl,
{
query: createDonationMutation,
variables: {
projectId: project.id,
transactionNetworkId: QACC_NETWORK_ID,
transactionId: generateRandomEvmTxHash(),
nonce: 1,
amount: amount,
token: QACC_DONATION_TOKEN_SYMBOL,
const result: AxiosResponse<ExecutionResult<{ createDonation: number }>> =
await axios.post(
graphqlUrl,
{
query: createDonationMutation,
variables: {
projectId: project.id,
transactionNetworkId: QACC_NETWORK_ID,
transactionId: generateRandomEvmTxHash(),
nonce: 1,
amount: amount,
token: QACC_DONATION_TOKEN_SYMBOL,
},
},
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
},
);
);

assert.isOk(donationsResponse);
const errors = donationsResponse.data.errors as GraphQLError[];
assert.isNotEmpty(errors);
assert.equal(errors[0]!.message, errorMessages.EXCEED_QACC_CAP);
assert.isOk(result);
const donationId = result.data.data?.createDonation as number;

const donation = await Donation.findOneBy({ id: donationId });

assert.isNotOk(donation?.earlyAccessRoundId);
assert.isNotOk(donation?.qfRound);
assert.isNotOk(donation?.earlyAccessRoundId);
assert.isNotOk(donation?.qfRoundId);

// const errors = donationsResponse.data.errors as GraphQLError[];
// assert.isNotEmpty(errors);
// assert.equal(errors[0]!.message, errorMessages.EXCEED_QACC_CAP);
});
}

Expand Down
25 changes: 16 additions & 9 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import { findActiveEarlyAccessRound } from '../repositories/earlyAccessRoundRepo
import { updateOrCreateProjectRoundRecord } from '../repositories/projectRoundRecordRepository';
import { updateOrCreateProjectUserRecord } from '../repositories/projectUserRecordRepository';
import { findActiveQfRound } from '../repositories/qfRoundRepository';
import { EarlyAccessRound } from '../entities/earlyAccessRound';
import { QfRound } from '../entities/qfRound';

const draftDonationEnabled = process.env.ENABLE_DRAFT_DONATION === 'true';
@ObjectType()
Expand Down Expand Up @@ -773,7 +775,7 @@ export class DonationResolver {
);
}

await qacc.validateDonation({
const hasCap = await qacc.validateDonation({
projectId,
networkId,
tokenSymbol: token,
Expand Down Expand Up @@ -825,6 +827,16 @@ export class DonationResolver {
// donationPercentage = (amount / totalValue) * 100;
// }
// }

let earlyAccessRound: EarlyAccessRound | null = null;
let qfRound: QfRound | null = null;

if (hasCap) {
earlyAccessRound = await findActiveEarlyAccessRound();
if (!earlyAccessRound) {
qfRound = await findActiveQfRound();
}
}
const donation = Donation.create({
amount: Number(amount),
transactionId: transactionTx,
Expand All @@ -838,7 +850,9 @@ export class DonationResolver {
isTokenEligibleForGivback,
isCustomToken,
isProjectVerified: project.verified,
createdAt: new Date(),
createdAt: donateTime,
earlyAccessRound: earlyAccessRound ?? undefined,
qfRound: qfRound ?? undefined,
segmentNotified: false,
toWalletAddress: toAddress,
fromWalletAddress: fromAddress,
Expand Down Expand Up @@ -871,13 +885,6 @@ export class DonationResolver {
// logger.error('get chainvine wallet address error', e);
// }
// }
const earlyAccessRound = await findActiveEarlyAccessRound();
if (!earlyAccessRound) {
donation.qfRound = await findActiveQfRound();
} else {
donation.earlyAccessRound = earlyAccessRound;
}
await donation.save();

let priceChainId;

Expand Down
6 changes: 5 additions & 1 deletion src/resolvers/draftDonationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class DraftDonationResolver {
toAddress = toAddress?.toLowerCase();
fromAddress = fromAddress?.toLowerCase();

await qacc.validateDonation({
const hasCap = await qacc.validateDonation({
projectId,
networkId,
tokenSymbol: token,
Expand All @@ -125,6 +125,10 @@ export class DraftDonationResolver {
donateTime: new Date(),
});

if (!hasCap) {
throw new Error(i18n.__(translationErrorMessagesKeys.EXCEED_QACC_CAP));
}

const draftDonationId = await DraftDonation.createQueryBuilder(
'draftDonation',
)
Expand Down
2 changes: 1 addition & 1 deletion src/services/donationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe(
fillStableCoinDonationsPriceTestCases,
);

describe.only(
describe(
'syncDonationStatusWithBlockchainNetwork test cases',
syncDonationStatusWithBlockchainNetworkTestCases,
);
Expand Down
10 changes: 6 additions & 4 deletions src/utils/qacc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const validateDonation = async (params: {
tokenSymbol: string;
amount: number;
donateTime: Date;
}): Promise<void> => {
}): Promise<boolean> => {
const { projectId, userAddress, tokenSymbol, networkId, donateTime } = params;

let user = await findUserByWalletAddress(userAddress)!;
Expand All @@ -36,9 +36,9 @@ const validateDonation = async (params: {
donateTime,
});

if (cap < params.amount) {
throw new Error(i18n.__(translationErrorMessagesKeys.EXCEED_QACC_CAP));
}
// if (cap < params.amount) {
// throw new Error(i18n.__(translationErrorMessagesKeys.EXCEED_QACC_CAP));
// }

// token is matched
if (
Expand Down Expand Up @@ -67,6 +67,8 @@ const validateDonation = async (params: {
throw new Error(i18n.__(translationErrorMessagesKeys.NOT_NFT_HOLDER));
}
}

return cap >= params.amount;
};

export default {
Expand Down

0 comments on commit 4d70d3a

Please sign in to comment.