1

The code is from freecodecamp.org, and a solidity tutorial from Youtube and theirs worked, but unfortunately mine throws this error:

Address must be type "payable"

// SPDX-License-Identifier: MIT

pragma solidity >=0.6.6 <0.9.0;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract FundMe {
    
    
    mapping(address => uint256) public addressToAmountFunded;
    address public owner;
    
    constructor() {
        owner = msg.sender;
    }

    function fund() public payable {
        uint256 minimumUSD = 50 * 10 ** 18;//=Gwei
        require(getConversionRate(msg.value) >= minimumUSD,"You need to send more Eth");
        addressToAmountFunded[msg.sender] += msg.value;
    }

    function getVersion() public view returns (uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
        return priceFeed.version();
    }

   
    function getPrice() public view returns (uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
        (,int256 answer,,,)=priceFeed.latestRoundData();
        return uint256(answer * 10000000000);
    }

    //10000000000 = Gwei which is why we added 10 zeros to getPrice(answer) to convert it to Wei amount
    function getConversionRate(uint256 ethAmount) public view returns (uint256){
        uint256 ethPrice = getPrice();
        uint256 ethAmountInUsd = (ethPrice * ethAmount)/ 1000000000000000000; //divide 2x because we added 10*((getPrice)answer))
        return ethAmountInUsd;
    }
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
    
    function withdraw() payable public {
        msg.sender.transfer( address (this).balance);

        
    }
}

1 Answer 1

0

When you use .transfer() or .send() on an address it must be an address payable. See this for more detailed information.

Those two methods are now discouraged because they have an hard coded gas amount of 2300, use .call{value: address(this).balance}(""), which does not require the address to be payable, instead.

More info here and here.

2
  • Thank you for the answer, I tried: function withdraw() public { msg.sender.call{value: address(this).balance}(""); } and got back from solidity: Warning: Return value of low-level calls not used. --> contracts/Fundme2.sol:44:9: | 44 | msg.sender.call{value: address(this).balance}(""); | ^^^^^^^^^^^^^^^^^^^^^ Commented Mar 20, 2023 at 15:50
  • Try this (bool success,) = msg.sender.call{value: address(this).balance}("")
    – FSainas
    Commented Mar 20, 2023 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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