From 974c487e395da5d73ecf95051f7fb55dd58a13be Mon Sep 17 00:00:00 2001 From: Theo Butler Date: Tue, 14 Feb 2023 11:07:41 -0500 Subject: [PATCH] contract: Fix use of block number in subscribe --- contract/contracts/Subscriptions.sol | 2 +- contract/test/contract.test.ts | 35 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/contract/contracts/Subscriptions.sol b/contract/contracts/Subscriptions.sol index dcd0913..1e26b79 100644 --- a/contract/contracts/Subscriptions.sol +++ b/contract/contracts/Subscriptions.sol @@ -105,7 +105,7 @@ contract Subscriptions is Ownable { uint128 rate ) public { require( - subscriptions[user].end <= uint64(block.number) || + subscriptions[user].end <= uint64(block.timestamp) || user == msg.sender, 'active subscription must have ended' ); diff --git a/contract/test/contract.test.ts b/contract/test/contract.test.ts index a69f3ae..b1aa60c 100644 --- a/contract/test/contract.test.ts +++ b/contract/test/contract.test.ts @@ -425,6 +425,41 @@ describe('Subscriptions contract', () => { initialBalance.sub(newSubValue) ); }); + it('0xMacro: should allow creating a new sub if there is an expired one', async function () { + const now = await latestBlockTimestamp(); + const start = now.add(100); + const end = now.add(500); + const rate = BigNumber.from(5); + const user = subscriber2.address; + await subscribe( + stableToken, + subscriptions, + subscriber1, + start, + end, + rate, + user + ); + + // advance past subscription end + mineNBlocks(600); + const newNow = await latestBlockTimestamp(); + expect(newNow).to.be.gte(end); + + const newStart = newNow.add(100); + const newEnd = newNow.add(500); + + // Should be able to create another subscription + await subscribe( + stableToken, + subscriptions, + subscriber1, + newStart, + newEnd, + rate, + user + ); + }); }); describe('unsubscribe', function () { it('should allow user to cancel an active subscription', async function () {