Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix truffle config with default ganache-cli settings #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,183 changes: 2,183 additions & 0 deletions build/contracts/Migrations.json

Large diffs are not rendered by default.

6,707 changes: 6,707 additions & 0 deletions build/contracts/SimpleBank.json

Large diffs are not rendered by default.

133 changes: 76 additions & 57 deletions contracts/SimpleBank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,113 @@
pragma solidity >=0.5.16 <0.9.0;

contract SimpleBank {

/* State variables
*/


// Fill in the visibility keyword.
// Hint: We want to protect our users balance from other contracts
mapping (address => uint) balances ;

// Fill in the visibility keyword
// Hint: We want to create a getter function and allow contracts to be able
// to see if a user is enrolled.
mapping (address => bool) enrolled;

// Let's make sure everyone knows who owns the bank, yes, fill in the
// appropriate visilibility keyword
address owner = msg.sender;


// Users balances, protected from other contracts.
mapping(address => uint) private balances;

// Enrolled users, allowed from other contracts to view.
mapping(address => bool) public enrolled;

// Who owns the bank.
address public owner = msg.sender;

/* Modifiers - better validations.
*/

// @notice Make sure only enrolled users can call functions.
modifier onlyEnrolled() {
require(
enrolled[msg.sender] == true,
"Only enrolled users can call this function"
);
_;
}

// @notice Make sure a user does not enroll twice.
modifier notEnrolled() {
require(enrolled[msg.sender] == false, "User already enrolled.");
_;
}

/* Events - publicize actions to external listeners
*/

// Add an argument for this event, an accountAddress
event LogEnrolled();

// Add 2 arguments for this event, an accountAddress and an amount
event LogDepositMade();
event LogEnrolled(address indexed accountAddress);

event LogDepositMade(address indexed accountAddress, uint amount);

// Create an event called LogWithdrawal
// Hint: it should take 3 arguments: an accountAddress, withdrawAmount and a newBalance
event LogWithdrawal();
event LogWithdrawal(
address indexed accountAddress,
uint withdrawAmount,
uint newBalance
);

/* Functions
*/

// Fallback function - Called if other functions don't match call or
// sent ether without data
// Typically, called when invalid data is sent
// Added so ether sent to this contract is reverted if the contract fails
// otherwise, the sender's money is transferred to contract
function () external payable {
// Set the owner to the creator of this contract.
constructor() {
owner = msg.sender;
}

// Fallback function - returns ether if something's wrong with the call.
fallback() external payable {
revert();
}

// Handle empty calldata.
receive() external payable {
}

/// @notice Get balance
/// @return The balance of the user
function getBalance() public returns (uint) {
// 1. A SPECIAL KEYWORD prevents function from editing state variables;
// allows function to run locally/off blockchain
// 2. Get the balance of the sender of this transaction
function getBalance() public view returns (uint) {
return balances[msg.sender];
}

/// @notice Enroll a customer with the bank
/// @return The users enrolled status
// Emit the appropriate event
function enroll() public returns (bool){
// 1. enroll of the sender of this transaction
function enroll() public notEnrolled returns (bool) {
address sender = address(msg.sender);
enrolled[sender] = true;
emit LogEnrolled(sender);
return enrolled[sender];
}

/// @notice Deposit ether into bank
/// @return The balance of the user after the deposit is made
function deposit() public returns (uint) {
// 1. Add the appropriate keyword so that this function can receive ether

// 2. Users should be enrolled before they can make deposits

// 3. Add the amount to the user's balance. Hint: the amount can be
// accessed from of the global variable `msg`

// 4. Emit the appropriate event associated with this function

// 5. return the balance of sndr of this transaction
function deposit() public payable onlyEnrolled returns (uint) {
address sender = address(msg.sender);
balances[sender] += msg.value;
emit LogDepositMade(sender, msg.value);
return balances[sender];
}

/// @notice Withdraw ether from bank
/// @dev This does not return any excess ether sent to it
/// @dev https://blog.b9lab.com/the-solidity-withdrawal-pattern-1602cb32f1a5
/// @param withdrawAmount amount you want to withdraw
/// @return The balance remaining for the user
function withdraw(uint withdrawAmount) public returns (uint) {
// If the sender's balance is at least the amount they want to withdraw,
// Subtract the amount from the sender's balance, and try to send that amount of ether
// to the user attempting to withdraw.
// return the user's balance.
function withdraw(uint withdrawAmount) public onlyEnrolled returns (uint) {
address sender = address(msg.sender);
// Ensure sender has enough funds.
require(
balances[sender] >= withdrawAmount,
"Withdraw amount is too much."
);

// optimistic accounting
balances[sender] -= withdrawAmount;

// 1. Use a require expression to guard/ensure sender has enough funds
// transfer
payable(sender).transfer(withdrawAmount);

// 2. Transfer Eth to the sender and decrement the withdrawal amount from
// sender's balance
// emit event
uint newBalance = balances[msg.sender];
emit LogWithdrawal(msg.sender, withdrawAmount, newBalance);

// 3. Emit the appropriate event for this message
return newBalance;
}
}
4,346 changes: 4,346 additions & 0 deletions contracts/artifacts/SimpleBank.json

Large diffs are not rendered by default.

Loading