[Ethereum] ‘ALERT: Trying to call a function on a non-contract address’ Why Do I Keeping This Error

ethereumjsjavascriptmetamasksolidityweb3js

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!

Best Answer

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.