Solidity

‘ALERT:試圖在非合約地址上呼叫函式’ 為什麼我會保持這個錯誤?

  • July 23, 2019

這是直接來自元遮罩的錯誤: 我不斷收到 ALERT: Trying call a function on a non-contract address error with metamask

這是我在向 IPFS 送出資訊後得到的一次錯誤: 在此處輸入圖像描述

我的智能合約:

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;}
}

合約實例化

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);

我將本教程用作此智能合約的指南。大多數情況下,一切都是相似的。Github 指南

如果您還有其他需要,我可以提供更多程式碼,但這似乎是造成大部分麻煩的地方。如果有人可以幫助我找出問題所在,那就太好了!

這裡有幾個不同的地方可能是錯誤的,它們都源於你的合約是否真的在你的程式碼中的那個地址:

  • 你真的部署了你的合約了嗎?您的程式碼假定列出的合約已經部署在該地址,它不進行任何部署。
  • 你確定 Metamask 連接到你的合約所在的同一個網路嗎?如果合約在測試網上,但您的 Metamask 正在嘗試與主網對話,那麼您會收到此錯誤。

錯誤消息並沒有對您撒謊,看起來您正在嘗試將交易發送到沒有合約的地址(至少在您正在與之交談的鏈上)。當我在 Etherscan 上搜尋地址時,什麼也沒有出現:https ://etherscan.io/address/0xb8ce805b2f346ead5fa5ea1d9756b10ff46642b8

如果你剛開始在這裡學習,你可以把它想像成一個智能合約文件是一個類,然後你將它部署到網路上以創建該類的一個實例。看起來您可能忘記創建實例了!Google搜尋“部署智能合約”以指明正確的方向。

編輯:還要確保您沒有將您的發送地址與契約的部署地址混淆。當您部署合約時,您的地址會發送一些合約程式碼,然後將其放置到新地址。這contractAddress包含在部署交易的收據中。然後,當您通過 載入契約時web.eth.Contract(ABI, address)address需要是契約自己的部署地址,而不是您發送部署交易的地址。

引用自:https://ethereum.stackexchange.com/questions/73225