Transfer Assignment

pragma solidity 0.7.5;

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 {
        
        require(balance[msg.sender] >= amount, "Balance not sufficient");

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

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

    }

}
1 Like

pragma solidity 0.7.5;

contract Bank {

mapping(address => uint) balance;
address owner;

event depositDone(uint amount, address indexed depostiedTo);
event amountTransfered(uint amount, address indexed transferedTo, address indexed transferedFrom);

modifier onlyOwner {
require(msg.sender == owner, “Only 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, “The amount you tried to withdraw is bigger than your balance”);

  uint previousSenderBalance = balance[msg.sender];
  msg.sender.transfer(amount);
  balance[msg.sender] -= amount;
  
  assert(balance[msg.sender] == previousSenderBalance - amount);

}

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);
emit amountTransfered(amount, recipient, msg.sender);

assert(balance[msg.sender] == previousSenderBalance - amount);
  
  
  //event Logs and further checks

}

function _transfer(address from, address to, uint amount) private {

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

}

}

1 Like
pragma solidity 0.7.5;

contract Bank {
    
    mapping(address => uint) balance;
    
    address owner;
    
    event depositDone(uint amount, address indexed depositedTo);
    event transferFunds(address indexed depositedTo, address indexed recipient, uint amount);
   
   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(amount <= balance[msg.sender]);
        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, "Balance not sufficient");
       require(msg.sender != recipient, "Don't transfer money to yourself");
      
    
        uint previousSenderBalance = balance[msg.sender];
    
     _transfer(msg.sender, recipient, amount);
     emit transferFunds(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

Assignment code:

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

1 Like

function withdraw (uint amount) public view returns (uint) {
require(balance[msg.sender] >= amount, “You don’t have enough funds.”);
msg.sender.transfer(amount);
return balance[msg.sender];
}

1 Like
pragma solidity 0.7.5;

contract Bank {
    
   mapping(address => uint) balance;
   
   address owner;
   
   event depositDone(uint amount, address depositedTo);
   
   event transferMade(uint amount, address depositedTo, address depositedFrom);
   
   event withdrawDone(uint amount, address withdrawTo);
   
   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, "Balance not sufficient");
      balance[msg.sender]-=amount;
      msg.sender.transfer(amount);
      return balance[msg.sender];
      
      emit withdrawDone(amount, 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, "Dont send money to yourself");
       
       uint previousSenderBalance = balance[msg.sender];
       
       _transfer(msg.sender, recipient, amount);
       
       assert(balance[msg.sender] == previousSenderBalance - amount);
       
       emit transferMade(amount, msg.sender, recipient);
   }
   
   function _transfer(address from, address to, uint amount) private {
       balance[from] -= amount;
       balance[to] += amount;
   }
}
1 Like
function withdrawEther(uint _amount)public returns(uint){
     
      require(balance[msg.sender]>= _amount, "No sufficient balance in your wallet");
      uint lastBalance = balance[msg.sender];
      payable(msg.sender).transfer(_amount);
      balance[msg.sender]-=_amount;
      
      assert(balance[msg.sender]==lastBalance-_amount);
      emit etherWithdrawed(balance[msg.sender],msg.sender);
      return balance[msg.sender];
       
    }
1 Like
pragma solidity ^0.7.5;

contract Withdraw {
    
    mapping(address => uint) balance;
    
    event newDeposit(address indexed account, uint oldBalance, uint newBalance);
    event newWithdraw(address indexed account, uint oldBalance, uint newBalance);



    function deposit() public payable returns(uint){
        uint oldBalance;
        oldBalance = balance[msg.sender];
        balance[msg.sender]+= msg.value;

        assert(balance[msg.sender] >=0);
        
        emit newDeposit(msg.sender,oldBalance,balance[msg.sender]);
        return balance[msg.sender];
    }
    
    function getBalance() view public returns (uint) {
        return balance[msg.sender];
    }
    
    function withdraw(uint _amount) public  returns(uint){
        require(balance[msg.sender]>=_amount,"Not sufficient funds." );
        uint oldBalance;
        oldBalance = balance[msg.sender];
        balance[msg.sender] -= _amount;
        msg.sender.transfer(_amount);
        
        
        assert(balance[msg.sender] >=0);
        assert(balance[msg.sender] == oldBalance - _amount);
        
        emit newWithdraw(msg.sender,oldBalance,balance[msg.sender]);
        return balance[msg.sender];
    }
}
1 Like

My code:

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

I knew something was missing which was: balance[msg.sender] -= amount;

Overall, I’m happy with the attempt

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

