You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js+Express处理智能合约交易无响应问题求助

Troubleshooting Your Rinkeby Transaction Response Issue

Hey there, let's break down why you're successfully sending transactions to the Rinkeby testnet but not getting a response back. Looking through your code, there are a couple of critical fixes that will resolve this:

1. Missing Callback Invocation on Successful Transaction

The core issue is in your sendTxn function: when the signed transaction is sent successfully and you get a transaction hash, you never trigger the callback to pass that result back to the thu function. This means your route handler never hits the success branch where res.send is supposed to run.

Fix this by adding the callback call in the success case:

function sendTxn(rawTx, callback) {
  var privateKeyBuffer = new Buffer(privateKey, 'hex');
  var transaction = new tx(rawTx);
  transaction.sign(privateKeyBuffer);
  var serializedTx = transaction.serialize().toString('hex');
  
  web3.eth.sendSignedTransaction( '0x' + serializedTx, function(err, txnHash) {
    if(err) {
      console.log("txn err", err);
      callback(err, null);
    } else {
      console.log("txn result", txnHash);
      // Add this line to pass the transaction hash back to the caller
      callback(null, txnHash);
    }
  }).catch((err) => {
    callback(err, null);
  });
}

2. Incorrect res.send Usage

Your route handler uses res.send("ket qua: ", output) which doesn't behave as intended. The res.send method only accepts one primary body parameter (or a status code followed by the body). Combine your message and output into a single string instead:

app.get('/thu', (req, res) => {
  thu(function(err, output){
    if(err){
      res.json({"err": ""+err, "output": output});
      return;
    }
    // Fix the res.send call to pass a single string
    res.send(`ket qua: ${output}`);
    // Alternative: res.send("ket qua: " + output);
  });
});

Optional: Wait for Transaction Receipt (For Execution Details)

If you need more than just a transaction hash—like confirmation that the contract method executed successfully, or emitted events—you can modify the sendSignedTransaction call to listen for the mined receipt:

function sendTxn(rawTx, callback) {
  var privateKeyBuffer = new Buffer(privateKey, 'hex');
  var transaction = new tx(rawTx);
  transaction.sign(privateKeyBuffer);
  var serializedTx = transaction.serialize().toString('hex');
  
  web3.eth.sendSignedTransaction('0x' + serializedTx)
    .on('transactionHash', (txnHash) => {
      console.log("txn broadcasted with hash:", txnHash);
    })
    .on('receipt', (receipt) => {
      console.log("txn mined, receipt:", receipt);
      callback(null, receipt);
    })
    .on('error', (err) => {
      console.log("txn err", err);
      callback(err, null);
    });
}

This will give you details like gas consumed, contract events, and transaction status.

Start with fixing the missing callback first—that's the immediate reason you're not getting responses. Let me know if you hit any other snags!

内容的提问来源于stack exchange,提问作者Pittie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:14:01