ethereum - Uncaught Error: Invalid Address web3.currentProvider MetaMask
问题描述
I have deployed my ERC721 contract to Rinkeby TestNet. The contract has been deployed successfully. I unable to invoke transactions with MetaMask. Spent the whole day looking to resolve this issue. Found some answers stating it the issue with localhosted files or the web3.js doesn't work with MetaMask.
<script>
if (typeof web3 != 'undefined') {
web3 = new Web3(web3.currentProvider) // what Metamask injected
console.log("existing web3: provider " + typeof web3);
} else {
// Instantiate and set Ganache as your provider
web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/api-key"));
console.log("new provider " + web3);
web3.eth.defaultAccount = web3.eth.accounts[0]
}
// The interface definition for your smart contract (the ABI)
var StarNotary = web3.eth.contract(
[contract-abi]
)
const starNotary = StarNotary.at('0x7cfAD6E80D992599d989166aABf536b21215544C')
function claimStar() {
web3.eth.getAccounts(function(error, accounts) {
if (error) {
hotsnackbar(false, error);
return
}
Uncaught Error: invalid address at u (web3.min.js:1) at inputTransactionFormatter (web3.min.js:1) at web3.min.js:1 at Array.map () at i.formatInput (web3.min.js:1) at i.toPayload (web3.min.js:1) at _.e [as sendTransaction] (web3.min.js:1) at c.sendTransaction (web3.min.js:1) at index.html:589 at web3.min.js:1
解决方案
Here is a complete demo which includes the introductory steps like authorizing the MetaMask contract and more.
https://fulldecent.github.io/spend-ERC20-create-ERC721/
Here is the particular code I think you will be interested in:
https://github.com/fulldecent/spend-ERC20-create-ERC721/blob/master/docs/index.html#L102-L114
if (window.ethereum) {
window.web3 = new Web3(ethereum);
$('#need-metamask').hide();
} else {
console.log('Non-Ethereum browser detected. Install MetaMask.');
return;
}
window.web3.version.getNetwork((err, netId) => {
if (netId == "3") {
$('#need-ropsten').hide();
}
});
https://github.com/fulldecent/spend-ERC20-create-ERC721/blob/master/docs/index.html#L121-L127
try {
await ethereum.enable();
$('#need-enable').hide();
} catch (error) {
console.log("ERROR: Enable account access and reload.");
return;
}
推荐阅读
- javascript - 使用带有 django 模板的数据集时出现 Javascript 未定义错误
- json - 使用 Python 将 OBDResponse 对象序列化为 JSON
- javascript - 为什么导航栏被第一个 p 元素向下推?nav 在一个单独的 vue 组件中
- amazon-s3 - 使用 Moto 将 s3 存储桶模拟为 IAM 用户
- tailwind-css - Tailwind 中切换 UI 状态的正确方法是什么?
- facebook - 使用 Facebook Live Comment API 时不显示姓名
- php - php 升级触发 sha256 的新警告
- numpy - 计算没有库的每个图像的直方图
- reactjs - 无法从 Firestore 中查询某些数据(反应)
- sqlite - TypeOrm - 无法在主函数之外创建连接