solidity - 函数调用中参数的类型无效.请求从地址到应付地址的隐式转换无效



我正在尝试将地址设置为应付地址,但是在msg.sender和地址(_author).transfer(msg.value)处出现错误。 它显示为函数调用中参数的无效类型。请求从地址到应付地址的隐式转换无效。每次替换相同的错误时,我都尝试了很多方法来解决。在向作者添加应付款项之前,这很好,但是当向作者添加应付款项时,它开始出错。在两者中,msg.sender 和 msg.value

<小时 />

pragma solidity >=0.4.0 <0.9.0;

contract SocialNetwork {
string public name;
uint public postCount = 0;
mapping(uint => Post) public posts;


struct Post {
uint id;
string content;
uint tipAmount;
address payable author;
}

event PostCreated(
uint id,
string content,
uint tipAmount,
address payable author
);

event PostTipped(
uint id,
string content,
uint tipAmount,
address payable author
);

constructor() public {
name = "Yash university Social Network";
}

function createPost(string memory _content) public {
//REquire Valid content
require(bytes(_content).length > 0);

// InCREMENT the post count
postCount ++;
// Create the post
posts[postCount] = Post(postCount, _content, 0, msg.sender);
// Trigger event 
emit PostCreated(postCount, _content, 0, msg.sender);

}

function tipPost(uint _id) public payable {

//fetch the post
Post memory _post = posts[_id];
//fetch the author
address payable _author = _post.author;
//pay the author
address(_author).transfer(msg.value);
//increment the tip post
_post.tipAmount = _post.tipAmount + msg.value;
//update the post
posts[_id] = _post;
//Trigger an event
emit PostTipped(postCount, _post.content, _post.tipAmount, _author); 

}
}

代码中存在一些问题:

1-在Post结构中,您将地址定义为应付:

struct Post {
uint id;
string content;
uint tipAmount;
address payable author;
}

但是当你创建一个帖子时,你传递的是msg.sender具有address类型。在v0.8.0之前,msg.sender是付费的,但因为你必须将其转换为payable(msgs.sender)。它应该是:

function createPost(string memory _content) public {
require(bytes(_content).length > 0);
postCount ++;
posts[postCount] = Post(postCount, _content, 0, payable(msg.sender)); 
emit PostCreated(postCount, _content, 0, payable(msg.sender));
}

2-在tipPost功能中,您将获得应付地址

address payable _author = _post.author;

但是然后你用address铸造它.在坚固性中,addresspayable address是两个不同的东西。sendtransfer仅适用于payable address类型。你不需要这个:

address(_author).transfer(msg.value);

相反,只是

_author.transfer(msg.value);

最新更新