3

This is the error from metamask directly: I keep getting ALERT: Trying to call a function on a non-contract address error with metamask

This is the error I get once after I submit the information to IPFS: enter image description here

My Smart Contract:

pragma solidity 0.5.3;

//Imports for safe math operations
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";

//Defining contract name lika class name
contract IPFSPosting{
  using SafeMath for uint256;

  //This struct is for the properties of a post
  struct Post{
    //Address of owner's Post
    address owner;
    //Hash of an image
    string imgHash;
    //Hash of a image caption
    string textHash;
  }

  //A Mapping list post from Post struct.
  mapping(uint256 => Post) post;

  //A counter for the posts mapping list.
  uint256 postCtr;
  event NewPost();
  //img and text hashes are sent to IPFS and stored
  function sendHash(string memory _img, string memory _text)
    public {
      postCtr = postCtr.add(1);
      Post storage posting = post[postCtr];
      posting.owner = msg.sender;
      posting.imgHash = _img;
      posting.textHash = _text;

      emit NewPost();
    }
  // Function that gets the img and text hashes
  // _index number from total post iteration
  // returns stored images and text hashes
  function getHash(uint256 _index)
  public view returns (string memory img, string memory text, address owner)
  {
    owner = post[_index].owner;
    img = post[_index].imgHash;
    text = post[_index].textHash;
  }
  //Gets the total length of total posts
  // Returns the total count of post
  function getCounter() public view returns(uint256){ return postCtr;}
}


Contract Instantiation

import web3 from './web3';
const address = "0xb8ce805b2f346ead5fa5ea1d9756b10ff46642b8";
const abi =[
    {
        "constant": false,
        "inputs": [
            {
                "name": "_img",
                "type": "string"
            },
            {
                "name": "_text",
                "type": "string"
            }
        ],
        "name": "sendHash",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "_index",
                "type": "uint256"
            }
        ],
        "name": "getHash",
        "outputs": [
            {
                "name": "img",
                "type": "string"
            },
            {
                "name": "text",
                "type": "string"
            },
            {
                "name": "owner",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getCounter",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "anonymous": false,
        "inputs": [],
        "name": "NewPost",
        "type": "event"
    }
]
export default new web3.eth.Contract(abi, address);

I am using this tutorial as a guide for this smart contract. Mostly everything is similar. Github Guide

If there's anything else you need I can provide more code, but this seems where most of the trouble is being caused. If anyone could help me to figure out what's wrong that'll be great!

1 Answer 1

5

There are a few different things that can be wrong here, and they all stem from whether your contract is really at that address in your code:

  • Have you actually deployed your contract yet? Your code there assumes that the listed contract is already deployed at that address, it doesn't do any deploying.
  • Are you sure Metamask is connected to the same network your contract is on? If the contract is on a testnet but your Metamask is trying to speak to mainnet, then you would get this error.

The error message isn't lying to you, it looks like you're trying to send a transaction to an address which isn't holding a contract (at least on the chain you're speaking to). When I search the address on Etherscan, nothing turns up: https://etherscan.io/address/0xb8ce805b2f346ead5fa5ea1d9756b10ff46642b8

If you're just starting to learn here, you can think of it like a smart contract file being a class, and you deploy it to a network in order to create an instance of that class. It looks like you may have forgotten to create your instance! Googling "deploying smart contracts" to get pointed in the right direction.

EDIT: Also make sure you aren't mixing up your sending address with the Contract's deployed address. When you deploy a contract, your address sends some contract code which then gets placed to a new address. That contractAddress is included in the receipt from your deployment transaction. When you then load your contract via web.eth.Contract(ABI, address), address needs to be the contract's own deployed address, not the one you sent the deployment transaction from.

4
  • I have deployed it on the ropsten testnet. You can clearly see that in first photo on the metamask header, my friend. The link you sent was for the mainnet, I haven't deployed it on the mainnet. See here: ropsten.etherscan.io/address/… With this added information, could you factor in another reason as to why this is occurring?
    – Ty Cooper
    Jul 23, 2019 at 20:17
  • I would keep trying to verify that the contract actually lives at the address you think it does. The one you sent me shows that it sent out some contract creation txns, and when I click on them, it takes me to this other address which is clearly shown to be holding a contract: ropsten.etherscan.io/address/… It seems like you've confused your personal sending address with the one the contract got deployed to, which would explain your error. Try plugging in address = "0xf4e8e52828533e8176c16b721a8082db260b9f5c".
    – ohsully
    Jul 23, 2019 at 20:51
  • @TyCooper -- notice that in the Ropsten link I included, that address is known to be holding a Contract, whereas in the one you sent, it's marked as just an Address.
    – ohsully
    Jul 23, 2019 at 20:54
  • 1
    So that address you sent work! My code is all fine and everything works! Thank you for your help!
    – Ty Cooper
    Jul 24, 2019 at 5:54

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.