I have a smart contract that accepts user funds and adds them to an AAVE lending pool (DAI for example). Something like this:
lendingPool.deposit(stableCoinAddress, amount, address(this), 0);
As you can see, smart contract receives minted
aTokens. The above code can be called by many users with different deposit amounts at different times.
Later on, somewhere else in the same contract, users can redeem their funds at any arbitrary time with any amount up to their original deposit:
lendingPool.withdraw(stableCoinAddress, amount + interest, userAddress);
I need to calculate and add each user's interest in AAVE pool and add it to the original deposited value, but it sounds kind of complicated. Can anybody give me a hint? (or better than that, a usable piece of code!)