`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 部署上收到错误
我想确保交易已执行并且我可以更新余额
这是更新的代码。 试试这个。
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));