Hello jon_m
Thank you for your detailed explanation.
Now I understand what is missing and why it has to be included!
I’ve adjusted the code accordingly.
Many Thanks!
Hello jon_m
Thank you for your detailed explanation.
Now I understand what is missing and why it has to be included!
I’ve adjusted the code accordingly.
Many Thanks!
Hey @alexhupe, @TRYLAST hope you guys are well.
It’s one of the syntax changes since solidity v0.8.0.
Carlos Z
Hi.
Re: https://github.com/filipmartinsson/solidity-0.7.5/blob/main/transfer-assignment-solution.sol
Doesn’t need the ‘withdraw function’ the ‘assert check’ at the end of it alike we have it in the ‘transfer function’ - for proper error handling?
assert(balance[msg.sender] == previousSenderBalance - amount);
thank you.
Yes, complete valid, probably filip did your forgot to add it, but off course is a good check point for the function
Carlos Z
function withdraw(uint value) public returns (uint) {
require(balance[msg.sender]>=value, "Balance not sufficient!");
msg.sender.transfer(value);
}
Checking the solution I realized that I forgot to include the code to update the balance after the withdraw:
balance[msg.sender] -= value;
Hey coders,
I have just completed Etherenum Smart Contract course here. I want to build a Crypto wallet app in flutter where users can deposit their any coin or token and withdraw. I want to do this for my Country wehre people struggle to buy crypto in an easy way.
Features
Please help me which course should I take to learn simple wallet app building or any simple wallet app template to learn the structure. Any recommendations would be highly appreciated.
Waiting coders…
Hey @TRYLAST, hope you are ok.
I think you should take a look at the Ethereum Dapp Programming course in the Academy, it focus on create the frontend for your dapp and how to connect it with web3 and the smart contract.
Carlos Z
Hi Guys,
I post below my transfer Assigment solution.
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
contract PlikiPliki {
mapping(address=>uint) balance;
address owner;
event depositDone(uint amount, address indexed depositedTo);
event transferMade(address from, address to, uint amounTransered);
modifier onlyOwner{
require(owner == msg.sender);
_;
}
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 withdraw(uint amount) public returns(uint){
uint msgContractBalance = getBalance();
require(amount <= msgContractBalance);
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public{
//some important checks
require(msg.sender != recipient);
require(balance[msg.sender] >= amount);
uint balanceBeforeTransfer = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
//some other contract logic;
assert(balanceBeforeTransfer == balance[msg.sender] + amount);
emit transferMade(msg.sender, recipient, amount);
}
function _transfer(address from, address to, uint amount) private{
balance[from] -= amount;
balance[to] += amount;
}
}
pragma solidity 0.7.5;
contract Bank{
mapping(address => uint) balance;
function Deposit() public payable{
balance[msg,sender]+= msg.value;
}
function withDraw(amount) public {
require(balance[msg.sender] >= amount, "insufficient balance");
msg.sender.transfer(amount);
}
}
Hi, All!
Here are two functions for transfer assignment.
function deposit() public payable returns (uint){
balance[msg.sender] += msg.value;
return balance[msg.sender];
}
function withdraw(uint amount) public isOwner returns (uint){
require(balance[msg.sender] >= amount, "Limit is exceeded");
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
return balance[msg.sender];
}
function withdraw(uint amount) public returns(uint){
require(balance[msg.sender] >= amount);
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
}
pragma solidity 0.7.5;
contract Bank {
mapping (address=>uint) balance;
address owner;
event depositDone(address indexed depositTo, uint amount);
event balanceTransferred(address indexed sender, address indexed recipient,uint amount);
modifier onlyOwner{
require(msg.sender == owner);
_;
}
constructor(){
owner = msg.sender;
}
function deposit() public payable returns (uint){
balance[msg.sender] += msg.value;
emit depositDone(msg.sender, msg.value);
return balance[msg.sender];
}
function withdraw(uint amount) public returns(uint) {
require(balance[msg.sender] >= amount,"Can't withdraw more than what has been deposited");
msg.sender.transfer(amount);
balance[msg.sender] -= amount; // adjust the balance after withdraw
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, "No point on transfering funds to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender,recipient,amount);
emit balanceTransferred(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 etherBank1 {
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(amount <= balance[msg.sender], "You stored less eth");
uint previousBalance = balance[msg.sender];
msg.sender.transfer(amount);
_withdraw(msg.sender, amount);
assert(balance[msg.sender] == previousBalance - amount);
}
function _withdraw (address from, uint amount) private {
balance[from] -= 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];
_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;
}
}
function withdraw(uint amount) public returns (uint){
require(amount<=balance[msg.sender], "Not enough funds");
uint oldBalance = balance[msg.sender];
payable(msg.sender).transfer(amount);
assert(balance[msg.sender]== oldBalance - amount);
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];
}
Hi all!
I tested this out and it worked as intended.
Just have one simple question, I see in the solution you subtracted the balance first then transfered it. I did the opposite. Does this cause for problems or is it okay?
pragma solidity 0.7.5;
contract transferAssignment {
/*
IVAN ON TECH - Transfer Assignment
1) Make sure users can not withdraw more than their balance
2) Correctly adjust the balance of the user after a withdrawal
Necessary steps:
- deposit (record which userAddress sent what balance)
- withdrawal (only balance that corelates to msg.sender)
- error events (if user does not have available balance to withdraw, ...)
*/
//mapping addresses to their balance
mapping(address => uint) balance;
//events to log whats happening
event depositConfirmed(uint balance, uint amount, address indexed depositedFrom);
event withdrawalConfirmed(uint balance, uint amount, address indexed withdrawTo);
//Deposit to smart contract
function deposit() public payable returns(uint){
//taking note of previous balance for assert
uint prev = balance[msg.sender];
//add deposit to balance
balance[msg.sender] += msg.value;
//make sure the balance got added
//new balance should be equal to prev + amount
assert(balance[msg.sender] == prev + msg.value);
//log succesfull deposit
emit depositConfirmed(balance[msg.sender], msg.value, msg.sender);
//output current balance
return balance[msg.sender];
}
//Withdraw from smart contract
function withdraw(uint amount) public returns(uint){
//check if the withdraw balance corresponds to users balance
require(balance[msg.sender] >= amount, "Balance insufficient");
//for assert
uint prev = balance[msg.sender];
//actually sending the eth
msg.sender.transfer(amount);
//set balance to new balance
balance[msg.sender] -= amount;
//make sure the balance is correct after transfer
//New balance should be equal to prev - amount
assert(balance[msg.sender] == prev - amount);
//log succesfull withdrawal
emit withdrawalConfirmed(balance[msg.sender], amount, msg.sender);
//output current balance
return balance[msg.sender];
}
//Get balance of sender account
function getBalance() view public returns(uint){
return balance[msg.sender];
}
}
My Code for the transfer assignement…
pragma solidity 0.7.5;
//SPDX-License-Identifier: UNLICENSED
contract Bank {
mapping(address => uint) balance;
//address owner = 0x803A219F1a575BF9D3E5874306DFB16352017c83;
address owner;
event depositDone (uint amount, address indexed receiver);
modifier onlyOwner {
require(msg.sender == owner);
_; //run the function (see addBalance below)
}
modifier costs (uint price) {
require(msg.value >= price);
_;
}
// thought about using a modifier in the withdrawal call to check the balance, maybe implement this later..
modifier hasFunds (uint amount) {
require(balance[msg.sender] >= amount);
_;
}
constructor(){
owner = msg.sender;
}
function deposit() public payable returns (uint) {
balance[msg.sender] += msg.value; // this is just for us to keep track of who deposited what, the balance of the contract is
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function getBalance() public view returns (uint){
return balance[msg.sender];
}
function withdraw(uint amount) payable public {
require(balance[msg.sender] >= amount, "Insufficient balance");
msg.sender.transfer(amount); //unit is wei - safest way to transfer is with the transfer function, built in error checking
balance[msg.sender] -= amount;
}
}
Hey @BallzOfCheeze, hope you are well.
At this level, there will is no issue by doing it like that, but it might represent an issue with a reentrancy attack, you can learn more about this “good security practices” in the Ethereum Smart Contract Security course that we offer .
Overall nice solution man
Carlos Z