Transactions
將 tx hex 推送到 mempool.space 的 API 時出現錯誤 400
我正在使用 mempool.space 提供的程式碼語法來處理來自他們網站的 POST 事務請求:
<https://mempool.space/api>
const mempoolJS = require("@mempool/mempool.js") const init = async () => { const { bitcoin: { transactions } } = mempoolJS({ hostname: 'mempool.space' }); const txHex = '0200000001fd5b5fcd1cb066c27cfc9fda5428b9be850b81ac440ea51f1ddba2f987189ac1010000008a4730440220686a40e9d2dbffeab4ca1ff66341d06a17806767f12a1fc4f55740a7af24c6b5022049dd3c9a85ac6c51fecd5f4baff7782a518781bbdd94453c8383755e24ba755c01410436d554adf4a3eb03a317c77aa4020a7bba62999df633bba0ea8f83f48b9e01b0861d3b3c796840f982ee6b14c3c4b7ad04fcfcc3774f81bff9aaf52a15751fedfdffffff02416c00000000000017a914bc791b2afdfe1e1b5650864a9297b20d74c61f4787d71d0000000000001976a9140a59837ccd4df25adc31cdad39be6a8d97557ed688ac00000000'; const txid = await transactions.postTx({ txHex }); console.log(txid); }; init();
此程式碼段不適用於我。我不斷得到一個
UnhandledPromiseRejectionWarning: Error: Request failed with status code 400
. 我不確定是不是因為我使用的是require而不是import?或者我沒有正確發送 POST 請求?或者沒有正確處理 Promise 語法?將不勝感激的建議。
我測試了你的程式碼,顯然程式碼是正確的,但你的十六進製字元串是錯誤的。API 響應如下:
data: 'sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}'
確保您有正確的十六進制來發布您的交易。:)
- 創建事務並獲取十六進制:
0200000000010119335721e24061cab07c9caf3a9ed331b85511af3d6be38ef37bda93ddce49880100000000fdffffff02a086010000000000160014243588dda3d0560986069a55b386c1d3417ecc2e19a903000000000016001469577cafbaa617f64cd468ff51a578aeb85cfcf202473044022066c1314f71f05cf1c2e27298dc056d7b66cbc2bab0b3d536ad084fa577d9a0650220349102e2d151309b7820ff848d39ba28c6763f972e90359300dbfd3131ff4fa1012102cc2868604a6db586db0d72654f71835a3d6c20e465eea4597509963f47bd3c65dbe91f00
- 使用Postman發送 POST 請求:
網址:https ://mempool.space/testnet/api/tx
正文:事務十六進制 3. 它將返回交易ID:
23b0c4ce54e13a846e6a9f50f11019071f98c53f1d82ea9c674de1c7a94af45a
使用 JavaScript 做同樣的事情:
- 點擊
</>
- 選擇 JavaScript 的選項之一。我使用了 JavaScript-Fetch:
var myHeaders = new Headers(); myHeaders.append("Content-Type", "text/plain"); var raw = "020000000001015af44aa9c7e14d679cea821d3fc5981f071910f1509f6a6e843ae154cec4b0230100000000fdffffff02a0860100000000001600141c97aee9cf15d8a9016c3a98c5a503ed49d012caec2102000000000016001463924d833ecc676de6dec0cf6c72c6c94bc1c5350247304402206fbb992ce8f8e35b5c229dae731709c7d82de404d882e62271ce6e7551e103ff02203e94d589766db4e583d862b6ad3951fe62c498b11fdeae85bfc37ef61e2af13a0121039072d28be7d83a8f4b1983da7a74d2bd28e80ee79d4ee2bf603e101865099378dce91f00"; var requestOptions = { method: 'POST', headers: myHeaders, body: raw, redirect: 'follow' }; fetch("https://mempool.space/testnet/api/tx", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error));
- 在https://jsfiddle.net/上執行此程序,您將獲得事務 id 作為響應:
bd0306a1fa6e262949c9856ebcea61c0857067e1d2c856d0216a9cf871e4a377