diff --git a/README.md b/README.md index 32b7889..17ac959 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,7 @@ This is a curated list of problems in Solidity for someone who is just starting 12. [**Address Book**](./contracts/AddressBook.sol) Contract to store and manage contact information. +13. [**Simple Bank**](./contracts/Bank.sol) + Contract to deposit,fixedeposit,takeloan and repayloan just like a bank. + --- diff --git a/contracts/Bank.sol b/contracts/Bank.sol new file mode 100644 index 0000000..7ec32ec --- /dev/null +++ b/contracts/Bank.sol @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract Bank { + uint public totalBankBalance; + mapping(address => uint) public deposits; + mapping(address => Fixeddeposits) public fixeddeposits; + mapping(address => uint) public coinbalance; + mapping(address => Loantakers) public loantakers; + + struct Fixeddeposits { + uint amount; + uint starttime; + uint endtime; + bool active; + } + + struct Loantakers { + uint amount ; + uint loantime; + uint loanendtime; + bool active; + } + function deposit() public payable { + require(msg.value > 0, "Cannot deposit 0"); + deposits[msg.sender] += msg.value; + coinbalance[msg.sender] += msg.value; + totalBankBalance += msg.value; + } + + function withdraw(address payable to, uint amount) public { + require(amount <= deposits[msg.sender], "Not enough balance to withdraw"); + require(amount <= coinbalance[msg.sender], "Can't withdraw more coins than you have"); + require(amount <= totalBankBalance, "Bank is currently out of money"); + + deposits[msg.sender] -= amount; + coinbalance[msg.sender] -= amount; + totalBankBalance -= amount; + + to.transfer(amount); + } + + function fixeddeposit() public payable { + require(msg.value > 0, "Cannot deposit 0"); + require(fixeddeposits[msg.sender].active == false, "Already fixed deposited"); + fixeddeposits[msg.sender] = Fixeddeposits({ + amount: msg.value, + starttime: block.timestamp, + endtime: fixeddeposits[msg.sender].starttime + 365 days, + active: true + }); + + coinbalance[msg.sender] += msg.value; + totalBankBalance += msg.value; + } + + function withdrawfixeddeposit(address payable to, uint amount) public { + Fixeddeposits storage fd = fixeddeposits[msg.sender]; + + require(fd.active == true, "You have not deposited anything"); + require(block.timestamp >= fd.starttime + 365 days, "FD is not matured yet"); + require(amount > 0, "Cannot withdraw less than zero"); + require(amount <= fd.amount, "Not enough in your fixed deposit"); + require(amount <= coinbalance[msg.sender], "Not enough coins to withdraw"); + require(amount <= totalBankBalance, "Bank is currently out of money"); + uint interest = (fd.amount * 8) / 100; + fd.amount += interest; + fd.amount -= amount; + coinbalance[msg.sender] -= amount; + totalBankBalance -= amount; + if (fd.amount == 0) { + fixeddeposits[msg.sender] = Fixeddeposits({ + amount: 0, + starttime: 0, + endtime: block.timestamp, + active: false + }); + } + + to.transfer(amount); + } + +function takeloan(address payable to, uint amount) public { + require(amount > 0, "Cannot loan less than zero"); + require(amount <= totalBankBalance, "Bank doesn't have enough money"); + require(!loantakers[msg.sender].active, "You already have an active loan"); + + loantakers[msg.sender] = Loantakers({ + amount: amount, + loantime: block.timestamp, + loanendtime: block.timestamp + 365 days, + active: true + }); + + totalBankBalance -= amount; + to.transfer(amount); +} + +function clearloan() public payable { + Loantakers storage lt = loantakers[msg.sender]; + require(lt.active, "You have no active loan"); + + uint interest = (lt.amount * 15) / 100; + uint totaldue = lt.amount; + + if (block.timestamp > lt.loanendtime) { + totaldue += interest; + } + + require(msg.value >= totaldue, "Not enough ETH sent to repay the loan + interest"); + + totalBankBalance += msg.value; + lt.amount = 0; + lt.loantime = 0; + lt.loanendtime = 0; + lt.active = false; + + } + +}