Transfer Assignment

i used a require function to check the balance in the balance mapping, then after the transfer was done i went back to the balance mapping to minus the amount of money transferred from the balance stored in the mapping.

i also got rid of the return(uint) since the function isn’t returning anything.


    function withdraw(uint amount) public{
        require(balance[msg.sender] >= amount);
        msg.sender.transfer(amount);

        balance[msg.sender] -= amount;
    }

It is not really need it, you are not using it any where else in the contract, so if you remove it, it will still work as expected.

Carlos Z

pragma solidity 0.7.5;

contract Bank {
    
    mapping(address => uint) balance;
    address owner;
    event depositDone(uint amount, address indexed depositedTo);
    event withdrawLog (address comingFrom, address indexed goingTo, uint amount);
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
    constructor() {
        owner = msg.sender;
    }

    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 not sufficient");
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
        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 send money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        assert(balance[msg.sender] == previousSenderBalance - amount);

        emit withdrawLog(recipient, msg.sender, amount);
    }

    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}
1 Like

You would require the amount to be less than your balance.

 require(balance[msg.sender] >= amount, "Insufficient Funds");

Looks like nobody has taken this course for some time, or at least not posting. It’s my second time through the course - did it first about a year ago, so running through for refresher to then go straight into 201 course. Has it been updated lately, or pretty much stuck in 2019/2020?

//Withdrawl function
    function withdrawl(address recipient, uint amount) public /*returns (uint)*/ {
        //check balance of msg.sender and don't allow sender to send funds to themselves
        require(balance[msg.sender] >= amount, "Insufficient balance for transfer.");
        require(msg.sender != recipient, "Cannot send funds to originating address.");
        
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;

        emit amountWithdrawn(amount, recipient);
    }
2 Likes
    function withdraw(uint amount) public returns (uint) {
        require(balance[msg.sender] >= amount);
        msg.sender.transfer(amount);
        return balance[msg.sender] -= amount;

    }
1 Like

There has not been an update on the course for at least a year, so it should be the same content, still its partially up to date with the solidity syntax, but there are breaking changes from 0.6 to 0.8.

Carlos Z

2 Likes

There seems to be a video missing on the solution to the “destroyable” contract. In the assignment description and at the end of the previous video, Filip mentioned in the next lecture he would go over the solution, but I can’t find the solution video.

Could you please give me the name of the lesson? I will take a look at it :nerd_face:

Carlos Z

Thanks Carlos…

It’s in “Inheritance Assignment” and “Inheritance Assignment Solution”. There is a link to FIlip’s code but no video explanation. And then when we move on to the next section on visibility, there is no longer any “destroyable” code in his contract and no mention of it in the video.

I’m just wondering if it was removed because that type of function is no longer used in practice?

Thanks!
Douglas

1 Like

I see, the destroyable assignment its quite simple, probably filip realized that after recording the inheritance video, because you just need to create a Destroyable contract which can only be used by the owner, and inherit to your contracts.

I think that is why there is no solution video for it, because its a quite simple assignment.

Carlos Z

Makes sense. Thank you!

Any reason it wouldn’t be included in the final project code? Is it not used often?

In order for for the function to check the balance and make sure you are not trying to send funds that are not yours you can use the following:

    function withdraw(uint amount) public returns (uint){
        require(balance[msg.sender] >= amount, "Not enough Funds: Enter amount less than current balance");
        balance[msg.sender] -= amount;
        msg.sender.transfer(amount);
        return balance[msg.sender];
    }

Exactly, although its the way you could destroy a contract, usually is not used often (i would be scare to use a stake contract that have a destroyable function that can be triggered whenever the owner wants :rofl:)

But still, if by any reason you need to destroy contract, this is how you could do it.

Carlos Z

1 Like

Great point re staking contract!! :upside_down_face:

1 Like
function withdraw(uint amount) public payable returns(uint) {
require(balances[msg.sender] >= amount, "Not enough balance!");

balances[msg.sender] -= amount;
msg.sender.transfer(amount);

return balances[msg.sender];
}
1 Like

this is how i handled this error

function withdraw(uint amount) public {

    require(balance[msg.sender] >= amount, "balance not sufficient");

    payable(msg.sender).transfer(amount);

    balance[msg.sender] -= amount;

}

if i use “msg.sender.transfer(amount)” instead of “payable(msg.sender).transfer(amount);” i have an error sayin msg.sender is not a payable object

1 Like
function withdraw(uint amount) public returns(uint) {
    
       require (balance[msg.sender]>=amount, "Balance not sufficient");
       balance[msg.sender]-=amount;
        msg.sender.transfer(amount);
        return balance[msg.sender];
    }`Preformatted text`
 event transferDone(uint amount, address indexed sentTo);
    

    function transfer(address recipient, uint amount) public payable {
        require(balance[msg.sender] >= amount, "no funds");
        require(msg.sender != recipient, "impossible");
        balance[msg.sender] -= amount;
        emit transferDone(msg.value, msg.sender);
        
        _transfer(msg.sender, recipient, amount);
                
        
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
1 Like