Transfer Assignment

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

i just added a require argument to make sure the balance of the user is higher or equal to the withdraw.`pragma solidity 0.7.4;

contract bank{

mapping(address => uint) balance;
address owner;
constructor(){
    owner = msg.sender;
}
modifier onlyowner {
    require(msg.sender == owner);
_;
}
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(amount<=balance[msg.sender],'not enoughbalance');
    msg.sender.transfer(amount);
    balance[msg.sender] -= amount;
    return balance[msg.sender];
}
function viewbalance() public view returns (uint){
    return balance[msg.sender];
}

function transfer(address reciver, uint amount) public{
   require(balance[msg.sender] >= amount, 'not enough balance');
   //require(msg.sender!= reciver, 'dont send funds to yourself');
    _sendbalance(msg.sender, reciver, amount);
    
}
function _sendbalance(address from, address to, uint amount) private{
    balance[from] -= amount;
    balance[to]+= amount;
}

}`

1 Like

We eliminate the problem using “require”

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

1 Like

Hey @bratulja, hope you are well.

Just curios, why did you decide to eliminate the require statement? :nerd_face:

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Assignment complete, my solution was to jam a bunch of checks in the function. Also insure to adjust the balance.

pragma solidity 0.7.5;

contract Bank {
    
    event depositComplete(uint amount, address indexed depositedTo);
    event withdrawComplete(uint amount, address withdrawnTo);
    
    mapping( address => uint) balance;
    address owner;
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _;  // run the function - this will be replaced with the code from calling function
    }
    
    constructor() {
        owner = msg.sender; // will be the person who deploys the contract
    }
    
    function deposit() public payable returns (uint) {
        emit depositComplete(msg.value, msg.sender);
        balance[msg.sender] += msg.value;
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public returns (uint) {
        require(amount <= balance[msg.sender], "Cannot withdraw more than your balance!");
        
        uint prevBalance = balance[msg.sender];
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
        
        assert(balance[msg.sender] == prevBalance - amount);
        emit withdrawComplete(amount, msg.sender);
        return balance[msg.sender];
    }
    
    function getBalance() public view returns (uint) {
        return balance[msg.sender];
    }
    
}
1 Like

On your solution to the transfer function I think that we should do the transfer first and then subtract from balance[msg.sender].


 function withdraw(uint amount) public returns (uint){
        require(balance[msg.sender] >= amount);
        balance[msg.sender] -= amount;  //here you subtracted before making the transfer.
        msg.sender.transfer(amount);
        return balance[msg.sender];
    }

// my code looks like this but I cant get my assert function to work after the transfer.  It gives me an error about undeclared identifier for previousBalance.  If I // the assert function then it runs but I wanted to do the assert.  

function transfer(address recipient, uint amount) public {
      
   
//call made to execute the transfer function using these parameters from 
//msg.sender to recipient and amount
       
//adding code to check for balance before transfer
       
       require(balance[msg.sender] >= amount, "Your money is too short to make this transaction");
      
       require(msg.sender != recipient,"You cannot send money to yourself that would be money laundering");  
      
      uint previousBalance;     //declared previousBalance
      
      previousBalance = balance[msg.sender];  //determine previous balance before transfer  
       
       _transfer (msg.sender, recipient, amount);
       
       emit transferDone(amount, recipient);
       
      
   }
   
   //Transfer function private. 
   //use underscore when the function is private
     function _transfer (address from, address to, uint amount)private {
           balance[from] -= amount;
           balance[to] += amount;
           
           
     assert(balance[msg.sender] = previousBalance - amount); //determine balance after transfer  // get an error here of undeclared identifier for previousBalance.  Our function is private so how can this function see previousbalance?
       }
