Hello Community,
I am working on my first little project after taking the Ethereum Programming 101 course.
So the contract is about āearning interestā.
- user deposits some ether
- user can query for interested earned since deposit
I got everything running so far as you can deposit some, query for your deposit, see the annual interest and also query for the interested earned since time of deposit. You can also get total annual interest for your total balance ( of all deposits).
Now I try to make it work so you can query āAll Interest earned so far from all depositsā.
I hope this makes sense. Here is my code:
pragma solidity 0.7.5;
pragma abicoder v2;
contract EarnInterest{
address saver;
deposit[] depositsList;
struct deposit{
uint id;
uint amount;
address depositor;
uint time;
uint annualInterest;
}
mapping (address => uint) depositLog;
mapping (address => uint) balance;
function newDeposit() public payable{
balance[msg.sender] += msg.value;
depositsList.push(deposit(depositsList.length, msg.value, msg.sender, block.timestamp, msg.value / 10));
}
function interest(uint _id) public {
// uint timePassed = block.timestamp - depositsList[_id].time;[
if(block.timestamp >= depositsList[_id].time){
balance[msg.sender] = balance[msg.sender] + (balance[msg.sender] / 10);
}
}
function MyBalance() public view returns(uint){
// uint annualInterest = balance[msg.sender] / 10;
//annual interest sind 10% bei 1 Jahr (31 536 000 sekunden)
//interest earned now = 31 536 000 / timepassed.
//earnedNow = timepassed / 1 Jahr * annualInterest
return balance[msg.sender];
}
function ShowTimePassed(uint _id) public view returns(uint){ // shows timePasseds since deposit for queried deposit
uint calcTimePassed = block.timestamp - depositsList[_id].time;
return (calcTimePassed);
}
function showAnnualInterest(uint _id) public view returns(uint){ //shows annual Interest for queried deposit
return depositsList[_id].annualInterest;
}
function showCurrentInterest(uint _id) public view returns(uint){ //shows current Interest earned from a queried deposit
uint timePassedNow = block.timestamp - depositsList[_id].time;
uint annualInterest = depositsList[_id].amount / 10;
uint secondInterest = annualInterest / 31536000;
uint InterestEarned = secondInterest * timePassedNow; //passes Interest earned till timePassedNow
return (InterestEarned);
}
function showTotalInterest() public view returns(uint){ //shows total annual Interest of balance of msg.sender
uint totalInterest = balance[msg.sender] / 10;
return totalInterest;
}
//HERE starts my question...
/* function showTotalCurrentInterest()public view returns(uint){ //supposed to show Total Current Interest of all deposits of msg.sender
}
*/
/*
function showUserDeposits()public view returns(uint, uint, address, uint, uint){ //supposed to show all Deposits of msg.sender
for(uint i = 0;i>=depositsList.length;i++){
bool isUserDeposit = false;
if(depositsList[i].depositor == msg.sender){
isUserDeposit = true;
return (depositsList[i].id, depositsList[i].amount, depositsList[i].depositor, depositsList[i].time, depositsList[i].annualInterest);
}
}
}
*/
}