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

feat: register secondary stellar donation by cron job #1797

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
133 changes: 126 additions & 7 deletions src/services/cronJobs/checkQRTransactionJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const cronJobTime =
(config.get('CHECK_QR_TRANSACTIONS_CRONJOB_EXPRESSION') as string) ||
'0 */1 * * * *';

async function getPendingDraftDonations() {
const getPendingDraftDonations = async () => {
return await DraftDonation.createQueryBuilder('draftDonation')
.where('draftDonation.status = :status', { status: 'pending' })
.andWhere('draftDonation.isQRDonation = true')
.getMany();
}
};

const getToken = async (
chainType: string,
Expand All @@ -43,6 +43,112 @@ const getToken = async (
.getOne();
};

const registerSecondaryDonation = async (
donation: DraftDonation,
fromWalletAddress: string,
prevTransactionId: string,
prevTransactionCreatedAt: string,
project: any,
token: any,
tokenPrice: any,
donor: any,
qfRound: any,
) => {
try {
// deteect similar transaction on stellar network with time difference of less/more than 1 minute
const response = await axios.get(
`${STELLAR_HORIZON_API}/accounts/${donation.toWalletAddress}/payments?limit=200&order=desc&join=transactions&include_failed=true`,
);

const transactions = response.data._embedded.records;
if (!transactions.length) return;

for (const transaction of transactions) {
const isSecondaryMatchingTransaction =
((transaction.asset_type === 'native' &&
transaction.type === 'payment' &&
transaction.to === donation.toWalletAddress &&
Number(transaction.amount) === donation.amount &&
transaction.source_account === fromWalletAddress) ||
(transaction.type === 'create_account' &&
transaction.account === donation.toWalletAddress &&
Number(transaction.starting_balance) === donation.amount &&
transaction.source_account === fromWalletAddress)) &&
Math.abs(
new Date(transaction.created_at).getTime() -
new Date(prevTransactionCreatedAt).getTime(),
) <= 60000 &&
transaction.transaction_hash !== prevTransactionId;

if (isSecondaryMatchingTransaction) {
if (
donation.toWalletMemo &&
transaction.type === 'payment' &&
transaction.transaction.memo !== donation.toWalletMemo
) {
logger.debug(
`Transaction memo does not match donation memo for donation ID ${donation.id}`,
);
return;
}

// Check if donation already exists
const existingDonation = await findDonationsByTransactionId(
transaction.transaction_hash?.toLowerCase(),
);
if (existingDonation) return;

const { givbackFactor, projectRank, bottomRankInRound, powerRound } =
await calculateGivbackFactor(project.id);

const returnedDonation = await createDonation({
amount: donation.amount,
project,
transactionNetworkId: donation.networkId,
fromWalletAddress: transaction.source_account,
transactionId: transaction.transaction_hash,
tokenAddress: donation.tokenAddress,
isProjectVerified: project.verified,
donorUser: donor,
isTokenEligibleForGivback: token.isGivbackEligible,
segmentNotified: false,
toWalletAddress: donation.toWalletAddress,
donationAnonymous: false,
transakId: '',
token: donation.currency,
valueUsd: donation.amount * tokenPrice,
priceUsd: tokenPrice,
status: transaction.transaction_successful ? 'verified' : 'failed',
isQRDonation: true,
toWalletMemo: donation.toWalletMemo,
qfRound,
chainType: token.chainType,
givbackFactor,
projectRank,
bottomRankInRound,
powerRound,
});

if (!returnedDonation) {
logger.debug(
`Error creating donation for draft donation ID ${donation.id}`,
);
return;
}

await syncDonationStatusWithBlockchainNetwork({
donationId: returnedDonation.id,
});
}
}
} catch (error) {
logger.debug(
`Error checking secondary transactions for donation ID ${donation.id}:`,
error,
);
}
};
Meriem-BM marked this conversation as resolved.
Show resolved Hide resolved

// Check for transactions
export async function checkTransactions(
donation: DraftDonation,
Expand All @@ -55,8 +161,7 @@ export async function checkTransactions(
return;
}

// Check if donation has expired
const now = new Date().getTime();
const now = Date.now();
const expiresAtDate = new Date(expiresAt!).getTime() + 1 * 60 * 1000;

if (now > expiresAtDate) {
Expand All @@ -73,8 +178,7 @@ export async function checkTransactions(
);

const transactions = response.data._embedded.records;

if (transactions.length === 0) return;
if (!transactions.length) return;

for (const transaction of transactions) {
const isMatchingTransaction =
Expand Down Expand Up @@ -145,7 +249,7 @@ export async function checkTransactions(

const returnedDonation = await createDonation({
amount: donation.amount,
project: project,
project,
transactionNetworkId: donation.networkId,
fromWalletAddress: transaction.source_account,
transactionId: transaction.transaction_hash,
Expand Down Expand Up @@ -199,6 +303,21 @@ export async function checkTransactions(
},
});

// Register secondary donation after 10 seconds
setTimeout(async () => {
await registerSecondaryDonation(
donation,
transaction.source_account,
transaction.transaction_hash,
transaction.created_at,
project,
token,
tokenPrice,
donor,
qfRound,
);
}, 10000);

return;
}
}
Expand Down
Loading