Transaction Response

Icon Link交易响应

一旦提交了交易,您可能想要提取有关交易结果的信息。SDK 提供了一个 TransactionResponse 类,其中包含帮助方法来公开以下信息:

  • 交易 ID
  • 状态 (已提交、成功、被挤出或失败)
  • 收据 (返回数据、日志、铸造/销毁、转账和恐慌/回滚)
  • 气体费用和使用情况
  • 交易的原始载荷,包括输入和输出
  • 交易的日期和时间
  • 包含交易的区块

首先,我们可以从已提交的交易结果中提取这些信息:

// #import { TransactionResponse };
 
// Call a contract function
const call = await contract.functions.increment_count(15).call();
 
// Pick off the transaction response
const transactionResponse: TransactionResponse = call.transactionResponse;
 
// Retrieve the full transaction summary
const transactionSummary = await transactionResponse.getTransactionSummary();

我们还可以使用交易请求的结果提取交易摘要:

// #import { ScriptTransactionRequest, TransactionResponse };
 
// Instantiate the transaction request using a ScriptTransactionRequest and set
// the script main function arguments
const transactionRequest = new ScriptTransactionRequest({
  script: scriptBytecode,
});
transactionRequest.setData(scriptAbi, scriptMainFunctionArguments);
 
// Fund the transaction
const txCost = await provider.getTransactionCost(transactionRequest);
 
transactionRequest.maxFee = txCost.maxFee;
transactionRequest.gasLimit = txCost.gasUsed;
 
await wallet.fund(transactionRequest, txCost);
 
// Submit the transaction
const response: TransactionResponse = await wallet.sendTransaction(transactionRequest);
 
// Generate the transaction summary
const transactionSummary = await response.getTransactionSummary();

或者,我们可以从存储的交易 ID 构建交易摘要:

// #import { TransactionResponse };
 
// Take a transaction ID from a previous transaction
const transactionId = previouslySubmittedTransactionId;
// 0x...
 
// Retrieve the transaction response from the transaction ID
const transactionResponse = await TransactionResponse.create(transactionId, provider);
 
// Generate the transaction summary
const transactionSummary = await transactionResponse.getTransactionSummary();