I don’t think this is right.
When you withdraw the amount you don’t update the balance
state variable.
Ah yes thank you, you are right my balance wont change after withdrawing. Thats a big mistake
I only needed to add balance[msg.sender] -= amount;
to the withdraw function now it works fine.
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(address => uint) balance;
event depositDone(uint amount, address to);
// function to deposit funds
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 payable returns(uint){
require(balance[msg.sender]>=amount, "not enough funds");
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
}
function getBalance() view public returns (uint) {
return balance[msg.sender];
}
}
Withdraw function
function withdraw(uint amount) public returns(uint){
require(balance[msg.sender] >= amount, "Balance not sufficient");
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
return balance[msg.sender];
}
function withdraw(uint amount) public retruns (uint){
require(balance[msg.sender] >= amount, "amount exceeds your deposite!");
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
}
pragma solidity 0.7.5;
contract Bank {
mapping(address => uint) balance;
function deposit() public payable returns(uint) {
balance[msg.sender] += msg.value;
return balance[msg.sender];
}
function withdraw(uint _amount) public returns(uint) {
require(_amount <= balance[msg.sender], "Not enough balance to withdraw");
msg.sender.transfer(_amount);
balance[msg.sender] -= _amount;
return balance[msg.sender];
}
function getBalance() public view returns(uint) {
return balance[msg.sender];
}
pragma solidity 0.8.13;
contract Bank {
mapping(address => UserAccount) accounts;
struct UserAccount {
address userAddress;
uint balance;
}
event balanceUpdated (address userAddress, uint balance);
/**
* User deposits money in their account.
*/
function deposit () public payable {
accounts[msg.sender].balance += msg.value;
emit balanceUpdated(msg.sender, accounts[msg.sender].balance);
}
/**
* User withdraws money from their account.
*/
function withdraw (uint amount) public {
require (amount > 0, "Cannot withdraw a zero amount.");
require (accounts[msg.sender].balance >= amount, "User does not have enough balance");
accounts[msg.sender].balance -= amount;
payable(msg.sender).transfer(amount);
emit balanceUpdated(msg.sender, accounts[msg.sender].balance);
}
/**
* User requests its own Balance.
*/
function getBalance() public view returns (uint) {
return getBalanceOf(msg.sender);
}
/**
* Requests Balance of any account.
* (Should be restricted in production or made private)
*/
function getBalanceOf(address userAddress) public view returns (uint) {
return accounts[userAddress].balance;
}
}
function withdraw(uint amount) public returns (uint){
require(balance[msg.sender] >= amount, "Balance infficient");
msg.sender.transfer(amount);
}
You might need to convert msg.sender
in a payable
address, check the solution above yours
Carlos Z
Note to Admins: there was no source code available to copy into Remix. Please provide source code so that students do not have to recreate the code from scratch.
Below, I have altered the code to allow for only withdrawing the amount that the address has deposited. Ways to improve upon this code is to include a statement that will eliminate inputs that are negative or zero.
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 {
require(balance[msg.sender] <= amount, "Cannot send more than your balance.");
msg.sender.transfer(amount);
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];
}
}
Hello Filip, this is my solution but I have the following problem:
When I deposit money it is in ether so amount * 10^18 but when I withdraw it is an uint representing wei. I would like to have a common currency convertion being done on the network.
Like receiving and sending USDT, is it possibly?
pragma solidity 0.8.13;
contract bankContract{
address owner;
modifier onlyOwner {
require(msg.sender == owner,"Not the owner");
_;
}
modifier costs(uint amount) {
require(balance[msg.sender] >= amount );
_;
}
event depositDone(uint amount, address depositedTo);
constructor(){
owner = msg.sender;
}
mapping(address => uint) balance;
function deposit() public payable returns (uint){
balance[msg.sender] += msg.value;
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function getBalance(address _address) public view returns (uint){
return balance[_address];
}
function withdraw(uint amount) public costs(amount) returns(uint){
uint previousBalance = balance[msg.sender];
balance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
assert(balance[msg.sender] == previousBalance - amount);
assert(balance[msg.sender] > 0);
return balance[msg.sender];
}
}
Hello Everyone,
This is my solution but I have the following problem:
When I deposit money it is in ether so amount * 10^18 but when I withdraw it is an uint representing wei. I would like to have a common currency convertion being done on the network.
Like receiving and sending USDT, is it possibly?
pragma solidity 0.8.13;
contract bankContract{
address owner;
modifier onlyOwner {
require(msg.sender == owner,"Not the owner");
_;
}
modifier costs(uint amount) {
require(balance[msg.sender] >= amount );
_;
}
event depositDone(uint amount, address depositedTo);
constructor(){
owner = msg.sender;
}
mapping(address => uint) balance;
function deposit() public payable returns (uint){
balance[msg.sender] += msg.value;
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function getBalance(address _address) public view returns (uint){
return balance[_address];
}
function withdraw(uint amount) public costs(amount) returns(uint){
uint previousBalance = balance[msg.sender];
balance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
assert(balance[msg.sender] == previousBalance - amount);
assert(balance[msg.sender] > 0);
return balance[msg.sender];
}
}
The next course “Ethereum Smart Contract Programming 201” or " Build a DEX On Ethereum" will teach you how to use interfaces for other contracts such has USDT, all balances of this erc20 contracts are represented on uint256
type, therefore they are represented also in wei, 1 USDT = 1 * 10 ^ 18
in the same way that 1 ether is 1 * 10 ^ 18 in wei.
You can do the conversions of this units but is not recommended because you could run into undesirable issues, the convertion commonly is made by the frontend dapp of your contract, like depositing 1 USDT, internally the frontend will convert this 1 USDT on his wei representation for the contract to understand the exact amount.
Carlos Z
Thanks Carlos!
I’m looking forward for the next course to learn how to interact with ERC20 but one step at a time.
function withdraw (uint amount) public returns (uint){
require (balance[msg.sender] <= amount "Not enough money");
msg.sender.transfer(amount);
return balance[msg.sender];
function withdraw(uint amount) public returns (uint) {
// error handling with response
require(balance[msg.sender] >= amount, "Insufficient funds to complete transaction");
msg.sender.transfer(amount);
function withdraw(uint amount) public returns (uint) {
require(balance[msg.sender] >= amount, "Insufficient balance to withdraw");
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
return balance[msg.sender];
}
I see that the solution has balance[msg.sender] -= amount; above msg.sender.transfer(amount);
Just curious if the order matters or not? I’m guessing best practice would be for me to switch the two lines around. Thanks!
function withdraw(uint amount) public returns (uint){
require(balance[msg.sender] >= amount, "Insufficient Balance");
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
return balance[msg.sender];
}
Added these lines of code for the withdraw function:
function withdraw(uint amount) public payable returns (uint){
require(balance[msg.sender] >=amount, "Balance not sufficient");
uint previousReceiverBalance = balance[msg.sender];
balance[msg.sender] -= amount;
assert(balance[msg.sender] == previousReceiverBalance - amount);
msg.sender.transfer(amount);
return balance[msg.sender];
}
Not sure if I needed these 2 lines in there, but it worked. Can’t withdraw more than what’s in the individual wallet balance, the balance function updates correctly, and the account field updates the ether amount correctly. Let me know if I shouldn’t have these two lines in there.
uint previousReceiverBalance = balance[msg.sender];
assert(balance[msg.sender] == previousReceiverBalance - amount);
Here is the full code underneath:
pragma solidity 0.7.5;
contract Bank {
//storage - permanent storage
//memory - temporary storage
//calldata - readonly
//strings, arrays, mappings, structs
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 payable returns (uint){
require(balance[msg.sender] >=amount, "Balance not sufficient");
uint previousReceiverBalance = balance[msg.sender];
balance[msg.sender] -= amount;
assert(balance[msg.sender] == previousReceiverBalance - 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 not sufficient");
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;
}
}
Please let me know if I need to correct anything. Thanks.
function withdraw(uint amount) public returns (uint) {
require(balance[msg.sender] >= amount, "Insufficient Funds: Please enter withdraw amount less than your current balance");
msg.sender.transfer(amount);
}
ezpz