Compilation error in code shown in Video ! It must be updated

Hi Filip,

Code has a compilation which is available on the below link, It says “transfer/send function is only available for address type payable not address”.
https://github.com/filipmartinsson/solidity-0.7.5/blob/main/transfer-assignment-solution.sol

Note:- In your video, You are using the transfer function to send eth but you did not mention to whom you are sending. In the transfer function, you just pass the amount as an argument but you missed the recipient address.

https://academy.ivanontech.com/lessons/transfer

pragma solidity 0.8.7;

contract Bank {
    
    mapping(address => uint) balance;
    
    event depositDone(uint amount, address indexed depositedTo);
    
    function deposit() public payable returns (uint)  {
        balance[msg.sender] += msg.value;
        emit depositDone(msg.value, msg.sender);
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public returns (uint){
        require(balance[msg.sender] >= amount);
        balance[msg.sender] -= amount;
        msg.sender.transfer(amount); // Compilation error in this line. " transfer is only available for adress type payable not adress"
        return balance[msg.sender];
    }
    
    function getBalance() public view returns (uint){
        return balance[msg.sender];
    }
    
    function transfer(address recipient, uint amount) public {
        require(balance[msg.sender] >= amount, "Balance not sufficient");
        require(msg.sender != recipient, "Don't transfer money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
                
        assert(balance[msg.sender] == previousSenderBalance - amount);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
    
}
1 Like

Hey @Gaurav_Sahoo, hope you are well.

The error comes from the solidity version that you are using to the contract, filip made this contract with the solidity version 0.7.5. While yours run at 0.8.7, which have some syntax changes long ago.

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

If you use payable(msg.sender).transfer(amount); instead, it will do the trick.

Carlos Z

1 Like

Hi @Gaurav_Sahoo,

Following up on the other point you raised…

payable(msg.sender).transfer(amount);   // Solidity v0.8 syntax

msg.sender.transfer(amount);            // Solidity v0.7 syntax

This line of code does not call our Bank contract’s transfer() function. Here, transfer is an address member (or method) which comes built into the Solidity language, with pre-defined functionality:

<address payable>.transfer(uint256 amount)

It automatically deducts the amount argument’s value (always in wei) from the contract address balance, and transfers it to   <address payable>   i.e. to the address the method is called on, which in our code is msg.sender . So, in other words, the wei amount is transferred from the contract address to the external address which calls the withdraw function. This pre-defined method must be called on a payable address (which explains why the v0.8 syntax change caused you an issue*), and with a uint256 argument.

Our Bank contract’s transfer function is different to the deposit and withdraw functions, in that it doesn’t involve any ether entering or leaving the Bank contract. It handles internal transfers of funds between existing Bank account holders. So, the net effect to the contract balance is zero. However, the contract still needs to adjust the amount each of the parties involved in the internal transfer is entitled to withdraw from the contract (their share of the total pooled funds) i.e. increase the recipient’s entitlement (balance) by the same amount the sender’s entitlement is reduced, with the code…

balance[from] -= amount;
balance[to] += amount;

So this is why the transfer function needs an additional recipient parameter.

I hope this clarifies things further for you. But just let me know if anything is still unclear, or if you have any further questions :slight_smile:


* I’ve explained the reason behind this syntax change in my reply to the same issue you have with the Inheritance Assignment code…