0

I make small samart contract using Remix and Ganache. I deploy it to Rinkeby test net and got problem to connect my dapp (small web site) to it. My web3 require.

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

My Rinkeby contract deploy address - 0xf2966f689fa2163d9b183933D1fA1Ac044B20915. Using Metamask to deploy.

2
  • Hi! I don't see any code here for connecting to the contract - could you include the web3 code you're using to connect, and whatever error message you're getting?
    – Linum Labs
    Commented May 16, 2021 at 6:53
  • Hi! I add all web3 smart contract emplementation code with Metamask. It works with Ganache BC. I deploy smart contract in Rinkeby TestNet, but i dont know how to connect web3 to it. Can you help me, plz...
    – Ixar
    Commented May 16, 2021 at 19:19

1 Answer 1

0

It looks like you're instantiating the contract using:

var contract = web3.eth.contract(contractAbi).at(contractAddress);

The docs have the following syntax (adapted to your case):

var contract = new web3.eth.Contract(contractAbi, contractAddress);

Please let us know if that helps!

1
  • This is not sims i need! I think problem in provider. I must specify Rinkeby node, not a local node on my computer
    – Ixar
    Commented May 21, 2021 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.