7

In my contract there is a buy method with 1 argument being overloaded by a 2nd one with 2 arguments:

function buy( uint id )
function buy( uint id, uint quantity )

Now I want to call the 1st method via web3, but also provide the options for the underlying sendTransaction, to specify the from, value and gas values.

How can I call this and prevent the 2nd buy method with 2 arguments to be called as happens currently with:

myContract.buy(id, { gas: 100000 })
  • Hey Joris -- please remember to select an answer as correct or, if you solved it yourself differently, add an answering explaining what/how you did it and then select it as correct. This will make it easy for users arriving here from search engines to find the solution to their problem. Thanks! – tayvano Apr 20 '16 at 4:12
7

Fabian Vogelsteller indicated on Gitter that we also have the signature as a method name available. Which is documented in EIP68, and looks like this:

myContract.transact({from: …}).myMethod(param1)
// or
myContract.call({from:...})['5fd434dd'](param1)

So specifically for my question, the solution was using:

myContract.buy['uint256'].buy(id, opts)
  • Note that the above is in the making and not yet possible in the current web3.js – Fabian Vogelsteller Apr 21 '16 at 21:42
  • @FabianVogelsteller has this been added to web3? – Aakil Fernandes Nov 5 '16 at 18:54
4

Maybe this is too obvious, and I am not trying to be flippant, but why not change the interface? buyQuantity for the second function.

  • Yes, that is what I eventually did – Joris Bontje Apr 19 '16 at 17:57
0

In web3 v1 it is documented here

myContract.methods.myMethod([param1[, param2[, ...]]])

Creates a transaction object for that method, which then can be called, send, estimated.

The methods of this smart contract are available through:

  • The name: myContract.methods.myMethod(123)
  • The name with parameters: myContract.methods['myMethod(uint256)'](123)
  • The signature: myContract.methods['0x58cf5f10'](123)

This allows calling functions with same name but different parameters from the JavaScript contract object.

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.