1

I have a scenario of deposit the amount on a particular address and keep the record of the total amount deposited at this address. The same way one can also withdraw the amount from this address and total should be deducted the withdrawn amount for that particular address.

Here, twick is there can be multiple accounts so there can be multiple addresses and we need to store final amount(deposit/withdraw) account wise. Here we are storing just random amount, not ether.

So my questions are

  1. To store the accounts we need to use the mapping but how to calculate the amount(deposit/withdraw) by using mapping?

  2. can we use the array here?

Thanks

Edit the below part

My program

pragma solidity ^0.4.18;

contract Banking {     
  uint deposit;       
  uint amount;       
  mapping(address => uint) accountBalance;       

  function setDeposit(uint amt)
  {
   uint new_deposit = deposit + amt;
   accountBalance[msg.sender] = new_deposit;
   deposit += amount;
  }
  function getAmount() constant returns (uint)
  {
    return(accountBalance[msg.sender]);
  }
  function withdraw(uint amt)
  {
    uint withdrawAmount = amt;
    uint newBalance = accountBalance[msg.sender] - withdrawAmount;
    accountBalance[msg.sender] = newBalance;
  }
}
2
  1. Use mapping (address => uint256) accountBalance; where an address is obviously the users' address and uint256 is the amount that the users has as balance. When a user sends ether to your contract update the amount by uint256 oldBalance = accountBalance[msg.sender]; uint256 newBalance = msg.value + oldBalance; accountBalance[msg.sender] = newBalance; Note that I have not thought about re-entrancy or over/underflow of the uint values.

  2. Using an array is not optimal but you could indeed use an array for this. For instance, you could create an array of structs which have the address and balance of the user. But again, this isn't optimal.

  • Thanks Gabe for your answer, want to ask you one more thing sorry I am the beginner. When depositing the amount we add the amount and store it in accountBalance[msg.sender] but when withdraw from the same account, how are we going deducted from that address? – urjit on rails Mar 20 '18 at 14:15
  • No problem. you would then need to retrieve the current balance, subtract the amount and save it back into the mapping. require(accountBalance[msg.sender] >= amountWithdrawal); This checks if the user has enough to withdraw the amount wanted, afterward, remove the balance from his account uint256 newBalance = accountBalance[msg.sender] - amountWithdrawal; accountBalance[msg.sender] = newBalance; After this you could send the balance to the user, like so msg.sender.transfer(amountWithdrawal); If this has answered your question you could set this as the correct answer. – Gabe Mar 20 '18 at 15:16
  • Thanks Let me know If I need improvement in my below functions function setDeposit(uint amt) { uint new_deposit = deposit + amt; accountBalance[msg.sender] = new_deposit; deposit += amount; } function getAmount() constant returns (uint) { return(accountBalance[msg.sender]); } function withdraw(uint amt) { uint withdrawAmount = amt; uint newBalance = accountBalance[msg.sender] - withdrawAmount; accountBalance[msg.sender] = newBalance; }} – urjit on rails Mar 21 '18 at 13:36
  • Could you add it as an update to your original post within code blocks to make it more readable? – Gabe Mar 21 '18 at 14:08
1

This feels very similar to ERC20 what approve() and transferFrom() functions do.

Please have a look at

This function lets multiple spenders use the tokens in the limit specified.

/// @dev Sets approved amount of tokens for spender. Returns success.
/// @param _spender Address of allowed account.
/// @param _value Number of approved tokens.
function approve(address _spender, uint256 _value) returns (bool success) {
    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
}

This function allows users spend the approved tokens within the limits for each of them. Multiple users can be approved to spend from the same account.

/// @dev Allows allowed third party to transfer tokens from one address to another. Returns success.
/// @param _from Address from where tokens are withdrawn.
/// @param _to Address to where tokens are sent.
/// @param _value Number of tokens to transfer.
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
    if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {
        balances[_to] += _value;
        balances[_from] -= _value;
        allowed[_from][msg.sender] -= _value;
        Transfer(_from, _to, _value);
        return true;
    }
    else {
        return false;
    }
}

Code was taken from the StandardToken.sol part of the SingularDTV contracts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.