0x0x230x

Untitled

Jan 21st, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. async preparePrivyTransaction(params: TransactionParams) {
  2. try {
  3. const instructions: TransactionInstruction[] = [];
  4.  
  5. if (params.isNative) {
  6. instructions.push(
  7. SystemProgram.transfer({
  8. fromPubkey: new PublicKey(params.senderAddress),
  9. toPubkey: new PublicKey(params.recipientAddress),
  10. lamports: params.amount,
  11. }),
  12. );
  13. } else if (params.tokenMint) {
  14. const senderTokenAccount = await this.getTokenAccount(
  15. params.senderAddress,
  16. params.tokenMint,
  17. );
  18. if (!senderTokenAccount) {
  19. throw new Error('Sender token account not found');
  20. }
  21.  
  22. const { tokenAccount: recipientTokenAccount, shouldCreate } =
  23. await this.getOrCreateTokenAccount(
  24. params.recipientAddress,
  25. params.tokenMint,
  26. );
  27.  
  28. if (shouldCreate) {
  29. instructions.push(
  30. createAssociatedTokenAccountInstruction(
  31. new PublicKey(params.senderAddress),
  32. recipientTokenAccount,
  33. new PublicKey(params.recipientAddress),
  34. new PublicKey(params.tokenMint),
  35. ),
  36. );
  37. }
  38.  
  39. instructions.push(
  40. createTransferInstruction(
  41. senderTokenAccount,
  42. recipientTokenAccount,
  43. new PublicKey(params.senderAddress),
  44. params.amount,
  45. ),
  46. );
  47. }
  48.  
  49. const { blockhash } = await this.connection.getLatestBlockhash({
  50. commitment: 'processed',
  51. });
  52.  
  53. const messageV0 = new TransactionMessage({
  54. payerKey: new PublicKey(params.senderAddress),
  55. recentBlockhash: blockhash,
  56. instructions,
  57. }).compileToV0Message();
  58.  
  59. return new VersionedTransaction(messageV0);
  60. } catch (error) {
  61. console.error('Error in preparePrivyTransaction:', error);
  62. throw error;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment