1

I have deployed a contract in a private blockchain using remix and ganache. Now I want to interact with it trough web3 and the console from my browser, but I'm not managing to interact with the contract functions in the browser. This in my index.html:

index.html

var simpleStorageAbi =
    [
        {
            "constant": false,
            "inputs": [],
            "name": "getValue",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "name": "val",
                    "type": "uint256"
                }
            ],
            "name": "setValue",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "constructor"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "value",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        }
    ];

var SimpleStorageContract = new web3.eth.Contract(simpleStorageAbi);
var simpleStorageInstance = new web3.eth.Contract(simpleStorageAbi, "(address)");

When I open the console from the browser the contract I type:

simpleStorageInstance

and I would expect the contract functions:

{…}
> getValue
> setValue
> value

instead I get this

Contract {_requestManager: RequestManager, givenProvider: null, providers: {…}, setProvider: ƒ, …}
BatchRequest: ƒ ()
clearSubscriptions: ƒ (keepIsSyncing)
currentProvider: (...)
defaultAccount: (...)
defaultBlock: (...)
defaultChain: (...)
defaultCommon: (...)
defaultHardfork: (...)
events: {allEvents: ƒ}
extend: ƒ ex(extension)
givenProvider: null
handleRevert: (...)
methods: {getValue: ƒ, 0x20965255: ƒ, getValue(): ƒ, setValue: ƒ, 0x} etc

I tried accessing the functions in this way

simpleStorageInstance.methods.setValue(5);

but I get nothing in return (no transaction returned).

{arguments: Array(1), call: ƒ, send: ƒ, encodeABI: ƒ, estimateGas: ƒ, …}

What am I doing wrong?

2
  • 1
    I think there is a possibility that you don't have the correct web3 provider. Can you please include your code where you got connected to ganache?
    – Masoud jt
    May 12, 2020 at 16:51
  • The connection to ganache was fine, it was the call function that was incomplete!
    – vfurci200
    May 12, 2020 at 18:22

1 Answer 1

1

Made it work, a piece of code was missing after the methods call.

Here the complete call function from the browser console:

simpleStorageInstance.methods.setValue(5).send({from: '(address)', gas: 3000000});

This sends the transaction that can be seen in ganache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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