简体   繁体   English

ParserError:应为“;” 但得到了“事件”——solidity 0.8 address payable(msg.sender)

[英]ParserError: Expected ';' but got 'event' -- solidity 0.8 address payable(msg.sender)

I don't know why I still get this error, I have already changed this address payable(msg.sender) to line 81 (I bold the line below) according to solidity 0.8 update, but it still gives an error.我不知道为什么我仍然得到这个错误,我已经根据 solidity 0.8 更新将这个address payable(msg.sender)更改为第 81 行(我将下面的行加粗),但它仍然报错。 Can anyone assist, please?有人可以帮忙吗?

I have indicated line 81 on this code below.我在下面的代码中指出了第 81 行。

CONSOLE ERROR控制台错误

**ParserError: Expected ';' **ParserError:应为“;” but got 'event'但得到了“事件”

| | 50 | 50 | event ImageCreated( | ^^^^^**事件 ImageCreated( | ^^^^^**


pragma solidity 0.8.12;

contract GistPin {
    string public name = "GistPin";
    uint256 public videoCount = 0;
    uint256 public imageCount = 0;
    mapping(uint256 => Image) public images;
    mapping(uint256 => Video) public videos;

    struct Image {
        uint256 id;
        string hash;
        string description;
        uint256 tipAmount;
        address payable author;
    }

    struct Video {
        uint256 id;
        string hash;
        string title;
        address author;
    }

    event VideoUploaded(
        uint256 id,
        string hash,
        string title,
        string description,
        address author
    );

    constructor() public {
        name = "GistPin";
    }

    function uploadVideo(
        string memory _videoHash,
        string memory _title,
        string memory _description
    ) public {
        // Make sure the video hash exists
        require(bytes(_videoHash).length > 0);
        // Make sure video title exists
        require(bytes(_title).length > 0);
        // Make sure video description exists
        require(bytes(_description).length > 0);
        // Make sure uploader address exists
        require(msg.sender != address(0));

        // Increment video id
        videoCount++;

        // Add video to the contract
        videos[videoCount] = Video(videoCount, _videoHash, _title, msg.sender);
        // Trigger an event
        emit VideoUploaded(
            videoCount,
            _videoHash,
            _title,
            _description,
            msg.sender
        );
    }

    event ImageCreated(
        uint256 id,
        string hash,
        string description,
        uint256 tipAmount,
        address payable author
    );

    event ImageTipped(
        uint256 id,
        string hash,
        string description,
        uint256 tipAmount,
        **address payable(msg.sender)**     // ERROR LINE 81
    );

    function uploadImage(string memory _imgHash, string memory _description) public {
    // Make sure the image hash exists
    require(bytes(_imgHash).length > 0);
    // Make sure image description exists
    require(bytes(_description).length > 0);
    // Make sure uploader address exists
    require(msg.sender!=address(0x0));

     // Increment image id
    imageCount ++;

    // Add Image to the contract
    images[imageCount] = Image(imageCount, _imgHash, _description, 0, msg.sender);
    // Trigger an event
    emit ImageCreated(imageCount, _imgHash, _description, 0, msg.sender);
  }

  function tipImageOwner(uint _id) public payable {
    // Make sure the id is valid
    require(_id > 0 && _id <= imageCount);
    // Fetch the image
    Image memory _image = images[_id];
    // Fetch the author
    address payable _author = _image.author;
    // Pay the author by sending them Ether
    address(_author).transfer(msg.value);
    // Increment the tip amount
    _image.tipAmount = _image.tipAmount + msg.value;
    // Update the image
    images[_id] = _image;
    // Trigger an event
    emit ImageTipped(_id, _image.hash, _image.description, _image.tipAmount, _author);
  }
}

can you add what the exact error your getting from the console你能添加你从控制台得到的确切错误吗

Try this:尝试这个:

// SPDX-License-Identifier: MIT 
pragma solidity 0.8.12;

contract GistPin {
  string public name = "GistPin";
  uint256 public videoCount = 0;
  uint256 public imageCount = 0;
  mapping(uint256 => Image) public images;
  mapping(uint256 => Video) public videos;

  struct Image {
    uint256 id;
    string hash;
    string description;
    uint256 tipAmount;
    address payable author;
  }

  struct Video {
    uint256 id;
    string hash;
    string title;
    address author;
  }

  event VideoUploaded(
    uint256 id,
    string hash,
    string title,
    string description,
    address author
  );

  constructor() {
    name = "GistPin";
  }

  function uploadVideo(
    string memory _videoHash,
    string memory _title,
    string memory _description
  ) public {
    // Make sure the video hash exists
    require(bytes(_videoHash).length > 0);
    // Make sure video title exists
    require(bytes(_title).length > 0);
    // Make sure video description exists
    require(bytes(_description).length > 0);
    // Make sure uploader address exists
    require(msg.sender != address(0));

    // Increment video id
    videoCount++;

    // Add video to the contract
    videos[videoCount] = Video(videoCount, _videoHash, _title, msg.sender);
    // Trigger an event
    emit VideoUploaded(
        videoCount,
        _videoHash,
        _title,
        _description,
        msg.sender
    );
  }

  event ImageCreated(
    uint256 id,
    string hash,
    string description,
    uint256 tipAmount,
    address payable author
  );

  event ImageTipped(
    uint256 id,
    string hash,
    string description,
    uint256 tipAmount,
    // Declare the variable 
    address payable myAddress
  );

   function uploadImage(string memory _imgHash, string memory _description) public {
    // Make sure the image hash exists
    require(bytes(_imgHash).length > 0);
    // Make sure image description exists
    require(bytes(_description).length > 0);
    // Make sure uploader address exists
    require(msg.sender!=address(0x0));

    // Increment image id
    imageCount ++;

    // Add Image to the contract

    images[imageCount] = Image(imageCount, _imgHash, _description, 0, payable(msg.sender));
    // Trigger an event

    emit ImageCreated(imageCount, _imgHash, _description, 0, payable(msg.sender));
  }

function tipImageOwner(uint _id) public payable {
    // Make sure the id is valid
    require(_id > 0 && _id <= imageCount);
    // Fetch the image
    Image memory _image = images[_id];
    // Fetch the author
    address payable _author = _image.author;
    // Pay the author by sending them Ether
    _author.transfer(msg.value);
    // Increment the tip amount
    _image.tipAmount = _image.tipAmount + msg.value;
    // Update the image
    images[_id] = _image;
    // Trigger an event
    emit ImageTipped(_id, _image.hash, _image.description, _image.tipAmount, payable(_author));
 }

}

You must declare a variable into ImageTipped event in this way: address payable myAddress .您必须以这种方式在ImageTipped event中声明一个变量: address payable myAddress And each time you call the event with emit you will have to pass it the msg.sender in this way: payable(msg.sender) .每次你用 emit 调用事件时,你都必须以这种方式将 msg.sender 传递给它: payable(msg.sender) For more information see line: 81 and 118 in my code.有关详细信息,请参阅我的代码中的第81118行。

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

相关问题 Solidity 基础知识:“msg.sender”代表什么 - Solidity basics: what "msg.sender" stands for 坚固性:ParserError:预期';' 但得到'(' - solidity: ParserError: Expected ';' but got '(' function 调用中的参数类型无效。 从地址到请求的应付地址的隐式转换无效。对于 msg.sender 和地址 0 - Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested.for msg.sender and adress 0 Solidity ParserError:预期的';' 但得到了“是” - Solidity ParserError: Expected ';' but got 'is' 在 Solidity 中,`msg.sender` 优于 `tx.origin` - `msg.sender` preferred over `tx.origin` in Solidity 如何在solidity 0.5.0中将etherenum发送到msg.sender - How to send etherenum to msg.sender in solidity 0.5.0 Solidity ParserError:预期的标识符,但得到了 '(' constructor() public { ^ - Solidity ParserError: Expected identifier but got '(' constuctor() public { ^ Solidity 错误:: ParserError: Expected '(' but got 'public' - Solidity Error :: ParserError: Expected '(' but got 'public' "Solidity 错误 :: ParserError: Expected &#39;,&#39; but got &#39;Number&#39;" - Solidity Error :: ParserError: Expected ',' but got 'Number' Solidity - 我如何从另一个合约转移到 msg.sender - Solidity - how do i transfer from another contract to msg.sender
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM