Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert when extending if amount is not multiple of rate (0xM L-02) #87

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/contracts/Subscriptions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ 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(amount / sub.rate);
Expand Down
32 changes: 24 additions & 8 deletions contracts/test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,28 @@ describe('Subscriptions contract', () => {
subscribeBlockNumber
);
});


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

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

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 () {
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 +702,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 +1423,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 +1441,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