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.