[英]ParserError: Expected ';' but got 'event' -- solidity 0.8 address payable(msg.sender)
我不知道为什么我仍然得到这个错误,我已经根据 solidity 0.8 更新将这个address payable(msg.sender)
更改为第 81 行(我将下面的行加粗),但它仍然报错。 有人可以帮忙吗?
我在下面的代码中指出了第 81 行。
控制台错误
| 50 | 事件 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);
}
}
你能添加你从控制台得到的确切错误吗
尝试这个:
// 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));
}
}
您必须以这种方式在ImageTipped event
中声明一个变量: address payable myAddress
。 每次你用 emit 调用事件时,你都必须以这种方式将 msg.sender 传递给它: payable(msg.sender)
。 有关详细信息,请参阅我的代码中的第81和118行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.