Skip to content

Commit

Permalink
fix: revert if amount to addto is not multiple of sub rate
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 18, 2023
1 parent 745d6a6 commit dc521d7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
3 changes: 2 additions & 1 deletion contracts/contracts/Subscriptions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ contract Subscriptions is Ownable {
Subscription memory sub = subscriptions[user];
require(sub.start != 0, 'no subscription found');
require(sub.rate != 0, 'cannot extend a zero rate subscription');
require(amount % sub.rate == 0, "amount not multiple of rate");

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

_setEpochs(sub.start, sub.end, -int128(sub.rate));
_setEpochs(sub.start, newEnd, int128(sub.rate));
Expand Down
21 changes: 3 additions & 18 deletions contracts/test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,33 +653,18 @@ describe('Subscriptions contract', () => {
);
});

it('should allow extending an active subscription with no rounding error', async function () {
it('should not allow extending an active subscription if amount is not multiple of rate', 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
);
const tx = subscriptions.addTo(subscriber1.address, amountToExtend);
await expect(tx).revertedWith('amount not multiple of rate');
});

it('should allow extending an active subscription', async function () {
Expand Down

0 comments on commit dc521d7

Please sign in to comment.