0

This works just fine:

myContractObject.doSomething(i, function(err, result) {
    if (err) {
      console.log(err)
    } else {
     doSomethingElse(result)
    }
})

However, this doesn't:

  const params = myContractObject.doSomething.getData(i)
  const transactionObject = {from: account, data: params}
  myContractObject.doSomething(transactionObject, function(err, result) {
    if (err) {
      console.log(err)
    } else {
     doSomethingElse(result)
    }
  })

Here's what params looks like: 0xd1656c170000000000000000000000000000000000000000000000000000000000000000.

And here's what transactionObject looks like: Object { from: "0x518f5defef41e5b9baed8c9fabc392dd5…", data: "0xd1656c170000000000000000000000000…" }

Here's the relevant section of the ABI of the contract:

{
  "constant": true,
  "inputs": [
    {
      "name": "_index",
      "type": "uint256"
    }
  ],
  "name": "doSomething",
  "outputs": [
    {
      "name": "",
      "type": "string"
    }
  ],
  "payable": false,
  "type": "function"
}

Am I misunderstanding the transactionObject? The data: section of sendTransaction is for specifying parameters to the contract method call, right?

I'm trying to set up a transaction handler function for a dApp I'm developing. That function needs to take a variable number of parameters; hence, my attempt at setting up the parameters in a transactionObject. Help!

0

I still don't know what the data key of a transactionObject is for, but it seems it isn't for sending parameters to method calls. So I've ended up solving my issue this way:

    _funcCallChecker(_func, _cb ) {
        if ( (typeof _func === 'function') || (typeof _cb === 'function') ) {
          return true
        } else {
          return false
        }   
    }

  _funcCallParamsChecker(_func, _params, _cb ) {
    if ( (typeof _func === 'function') || (typeof _cb === 'function') || (Array.isArray(_params)) ) {
      return true
    } else {
      return false
    }   
  }

  _call (_caller, _func, _cb ) {
    if ( this._funcCallChecker(_func, _cb) ) {
      _func( function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }   
}

  _call1Params (_caller, _func, _params, _cb ) {
    if ( this._funcCallParamsChecker(_func, _params, _cb ) ) {
      _func(_params[0], function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }  
 }

  _call2Params (_caller, _func, _params, _cb ) {
    if ( this._funcCallParamsChecker(_func, _params, _cb ) ) {
      _func(_params[0], _params[1], function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }  
 }

  _call3Params (_caller, _func, _params, _cb ) {
    if ( this._funcCallParamsChecker(_func, _params, _cb ) ) {
      _func(_params[0], _params[1], _params[2], _params[3], function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }  
 }

  _call4Params (_caller, _func, _params, _cb ) {
    if ( this._funcCallParamsChecker(_func, _params, _cb ) ) {
      _func(_params[0], _params[1], _params[2], _params[3], function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }   
  }

  _call5Params (_caller, _func, _params, _cb ) {
    if ( this._funcCallParamsChecker(_func, _params, _cb ) ) {
      _func(_params[0], _params[1], _params[2], _params[3], _params[4], function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }   
 }

  _call6Params (_caller, _func, _params, _cb ) {
    if ( this._funcCallParamsChecker(_func, _params, _cb ) ) {
      _func(_params[0], _params[1], _params[2], _params[3], _params[4], _params[5], function(err, result) {
        if (err) {
          console.log(err)
        } else {
          _cb(_caller, result)
        }
      })
    }  
 }


  callHandler (_caller, _func, _cb ) {
    this._call(_caller, _func, _cb)   
 }

  callParamHandler (_caller, _func, _params, _cb ) {
    switch(_params.length) {
      case 1:
          this._call1Params(_caller, _func, _params, _cb)
          break;
      case 2:
          this._call2Params(_caller, _func, _params, _cb)
          break;
      case 3:
          this._call3Params(_caller, _func, _params, _cb)
          break;
      case 4:
          this._call4Params(_caller, _func, _params, _cb)
          break;
      case 5:
          this._call5Params(_caller, _func, _params, _cb)
          break;
      case 6:
          this._call6Params(_caller, _func, _params, _cb)
          break;
      default:
          this._call(_caller, _func, _cb )
    }   
}

....where _caller is the callee, _func is the ethereum contract method call, _params is an array of parameteres to that method call, and _cb is the callee's callback function (where I can set react's state with results etc.). Anyway, I can call this stuff like this:

let params = [this.props.ipfsHash, this.objectData.category, this.objectData.format]
this.props.web3.callParamHandler(this, this.premisObject.setObject, params, this._setObject)

...and so on. I've got it down as 'a bit of a hack'. Unfortunately, I don't know javascript well enough to do something more elegant - perhaps the spread operator could help, somehow? Anyway, that works for now....

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.