Last active
October 11, 2019 15:48
-
-
Save adam-hanna/8bf12aefd665f0ae28d17ad3eb6b5ed5 to your computer and use it in GitHub Desktop.
AE Prezzo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare the source file compiler version | |
pragma solidity ^0.5.0; | |
contract SimpleBank { | |
// Declare state variables outside function, persist through life of contract | |
mapping (address => uint) private balances; | |
address public owner; | |
event LogDepositMade(address accountAddress, uint amount); | |
constructor() public { | |
owner = msg.sender; | |
} | |
/// @notice Deposit ether into bank | |
/// @return The balance of the user after the deposit is made | |
function deposit() public payable returns (uint) { | |
require((balances[msg.sender] + msg.value) >= msg.value); | |
balances[msg.sender] += msg.value; | |
emit LogDepositMade(msg.sender, msg.value); // fire event | |
return balances[msg.sender]; | |
} | |
/// @notice Withdraw ether from bank | |
/// @dev This does not return any excess ether sent to it | |
/// @param withdrawAmount amount you want to withdraw | |
/// @return The balance remaining for the user | |
function withdraw(uint withdrawAmount) public returns (uint remainingBal) { | |
require(withdrawAmount <= balances[msg.sender]); | |
balances[msg.sender] -= withdrawAmount; | |
// this automatically throws on a failure, which means the updated balance is reverted | |
msg.sender.transfer(withdrawAmount); | |
return balances[msg.sender]; | |
} | |
/// @notice Transfer to another address | |
/// @param transferAmount amount you want to transfer | |
/// @param recipient to which you want to transfer | |
/// @return The balance remaining for the user | |
function transfer(uint transferAmount, address recipient) public returns (uint remainingBal) { | |
require(transferAmount <= balances[msg.sender]); | |
require((balances[recipient] + transferAmount) >= transferAmount); | |
balances[msg.sender] -= transferAmount; | |
balances[recipient] += transferAmount; | |
return balances[msg.sender]; | |
} | |
/// @notice Get balance | |
/// @return The balance of the user | |
function balance() view public returns (uint) { | |
return balances[msg.sender]; | |
} | |
} |
npm install -g truffle ganache-cli live-server
truffle init
truffle migrate -network development
ganache-cli
live-server
// js
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
await ethereum.enable();
const contractAddress = "0xF110Cca894a8c9EA63Bf935Fba56fE102fE29bE3"
contract = new web3.eth.Contract(abi, contractAddress);
let message = await contract.methods.balance().call();
accounts = await web3.eth.getAccounts();
message = await contract.methods.deposit().send({value: 100, from: accounts[0]})
message = await contract.methods.transfer(20, "0x1C6b858D31B6b17d3910eEA642Bfb2C45491DeE8").send({from: "0x4d3DA1Cc86fe05057ea51f37D3DeCe480c4E4Bd2"});
message = await contract.methods.withdraw(5).send({from: "0x4d3DA1Cc86fe05057ea51f37D3DeCe480c4E4Bd2"});
message = await contract.methods.balance().call({from: "0x4d3DA1Cc86fe05057ea51f37D3DeCe480c4E4Bd2"});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment