簡體   English   中英

沒有數據“網絡”:{},在智能合約中。json

[英]There is no data "networks": {}, in Smart contract .json

我正在嘗試從nuxt.js應用程序連接元掩碼

我所做的是這樣的

1. yarn truffle init

contract/SingleNumRegister.sol

pragma solidity ^0.8.10;
contract SingleNumRegister{
    struct StoreNumber{
        address from;
        uint256 number;
    }
    StoreNumber[] public storeNumbers;
    function set(uint256 num) public {
        storeNumbers.push(StoreNumber(msg.sender,num));

    }
    function get() public view returns(uint256){
        uint256 index = storeNumbers.length -1;
        return storeNumbers[index].number;
    }
}

3. yarn truffle build it /build/contracts/SingleNumRegister.json

4.make migrations/2_deploy_contract.js

const SingleNumRegister = artifacts.require('SingleNumRegister')
module.exports = function(deployer){
    deployer.deploy(SingleNumRegister)
}

5.為我當地的Ganache更改 truffle truffle-cinfig.js

networks: {
 development: {
  host: "127.0.0.1",     // Localhost (default: none)
  port:7545,            // Standard Ethereum port (default: none)
  network_id: "*",       // Any network (default: none)
 },

6. yarn truffle deploy -> 部署成功。

然后,我嘗試使用此腳本plugins/web3.js進行訪問。

import Web3 from "web3"
import artifacts from "~~/build/contracts/SingleNumRegister.json"

export default async function(context,inject){

    let web3;
    
    if (typeof window != 'undefined' && typeof window.ethereum != 'undefined'){
        web3 = new Web3(window.ethereum)
        window.ethereum.enable().catch((error) =>{
            console.log(error)
        })
    }else if (
        typeof window !== 'undefined' &&
        typeof window.web3 !== 'undefined'
    ){
        web3 = new Web3(window.web3.currentProvider)
    }else {
        const httpEndpoint = "http://127.0.0.1:7545"
        web3 = new Web3(new Web3.providers.HttpProvider(httpEndpoint))
        inject('web3',web3)
    }
   
    let networkId = await web3.eth.net.getId()
    console.log("networkId:"+ networkId);
    let contract = new web3.eth.Contract(
        artifacts.abi,
        artifacts.networks[networkId].address
    )
    inject('web3',web3)
    inject('contract',contract)
}

它顯示錯誤client.js:227 TypeError: Cannot read properties of undefined (reading 'address')

SingleNumRegister.json中,沒有網絡數據。

這個文件是在流程2中制作的,我沒有為此做任何事情。

  "networks": {},
  "schemaVersion": "3.4.4",
  "updatedAt": "2022-01-04T08:12:18.731Z",
  "devdoc": {
    "kind": "dev",
    "methods": {},
    "version": 1
  },

這是對的嗎? 或者我應該在哪里修復???

truffle compile run

truffle migrate

瞧! 你應該有這樣的東西,

 "networks": {
    "1641312206935": {
      "events": {},
      "links": {},
      "address": "0xc447AFC10846f39540EF1e2CC6F35f91903d8f94",
      "transactionHash": "0x06803935f6bb461092b748953ab556cef2d9ceb610761c67193eb1cf6f07f970"
    }
  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM