Solana 交易确认

问题描述 投票:0回答:1
`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
  }`

是否有人在 .confirmTransaction() 行“content-src”中违反内容安全指令:self 并知道如何解决它? 我在 localhost 和我的自定义 vercel 部署上收到错误

我想确保交易已执行并且我可以更新余额

blockchain rpc solana
1个回答
0
投票

这是更新的代码。 试试这个。

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));
© www.soinside.com 2019 - 2024. All rights reserved.