首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接我的DAPP与Rinkeby智能合同的问题

连接我的DAPP与Rinkeby智能合同的问题
EN

Ethereum用户
提问于 2021-05-14 20:00:17
回答 1查看 545关注 0票数 0

我用Remix和Ganache签了小samart合同。我把它部署到Rinkeby测试网,遇到了问题,要将我的dapp (小型网站)连接到它。我的web3要求。

代码语言:javascript
复制
    window.addEventListener('load', async () => {
 // Wait for loading completion to avoid race conditions with web3 injection timing.
  if (window.ethereum) {
    const web3 = new Web3(window.ethereum);
    try {
      // Request account access if needed
      await window.ethereum.enable();
      // Acccounts now exposed
      return web3;
    } catch (error) {
      console.error(error);
    }
  }
  // Legacy dapp browsers...
  else if (window.web3) {
    // Use Mist/MetaMask's provider.
    const web3 = window.web3;
    console.log('Injected web3 detected.');
    return web3;
  }
  // Fallback to localhost; use dev console port by default...
  else {
    const provider = new Web3.providers.HttpProvider('http://127.0.0.1:9545');
    const web3 = new Web3(provider);
    console.log('No web3 instance injected, using Local web3.');
    return web3;
  }
});

// Initialize Web3

     const Web3 = require("web3")

      const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));

    //const web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/0xf2966f689fa2163d9b183933D1fA1Ac044B20915'));

//  const web3 = new Web3(new Web3.providers.currentProvider)

// const web3 = window.web3;

    web3.eth.defaultAccount = web3.eth.accounts[0]

console.log(web3.eth.defaultAccount)

      // Set Contract Abi
      var contractAbi = [
    {
        "constant": false,
        "inputs": [
            {
                "internalType": "string",
                "name": "_Name",
                "type": "string"
            }
        ],
        "name": "registerName",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "constant": true,
        "inputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "name": "addressesArray",
        "outputs": [
            {
                "internalType": "address payable",
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "internalType": "address",
                "name": "_Address",
                "type": "address"
            }
        ],
        "name": "checkAddress",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "internalType": "string",
                "name": "_Name",
                "type": "string"
            }
        ],
        "name": "checkName",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "internalType": "address",
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "myAddressInfo",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "name": "namesArray",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "namesArrayLenght",
        "outputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "ownerAddress",
        "outputs": [
            {
                "internalType": "address payable",
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "registered",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]; // Add Your Contract ABI here!!!

      // Set Contract Address
      var contractAddress = '0xf2966f689fa2163d9b183933D1fA1Ac044B20915'; // Add Your Contract address here!!!

      // Set the Contract
      var contract = web3.eth.contract(contractAbi).at(contractAddress);

我的Rinkeby合同部署地址- 0xf2966f689fa2163d9b183933D1fA1Ac044B20915。使用Metamask进行部署。

EN

回答 1

Ethereum用户

发布于 2021-05-20 07:48:11

看起来,您正在使用以下方法实例化契约:

代码语言:javascript
复制
var contract = web3.eth.contract(contractAbi).at(contractAddress);

文档具有以下语法(适合您的情况):

代码语言:javascript
复制
var contract = new web3.eth.Contract(contractAbi, contractAddress);

如果这有帮助,请告诉我们!

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/98954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档