//Also for the withdraw function I was trying to use the emit and the error says that it is not reachable
 function withdraw(uint amount)public returns(uint){
           //mgs.sender is an address 
          
           require(balance[msg.sender] >= amount, "You do not have enough cash");

           msg.sender.transfer(amount);  //send from msg.sender account
           
           balance[msg.sender] -= amount;
           
           return balance[msg.sender];
           
           emit withdrawDone(amount, msg.sender);  //can emit be used in private function? When I changed to view so it could see the event then it does not work either get error about non-payable.

//The full code looks like this:

pragma solidity 0.7.5;

import "./ownable.sol";
 
contract Bank is Ownable {
     
    mapping(address => uint)balance;
    
// add an event function to record transaction before it is executed
   
//To add deposit of money/ether to any account

// You can also add an index to query a variable
    
     event depositedTo(uint amount, address depositedTo);
     
     event balanceAdded(uint amount, address depositedTo);
     
     event transferDone(uint amount, address depositedTo);
     
     event withdrawDone(uint amount, address depositedFrm);
       
       function Deposit()public payable returns(uint){
          
           balance[msg.sender] += msg.value;
           
           emit depositedTo(msg.value, msg.sender);
           
           return balance[msg.sender];
       }
       
   // To get balance from any account 
  // function getBalance(address _to)public view returns ( uint){
       
      // return balance[ _to];
      
      //To get balance from msg.sender account
       
       function getMoreBalance()public view returns ( uint){
       
       return balance[msg.sender];
   }
   
   //To transfer money from msg.sender to any recipient
   //And to add requirments before fucntion is executed.
    
   
   function transfer(address recipient, uint amount) public {
      
      //call made to execute the transfer function using these parameters from msg.sender to recipient and amount
       //adding code to check for balance before transfer
       
       require(balance[msg.sender] >= amount, "Your money is too short to make this transaction");
      
       require(msg.sender != recipient,"You cannot send money to yourself that would be money laundering");  
      
      uint previousBalance;
      
      previousBalance = balance[msg.sender];  //determine previous balance before transfer
       
       _transfer (msg.sender, recipient, amount);
       
       emit transferDone(amount, recipient);
       
      
   }
   
   //Transfer function private. 
   //use underscore when the function is private
     function _transfer (address from, address to, uint amount)private {
           balance[from] -= amount;
           balance[to] += amount;
           
           
     // assert(balance[msg.sender] = previousBalance - amount); //determine balance after transfer
       }
       
       //To send money to any account use withdraw function
       
       function withdraw(uint amount)public returns(uint){
           //mgs.sender is an address 
          
           require(balance[msg.sender] >= amount, "You do not have enough cash");

           msg.sender.transfer(amount);  //send from msg.sender account
           
           balance[msg.sender] -= amount;
           
           return balance[msg.sender];
           
           emit withdrawDone(amount, msg.sender);
           
           
           //To send from other address rather than msg.sender
           // address payable toSender = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;//to send from specific account
          // toSender.transfer(amount);
          
          // or you can do this all within the function 
          
          //function withdraw(uint amount, address 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2) public payable returns(uint,address){
         //  return(amount, )
       }
   }
   
           

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

pragma solidity 0.7.5;

contract Bank {
mapping(address => uint) balance;

address owner;
event depositDone(uint amount, address indexed depositedTo);

modifier onlyOwner {
    require(msg.sender == owner);
    _;//run the function
    
}

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) {
  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 view {
        require(balance[msg.sender] >= amount, "Balance not sufficient");
        require(msg.sender != recipient, "Don't transfer money to yourself");
        
    
    }
}

pragma solidity 0.7.5;

contract Bank {
mapping(address => uint) balance;

address owner;
event depositDone(uint amount, address indexed depositedTo);

modifier onlyOwner {
    require(msg.sender == owner);
    _;//run the function
    
}

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);
  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 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

Hello all,

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

1 Like

pragma solidity 0.7.5;

contract MyBank {

mapping(address => uint) balance;

function Deposit() public payable returns(uint){
    require(msg.sender.balance >= msg.value, "You don't have enough funds!");  //The ethereum blockchain stores address.balance
    balance[msg.sender] = balance[msg.sender] + msg.value; //balance[address] exists only within this bank program
    return balance[msg.sender];
}

//"toWithdraw" will be converted to eth
function WithdrawEth(uint toWithdraw) public returns(uint){
    toWithdraw = toWithdraw*10**18;
    require(balance[msg.sender] >= toWithdraw, "You are trying to withdraw more than you have deposited!");
    msg.sender.transfer(toWithdraw);
    balance[msg.sender] = balance[msg.sender] - toWithdraw;
    return balance[msg.sender];
    
}


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

}

1 Like

I added a require function to make sure the sender has the balance.
Then I updated the balance for that sender

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

My withdraw function solution:

function withdraw(uint amount) public returns(uint) {
    require (balance[msg.sender] >= amount, "This contract has an insuffienct balance.");
    uint previousBalance = balance[msg.sender];
    msg.sender.transfer(amount);
    previousBalance -= amount;
    balance[msg.sender] = previousBalance;
    return balance[msg.sender];
}

Hey @Cecilia

You solution is correct, however you can code the same function by using less commands, therefore less gas :slight_smile:

function withdraw(uint amount) public returns(uint) {
    require (balance[msg.sender] >= amount, "This contract has an insuffienct balance.");
    balance[msg.sender] -= amount;
    msg.sender.transfer(amount);
    return balance[msg.sender];
}

Happy coding,
Dani

1 Like
    function withdraw(uint amount) public returns (uint) {
        // msg.sender --> address
        require(balance[msg.sender] >= amount, "Balance not sufficient");
        balance[msg.sender] -= amount;
        msg.sender.transfer(amount);
        return balance[msg.sender];
    }

When coding smart contracts in reality, we need to compensate these values for gas prices, correct? I was able to deposit/withdraw 1 ETH, but wouldn’t we need to make sure our balance covers the gas prices as well?

Hi @MARTIS

The gas cost of a transaction is paid by the user that sends the transaction, not by your smart contract.

Cheers,
Dani

1 Like
pragma solidity 0.7.5;

contract Bank {
    
    mapping(address => uint) balance;
    
    address owner;
    
    event depositDone(uint amount, address indexed depositedTo);
    event withdrawDone(uint amount, address indexed withdrawFrom);
    
    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, "Insufficient Balance");
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
        emit withdrawDone(amount, msg.sender);
        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, "Insufficient Balance");
        require(msg.sender != recipient, "Cannot send to Self");
        
        uint previousSenderBalance = balance[msg.sender];
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
        
        _transfer(msg.sender, recipient, amount);
  
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;        
    }
}
1 Like

