Transfer Assignment

function withdraw(uint amount) public {

    require(balance[msg.sender] >= amount, "Balance not enough");  //checks if balance ok
    
    uint originalBalance = balance[msg.sender];
    
    msg.sender.transfer(amount);
    emit withdrawDone(amount, msg.sender);
    
    balance[msg.sender] -= amount; //returns adjusted amount
    assert(balance[msg.sender] == originalBalance - amount); //error check
}
1 Like

contract Bank {
    
    mapping(address => uint) balance;
    address owner;
    
    event depositDone(uint amount, address indexed depositedTo);
    
    
    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, "You dont have enough money");
        balance[msg.sender] -= amount;
        msg.sender.transfer(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 sufficent");
        require(msg.sender != recipient, "You cant 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

wow I cant believe i actually new what filip was asking me to do and figured out how to do it. I know I have a long way to go but were definitely making progress here. I just added a require statment to the withdraw function and added a return for the balance minus the amount using the -= operator or whatever its called.

pragma solidity 0.8.1;

contract goinBack{

mapping (address => uint) balance;

address owner;

event depositDone(uint amoubt, address indexed depositedTo);

modifier onlyOwner {
require (msg.sender == owner, “Only the owner can add and send money”);
_;
}

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, “you didnt put that much in the contract”);
payable (msg.sender).transfer (amount);
return balance [msg.sender] -=amount;
}

function getBalance ( )public view returns(uint){
return balance[msg.sender];
}

function transfer (address recipient, uint amount) public{
require(balance [msg.sender]>=amount, “you broke bro”);
require(msg.sender!= recipient, “dont send money to yourself”);

  uint previousSenderBalance = balance[msg.sender]; // this variable was made just to assert that the balance is acurrate
  
  _transfer(msg.sender, recipient, amount);

  assert(balance[msg.sender]== previousSenderBalance - amount); // assert is used to check that the balance is what it should be after the ftransfer

}

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

}

}

1 Like

Withdraw function to check user cannot withdraw more than there balance and adjust balance after withdrawal.

function withdraw(uint256 amount) public {
require(amount <= balance[msg.sender], “Not sufficient balance to withdraw”);
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
}

2 Likes

function withdraw(unit amount) onlyOwner returns (uint){

require (balance[msg.sender]=>amount, “No money, No honey”);

1 Like
pragma solidity 0.7.5;

contract Etherbank{
    address owner;
    mapping (address=>uint) balance;
    
    event depositDone(uint,address);
    
    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 getbalance() public view returns(uint){
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public returns(uint){
        require(balance[msg.sender]>=amount,"you do not have the required funds");
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
    }
    
    function tranfer(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];
    }
}
1 Like
function withdraw(uint _amountToWithdraw) public {
    require(balance[msg.sender]>=_amountToWithdraw, "no money");
    uint previousBalance=balance[msg.sender];
    msg.sender.transfer(_amountToWithdraw);
    balance[msg.sender] -=_amountToWithdraw;
    assert(balance[msg.sender]==previousBalance-_amountToWithdraw);
}
1 Like

Hello, I found this solution:
function withdraw(uint amount)public returns(uint){
//check balance

    require(balance[msg.sender] >= amount, "You can't withdraw more than you have" );
    msg.sender.transfer(amount);
    
    //Adjust balance
    
    balance[msg.sender] -= amount;
}

It works but I get a warning: “Unamed return variable can remain unassigned”

1 Like
   function withdraw(uint amount) public returns (uint) {
	    require(balance[msg.sender] >= amount, "Insufficient balance to withdraw the requested amount");
	    
	    balance[msg.sender] -= amount;
	    msg.sender.transfer(amount);
	    
	    return balance[msg.sender];
    }
1 Like

here is

function witdraw(uint amount) public returns(uint) {
        require (balance[msg.sender] >= amount, "You don't have any balance into your bank account");
        uint toTransfer = amount;
        balance[msg.sender] -= amount;
        assert(balance[msg.sender] >= 0);
        msg.sender.transfer(toTransfer);
        return toTransfer;
}
1 Like

Almost great. the assert should be at the end (before return, return is the last function that you should run on every function).

Carlos Z

1 Like

excerpt is:

function withdraw(uint amount) public returns (uint){

/* msg.sender is an adress, so is the next 2 lines
address payable toSend = 0x617F2E2fD72FD9D5503197092aC168c91465E7f2;
toSend.transfer(amount); */

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

}

1 Like

A trivial question but could it be possible to query how much ether has the msg.sender deposited to the contract address directly from the blockchain, instead of storing the data using state variables (i.e. the balance mapping)?
Thanks

function withdrawFunds(uint amount) public payable returns (uint) {
        
        //amount is input as Wei so
        uint retire = amount * 1000000000000000000; // convert into ether
        require(balance[msg.sender] >= retire, "Not enough amount");
        emit balanceOut(retire, msg.sender);
        balance[msg.sender] -= retire;
        
        msg.sender.transfer(amount);

I have added a conversion wei into ether, because i realized that the amount as argument is readed as wei, so have to convert.
If I dont do the conversion my require does no work as it should
Any one knows a better solution?

Hey @karips

You you cannot, you can only fetch the contract balance not the single-user balance.

Cheers,
Dani

1 Like

Hi @zero0_cero

The withdraw function should not be payable because you are not sending ether to it.
The amount is generally always in wei, so when you call the function you will so something like

withdrawFunds(10 ** 18)

10**18 = ether

Cheers,
Dani

How can I implement that? as function parameter?

Nice little exercise :slight_smile:

function withdraw(uint amount) public {
require(balance[msg.sender] >= amount, “Balance not sufficient”);
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
}

Question: does it matter that “msg.sender.transfer(amount);” comes before “balance[msg.sender] -= amount;”? Is there a specific rule that stipulates the order or is it down to personal preference?

1 Like
    function withdraw(uint amount) public returns (uint) {
        require(amount <= balance[msg.sender]);
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
    }

This compiles right and withdraws subtracting an amount from the original balance but for some reason the balance after the withdraw is not the right the number. Can you explain to me why that it? I think it is because it is charging some Ethereum gas fees for completing that type of function but I may be wrong. Thanks!

nevermind figured it out it!

1 Like