contract agram {
string public name;
uint public imageCount = 0;
mapping(uint => Image) public images;
struct Image {
uint id;
string hash;
string description;
uint tipAmount;
address payable author;
}
event ImageCreated(
uint id,
string hash,
string description,
uint tipAmount,
address payable author
);
event ImageTipped(
uint id,
string hash,
string description,
uint tipAmount,
address payable author
);
constructor() public {
name = "agram";
}
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(0));
// 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);
}```
-
1Did you copy this code from somhwere? Coz it seems you are missing "}" at the end to close the contract. However, there are other issues after that. But you might be able to solve them, as they are more common.– SkyFeb 1, 2022 at 9:14
Add a comment
|
1 Answer
contract agram {
string public name;
uint public imageCount = 0;
mapping(uint => Image) public images;
struct Image {
uint id;
string hash;
string description;
uint tipAmount;
address payable author;
}
event ImageCreated(
uint id,
string hash,
string description,
uint tipAmount,
address payable author
);
event ImageTipped(
uint id,
string hash,
string description,
uint tipAmount,
address payable author
);
constructor() public {
name = "agram";
}
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(0));
// 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, _author);
}
}
I edited the code so it compiles without error, only warnings. Hope this helps you edit further changes.