add getTotalowed and Withdrawal functionality

This commit is contained in:
Barry Gitarts 2019-04-10 15:32:37 -04:00
parent 1d5961e408
commit e031827f35
3 changed files with 41 additions and 16 deletions

View File

@ -61,19 +61,11 @@ contract Subscription {
uint256 totalBalances
);
// Employees start at index 1, to allow us to use employees[0] to check for non-existent address
// mappings with employeeIds
uint256 public nextAgreement;
struct Receiver {
address accountAddress; // unique, but can be changed over time
mapping(address => uint8) allocation;
}
struct Payor {
address accountAddress;
mapping(address => uint256) balances;
}
event WithdrawFunds(
address receiver,
uint256 amount,
bytes32 agreementId
);
struct Agreement {
address receiver;
@ -124,12 +116,20 @@ contract Subscription {
return (now.sub(agreement.lastPayment)).mul(agreement.payRate);
}
function getTotalOwed(bytes32 agreementId) view public returns (uint256) {
uint256 amountOwed = getAmountOwed(agreementId);
uint256 interestOwed = getInterestOwed(amountOwed);
return interestOwed.add(amountOwed);
}
function withdrawFunds(bytes32 agreementId, uint256 amount) public {
uint256 amountOwed = getAmountOwed(agreementId);
if (amount == 0 || amountOwed == 0) return;
require(amount <= amountOwed, "amount can not exceed amount owed");
Agreement storage agreement = agreements[agreementId];
uint256 payorBalance = payorBalances[agreement.payor];
// consider marking subscription terminated in this case
require(amount <= payorBalance, "amount can not exceed payor balance");
// withdraw from savings to subscription contract
@ -139,7 +139,7 @@ contract Subscription {
dai.transfer(msg.sender, amount);
payorBalances[agreement.payor] = payorBalance.sub(amount);
totalBalances = totalBalances.sub(amount);
//emit MemberPaid( recipient, amount, justification);
emit WithdrawFunds(msg.sender, amount, agreementId);
}
function supply(uint256 amount) public returns (uint256) {

File diff suppressed because one or more lines are too long

View File

@ -52,7 +52,7 @@ contract("subscription", function () {
await TestToken.methods.mint(toWei('1000000')).send({from: payor})
})
describe('createAgreement and supply withdraw flow', function() {
describe('createAgreement and supply withdraw flow', function() {
const annualSalary = toWei("100000")
let returnValues;
@ -92,7 +92,11 @@ contract("subscription", function () {
const owed = await Subscription.methods.getInterestOwed(
accrued.toString()
).call({from: receiver})
const totalOwed = await Subscription.methods.getTotalOwed(
returnValues.agreementId
).call({from: receiver})
assert(owed < approxInterest, "The amount owed is higher than expected")
assert(Number(totalOwed) >= Number(owed), "totalOwed can not be less than owed")
})
it('should allow a payor to supply token', async function() {
@ -103,5 +107,26 @@ contract("subscription", function () {
const returned = supply.events.SupplyReceived.returnValues
assert.equal(amount, returned.amount, 'returned amount does not match')
})
it('should allow the receiver to withdraw funds accrued', async function() {
const accrued = 1000 * 10 * (annualSalary / SECONDS_IN_A_YEAR)
const approxInterest = accrued * (0.04 / 12 / 30 / 24)
const totalOwed = await Subscription.methods.getTotalOwed(
returnValues.agreementId
).call({from: receiver})
const withdrawn = await Subscription.methods.withdrawFunds(
returnValues.agreementId,
totalOwed
).send({ from: receiver })
const returned = withdrawn.events.WithdrawFunds
assert.equal(Number(returned.returnValues.amount), Number(totalOwed), "withdraw failed or returned amount incorrect")
const totalOwed2 = await Subscription.methods.getTotalOwed(
returnValues.agreementId
).call({from: receiver})
assert.equal(totalOwed2, "0", "Not all funds withdrawn")
})
//TODO allow payor to withdraw funds and terminate subscription.
//TODO allow receiver to terminate subscription
})
})