Skip to content

Commit

Permalink
fix: tests and simplify contract code
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 199d51d commit 30137d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 3 additions & 4 deletions contracts/contracts/Subscriptions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,8 @@ contract Subscriptions is Ownable {
require(sub.start != 0, 'no subscription found');
require(sub.rate != 0, 'cannot extend a zero rate subscription');

uint64 oldEnd = uint64(Math.max(sub.end, block.timestamp));
uint64 newEnd = oldEnd + uint64(amount / sub.rate);
require(block.timestamp < newEnd, 'new end cannot be in the past');
uint64 newEnd = uint64(Math.max(sub.end, block.timestamp)) +
uint64(amount / sub.rate);

_setEpochs(sub.start, sub.end, -int128(sub.rate));
_setEpochs(sub.start, newEnd, int128(sub.rate));
Expand All @@ -292,7 +291,7 @@ contract Subscriptions is Ownable {
bool success = token.transferFrom(msg.sender, address(this), amount);
require(success, 'IERC20 token transfer failed');

emit Extend(user, oldEnd, newEnd, amount);
emit Extend(user, sub.end, newEnd, amount);
}

function setRecurringPayments(address _recurringPayments) public onlyOwner {
Expand Down
12 changes: 6 additions & 6 deletions contracts/test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {time} from '@nomicfoundation/hardhat-network-helpers';

import {expect} from 'chai';
import * as deployment from '../utils/deploy';
import {getAccounts, Account, toGRT} from '../utils/helpers';
import {getAccounts, Account, toGRT, provider} from '../utils/helpers';

import {Subscriptions} from '../types/contracts/Subscriptions';
import {StableToken} from '../types/contracts/test/StableMock.sol/StableToken';
Expand Down Expand Up @@ -624,7 +624,7 @@ describe('Subscriptions contract', () => {
await expect(tx).revertedWith('no subscription found');
});

it('should revert when the new end time is in the past', async function () {
it('should allow extending an expired subscription', async function () {
const now = await latestBlockTimestamp();
const start = now.add(500);
const end = now.add(1000);
Expand All @@ -643,15 +643,14 @@ describe('Subscriptions contract', () => {
// mine past the newEnd
await mineNBlocks(1500);

const tx = addToSubscription(
await addToSubscription(
stableToken,
subscriptions,
recurringPayments,
subscriber1.address,
amountToExtend,
subscribeBlockNumber
);
await expect(tx).revertedWith('new end cannot be in the past');
});

it('should allow extending an active subscription', async function () {
Expand Down Expand Up @@ -1406,13 +1405,14 @@ async function addToSubscription(
const beforeContractBalance = await stableToken.balanceOf(
subscriptions.address
);
const newEnd = beforeSub.end.add(amount.div(beforeSub.rate));
// const additionalTokens = beforeSub.rate.mul(newEnd.sub(beforeSub.end));

// * 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;

// * Check events
const newEnd = BigNumber.from(Math.max(beforeSub.end.toNumber(), txTimestamp)).add(amount.div(beforeSub.rate));
await expect(tx)
.to.emit(subscriptions, 'Extend')
.withArgs(user, beforeSub.end, newEnd, amount);
Expand Down

0 comments on commit 30137d9

Please sign in to comment.