0
function buyStar(uint256 _tokenId) public payable {
        require(starsForSale[_tokenId] > 0, "The star must be available for sale!"); // the star to buy must be in starsForSale mapping
        uint256 starPrice = starsForSale[_tokenId]; // get the price of the star for sale
        address starOwner = ownerOf(_tokenId); // get the owner of the star
        require(msg.value >= starPrice, "You need to have sufficient funds!"); // check the bidding price is greater than or equal to the stars for sale price
        _transfer(starOwner, msg.sender, _tokenId);
        address payable starOwnerAddressPayable = payable(starOwner);
        starOwnerAddressPayable.transfer(starPrice);
        if(msg.value > starPrice) {
            msg.sender.transfer(msg.value - starPrice);
        }
    }

The error which is showing up is ParserError: Expected ';' but got 'payable' address payable starOwnerAddressPayable = starOwner;

Compilation failed. See above.

what is the latest way to overcome this.

2
  • Hi there. What version of the compiler are you using? May 31, 2021 at 9:00
  • Hi. I am using ^0.8.4. Solved. function _make_payable(address x) internal pure returns(address payable) { return payable(address(uint160(x)); } added this ^ function and made starOwnerAddressPayable = _make_payable(starOwner). May 31, 2021 at 16:47

2 Answers 2

0

I don't know what your actual code is so I made function for reference it works fine for me.

pragma solidity ^0.8.0;


contract test {
    uint256[] starsForSale;
    
    function ownerOf(uint256 some) public returns(address){
        return msg.sender;
    }
    
    function _transfer(address from, address _to, uint256 amount) public {
        
    }
    

    function buyStar(uint256 _tokenId) public payable {
        require(starsForSale[_tokenId] > 0, "The star must be available for sale!"); // the star to buy must be in starsForSale mapping
        uint256 starPrice = starsForSale[_tokenId]; // get the price of the star for sale
        address starOwner = ownerOf(_tokenId); // get the owner of the star
        require(msg.value >= starPrice, "You need to have sufficient funds!"); // check the bidding price is greater than or equal to the stars for sale price
        _transfer(starOwner, msg.sender, _tokenId);
        address payable starOwnerAddressPayable = payable(starOwner);
        starOwnerAddressPayable.transfer(starPrice);
        if(msg.value > starPrice) {
            payable(msg.sender).transfer(msg.value - starPrice);
        }
    }
}

I think you got error in address payable starOwnerAddressPayable = starOwner;.

But in your code you wrote like address payable starOwnerAddressPayable = payable(starOwner); so I think you forgot to save?

1
  • Actually the _transfer and ownerOf are ERC721 methods. in the ERC721.sol . yes the error was in line address payable starOwnerAddressPayable = payable(starOwner);. Which was rectified by using _make_payable function. function _make_payable(address x) internal pure returns(address payable) { return payable(address(uint160(x)); } . it was solved. Jun 1, 2021 at 8:50
0

Yes the error was in line

address payable starOwnerAddressPayable = payable(starOwner);

Which was rectified by using _make_payable function. It was solved.

Final Code review-

function _make_payable(address x) internal pure returns (address payable) {
        return payable(address(uint160(x)));
    }

    function buyStar(uint256 _tokenId) public payable {
        require(starsForSale[_tokenId] > 0, "The star must be available for sale!"); // the star to buy must be in starsForSale mapping
        uint256 starPrice = starsForSale[_tokenId]; // get the price of the star for sale
        address starOwner = ownerOf(_tokenId); // get the owner of the star
        require(msg.value >= starPrice, "You need to have sufficient funds!"); // check the bidding price is greater than or equal to the stars for sale price
        _transfer(starOwner, msg.sender, _tokenId);
        address payable starOwnerAddressPayable = _make_payable(starOwner);
        address payable newOwnerAddressPayable = _make_payable(msg.sender);
        starOwnerAddressPayable.transfer(starPrice);
        if(msg.value > starPrice) {
            newOwnerAddressPayable.transfer(msg.value - starPrice);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.