Okay so I think I got it right hope my code works. So firstly to sort out the balance issue we will use require.

require(balance[msg.sender] >= amount, “insufficient balance for this transaction!”);

then we will do the balance issue

uint previousBalance = balance[msg.sender];

msg.sender.transfer(amount);

balance[msg.sender] = previousBalance - amount;

return balance[msg.sender];

1 Like
diff --git a/transfer-assignment-solution.sol b/transfer-assignment-solution.sol
index 60b7aea..68e93fe 100644
--- a/transfer-assignment-solution.sol
+++ b/transfer-assignment-solution.sol
@@ -13,6 +13,8 @@ contract Bank {
     }
     
     function withdraw(uint amount) public returns (uint){
+        require(amount <= balance[msg.sender]);
+        balance[msg.sender] -= amount;
         msg.sender.transfer(amount);
     }
1 Like

pragma solidity 0.7.5;
contract Bank {

mapping(address => uint) balance;

function deposit(uint _add) payable public {
    balance[msg.sender] += _add;
}

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

function transfer(address recipient, uint amount) public {
    require(balance[msg.sender] >= amount);
    require(msg.sender != recipient);
    balance[msg.sender] -= amount;
    balance[recipient] += amount;
}

function withdraw(uint amount) public returns(uint) {
    require(balance[msg.sender] >= amount);
    balance[msg.sender] -= amount;
    return balance[msg.sender];
   
}

}
**Here is my solution. **
I also have a question, why did you use this line in the withdraw function, I did not use and it works for me?
image

1 Like