pragma solidity 0.5.9;
contract Testing{
function participate()public payable{
uint winner = 9;
require(msg.value == 0.1 ether);
if ( winner==9)
{
require(msg.sender.call.value(this.balance)());
}
}
}我收到以下错误消息:
solc testing8_2.sol testing8_2.sol:13:42: Error:成员"balance“在合同测试中依赖于参数的查找后未找到或不可见。使用" address ( this ).balance“访问该地址成员。require(msg.sender.call.value(this.balance)());
但如果我尝试:
if ( winner==9)
{
require(msg.sender.call.value(address(this).balance)());
}我收到以下错误消息:
solc testing8_2.sol testing8_2.sol:13:20: Error:函数调用的错误参数计数:给出的参数为0,但期望为1。此函数需要一个单字节参数。使用"“作为参数来提供空的调用数据。require(msg.sender.call.value(address(this).balance)());^--------------------------------------------^ testing8_2.sol:13:12: Error:在依赖于参数的查找之后没有找到匹配声明。require(msg.sender.call.value(address(this).balance)());^-^
请有人指点我如何解决这个问题:
祖尔菲。
发布于 2020-04-18 06:05:48
您只是忽略了call的字节参数:
pragma solidity 0.5.9;
contract Testing {
function participate() public payable{
uint winner = 9;
require(msg.value == 0.1 ether);
if (winner == 9)
{
(bool success, ) = msg.sender.call.value(address(this).balance)("");
require(success, "Transfer failed.");
}
}
}由于您只想将以太发送到地址,而不想调用该地址上的函数,所以字节参数只是一个空的""。让我们假设您希望在deposit()地址调用一个msg.sender函数。然后你会写:
function makeDeposit(address bankAddress) public payable {
bytes32 functionHash = keccak256("deposit()");
bytes4 function4bytes = bytes4(functionHash);
bytes payload = abi.encode(function4bytes);
if (msg.value > 0) {
(bool success,) = bankAddress.call.value(msg.value)(payload);
require(success, "Ether transfer failed.");
}
}示例摘自本有关如何使用call.value的伟大教程。
https://ethereum.stackexchange.com/questions/82586
复制相似问题