pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender]= balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender]=balance[msg.sender].sub(amount);
payable(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]=balance[from].sub(amount);
balance[to]=balance[to].add(amount);
}
}
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safe_math.sol";
contract Bank is Ownable {
using SafeMath for uint;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(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] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
This is it.
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(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] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";
contract Bank is Ownable {
using SafeMath for uint;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender]=balance[msg.sender].sub(amount);
payable(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] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
This was pretty straight forward,
pragma solidity 0.8.0;
pragma abicoder v2;
import "./SafeMath.sol";
contract Bank {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] =balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(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] = balance[from].sub (amount);
balance[to] = balance[to].add (amount);
}
}
Slamminâ my code in here before cruising to work. As @Moneymonty said, this was pretty straightforward. Imported SafeMath.sol, declared the usage in the contract, and then replaced the half-dozen implementations or so. Very nice.
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender].sub(amount);
payable(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.sub(amount));
}
function _transfer(address from, address to, uint amount) private {
balance[from].sub(amount);
balance[to].add(amount);
}
}
Cheers yâall!
Hey man, @Lane11
I had the same issue before, try to use an âhttpâ connection instead of an âhttpsâ connection on your browser or allow mixed connections. It should be in the browser settings. Had this problem for a while now, Hope this helps.
I have a question, my getBalance function is showing an error, after I made changes to this assignment.
Hereâs my assignment, I tried implementing the âchecks - effects - interactions patternâ as well. If I have mistakes or errors (probably there is), please point them out! Many Thanks!
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";
contract Bank is Ownable {
using SafeMath for uint;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
uint toTransfer = balance[msg.sender];
balance[msg.sender] = balance[msg.sender].sub(amount);
(bool success,) = msg.sender.call{value: toTransfer}("");
if (!success){
balance[msg.sender] = toTransfer;
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.sub(amount));
}
function _transfer(address from, address to, uint amount) private {
balance[from] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
Hey @CryptoXyz
You have to close the curly brackets in your function withdraw
.
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
uint toTransfer = balance[msg.sender];
balance[msg.sender] = balance[msg.sender].sub(amount);
(bool success,) = msg.sender.call{value: toTransfer}("");
if (!success){
balance[msg.sender] = toTransfer;
return balance[msg.sender];
}
<<--- missing curly brackets.
Cheers,
Dani
HAHAHAHAHA I feel so dumb! LOL
Thanks for pointing this out @dan-i, I kept thinking whatâs wrong with my getBalance function HAHAHA. Youâre a life saver!
Here is my solution:
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].add(amount);
payable(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] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
pragma solidity 0.8.0;
pragma abicoder v2;
import "./ownable.sol";
import "./SafeMath.sol";
contract Bank is Ownable {
using SafeMath for uint;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(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] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
pragma solidity 0.8.0;
pragma abicoder v2;
import â./ownable.solâ;
import â./safemath.solâ;
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
function deposit() public payable returns (uint) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(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] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
Hey @Paul_Kwok
Good job with your code.
Please read this faq about how to correctly post code in the forum: FAQ - How to post code in the forum
Regards,
Dani
pragma solidity 0.7.5;
import "./Ownable.sol";
import "./Destroyable.sol";
import "./Safemath.sol";
contract BankAccount is Ownable, Destroyable{
using SafeMath for uint256;
mapping(address => uint) balance;
//Events for transferring and depositing:
event balanceTransferred(address indexed recipient, uint amount);
event depositDone(uint amount, address indexed sender);
function deposit() public payable returns (uint) {
require(msg.value > 0, "Must be deposit more than 0!");
balance[msg.sender] = balance[msg.sender].add(msg.value);
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
//This function withdraws from contract to address:
function withdraw(uint amount) public returns (uint) {
require(balance[msg.sender] >= amount, "Insufficient funds!");
require(amount > 0, "Must withdraw more than 0!");
uint initialBalance = balance[msg.sender];
balance[msg.sender] = balance[msg.sender].sub(amount);
msg.sender.transfer(amount);
return balance[msg.sender];
assert(initialBalance > balance[msg.sender]);
}
//This shows the current balance of the address selected:
function getBalance() public view returns (uint) {
return balance[msg.sender];
}
//This function transfers balances from an input beside the function button, units in wei:
function transferBalance(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Insufficient amount!");
require(msg.sender != recipient, "Cannot send to yourself!");
emit balanceTransferred(recipient, amount);
uint previousBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousBalance.sub(amount));
}
//This is the internal transfer function:
function _transfer (address from, address to, uint amount) private {
balance[from] = balance[from].sub(amount);
balance[to] = balance[to].add(amount);
}
}
Hello, hereâs my code (I commented out the original addition statements).
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint256) balance;
address[] customers;
event depositDone(uint256 amount, address indexed depositedTo);
function deposit() public payable returns (uint256) {
balance[msg.sender] = balance[msg.sender].add(msg.value);
//balance[msg.sender] += msg.value;
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint256 amount) public onlyOwner returns (uint256){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
//balance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}
function getBalance() public view returns (uint256){
return balance[msg.sender];
}
function transfer(address recipient, uint256 amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");
uint256 previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
function _transfer(address from, address to, uint256 amount) private {
balance[from] = balance[from].sub(amount);
//balance[from] -= amount;
balance[to] = balance[to].add(amount);
//balance[to] += amount;
}
}
Hello, here is a modified bank contract.
Thanks and good luck everyone for your studies!
Matt
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
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 onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
payable(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;
}
}
pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";
contract Bank is Ownable {
using SafeMath for uint256;
mapping(address => uint) balance;
address[] customers;
event depositDone(uint amount, address indexed depositedTo);
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 onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
payable(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;
}
}
Here is my updated Bank contract! Cheers!
Oh thanks Dani, youâre right
back to the drawing board
Matt