btc.sendBatchRequest
通过 btc 对象批量提交 BTC 相关请求。每个请求独立执行,单个请求失败不影响其他请求。
支持的方法
sendTransaction- 发送交易signTransaction- 签名交易
用法
ts
const requests = [
{
method: "sendTransaction",
params: {
toAddress: "bc1qxyz...",
amount: "100000",
broadcastEnabled: true,
},
},
{
method: "signTransaction",
params: {
txHex: "0200000001...",
type: "legacy",
prevOutScriptsHex: ["76a914...88ac"],
},
},
];
const results = await Turing.btc.sendBatchRequest(requests);参数
ts
type BtcBatchRequestMethod = "sendTransaction" | "signTransaction";
interface BtcBatchRequest {
method: BtcBatchRequestMethod; // 要调用的 BTC 方法名
params: BtcSendTransaction | BtcSignTransaction; // 对应方法的参数
}| 字段 | 类型 | 说明 |
|---|---|---|
method | BtcBatchRequestMethod | 要调用的 BTC 方法名:"sendTransaction" 或 "signTransaction" |
params | BtcSendTransaction | BtcSignTransaction | 对应方法的参数,参见各方法文档 |
返回值
返回结果数组,每个元素对应一个请求的结果,顺序与输入一致。
ts
type BtcBatchResponse = Array<
BtcSendTransactionResponse | BtcSignTransactionResponse
>;| 字段 | 类型 | 说明 |
|---|---|---|
txid | string | sendTransaction 广播成功时的交易哈希 |
txraw | string | sendTransaction 不广播时的交易原始数据 |
sigs | string[] | signTransaction 返回的签名 hex 数组 |
error | string | 请求失败时的错误信息 |
错误处理
ts
const results = await Turing.btc.sendBatchRequest(requests);
results.forEach((result, index) => {
if (result.error) {
console.error(`请求 ${index + 1} 失败:`, result.error);
} else {
console.log(`请求 ${index + 1} 成功:`, result);
}
});示例
批量发送和签名
ts
const requests = [
{
method: "sendTransaction",
params: {
toAddress: "bc1qxyz...",
amount: "100000",
broadcastEnabled: true,
},
},
{
method: "signTransaction",
params: {
txHex: "0200000001...",
type: "legacy",
prevOutScriptsHex: ["76a914...88ac"],
},
},
];
const results = await Turing.btc.sendBatchRequest(requests);
results.forEach((result, index) => {
if (result.error) {
console.error(`请求 ${index + 1} 失败:`, result.error);
} else {
console.log(`请求 ${index + 1} 成功:`, result);
}
});