add time util

use safemath
This commit is contained in:
Barry Gitarts 2019-04-09 14:33:13 -04:00
parent 543a9b705f
commit 32bf408d08
5 changed files with 63 additions and 12 deletions

View File

@ -37,6 +37,10 @@ module.exports = {
}
},
test: {
enabled: false
},
// merges with the settings in default
// used with "embark run privatenet"
privatenet: {

View File

@ -20,6 +20,10 @@ contract Subscription {
using SafeMath64 for uint64;
using SafeMath8 for uint8;
uint128 internal constant ONE = 10 ** 18; // 10^18 is considered 1 in the price feed to allow for decimal calculations
uint64 internal constant MAX_UINT64 = uint64(-1);
uint256 internal constant MAX_ACCRUED_VALUE = 2**128;
address compoundAddress = 0x3FDA67f7583380E67ef93072294a7fAc882FD7E7;
address daiAddress = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
CompoundContract compound = CompoundContract(compoundAddress);
@ -107,7 +111,8 @@ contract Subscription {
function getAmountOwed(bytes32 agreementId) view public returns (uint256) {
Agreement memory agreement = agreements[agreementId];
return (now - agreement.lastPayment) * agreement.payRate;
//TODO check for enddate, use instead of now
return (now.sub(agreement.lastPayment)).mul(agreement.payRate);
}
function withdrawFunds(address recipient, bytes32 agreementId) public {
@ -141,7 +146,7 @@ contract Subscription {
agreement.receiver = receiver;
agreement.payor = payor;
agreement.token = token;
agreement.payRate = annualAmount / 325.25 days;
agreement.payRate = annualAmount.div(325.25 days);
agreement.lastPayment = startDate > 0 ? startDate : now;
agreement.endDate = MAX_UINT64;
agreement.description = description;

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,5 @@
/*global contract, config, it, assert*/
const utils = require('../utils/testUtils.js');
const Subscription = require('Embark/contracts/Subscription');
const TestToken = require('Embark/contracts/TestToken');
@ -50,28 +51,37 @@ contract("subscription", function () {
await TestToken.methods.mint(toWei('1000000')).send({from: payor})
})
describe('createAgreement', function() {
describe('createAgreement and withdraw flow', function() {
const annualSalary = toWei("100000")
let returnValues;
it("should create an agreement", async function () {
let balance = await web3.eth.getBalance(accounts[8])
const args = [
receiver,
payor,
TestToken.address,
toWei("100000"),
annualSalary,
"0",
"ipfs/hash"
]
const agreementCreation = await Subscription.methods.createAgreement(
...args
).send({ from: payor })
const returnValues = agreementCreation.events.AddAgreement.returnValues
args.forEach((arg, i) => {
const val = returnValues[i+1]
if (!addAgreementStartDateIdx) {
returnValues = agreementCreation.events.AddAgreement.returnValues
args.slice(0,4).forEach((arg, i) => {
const val = returnValues[i+1]
assert.equal(val, arg, `${val} does not match arg ${arg}`)
}
})
});
})
});
it('should get amount owed to receiver', async function() {
const accured = '35585162410681240'
await utils.increaseTime(10)
const owed = await Subscription.methods.getAmountOwed(
returnValues.agreementId
).call({from: receiver})
assert.equal(owed, accured, 'Owned amount returned not equal to expected')
});
})
})

32
utils/testUtils.js Normal file
View File

@ -0,0 +1,32 @@
exports.increaseTime = async (amount) => {
return new Promise(function(resolve, reject) {
web3.currentProvider.sendAsync(
{
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [+amount],
id: new Date().getSeconds()
},
async (error) => {
if (error) {
console.log(error);
return reject(err);
}
await web3.currentProvider.sendAsync(
{
jsonrpc: '2.0',
method: 'evm_mine',
params: [],
id: new Date().getSeconds()
}, (error) => {
if (error) {
console.log(error);
return reject(err);
}
resolve();
}
)
}
)
});
}