Created
August 10, 2021 09:50
-
-
Save madhavanmalolan/699c3b434a2b18a66b200f0620c02f0f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=
This file contains 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
pragma solidity >=0.7.0 <0.9.0; | |
contract SmartBankAccount { | |
uint totalContractBalance = 0; | |
function getContractBalance() public view returns(uint){ | |
return totalContractBalance; | |
} | |
mapping(address => uint) balances; | |
mapping(address => uint) depositTimestamps; | |
function addBalance() public payable { | |
balances[msg.sender] = msg.value; | |
totalContractBalance = totalContractBalance + msg.value; | |
depositTimestamps[msg.sender] = block.timestamp; | |
} | |
function getBalance(address userAddress) public view returns(uint) { | |
uint principal = balances[userAddress]; | |
uint timeElapsed = block.timestamp - depositTimestamps[userAddress]; //seconds | |
return principal + uint((principal * 7 * timeElapsed) / (100 * 365 * 24 * 60 * 60)) ; //simple interest of 7% per year | |
} | |
function withdraw() public payable { | |
address payable withdrawTo = payable(msg.sender); | |
uint amountToTransfer = getBalance(msg.sender); | |
withdrawTo.transfer(amountToTransfer); | |
totalContractBalance = totalContractBalance - amountToTransfer; | |
balances[msg.sender] = 0; | |
} | |
function addMoneyToContract() public payable { | |
totalContractBalance += msg.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment