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:
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:
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: