我用Remix和Ganache签了小samart合同。我把它部署到Rinkeby测试网,遇到了问题,要将我的dapp (小型网站)连接到它。我的web3要求。
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进行部署。
发布于 2021-05-20 07:48:11
看起来,您正在使用以下方法实例化契约:
var contract = web3.eth.contract(contractAbi).at(contractAddress);文档具有以下语法(适合您的情况):
var contract = new web3.eth.Contract(contractAbi, contractAddress);如果这有帮助,请告诉我们!
https://ethereum.stackexchange.com/questions/98954
复制相似问题