Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
tranch premiums are now specified as an array of values (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao authored Jan 30, 2020
1 parent 820d067 commit 82826bc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
10 changes: 7 additions & 3 deletions contracts/PolicyImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract PolicyImpl is EternalStorage, Controller, IProxyImpl, IPolicyImpl, ITra
function createTranch (
uint256 _numShares,
uint256 _pricePerShareAmount,
uint256 _premiumAmount,
uint256[] memory _premiums,
address _initialBalanceHolder
)
public
Expand All @@ -91,7 +91,7 @@ contract PolicyImpl is EternalStorage, Controller, IProxyImpl, IPolicyImpl, ITra
{
require(_numShares > 0, 'invalid num of shares');
require(_pricePerShareAmount > 0, 'invalid price');
require(_premiumAmount > 0, 'invalid premium');
require(_premiums.length < calculateMaxNumOfPremiums(), 'too many premiums');

// instantiate tranches
uint256 i = dataUint256["numTranches"];
Expand All @@ -100,7 +100,7 @@ contract PolicyImpl is EternalStorage, Controller, IProxyImpl, IPolicyImpl, ITra
// setup initial data for tranch
dataUint256[string(abi.encodePacked(i, "numShares"))] = _numShares;
dataUint256[string(abi.encodePacked(i, "pricePerShareAmount"))] = _pricePerShareAmount;
dataUint256[string(abi.encodePacked(i, "premiumAmount"))] = _premiumAmount;
dataManyUint256[string(abi.encodePacked(i, "premiums"))] = _premiums;

// deploy token contract
TranchToken t = new TranchToken(address(this), i);
Expand Down Expand Up @@ -204,6 +204,10 @@ contract PolicyImpl is EternalStorage, Controller, IProxyImpl, IPolicyImpl, ITra
}
}

function calculateMaxNumOfPremiums() public view returns (uint256) {
// first 2 payments + (endDate - startDate) / paymentInterval - 1
return (dataUint256["maturationDate"] - dataUint256["startDate"]) / dataUint256["premiumIntervalSeconds"] + 1;
}

// TranchTokenImpl - queries //

Expand Down
2 changes: 1 addition & 1 deletion contracts/base/IPolicyImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract IPolicyImpl {
function createTranch (
uint256 _numShares,
uint256 _pricePerShareAmount,
uint256 _premiumAmount,
uint256[] memory _premiums,
address _initialBalanceHolder
) public returns (uint256);
function getNumTranches () public view returns (uint256);
Expand Down
19 changes: 16 additions & 3 deletions test/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ contract('Policy', accounts => {
})
})

describe('tranches', () => {
describe.only('tranches', () => {
const tranchNumShares = 10
const tranchPricePerShare = 100

Expand All @@ -186,10 +186,23 @@ contract('Policy', accounts => {
await createTranch(policy, { denominationUnit: etherToken.address }).should.be.rejectedWith('must be policy manager')
})

it('all values must be valid', async () => {
it('all basic values must be valid', async () => {
await createTranch(policy, { numShares: 0 }, { from: accounts[2] }).should.be.rejectedWith('invalid num of shares')
await createTranch(policy, { pricePerShareAmount: 0 }, { from: accounts[2] }).should.be.rejectedWith('invalid price')
await createTranch(policy, { premiumAmount: 0 }, { from: accounts[2] }).should.be.rejectedWith('invalid premium')
})

it('and premium array is valid', async () => {
policyStartDate = ~~(Date.now() / 1000)

const createPolicyTx = await createPolicy(entity, policyImpl.address, {
startDate: policyStartDate,
maturationDate: policyStartDate + 30,
premiumIntervalSeconds: 20,
}, { from: entityManagerAddress })
const policyAddress = extractEventArgs(createPolicyTx, events.NewPolicy).policy
policy = await IPolicyImpl.at(policyAddress)

await createTranch(policy, { premiums: [1, 2, 3, 4, 5] }, { from: accounts[2] }).should.be.rejectedWith('too many premiums')
})

it('can be created and have initial balance auto-allocated to policy impl', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ export const createTranch = (policy, attrs, ...callAttrs) => {
const {
numShares = 10,
pricePerShareAmount = 1,
premiumAmount = 1,
premiums = [1, 1, 1],
initialBalanceHolder = ADDRESS_ZERO,
} = attrs

return policy.createTranch(
numShares,
pricePerShareAmount,
premiumAmount,
premiums,
initialBalanceHolder,
...callAttrs,
)
Expand Down

0 comments on commit 82826bc

Please sign in to comment.