Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async sendSolanaTransaction(params: {
- walletAddress: string;
- walletId: string;
- tokenAddress?: string;
- to: string;
- amount: bigint;
- isNative: boolean;
- }): Promise<{ hash: string; status: 'success' | 'failed' }> {
- try {
- const transaction = await solanaService.preparePrivyTransaction({
- senderAddress: params.walletAddress,
- recipientAddress: params.to,
- tokenMint: params.tokenAddress,
- amount: params.amount,
- isNative: params.isNative,
- });
- const response =
- await this.client.walletApi.solana.signAndSendTransaction({
- walletId: params.walletId,
- caip2: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
- transaction,
- });
- if (!response || !response.hash) {
- throw new Error('No transaction hash received');
- }
- return {
- hash: response.hash,
- status: 'success',
- };
- } catch (error) {
- console.error('❌ Error sending Solana transaction:', error);
- throw error;
- }
- }
- async preparePrivyTransaction(params: TransactionParams) {
- try {
- const instructions: TransactionInstruction[] = [];
- if (params.isNative) {
- instructions.push(
- SystemProgram.transfer({
- fromPubkey: new PublicKey(params.senderAddress),
- toPubkey: new PublicKey(params.recipientAddress),
- lamports: params.amount,
- }),
- );
- } else if (params.tokenMint) {
- const senderTokenAccount = await this.getTokenAccount(
- params.senderAddress,
- params.tokenMint,
- );
- if (!senderTokenAccount) {
- throw new Error('Sender token account not found');
- }
- const { tokenAccount: recipientTokenAccount, shouldCreate } =
- await this.getOrCreateTokenAccount(
- params.recipientAddress,
- params.tokenMint,
- );
- if (shouldCreate) {
- instructions.push(
- createAssociatedTokenAccountInstruction(
- new PublicKey(params.senderAddress),
- recipientTokenAccount,
- new PublicKey(params.recipientAddress),
- new PublicKey(params.tokenMint),
- ),
- );
- }
- instructions.push(
- createTransferInstruction(
- senderTokenAccount,
- recipientTokenAccount,
- new PublicKey(params.senderAddress),
- params.amount,
- ),
- );
- }
- const { blockhash } = await this.connection.getLatestBlockhash({
- commitment: 'recent',
- });
- const messageV0 = new TransactionMessage({
- payerKey: new PublicKey(params.senderAddress),
- recentBlockhash: blockhash,
- instructions,
- }).compileToV0Message();
- return new VersionedTransaction(messageV0);
- } catch (error) {
- console.error('Error in preparePrivyTransaction:', error);
- throw error;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment