dappuniversity / decentragram

131 stars 145 forks source link

issue to compile contract #6

Open Akhilleshgoswami opened 3 years ago

Akhilleshgoswami commented 3 years ago

Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested. `pragma solidity >=0.5.0;

contract Decentragram { string public name; uint256 public imageCount = 0; mapping(uint256 => Image) public images;

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

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 author
);

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

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);**

//the error is just because msg.sender is address and we can't pass an address to address payable }

function tipImageOwner(uint256 _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);

//"send" and "transfer" are only available for objects of type "address payable", not "address".

    // 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
    );
}

} `

markanft commented 3 years ago

Probably you are using Solidity Compiler with a version >0.8.0

Check https://docs.soliditylang.org/en/v0.8.2/080-breaking-changes.html#new-restrictions

unfixedbug commented 2 years ago

lower the solidity compiler version