From a935e3653c156df9aed6833558473ae37cdb6287 Mon Sep 17 00:00:00 2001 From: Adw8 Date: Thu, 6 Feb 2025 18:04:08 +0530 Subject: [PATCH] Store escrow in tweet table on successful lock creation --- src/entity/Tweet.ts | 6 +++++ src/locker-utils/index.ts | 1 + src/utils/create-lock.ts | 2 +- src/utils/verifyTweet.ts | 57 ++++++++++++++++++++++++--------------- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/entity/Tweet.ts b/src/entity/Tweet.ts index 888a9d9..c3f5a07 100644 --- a/src/entity/Tweet.ts +++ b/src/entity/Tweet.ts @@ -10,4 +10,10 @@ export class Tweet { @Column({ unique: true }) transactionSignature!: string; + + @Column({ type: 'boolean', nullable: true }) + isLockCreated!: boolean | null; + + @Column({ type: 'text', unique: true, nullable: true }) + lockEscrow!: string | null; } diff --git a/src/locker-utils/index.ts b/src/locker-utils/index.ts index 58f102a..40628c5 100644 --- a/src/locker-utils/index.ts +++ b/src/locker-utils/index.ts @@ -186,6 +186,7 @@ export async function createVestingPlanV2(params: CreateVestingPlanParams) { if(confirmedTransaction === null) { console.error('Transaction failed for', error.signature); + throw error; } return escrow; diff --git a/src/utils/create-lock.ts b/src/utils/create-lock.ts index ca67d16..727fa53 100644 --- a/src/utils/create-lock.ts +++ b/src/utils/create-lock.ts @@ -34,7 +34,7 @@ const provider = new anchor.AnchorProvider( anchor.setProvider(provider); -export async function createLock(tokenLockerKeypair: anchor.web3.Keypair, recipientPubKey: anchor.web3.PublicKey, duration: BN, balance: BN): Promise { +export async function createLock(tokenLockerKeypair: anchor.web3.Keypair, recipientPubKey: anchor.web3.PublicKey, duration: BN, balance: BN): Promise { if (balance.eq(new BN(0))) { console.log('No balance available to create lock, skipping...'); diff --git a/src/utils/verifyTweet.ts b/src/utils/verifyTweet.ts index d102944..f20a274 100644 --- a/src/utils/verifyTweet.ts +++ b/src/utils/verifyTweet.ts @@ -23,34 +23,47 @@ export async function verifySignatureInTweet(transactionSignature: string): Prom } export async function processTweet(txSignature: string, memeUrl: string | null) { - return await (global.appDataSource as DataSource).transaction(async (transactionalEntityManager) => { - const tweetRepository = transactionalEntityManager.getRepository(global.entities.Tweet as EntityTarget); + const tweetRepository = (global.appDataSource as DataSource).getRepository( + global.entities.Tweet as EntityTarget + ); - const tweet = await tweetRepository.save({ - transactionSignature: txSignature, - url: memeUrl, - }); + const tweet = await tweetRepository.save({ + transactionSignature: txSignature, + url: memeUrl, + }); - const isFourthUser = tweet.id % 4 === 0; + const isFourthUser = tweet.id % 4 === 0; - try { - if (isFourthUser) { - const { authority, amount } = await extractTxInfo(txSignature); + try { + if (isFourthUser) { + const { authority, amount } = await extractTxInfo(txSignature); - if (!authority || Number(amount) <= 0) { - return { error: "Invalid transaction details" } - } - - const escrow = await createRewardLock(authority, amount); - - return { success: true, data: { escrow } }; + if (!authority || Number(amount) <= 0) { + return { error: "Invalid transaction details" }; } - return { success: true, message: 'Tweet verified' } - } catch (error) { - console.error('Error locking tokens.'); + const escrow = await createRewardLock(authority, amount); - throw new Error("Transaction failed."); + if (!escrow) { + throw new Error("Lock not created"); + } + + await tweetRepository.update(tweet.id, { + isLockCreated: true, + lockEscrow: escrow.toString() + }); + + return { success: true, data: { escrow } }; } - }); + + return { success: true, message: 'Tweet verified' }; + } catch (error) { + await tweetRepository.update(tweet.id, { + isLockCreated: false, + }); + + console.error('Error locking tokens.'); + + throw new Error("Transaction failed."); + } }