简体   繁体   English

inpage.js:1 未捕获错误:地址无效

[英]inpage.js:1 Uncaught Error: invalid address

This is my contract这是我的合同

pragma solidity ^0.5.11;
contract test{
mapping(string => address payable) pizzaPlace;
address contractOwner;
address payable userWallet;
constructor() public{
    contractOwner=msg.sender;
    pizzaPlace["piaazOne"]=0xd806F6b4888ff997dB4A073fD9EdD40ab92BCbD4;
}
function paymentHandlerer() public payable returns(bool){
        userWallet = pizzaPlace["pizzaOne"];
        userWallet.transfer(msg.value);
        return true;
}
function showMessage() public view returns(string memory){
    return "Hello Solidity";
}

I have been learning solidity for a while but this is something I seem not to tackle.我已经学习了 Solidity 一段时间,但这似乎是我没有解决的问题。 The above contract was written in remix and I am using Ganache on my local machine to deploy contracts.上面的合约是用 remix 编写的,我在本地机器上使用 Ganache 来部署合约。

Following is my code for function call:以下是我的函数调用代码:

const contractAddress = '0xEe1C4287b37de1ADFE2aD78C7B7c96D7694093cb';
const contractAbi=[...];
var contract = web3.eth.contract(contractAbi).at(contractAddress);
contract.showMessage(function (error, message) {
    $("#targetText").html(message);
});
const btn = document.querySelector("#sendEtherBtn");
btn.addEventListener("click", function () {
contract.paymentHandlerer(
{"to":contractAddress,
 "from":web3.eth.accounts[0],
 "value":web3.toWei("10.0","ether")
});
});

When I run this code.当我运行此代码时。 The function showMessage() does get called as I see the message "Hello Solidity" on my browser screen.当我在浏览器屏幕上看到消息“Hello Solidity”时,函数 showMessage() 确实被调用了。 This proves that the contract is running fine.这证明合约运行良好。 In the second function, I want to transfer some ether from msg.sender to the receiver but the function runs into an error all the time.在第二个函数中,我想将一些以太从 msg.sender 传输到接收器,但该函数一直遇到错误。

The error goes like this:错误是这样的:

Uncaught Error: invalid address
at c (inpage.js:1)
at inputTransactionFormatter (inpage.js:1)
at inpage.js:1
at Array.map (<anonymous>)
at o.formatInput (inpage.js:1)
at o.toPayload (inpage.js:1)
at w.e [as sendTransaction] (inpage.js:1)
at u.sendTransaction (inpage.js:1)
at u.execute (inpage.js:1)
at HTMLButtonElement.<anonymous> (index.html:101)

Can anyone please help me solve it.任何人都可以帮我解决它。 Also, I am not using node js or anything just plain Js and JQuery另外,我没有使用 node js 或任何简单的 Js 和 JQuery

In Web3.js v1.0, web3.eth.accounts is not populated by default.Web3.js v1.0 中,默认情况下不填充web3.eth.accounts

Instead, you need to get accounts using something like:相反,您需要使用以下内容获取帐户:

web3.eth.getAccounts().then(console.log);
> ["0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "0xDCc6960376d6C6dEa93647383FfB245CfCed97Cf"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM