Transfer Assignment

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

    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];
    }
1 Like

Hello, I was in a hurry the other day and didn’t realise that the icon is here. Anyway. My code is below. As mentioned, everything works except the Government function. It does not record the transfer. Thanks

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 payable; 
    
}

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{value: 1 ether}(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 payable {
        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);
    
    }

    function getBalance()public view returns(uint) {
    return address(this).balance;
    }
        
}
type or paste code here
1 Like
function withdraw(uint amount) public payable returns (uint) {
        require(balance[msg.sender] >= amount, "Not enough in balance");
        
        uint previousSenderBalance = balance[msg.sender];
        
        balance[msg.sender] -= amount;
        
        msg.sender.transfer(amount);
        
        emit withdrawDone(amount, msg.sender);
        
        return balance[msg.sender];
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
    }
1 Like

Hey @NetworkP, hope you are well.

Have you check that the address of the government contract is the same before you deploy the bank contract?

Carlos Z

Carlos, thanks for replying. That was the issue! I did not have the bank contract address correct, as I realised that each time we deploy, the contract address changes.

Thank you Brother :slightly_smiling_face:

1 Like
function transferFunds (uint amount) public returns (uint) {

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

uint previousBalance = balance [msg.sender];
msg.sender.transferFunds (amount);
assert (previousBalance == balance [msg.sender] + amount);
1 Like
pragma solidity 0.7.5;

contract Bank{
    
    mapping (address => uint) balance; 
    address owner ; 
    
    event movementDone (uint amount, address indexed depositedTo); 
    
    constructor() {
        owner = msg.sender;
    }
    
    modifier OnlyOwner (){
        require(msg.sender == owner); 
        _;
    }
    
    function deposit () public payable returns (uint){
        balance[msg.sender] += msg.value; 
        emit movementDone (msg.value , msg.sender);
        return (balance[msg.sender]); 
    }
    
    function withdraw (uint amount) public returns (uint){
        require (balance[msg.sender]>=amount); 
        msg.sender.transfer(amount); 
        balance[msg.sender] -= amount;
        emit movementDone(-amount, msg.sender);
        return (balance[msg.sender]); 
    }
    
    function getBal() public view returns (uint){
        return balance[msg.sender]; 
    }
    function transferR (address recipient, uint amount) public{
        require (balance[msg.sender]>=amount, "Not enough balance");
        require (msg.sender != recipient, "Don't transfer money to yourself !"); 
        uint preTransferSenderBal = balance[msg.sender]; 
        _transfer(msg.sender,recipient,amount);
        assert(balance[msg.sender] == preTransferSenderBal-amount);
    }
    
    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);


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

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

}
1 Like