valentine
valentine

Reputation: 9

Solana transaction confirm

`const sendTransaction = async (
    transaction: Transaction | VersionedTransaction,
    connection: Connection,
    options?: SendTransactionOptions
  ) => {
    // Use the original sendTransaction method in the implementation
    const signature = await originalSendTransaction.call(
      provider,
      transaction,
      connection,
      options
    )
    const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();

    // Wait for the transaction to be confirmed
    await connection.confirmTransaction({
      signature,
      blockhash,
      lastValidBlockHeight
    }, 'recent')

    this.syncBalance(this.getAddress() ?? '')

    return signature
  }`

Does anyone get Content Security Directive violation here in .confirmTransaction() row, "content-src": self and knows how to fix it? I get the error on localhost and on my custom vercel deployment

I wanted to be sure that the transaction is executed and I can update the balance

Upvotes: 0

Views: 122

Answers (1)

Shiny
Shiny

Reputation: 1

This is updated code. Try this.

import {
  Connection,
  Transaction,
  VersionedTransaction,
  SendTransactionOptions,
} from '@solana/web3.js';

// Mock originalSendTransaction to demonstrate how it might be called.
const originalSendTransaction = async (provider, transaction, connection, options) => {
  // Simulate sending a transaction
  return 'mock-signature';
};

const provider = { /* your provider setup */ };

const sendTransaction = async (
  transaction: Transaction | VersionedTransaction,
  connection: Connection,
  options?: SendTransactionOptions
) => {
  try {
    const signature = await originalSendTransaction.call(
      provider,
      transaction,
      connection,
      options
    );

    const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();

    // Wait for the transaction to be confirmed
    await connection.confirmTransaction({
      signature,
      blockhash,
      lastValidBlockHeight
    }, 'recent');

    // Simulate balance sync function (replace with your actual method)
    const syncBalance = async (address: string) => {
      console.log(`Syncing balance for address: ${address}`);
    };

    syncBalance(provider.getAddress() ?? '');

    return signature;
  } catch (error) {
    console.error('Failed to send or confirm transaction:', error);
    throw error; // Rethrow or handle error as needed
  }
};

const connection = new Connection('https://api.mainnet-beta.solana.com', 'recent');
const transaction = new Transaction(); // Set up your transaction

// Example usage
sendTransaction(transaction, connection)
  .then(signature => console.log(`Transaction confirmed with signature: ${signature}`))
  .catch(error => console.error('Error in transaction:', error));

Upvotes: 0

Related Questions