Transfer Assignment

Here is my solution. Hope this is correct.

// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
contract Bank {

    address owner;

    mapping(address => uint) balance;

    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 getBalance() public view returns (uint) {
        return balance[msg.sender];
    }

    function transfer(address receipient, uint _transferAmount) public returns(uint) {
        balance[msg.sender] -= _transferAmount;
        balance[receipient] += _transferAmount;
        return balance[receipient];
    }

    function withdraw(uint amount) public payable returns (uint)   {
        require (balance[msg.sender] >= amount, "You don't have enough funds!");
        uint previousBalance = balance[msg.sender];
        balance[msg.sender] -= amount;
        msg.sender.transfer (amount);
        assert (balance[msg.sender] == previousBalance - amount);
        return balance[msg.sender];
    }

}

1 Like

function withdraw(uint amount) public returns(uint) {
require(balance[msg.sender] >= amount, “Unable to withdraw more than your balance”);
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
return balance[msg.sender];

}

1 Like
pragma solidity 0.8.24;

contract Bank {

    struct User {

        address id;

        uint balance;

    }

    mapping(address => User) users;

    address owner;

    event depositDone(uint amount, address indexed depositedTo);

    constructor () {

        owner = msg.sender;

    }

    function deposit() public payable returns (uint) {

        users[msg.sender].balance += msg.value;

        emit depositDone(msg.value, msg.sender);

        return users[msg.sender].balance;

    }

    function withdraw(uint amount) public {

        require(users[msg.sender].balance >= amount, "Not enough balance!");

       

        address payable toSend = payable(contract address);

        toSend.transfer(amount);

        msg.sender.transfer(amount);

       

        users[msg.sender].balance -= amount;

    }

    function updateBalance(address id, uint balance) public {

        users[id].balance = balance;

    }

    function getBalance(address id) view public returns (uint) {

        return users[id].balance;

    }

}
1 Like
    function withdraw(uint amount) public returns (uint) {
        require(amount <= balance[msg.sender], "You cannot withdraw more than your balance");
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
    }

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

        //address payable toSend = "address";

        //toSend.transfer(amount);

        require(msg.sender.balance >= amount, "Not enought Fund");

        balance[msg.sender] -= amount; // update the balance of the user

        payable(msg.sender).transfer(amount); // msg.sender already a payable address!

}

pragma solidity ^0.7.5;

contract Bank{

mapping(address => uint) balance;

address owner;

modifier onlyOwner{

  require(msg.sender == owner);

  _;  

}

event depositeDone(uint amount, address indexed depositedTo);

constructor(){

    owner = msg.sender;

}

// 22133 gas / 21239

function deposite() public payable returns(uint){

        emit depositeDone(msg.value, msg.sender);

        balance[msg.sender] += msg.value;

        return balance[msg.sender];

}

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

        //address payable toSend = "address";

        //toSend.transfer(amount);

        require(msg.sender.balance >= amount, "Not enought Fund");

        balance[msg.sender] -= amount; // update the balance of the user

        payable(msg.sender).transfer(amount); // msg.sender already a payable address!

}

function getBalance() public view returns(uint){

    return balance[msg.sender];

}

function transfer(address recipient, uint amount) public {

    // check balance of msg.sender

    require(balance[msg.sender]>=amount,"Not enought Fund");

    require(msg.sender != recipient,"Not to same wallet please");

    uint previousSenderBalance = balance[msg.sender];

    _transfer(msg.sender, recipient, amount);

    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;

}

}