Store escrow in tweet table on successful lock creation

This commit is contained in:
Adw8 2025-02-06 18:04:08 +05:30
parent 5be2edfd43
commit c923edf3c5
4 changed files with 43 additions and 23 deletions

View File

@ -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;
}

View File

@ -186,6 +186,7 @@ export async function createVestingPlanV2(params: CreateVestingPlanParams) {
if(confirmedTransaction === null) {
console.error('Transaction failed for', error.signature);
throw error;
}
return escrow;

View File

@ -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<anchor.web3.PublicKey | void> {
export async function createLock(tokenLockerKeypair: anchor.web3.Keypair, recipientPubKey: anchor.web3.PublicKey, duration: BN, balance: BN): Promise<anchor.web3.PublicKey | undefined> {
if (balance.eq(new BN(0))) {
console.log('No balance available to create lock, skipping...');

View File

@ -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<Tweet>);
const tweetRepository = (global.appDataSource as DataSource).getRepository(
global.entities.Tweet as EntityTarget<Tweet>
);
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.");
}
}