contract EthBank {
    
    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);
        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 insufficient");
        require(msg.sender != recipient, "You can't send doe 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

Hi, Can someone help me out? The transfer part of my code works fine, but ‘getTransaction’ which should log the transaction to the government contract does not work. Everything is compiled right but I get error message…call to Financial.getTransaction errored: VM error: invalid opcode.

‘invalid opcode
call to financial.getTransaction errored
Debug the transaction to get more information’

pragma solidity 0.7.5;

import “./Ownable.sol”;

//External contract wants to pull transaction data form this(bankOwnable) contract//

interface FinancialInterface {
function addTransaction (address _from, address _to, uint _amount) external;

}

contract bank is Ownable {

FinancialInterface FinancialInstance = FinancialInterface(0xf8e81D47203A594245E36C48e151709F0C19fBe8);



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 onlyOwner returns (uint) {
    require(balance[msg.sender]>= amount);  
    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, "Insufficient balance");
    require(msg.sender != recipient, "Can not send funds to yourself");
    
    uint previousSenderBalance = balance[msg.sender];
    
    _transfer(msg.sender, recipient, amount);
    
    FinancialInstance.addTransaction(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;
    }
}

pragma solidity 0.7.5;

contract Financial {

struct Transaction {
    address from;
    address to;
    uint amount;
    uint txId;
    
}

Transaction[] transactionLog;

function addTransaction (address _from, address _to, uint _amount)external {
    transactionLog.push( Transaction(_from, _to, _amount, transactionLog.length));
} 

function getTransaction(uint _index)public view returns(address, address, uint) {
    return (transactionLog[_index].from, transactionLog[_index].to, transactionLog[_index].amount);
}

}

Thanks

1 Like

pragma solidity 0.7.5;

contract Bank {

mapping(address => uint) balance;
address owner;

event depositDone(uint amount, address depositedTo);
event amountTransfered(uint amount, address trasferedFrom, address trasferedTo);

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, "Balance not sufficent!" );
    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 recipeint, uint amount) public {
     require(balance[msg.sender] >= amount, "Balance not sufficent!" );
     require(msg.sender != recipeint);
     
     uint previousSenderBalance = balance[msg.sender];
     
     _transfer(msg.sender, recipeint, amount);
     
     
     
     assert(balance[msg.sender] == previousSenderBalance - amount);
     //event logs and further checks
     
     emit amountTransfered(amount, msg.sender, recipeint);
 }
 
 function _transfer(address from, address to, uint amount) private{
     balance[from] -= amount;
     balance[to] += amount;
 }

}

1 Like

Hey @NetworkP, hope you are well.

Could you please share your code properly in this way so i can review it properly? i have too many syntax issues if I copy the one you provide.

Carlos Z

Hello, thanks for getting back. However, I do not seem to have this feature as I use ‘openOffice’ and not MS Word. Do you know where it could be in openOffice? I’ve had a look but can not find it. If not, don’t worry. I’ll figure it out.

I found some info online. Sounds and looks long winded as this feature clearly isn’t built in to the openoffice program as with MS Word. I’ll sort something out. Thanks

function deposit() public payable returns(uint){
balance [msg.sender]+= msg.value;
require(balance[msg.sender]>=msg.value)
}

1 Like

Hi everyone,

Here’s my solution to the assignment:

//SPDX-License-Identifier: UNLICENSED
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, "You have insufficient balance.");
        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 is insufficient!");
        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;
    }
}

Let me know what you think of it. I’m pretty keen to hear your thoughts.

1 Like

My Solution for the Withdraw function:

function withdraw(uint amount) public returns(uint) {
require(balance[msg.sender] >= amount, “Not enough money for withdraw”);

    uint assertTransfer = balance[msg.sender];
    
    msg.sender.transfer(amount);
    
    assert(balance[msg.sender] == assertTransfer - amount);
    
    
}
1 Like

I added require to the withdraw function to check that the account making the withdraw has enough balance for the withdrawal amount. Then I added an additional function _withdraw to update the balance of the accountHolder.

pragma solidity 0.7.5;

contract Bank {
    
    mapping(address => uint) balance;
    address owner;
    
    event depositDone(uint amount, address indexed depositedTo);
    
    // event transfer assignment
    event transferWhybroke(uint amount, address indexed transferredTo, address indexed originAccount);
    
    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, "Bank robbery interrupted.");
        msg.sender.transfer(amount);
        _withdraw(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 don't have enough moneys XD");
        require(msg.sender != recipient, "Stop slapping yourself XD");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
        
        emit transferWhybroke(amount, recipient, msg.sender); // response to assignment
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
    
    function _withdraw(address accountHolder, uint amount) private {
        balance[accountHolder] -= amount;
    }
        
}
1 Like

Hello there is my solution to the assignement. Thanks for the feedback !

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

function getBalance() public view returns(uint) {
    uint updatedBalance = balance[msg.sender] -= amount; 
    return updatedBalance;
}
1 Like