0

I've got a Smart Contract that works perfectly when deployed to localhost: 8545 (using Ganache) with my Web3 Dapp. But when I put it on Rinkeby, I'm running into problems,.

The Contract is super-simple: all it does is let you Set and Get the value uint variable x:

pragma solidity ^0.4.16; 


contract Incrementor { 

    uint x; 


    constructor() public { 
       // Give x in initial value of 10:
       x = 10; 
    } 


    function getX() external view returns (uint xValue) { 
        return (x); 
    } 


    function incrementX(uint byThisMuch) public { 
        x += byThisMuch; 
    } 


}

After running it successfully on localhost: 8545 I've now deployed it to the Rinkeby TestNet (via Remix) and am using Infura to connect to it - but I'm suddenly experiencing inconsistent behavior there. Specifically, my getX() function works, but my setter incrementX() function gives me errors:

accounts error

Its basically not picking my address.

NOTE: this problem has been happening even BEFORE the Nov. 6 2018 Metamask update

Here's how I'm setting up Web3 in my HTML:

if(typeof(web3) !== 'undefined') {
            console.log("Web3 provider found! Current Provider is WORKING!");
            web3 = new Web3(web3.currentProvider);
        }
        else {
            console.log("1. NO Web3 provider found!  Gonna set one up...");

            // Connect to Infura:
            web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/<MY API KEY"));

            // Or use this when connecting to `localhost`:
            // web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));


            console.log("2. OK, NOW we DO have a PROVIDER --> HttpProvider = ", web3.providers.HttpProvider);

            web3.eth.defaultAccount = web3.eth.accounts;
            console.log("3. ALL ACCOUNT - web3.eth.accounts = ", web3.eth.accounts);
            console.log("4. web3.eth.defaultAccount = ", web3.eth.defaultAccount);

            // Also tried with 'getAccounts' (same as `.accounts` - but asynchronous)
            console.log("5. web3.eth.getAccounts() ==> ", web3.eth.getAccounts());

            web3.eth.getAccounts(function(error, result) { 
                if(error != null)
                    console.log("7. Error! ", error);
                else
                    console.log("7. Result = ", result);
            });

            console.log("Exiting the WEB3 JAM!");

        };

Again, refer to console log above to see what's happening.

What's strange is that I am definitely connecting and talking to the Smart Contract on Rinkeby - and I know this because my getX() button-click action is returning the right values:

getter works

But when I try to set it - that's when I get that error.

Really, its more about the fact that my address keeps coming back as undefined - which I think is the real issue here:

And here's a quick screen grab of what the App's interface looks like:

DAPP Interface

1 Answer 1

0

Infura do not save your keys. When you were connected to your local node this worked as your local node have your accounts. You need to set your account manually by loading your keys. The reason why getX works is because the function is view and can be executed in Infura nodes without sending a transaction. incrementX change the contract state and therefore you need to submit a transaction.

I recommend to use a key manager like Metamask, on the contrary, you will need to expose your keys to sign the transactions.

8
  • OK - but I AM using Metamask. I'm currently logged into the Rinkeby Testnet through my Metamask (using Chrome.) Isn't that what I should be doing?
    – Mark55
    Nov 7, 2018 at 19:19
  • Metamask is not injecting web3, that is why you have: ""1. NO Web3 provider found! Gonna set one up..." in the console. Check that metamask is unlocked.
    – Jaime
    Nov 7, 2018 at 20:19
  • just to make sure, when you say " Check that metamask is unlocked" - you mean that I'm signed into my account? Or is there some other "unlocking" that I have to do that I'm not aware of?
    – Mark55
    Nov 7, 2018 at 20:37
  • I'm starting to wonder if the check for MetaMask injection is an asynchronous type of event. Cause everytime I've tested this, I HAVE been logged into MetaMask - but its clearly not recognizing that fact, and it immediately proceeds to the else part of my logic and tries to go via infura...
    – Mark55
    Nov 7, 2018 at 20:58
  • just to sign into your account. Check the new proposed method to log in after nov 2 here
    – Jaime
    Nov 7, 2018 at 21:01

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.