implement interest owed
This commit is contained in:
parent
2b4a6dfd64
commit
1d5961e408
|
@ -2,7 +2,7 @@ pragma solidity ^0.5.3;
|
|||
import "../token/ERC20Token.sol";
|
||||
|
||||
|
||||
|
||||
// DO NOT USE IN PRODUCTION
|
||||
// Contract to mock interactions with Compound.finance
|
||||
|
||||
contract Compound {
|
||||
|
@ -35,7 +35,7 @@ contract Compound {
|
|||
uint supplyTime = supplyTimes[msg.sender];
|
||||
uint balance = supplyBalances[msg.sender];
|
||||
uint timeDelta = now - supplyTime;
|
||||
uint accruedInterest = timeDelta * periodicRate;
|
||||
uint accruedInterest = balance * timeDelta * periodicRate / percentBase;
|
||||
return accruedInterest + balance;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ contract Subscription {
|
|||
uint256 public totalBalances;
|
||||
uint256 constant base = 10 ** 18;
|
||||
uint256 constant percentBase = 100 * base;
|
||||
uint256 public minDeposit = 12;
|
||||
|
||||
constructor(
|
||||
address _compoundAddress,
|
||||
|
@ -110,6 +111,13 @@ contract Subscription {
|
|||
return target * percent/percentBase;
|
||||
}
|
||||
|
||||
function getInterestOwed(uint256 amountOwed) view public returns (uint256) {
|
||||
uint256 totalAmount = compound.getSupplyBalance(address(this), daiAddress);
|
||||
uint256 totalInterest = totalAmount - totalBalances;
|
||||
uint256 interestOwed = totalInterest * amountOwed / totalBalances;
|
||||
return interestOwed;
|
||||
}
|
||||
|
||||
function getAmountOwed(bytes32 agreementId) view public returns (uint256) {
|
||||
Agreement memory agreement = agreements[agreementId];
|
||||
//TODO check for enddate, use instead of now
|
||||
|
@ -156,7 +164,10 @@ contract Subscription {
|
|||
external
|
||||
{
|
||||
require(msg.sender == payor, "Agreement must be created by payor");
|
||||
require(annualAmount > 0, "AnnualAmount can not be zero");
|
||||
bytes32 agreementId = keccak256(abi.encode(receiver, payor, token, annualAmount, startDate, description));
|
||||
uint supplyAmount = annualAmount / minDeposit;
|
||||
supply(supplyAmount);
|
||||
Agreement storage agreement = agreements[agreementId];
|
||||
|
||||
agreement.receiver = receiver;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -85,6 +85,16 @@ contract("subscription", function () {
|
|||
assert.equal(owed, accrued, `Owned: ${owed} amount returned not equal to expected ${accrued}`)
|
||||
});
|
||||
|
||||
it('should get interest owed from compound', async function() {
|
||||
const accrued = 1000 * 10 * (annualSalary / SECONDS_IN_A_YEAR)
|
||||
const approxInterest = accrued * (0.04 / 12 / 30 / 24)
|
||||
await utils.increaseTime(1000)
|
||||
const owed = await Subscription.methods.getInterestOwed(
|
||||
accrued.toString()
|
||||
).call({from: receiver})
|
||||
assert(owed < approxInterest, "The amount owed is higher than expected")
|
||||
})
|
||||
|
||||
it('should allow a payor to supply token', async function() {
|
||||
const amount = toWei('100000')
|
||||
const supply = await Subscription.methods.supply(
|
||||
|
|
Loading…
Reference in New Issue