From 11d136fad64c25fbcbd63d8e03baccbbb76ad1ea Mon Sep 17 00:00:00 2001 From: XingqiWang Date: Sat, 16 Nov 2024 12:52:23 +0100 Subject: [PATCH] updata 08-v0 --- 08-v0/alt.ts | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 08-v0/alt.ts diff --git a/08-v0/alt.ts b/08-v0/alt.ts new file mode 100644 index 0000000..a184358 --- /dev/null +++ b/08-v0/alt.ts @@ -0,0 +1,160 @@ +import { + Connection, + PublicKey, + Keypair, + TransactionMessage, + VersionedTransaction, + SystemProgram, + AddressLookupTableProgram +} from '@solana/web3.js'; +import fs from "fs"; + +// 创建RPC连接 +const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed"); +// const connection = new Connection("https://mainnet-ams.chainbuff.com", "confirmed"); + +// 本地导入钱包 +// const fromSecretKey = Uint8Array.from(JSON.parse(fs.readFileSync("wallet.json"))); +const secretKey = Uint8Array.from(JSON.parse(fs.readFileSync("web3xFMwEPrc92NeeXdAigni95NDnnd2NPuajTirao2.json"))); +const payer = Keypair.fromSecretKey(secretKey); + +async function createALT() { + + // 获取当前slot + const slot = await connection.getSlot("confirmed"); + + // 创建ALT + const [lookupTableInstruction, lookupTableAddress] = + AddressLookupTableProgram.createLookupTable({ + authority: payer.publicKey, + payer: payer.publicKey, + recentSlot: slot, + }); + + console.log("lookup table address:", lookupTableAddress.toBase58()); + + // 创建v0 message + const { blockhash } = await connection.getLatestBlockhash(); + const messageV0 = new TransactionMessage({ + payerKey: payer.publicKey, + recentBlockhash: blockhash, // 最近的区块hash + instructions: [lookupTableInstruction], // 指令数组 + }).compileToV0Message(); + + // 创建v0交易并签名 + const transaction = new VersionedTransaction(messageV0); + transaction.sign([payer]); + + // 模拟交易 + const simulateResult = await connection.simulateTransaction(transaction); + console.log("模拟交易结果: ", simulateResult); + + // 发送交易 + const signature = await connection.sendTransaction(transaction); + console.log(`交易已发送: https://solscan.io/tx/${signature}`); +} + +async function addAddresses() { + + const lookupTableAddress = new PublicKey('2qqXrZZSG9naivqMyWHHUDRFVNh3YthsTbN5EPU8Poo5') + + // 添加账户到ALT + const extendInstruction = AddressLookupTableProgram.extendLookupTable({ + lookupTable: lookupTableAddress, + payer: payer.publicKey, + authority: payer.publicKey, + addresses: [ + payer.publicKey, + new PublicKey('buffaAJKmNLao65TDTUGq8oB9HgxkfPLGqPMFQapotJ'), + SystemProgram.programId, // + ], + }); + + // 创建v0 message + const { blockhash } = await connection.getLatestBlockhash(); + const messageV0 = new TransactionMessage({ + payerKey: payer.publicKey, + recentBlockhash: blockhash, // 最近的区块hash + instructions: [extendInstruction], // 指令数组 + }).compileToV0Message(); + + // 创建v0交易并签名 + const transaction = new VersionedTransaction(messageV0); + transaction.sign([payer]); + + // 模拟交易 + const simulateResult = await connection.simulateTransaction(transaction); + console.log("模拟交易结果: ", simulateResult); + + // 发送交易 + const signature = await connection.sendTransaction(transaction); + console.log(`交易已发送: https://solscan.io/tx/${signature}`); + +} + +async function transfer() { + + const lookupTableAddress = new PublicKey('2qqXrZZSG9naivqMyWHHUDRFVNh3YthsTbN5EPU8Poo5') + + // 获取ALT + const ALT = await connection.getAddressLookupTable(lookupTableAddress); + const lookupTableAccount = ALT.value; + if (!ALT.value) { + throw new Error("lookupTableAccount不存在"); + } + console.log('lookupTableAccount:', lookupTableAccount) + + // 目标地址 + const toAddress = new PublicKey('buffaAJKmNLao65TDTUGq8oB9HgxkfPLGqPMFQapotJ'); + + // 转账指令 + const instruction = SystemProgram.transfer({ + fromPubkey: payer.publicKey, + toPubkey: toAddress, + lamports: 1000, // 1000 lamports + }); + + // 创建v0 message + const { blockhash } = await connection.getLatestBlockhash(); + const messageV0 = new TransactionMessage({ + payerKey: payer.publicKey, + recentBlockhash: blockhash, // 最近的区块hash + instructions: [instruction], // 指令数组 + }).compileToV0Message([lookupTableAccount]); + + // 创建v0交易并签名 + const transaction = new VersionedTransaction(messageV0); + transaction.sign([payer]); + + // 模拟交易 + const simulateResult = await connection.simulateTransaction(transaction); + console.log("模拟交易结果: ", simulateResult); + + // 发送交易 + const signature = await connection.sendTransaction(transaction); + console.log(`交易已发送: https://solscan.io/tx/${signature}`); + +} + +async function parseTx() { + + const parsedTransaction1 = await connection.getParsedTransaction('4LwygRtiF9ZCrbGKoh8MEzmxowaRHPaDc1nsinkv72uXU2cUCuZ8YskBBgsvbBEMZ5Pqpf6C6WcXtCkqAuLZand1', { + commitment: "confirmed", + maxSupportedTransactionVersion: 0 + }); + console.log(`已解析的v0交易: ${JSON.stringify(parsedTransaction1)}\n`); + +} + + +// 创建ALT +// createALT(); + +// 为ALT添加账户 +// addAddresses(); + +// 使用ALT的转账 +// transfer(); + +// 获取解析的v0交易 +parseTx(); \ No newline at end of file