From 30137d974e0aff370412764a76f363a645977944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 14 Sep 2023 11:16:24 -0300 Subject: [PATCH] fix: tests and simplify contract code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- contracts/contracts/Subscriptions.sol | 7 +++---- contracts/test/contract.test.ts | 12 ++++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/contracts/contracts/Subscriptions.sol b/contracts/contracts/Subscriptions.sol index ea0fc04..2447901 100644 --- a/contracts/contracts/Subscriptions.sol +++ b/contracts/contracts/Subscriptions.sol @@ -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)); @@ -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 { diff --git a/contracts/test/contract.test.ts b/contracts/test/contract.test.ts index 3813b03..e1bdea4 100644 --- a/contracts/test/contract.test.ts +++ b/contracts/test/contract.test.ts @@ -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'; @@ -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); @@ -643,7 +643,7 @@ describe('Subscriptions contract', () => { // mine past the newEnd await mineNBlocks(1500); - const tx = addToSubscription( + await addToSubscription( stableToken, subscriptions, recurringPayments, @@ -651,7 +651,6 @@ describe('Subscriptions contract', () => { amountToExtend, subscribeBlockNumber ); - await expect(tx).revertedWith('new end cannot be in the past'); }); it('should allow extending an active subscription', async function () { @@ -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);