TL;DR I'm very confused by the Contract API in web3: https://web3js.readthedocs.io/en/v1.2.6/web3-eth-contract.html

myContract.methods not available in geth console either with Geth 1.9.11-stable and 1.9.12-unstable on Ubuntu 16.0.4 (fast sync'd)

Is that a version issue? Do I have to come back to a previous version to have this feature?

IN MORE DETAIL I'm currently trying to use this API from my geth console to retrieve the encoded transaction string, to be signed and submitted

When I use on Remix (remix.ethereum.org), after I've compiled the SimpleStorage contract and deployed to Ropsten, I can access the methods as below

On Remix console:
> abi = [{"constant": false,"inputs": [{"name": "x","type": "uint256"}],"name": "set","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "get","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"}]

> mySimpleStorage = new web3.eth.Contract(abi)

> mySimpleStorage.methods
  "set": "function () { [native code] }",
  "0x60fe47b1": "function () { [native code] }",
  "set(uint256)": "function () { [native code] }",
  "get": "function () { [native code] }",
  "0x6d4ce63c": "function () { [native code] }",
  "get()": "function () { [native code] }"

> tx = mySimpleStorage.methods.set("x")

> tx.encodeABI()

That is what I expect based on documentation

With Geth console:

The web3.eth.Contract (uppercase) don't even exist:

> mySimpleStorage = new web3.eth.Contract(abi)
TypeError: Value is not an object: undefined
        at <eval>:1:19(5)

I've to use web3.eth.contract (lowercase):

> mySimpleStorage = new web3.eth.contract(abi)
  abi: [{
      constant: false,
      inputs: [{...}],
      name: "set",
      outputs: [],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  eth: {},
  at: function(address, callback),
  getData: function(),
  new: function()

Anyway the methods property is missing and it is impossible to call encodeABI()

> tx = mySimpleStorage.methods
> tx

2 Answers 2

const contract = new web3.eth.Contract(abi, address);

I think you forgot to the 2nd parameters, which is the smart contract address.


I think I understood the problem. Geth console supports web3 0.2x.x and not 1.x. So there is no encodeABI(), you have to use getData() instead and the whole syntax is different. The documentation of this older interface is here: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract

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.