【问题标题】:solidity - Invalid type for argument in function call. Invalid implicit conversion from address to address payable requestedsolidity - 函数调用中的参数类型无效。请求的从地址到应付地址的隐式转换无效
【发布时间】:2022-08-19 23:56:55
【问题描述】:

我正在尝试提供应付地址,但在 msg.sender 和 address(_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); 
    
        }
    }

    标签: typeerror


    【解决方案1】:

    您的代码中有一些问题:

    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 进行投射。在solidity addresspayable address 是两个不同的东西。 sendtransfer 仅适用于 payable address 类型。你不需要这个:

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

    而只是

       _author.transfer(msg.value);
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2022-08-13
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 2022-08-20
      • 2019-12-21
      相关资源
      最近更新 更多