0

Normally, for a contract call (i.e. something that does not update ethereum), I would simply do: var something = myContractObject.getSomething().

I've just started using metamask, and unfortunately, that breaks with the complaint that such calls are not asynchronous. Why? They've always returned synchronously for me! Is that because I've only used testrpc or a private network?

Anyway, if I must make such calls async', since I make many such calls, rather than write callbacks for every one of them, I'm trying to write a function to handle it all for me. Something like this:

callHandler (_transactionObject) {
  let value = undefined
  this.web3.eth.call( _transactionObject, function(err, result) {
    if (err) {
      console.log(err)
      return undefined
    } else {
      return result
    }
  })
}

That relies on the parameter _transactionObject. I've written a little function that tries forming that:

getTransactionObject ( _data ) {
  const account = this.account
  return { from: account, data: _data }
}

And here's how I call that function:

const data = myContractObject.getSomething.getData()
var transactionObject = getTransactionObject(data)

Then I simply try and replicate var something = myContractObject.getSomething(), by calling callHandler, i.e. var something = callHandler(transactionObject).

...but that's not working; it seems that myContractObject.getSomething.getData() (which I got from here: https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-methods), is not returning the right call data for the transactionObject. In which case, I'm a bit stuck - how do I get the call data I need and form the correct transactionObject?

ps. I realise there are other problems with that code, not least var something = callHandler(transactionObject) is not async', so the assignment to something needs wrapping in some async' handler....

1

Rather than using const data = myContractObject.getSomething.getData() to create a transactionObject, I solved this by just passing in myContractObject.getSomething to callHandler, along with a callback function. Something like this:

callHandler( myContractObject.getSomething, callbackFunction )

And callHandler looks something like this:

callHandler (_func, _cb ) {
_func( function(err, result) {
  if (err) {
    console.log(err)
  } else {
    console.log("got result " + result)
    _cb(result)
  }
})

}

...works nicely :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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