-1

I am using the code syntax provided by mempool.space for a POST transaction request from their website here:

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();

This code snippet isn't working for me. I keep getting a UnhandledPromiseRejectionWarning: Error: Request failed with status code 400. I am unsure if it's because I am using the require instead of import? Or I am not correctly sending a POST request? Or not handling the Promise syntax correctly?

Would appreciate suggestions.

3
  • Try this: pastebin.com/raw/3DL7ULsW
    – user103136
    Sep 4, 2021 at 10:34
  • Would I be entering all this into the same .js file?
    – jiamijiang
    Sep 4, 2021 at 12:46
  • I will share the steps for this workaround in answer below.
    – user103136
    Sep 4, 2021 at 13:03

2 Answers 2

3

I tested your code and apparently the code is right but your Hex string is wrong. The API response is as follows:

data: 'sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}'

Make sure that you have right Hex to post your tx. :)

0
  1. Create a transaction and get hex:
0200000000010119335721e24061cab07c9caf3a9ed331b85511af3d6be38ef37bda93ddce49880100000000fdffffff02a086010000000000160014243588dda3d0560986069a55b386c1d3417ecc2e19a903000000000016001469577cafbaa617f64cd468ff51a578aeb85cfcf202473044022066c1314f71f05cf1c2e27298dc056d7b66cbc2bab0b3d536ad084fa577d9a0650220349102e2d151309b7820ff848d39ba28c6763f972e90359300dbfd3131ff4fa1012102cc2868604a6db586db0d72654f71835a3d6c20e465eea4597509963f47bd3c65dbe91f00
  1. Use Postman to send a POST request with:

    URL: https://mempool.space/testnet/api/tx

    Body: Transaction hex

  2. It will return transaction id:

23b0c4ce54e13a846e6a9f50f11019071f98c53f1d82ea9c674de1c7a94af45a

postman

Doing same thing using JavaScript:

  1. Click on </>

postman-code

  1. Select one of the options for JavaScript. I used 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));

  1. Run this on https://jsfiddle.net/ and you will get the transaction id in response:

fiddle

bd0306a1fa6e262949c9856ebcea61c0857067e1d2c856d0216a9cf871e4a377

4
  • Thank you. This was helpful. Except I had to tweak the top line to this: ``` var myHeaders = new fetch.Headers() ``` Also, I got an error back from their server saying: ``` sendrawtransaction RPC error: {"code":-25,"message":"bad-txns-inputs-missingorspent"} ``` But I am sure there is an input already specified in my raw tx hex. I'll share the hex below.
    – jiamijiang
    Sep 5, 2021 at 2:05
  • 0200000001661f29d4f3a3ccedf3865b51ade356715f8b201c6c1299c0ab64ba502d4ae274000000006a4730440220516e15b6354f17e3f02711107624005e6ccd26cf61ef21b35e91315dfbd33e9f022049dadb73ca3e814de9da0b35ff20a8ca82147dda138ede948ccac5621a5bc1bb01210384485d43e938f8c68c4ac903d4d5a97fe842f66c12b860d8d2e1242d2b01fc1fffffffff0160ea00000000000016001490638c5df76b1a37dc852e21ed5be5b66dbe199100000000
    – jiamijiang
    Sep 5, 2021 at 2:06
  • There is something wrong with the transaction. Maybe some inputs already spent.
    – user103136
    Sep 5, 2021 at 9:30
  • actually the reason was because i was signing it incorrectly. But its all fixed now. Thanks again.
    – jiamijiang
    Sep 13, 2021 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.