Transfer Assignment

pragma solidity 0.7.5;

contract BankKyle {

mapping (address => uint) balance;
address owner;
event depositDone(uint amount, address indexed depositedTo);
event balanceTransfered(uint amount, address indexed transferedFrom, address indexed transferedTo);

modifier onlyOwner {
    
    require(msg.sender == owner, "function restricted to 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 (amount <= balance[msg.sender], "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 insufficient");
    require(msg.sender != recipient, "Transfer to self not allowed");
    
    uint previousSenderBalance = balance[msg.sender];
    
    _transfer(msg.sender, recipient, amount);

    assert(balance[msg.sender] == previousSenderBalance - amount);
    emit balanceTransfered(amount, msg.sender, recipient);
}

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

}

1 Like

To make sure the sender has enough balance I used the require function and after that modify the balance.

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

1 Like

I don’t really like this smart contract. It’s allowing users to just deposit and withdraw from an owner’s wallet? I think it’s a little strange. Why would somebody do this?

 // Allow end user to withdraw only what they've deposited. 
    function withdraw(uint amount) public payable returns(uint) {
        // check balance of msg.sender is sufficient.
        require(balances[msg.sender] >= amount, "Error: Not enough ether deposited. ");
        
        // Adjust balance
        balances[msg.sender] -= amount;
        msg.sender.transfer(amount);
        return balances[msg.sender];
    }
1 Like

Hey @jrdefideveloper

The function you posted allows a user to only withdraw ether that the user itself deposited in the contract.
If I deposit one ether in your contract and you deposit 5 ether, you don’t want me to withdraw also your tokens :slight_smile:

In the function you’ve posted, I will be able to withdraw 1 ether, while you will be able to withdraw 5 ether.

Cheers,
Dani

Hey @Lane11

You mean require(balance[msg.sender] >= amount);?

Its a conditional that verify if the balance of msg.sender have enough or equal balance to the amount that desire to withdraw.

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

Carlos Z.

1 Like

Yes but why would anyone deposit into a smart contract for no reason? There should be incentives for moving their money into a contract

Hi @jrdefideveloper

This is a lesson about the transfer method, so Filip explained you how to use .transfer.
Of course you will then code smart contracts that actually do something meaningful :slight_smile:

Cheers,
Dani

Just the withdraw function:

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

Something needed to be returned because of the function header, but not sure why. It doesn’t seem to be used except in debugging?

1 Like
    function withdraw(uint amount) public returns (uint){
        
        require(balance[msg.sender] >= amount, "Not enough balance!");
        
        uint prevBalance = balance[msg.sender];
        
        msg.sender.transfer(amount); //amount transferred in wei
        
        balance[msg.sender] -= amount;
        
        assert(balance[msg.sender] == prevBalance - amount);
    }
1 Like

No, i think about msg.sender.transfer(amount). I did not use it and the code works.

…
function withdraw(uint _amount) public returns (uint) {
require(balance[msg.sender] >= _amount, “You don’t have sufficient funds to withdraw.”);

    msg.sender.transfer(_amount);
    balance[sendTo] -= _amount;
    
    return balance[sendTo]; 
}

…

1 Like

pragma solidity 0.7.5;

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

address owner;

event confirmdeposit(uint amount,address depositedto);

modifier onlyowner {

_;

}

constructor(){
owner =msg.sender;
}
function DEPOSIT() public payable returns(uint){
balance[msg.sender]+=msg.value;
emit confirmdeposit(msg.value,msg.sender);
return balance[msg.sender];

}
function withdraw(uint amount)public payable 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 reciepent,uint amount) public {
require(balance[msg.sender]>=amount);
require(msg.sender !=reciepent);

uint pervioussenderbalance=balance[msg.sender];

 transferexecutive(msg.sender,reciepent,amount);
 
 assert(balance[msg.sender]==pervioussenderbalance - amount);
}
function transferexecutive(address originating,address reaching,uint amount)private {
 balance[originating]-= amount;
 balance[reaching] += amount;
}

}

1 Like

pragma solidity 0.7.5;

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

address owner;

event confirmdeposit(uint amount,address depositedto);

modifier onlyowner {

_;

}

constructor(){
owner =msg.sender;
}
function DEPOSIT() public payable returns(uint){
balance[msg.sender]+=msg.value;
emit confirmdeposit(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 reciepent,uint amount) public {
require(balance[msg.sender]>=amount);
require(msg.sender !=reciepent);

uint pervioussenderbalance=balance[msg.sender];

 transferexecutive(msg.sender,reciepent,amount);
 
 assert(balance[msg.sender]==pervioussenderbalance - amount);
}
function transferexecutive(address originating,address reaching,uint amount)private {
 balance[originating]-= amount;
 balance[reaching] += amount;
}

}

in the other answer i did
balance[msg.sender]+=(amount);
instead of
balance[msg.sender]-=(amount);
in other to get the balance after all the withdrawals

1 Like

Hey @stevesirag

Actually there is no need to return the balance here :slight_smile:
Of course that’s not a rule, it really depends on what you want to do with the returned balance.

Cheers,
Dani

Hey @mervxxgotti

The assert statement is not necessary in this case, it would just consume more gas than needed.
Keep in mind that a .transfer method would fail is something goes wrong.

Cheers,
Dani

1 Like
function withdraw(uint amount) public returns (uint){
        require(balance[msg.sender]>=amount, "Impossible");
        balance[msg.sender] -= amount;
        msg.sender.transfer(amount);
        
    }
    
    function displayBalance() 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 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 widthdraw(uint amount) public payable returns (uint) {
        
        // check balance of sender
        require(balance[msg.sender] >= amount);
        
        // adjust balance of sender
        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 {
        
        // check balance of msg.sender
        require(balance[msg.sender] >= amount, "Balance not sufficient");
        // don't allow msg.sender to send funds to themselves
        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
function withdraw(uint amount) public {
        
        //Check balance mapping 
        //Use error handling function
        require(balance[owner] >= amount);
        balance[msg.sender] -= amount;
        msg.sender.transfer(amount);
    }
2 Likes

thank you! What do you mean .transfer would fail if something goes wrong? what do you mean by “something goes wrong”?