Skip to content

Commit

Permalink
fix: round up sub end time when extending (0xM L-02)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <tomas@edgeandnode.com>
  • Loading branch information
tmigone committed Sep 14, 2023
1 parent 30137d9 commit 745d6a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion contracts/contracts/Subscriptions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ contract Subscriptions is Ownable {
require(sub.rate != 0, 'cannot extend a zero rate subscription');

uint64 newEnd = uint64(Math.max(sub.end, block.timestamp)) +
uint64(amount / sub.rate);
uint64(Math.ceilDiv(amount, sub.rate));

_setEpochs(sub.start, sub.end, -int128(sub.rate));
_setEpochs(sub.start, newEnd, int128(sub.rate));
Expand Down
47 changes: 39 additions & 8 deletions contracts/test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,43 @@ describe('Subscriptions contract', () => {
subscribeBlockNumber
);
});


it('should allow extending an active subscription with no rounding error', async function () {
const now = await latestBlockTimestamp();
const start = now;
const end = now.add(1000);
const rate = BigNumber.from(7);
const amountToExtend = BigNumber.from(2000); // newEnd: end + 2000/7 = 1000 + 286 = 1286

const subscribeBlockNumber = await subscribe(
stableToken,
subscriptions,
subscriber1,
start,
end,
rate
);

// mine past the start of the subscription
await mineNBlocks(150);

await addToSubscription(
stableToken,
subscriptions,
recurringPayments,
subscriber1.address,
amountToExtend,
subscribeBlockNumber
);
});

it('should allow extending an active subscription', async function () {
const now = await latestBlockTimestamp();
const start = now;
const end = now.add(1000);
const rate = BigNumber.from(5);
const amountToExtend = BigNumber.from(2000); // newEnd: end + 2000/5 = 1000 + 400 = 1400

const subscribeBlockNumber = await subscribe(
stableToken,
subscriptions,
Expand Down Expand Up @@ -688,7 +717,7 @@ describe('Subscriptions contract', () => {
const end = now.add(1000);
const rate = BigNumber.from(5);
const amountToExtend = BigNumber.from(2000); // newEnd: end + 2000/5 = 1000 + 400 = 1400

const subscribeBlockNumber = await subscribe(
stableToken,
subscriptions,
Expand Down Expand Up @@ -1409,10 +1438,14 @@ async function addToSubscription(
// * Tx
const tx = subscriptions.connect(signer.signer).addTo(user, amount);
const receipt = await (await tx).wait();
const txTimestamp = (await subscriptions.provider.getBlock(receipt.blockNumber!)).timestamp;
const txTimestamp = (
await subscriptions.provider.getBlock(receipt.blockNumber!)
).timestamp;

// * Check events
const newEnd = BigNumber.from(Math.max(beforeSub.end.toNumber(), txTimestamp)).add(amount.div(beforeSub.rate));
const newEnd = BigNumber.from(
Math.max(beforeSub.end.toNumber(), txTimestamp)
).add(Math.ceil(amount.toNumber() / beforeSub.rate.toNumber()));
await expect(tx)
.to.emit(subscriptions, 'Extend')
.withArgs(user, beforeSub.end, newEnd, amount);
Expand All @@ -1423,9 +1456,7 @@ async function addToSubscription(
subscriptions.address
);
expect(afterBalance).to.eq(beforeBalance.sub(amount));
expect(afterContractBalance).to.eq(
beforeContractBalance.add(amount)
);
expect(afterContractBalance).to.eq(beforeContractBalance.add(amount));

// * Check state
const afterSub = await subscriptions.subscriptions(user);
Expand Down

0 comments on commit 745d6a6

Please sign in to comment.