トランザクションの送信、シミュレート、および管理のためのメソッド。
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
トランザクションを実行します。成功時はメタデータを返し、エラー時は失敗の詳細を返します。
const result = client.svm.sendTransaction(signedTx);// Check for errorsif ("err" in result && result.err()) {console.error("Transaction failed:", result.err());return;}// Access result properties (note: these are getter functions)console.log("Signature:", result.signature());console.log("Compute units:", result.computeUnitsConsumed());console.log("Logs:", result.logs());
トランザクション結果のプロパティ(err()、computeUnitsConsumed()、
logs()、signature())はゲッター関数であり、プロパティではありません。常に
括弧を付けて呼び出してください。
TransactionMetadata のメソッド
| メソッド | 戻り値 | 説明 |
|---|---|---|
signature() | Uint8Array | トランザクション署名のバイト列 |
computeUnitsConsumed() | bigint | 使用されたコンピュートユニット数 |
logs() | string[] | プログラムログ |
innerInstructions() | InnerInstruction[] | 内部(CPI)instructions |
returnData() | TransactionReturnData | プログラムからの戻りデータ |
prettyLogs() | string | カラー付きのフォーマット済みログ |
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
状態を変更せずにトランザクションをシミュレートします。
const simResult = client.svm.simulateTransaction(signedTx);if ("err" in simResult && simResult.err()) {console.error("Simulation failed:", simResult.err());return;}// Use .meta() to get TransactionMetadataconst meta = simResult.meta();console.log("Would use", meta.computeUnitsConsumed(), "compute units");console.log("Logs:", meta.logs());// Get post-execution account statesconst postAccounts = simResult.postAccounts();console.log("Post-state accounts:", postAccounts);
シミュレーションは状態を変更しません。送信前にトランザクションを確認するために使用してください。
ブロックハッシュの管理
latestBlockhash
latestBlockhash(): Blockhash
現在のブロックハッシュを取得します。
const blockhash = client.svm.latestBlockhash();console.log("Blockhash:", blockhash);
getLatestBlockhash(client.rpc 経由)
トランザクションを手動で構築するために、有効期限情報付きのブロックハッシュを取得します。
blockhash と lastValidBlockHeight の両方を返すため、RPCヘルパーを使用してください。
import {createTransactionMessage,setTransactionMessageLifetimeUsingBlockhash} from "@solana/kit";const { value: blockhashLifetime } = await client.rpc.getLatestBlockhash().send();const tx = setTransactionMessageLifetimeUsingBlockhash(blockhashLifetime,createTransactionMessage({ version: 0 }));
systemProgram() や tokenProgram() などのプログラムプラグインを使用する場合、
ブロックハッシュを自分で取得する必要はありません。client.sendTransaction()(および
instructionチェーン上の .sendTransaction() ヘルパー)が自動的に処理します。
expireBlockhash
expireBlockhash(): LiteSVM
新しいブロックハッシュに進めます。ブロックハッシュの有効期限切れシナリオのテストに便利です。
const oldBlockhash = client.svm.latestBlockhash();client.svm.expireBlockhash();const newBlockhash = client.svm.latestBlockhash();console.log("Old:", oldBlockhash);console.log("New:", newBlockhash);// Blockhashes will be different
トランザクション履歴
送信後にトランザクションを取得するには、トランザクション履歴を有効にしてください:
// Enable history storage (stores last N transactions)client.svm.withTransactionHistory(100n);// Send a transactionconst result = client.svm.sendTransaction(signedTx);// Later, retrieve it by signature (base58 string)// Note: getTransaction expects a base58-encoded signature string
Is this page helpful?