0

ive been trying for days to create a contract and simply call a function on it. Just to learn. But i can for the love of god not figure out what im doing wrong. I got truffle and Ganache set up, and i can connect to each. But i can never fire of a contract.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <title>title</title>
  </head>
  <body>
      <script>
    window.addEventListener('load', async () => {
        // Modern dapp browsers...
        if (window.ethereum) {
          window.web3 = new Web3(ethereum);
          try {
            // Request account access if needed
            await ethereum.enable();
            // Acccounts now exposed
            web3.eth.sendTransaction({/* ... */});
          } catch (error) {
            // User denied account access...
          }
          console.log("matamask wie");
        }
        // Legacy dapp browsers...
        else if (window.web3) {
          window.web3 = new Web3(web3.currentProvider);
          // Acccounts always exposed
          web3.eth.sendTransaction({/* ... */});
        }
        // Non-dapp browsers...
        else {
          console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
        }
        startapp();
      });
      </script>
      <script>
     function startapp() {
var address = "0xA2B5f586B69f8d82a18CC5a453174E9F90eD6520";
var 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"
    }
  ];
var SimpleStorage = new web3.eth.contract(abi, address);

SimpleStorage.methods.get().call()
.then(console.log);
     };
      </script>
  </body>
</html>

and my simple solidity code

    pragma solidity 0.5.8;
contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

Please help me fire of a smart contract function. Im very frustrated right now!!!!

  • What response are you getting? Have you deployed the contract on ganache? – Samuel Dare Oct 2 '19 at 8:10
  • Yes, the contract is deployed on ganache. The response im getting is Uncaught (in promise) TypeError: Cannot read property 'get' of undefined – Cryptojanne Oct 2 '19 at 8:11
  • You are not loading your copy of web3. Metamask injects its own copy but you shouldn't rely on it since it is v0.20 making your call fail. – Ismael Oct 2 '19 at 13:56
  • I figured that was the problem. I'm now trying to get 1.x web3 since they have the web3.min.js. But i still want users to be able to use the metamask plugin, how would the code look for that? im currently googling around like a madman! – Cryptojanne Oct 2 '19 at 14:03
  • @Cryptojanne I think you are looking for these ethereum.stackexchange.com/questions/55745/… – Ismael Oct 2 '19 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.