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);
}
});在线示例
在线示例默认关闭批量广播,并把 sendTransaction 请求强制设为 broadcastEnabled: false。只有开启广播总开关后,各项才会按 JSON 中的设置执行。
在线示例
Turing.btc.sendBatchRequest()
待检测
请使用页面顶部的钱包入口连接后再执行。
已预置模拟数据;可直接查看调用效果,或清空后换成自己的交易数据。
当前钱包网络未知
本次执行中的可控交易均只签名,不广播。
默认关闭,并强制可控的交易请求使用 broadcastEnabled: false。开启后仍按每一项 JSON 的设置执行。
代码预览
const requests = [
{
"method": "sendTransaction",
"params": {
"toAddress": "1BitcoinEaterAddressDontSendf59kuE",
"amount": "100000",
"broadcastEnabled": false
}
},
{
"method": "signTransaction",
"params": {
"txHex": "020000000133333333333333333333333333333333333333333333333333333333333333330000000000ffffffff01a0860100000000001976a914444444444444444444444444444444444444444488ac00000000",
"type": "legacy",
"prevOutScriptsHex": [
"76a914555555555555555555555555555555555555555588ac"
]
}
}
];
const result = await Turing.btc.sendBatchRequest(requests);
console.log(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);
}
});