Skip to content

Instantly share code, notes, and snippets.

@focusj
Created January 4, 2025 08:20
Show Gist options
  • Select an option

  • Save focusj/7b690fa965e3435f596d84dde0b997a9 to your computer and use it in GitHub Desktop.

Select an option

Save focusj/7b690fa965e3435f596d84dde0b997a9 to your computer and use it in GitHub Desktop.
Solana使用 web3.js 发送 serialized tx strig
const privateKey = new Uint8Array(bs58.decode(privateKeyString));
const sender = Keypair.fromSecretKey(privateKey);
const connection = new Connection("https://mainnet.helius-rpc.com/?api-key=637cde60-2fbe-44c9-9395-cfbdfb43e248", 'confirmed'); // 替换为你的连接
const parsedTx = web3.VersionedTransaction.deserialize(bs58.decode("3nATh5zVLTyxA7TgF7nLbCHZKjSh48Vo4Kv5YR8Xh78G6zJ16QMPEKKYZ8V9UJetmf4MsPQeUxEuTDkoUsYq3FNcmm1MKtjPEnr53wawHLCA5pvYa8QUzwRgGqsX5PMyXuxZE8eDhTq8JS8pa3L6s7gnab8XngijgsVwAzQJmY38fA8KnXRcYSQBaM1dofDL2Kdt8Xi21cFYq2AX1PEiE53V6ik5TZxMhZR3LjKJmm365toVpZhYtZomEa2Q6uFziAbXLKWBAeAMLHM8CbzAMKAdEss5HtH49Ts99FHBeNSQJJaEivFyPD8wEEf488UbjwUpMYyoBi3isYmHwvAxFQJUVdWVp5Z5zyZ5mUi7HjBpre5B3yXAVcFv1kMMgiiCKTFRb3LJXQn1BM3r5Q2YcNEwXSR6cUv9QJFpBcnkMnsHAbHwwUxDakWFwhcddqe767DPRhE6Drgd1gJJrg4kMsvQ8Ez6D4JhnfuB8SS4sVN2dVj3TDPZ6KA8snzofmWfNnXC9eAPgEJndQTh3cmxkC1VFnZSXf4grT3tufU9cCJoKeaXhQfcZKPYU2ejfrWiTJia3NmajZ4b2ZqCpLtEeiU5PUWtKFtRah23W42KShL9ooExpg6Nqvt4M9oiKd2SnQE58j1FFYBdT7sWqjh9xo1NqDaqW4A9aJaVHKiehY7NytbzkuDenytvbus5ekxDsDNq1qFZFgYisiy5pAkkvgAs6y6qYUCJP6hv6RbFUc8mwBJB9U7S85q7JRjsLptymi3cZj182QrCPTsCFY5wAwbiWCu6xtbiqH2zsCmJCbd5yCTM2ZrYzcmVAQ4cAU9cyuwL5BTbyCRrxFKmGvK8mLym6UArpetANbAUvePfzDioqZrEQvPaW7JYhFjKMvkndX3EHeFkQVqrGzZoRzSHLGQE8KUeiffJU6Rx2UgTvzZRHVmgSyxcDwjxYqsoqPFMf21Ea3nCo"))
const blockhash = (await connection.getLatestBlockhash("confirmed")).blockhash
let transaction = new web3.Transaction({
feePayer: sender.publicKey, recentBlockhash: blockhash
});
parsedTx.message.recentBlockhash = blockhash;
transaction.add(parsedTx.message.compiledInstructions)
// const SwapSig = await web3.sendAndConfirmTransaction(connection, transaction, [sender]);
// console.log('SIGNATURE', SwapSig);
//
// const swapRs = await connection.confirmTransaction(SwapSig);
// console.log('Transaction confirmed:', swapRs);
parsedTx.sign([sender]);
const signature = await connection.sendTransaction(parsedTx, {skipPreflight: true});
console.log(signature)
const rs = await connection.confirmTransaction(signature);
console.log('Transaction confirmed', rs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment