2

The following code in truffle (v4.0-beta) is failing:

.then(() => web3.eth.sendTransaction(
        {from: accounts[0], to: swt.address, value: 1e10}))

Error: Synchronous requests are not supported.

Is there a way to send ETH using asynchronous transaction?

2 Answers 2

3

This is not a truffle issue. This is related to web3. web3 doesn't support synchronous request. You can use sendTransaction function implementing callback.

web3.eth.sendTransaction({from: accounts[0], to: swt.address, value: 1e10},function(err,txHash){
    if(err){
        console.log("Could not send transaction");
    }
    else{
        console.log("Transaction send successfully. Transaction Hash:",txHash );
    }
})
1

Truffle 4 in it's test framework requires that all functions are Promise. web3.eth functions are synchronous. So, to use them in the Truffle framework we need to wrap them:

// wraps web3 sync/callback function into a Promise
function web3AsynWrapper (web3Fun) {
  return function (arg) {
    return new Promise((resolve, reject) => {
      web3Fun(arg, (e, data) => e ? reject(e) : resolve(data))
    })
  }
}

exports.sendTransaction = web3AsynWrapper(web3.eth.sendTransaction)
exports.getBalance = web3AsynWrapper(web3.eth.getBalance)

The same issue exists when you are connecting using the IPC provider: Synchronous requests are not supported by the IPC provider

2
  • do you know if there is a web3 lib already fully wrapped like this? Wondering why this is not per default already the case.
    – n1cK
    Commented Oct 6, 2017 at 12:13
  • Yes, web3 v1.0. It is out but still in beta. It comes with other goodies as well: Wallet management, transaction signature, gas analysis... Commented Oct 9, 2017 at 8:37

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.