0x0x230x

Untitled

Jan 21st, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. async sendSolanaTransaction(params: {
  2. walletAddress: string;
  3. walletId: string;
  4. tokenAddress?: string;
  5. to: string;
  6. amount: bigint;
  7. isNative: boolean;
  8. }): Promise<{ hash: string; status: 'success' | 'failed' }> {
  9. try {
  10. const transaction = await solanaService.preparePrivyTransaction({
  11. senderAddress: params.walletAddress,
  12. recipientAddress: params.to,
  13. tokenMint: params.tokenAddress,
  14. amount: params.amount,
  15. isNative: params.isNative,
  16. });
  17.  
  18. const response =
  19. await this.client.walletApi.solana.signAndSendTransaction({
  20. walletId: params.walletId,
  21. caip2: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
  22. transaction,
  23. });
  24.  
  25. if (!response || !response.hash) {
  26. throw new Error('No transaction hash received');
  27. }
  28.  
  29. return {
  30. hash: response.hash,
  31. status: 'success',
  32. };
  33. } catch (error) {
  34. console.error('❌ Error sending Solana transaction:', error);
  35. throw error;
  36. }
  37. }
  38.  
  39.  
  40.  
  41. async preparePrivyTransaction(params: TransactionParams) {
  42. try {
  43. const instructions: TransactionInstruction[] = [];
  44.  
  45. if (params.isNative) {
  46. instructions.push(
  47. SystemProgram.transfer({
  48. fromPubkey: new PublicKey(params.senderAddress),
  49. toPubkey: new PublicKey(params.recipientAddress),
  50. lamports: params.amount,
  51. }),
  52. );
  53. } else if (params.tokenMint) {
  54. const senderTokenAccount = await this.getTokenAccount(
  55. params.senderAddress,
  56. params.tokenMint,
  57. );
  58.  
  59. if (!senderTokenAccount) {
  60. throw new Error('Sender token account not found');
  61. }
  62.  
  63. const { tokenAccount: recipientTokenAccount, shouldCreate } =
  64. await this.getOrCreateTokenAccount(
  65. params.recipientAddress,
  66. params.tokenMint,
  67. );
  68.  
  69. if (shouldCreate) {
  70. instructions.push(
  71. createAssociatedTokenAccountInstruction(
  72. new PublicKey(params.senderAddress),
  73. recipientTokenAccount,
  74. new PublicKey(params.recipientAddress),
  75. new PublicKey(params.tokenMint),
  76. ),
  77. );
  78. }
  79.  
  80. instructions.push(
  81. createTransferInstruction(
  82. senderTokenAccount,
  83. recipientTokenAccount,
  84. new PublicKey(params.senderAddress),
  85. params.amount,
  86. ),
  87. );
  88. }
  89.  
  90. const { blockhash } = await this.connection.getLatestBlockhash({
  91. commitment: 'recent',
  92. });
  93.  
  94. const messageV0 = new TransactionMessage({
  95. payerKey: new PublicKey(params.senderAddress),
  96. recentBlockhash: blockhash,
  97. instructions,
  98. }).compileToV0Message();
  99.  
  100. return new VersionedTransaction(messageV0);
  101. } catch (error) {
  102. console.error('Error in preparePrivyTransaction:', error);
  103. throw error;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment