forked from nucypher/nucypher-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nucypher#277 from vzotova/subscription-5
General improvements for Subscription
- Loading branch information
Showing
6 changed files
with
165 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
contracts/contracts/coordination/subscription/EncryptorSlotsSubscription.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "./AbstractSubscription.sol"; | ||
|
||
/** | ||
* @title Subscription that includes payment for enryptor slots | ||
* @notice Manages the subscription information for rituals. | ||
*/ | ||
abstract contract EncryptorSlotsSubscription is AbstractSubscription { | ||
uint32 public startOfSubscription; | ||
uint256 public usedEncryptorSlots; | ||
// example of storage layout | ||
// mapping(uint256 periodNumber => Billing billing) public billingInfo; | ||
|
||
uint256[20] private gap; | ||
|
||
/** | ||
* @notice Sets the coordinator and fee token contracts | ||
* @dev The coordinator and fee token contracts cannot be zero addresses | ||
* @param _coordinator The address of the coordinator contract | ||
* @param _subscriptionPeriodDuration Maximum duration of subscription period | ||
* @param _yellowPeriodDuration Duration of yellow period | ||
* @param _redPeriodDuration Duration of red period | ||
*/ | ||
constructor( | ||
Coordinator _coordinator, | ||
uint32 _subscriptionPeriodDuration, | ||
uint32 _yellowPeriodDuration, | ||
uint32 _redPeriodDuration | ||
) | ||
AbstractSubscription( | ||
_coordinator, | ||
_subscriptionPeriodDuration, | ||
_yellowPeriodDuration, | ||
_redPeriodDuration | ||
) | ||
{} | ||
|
||
function isPeriodPaid(uint256 periodNumber) public view virtual returns (bool); | ||
|
||
function getPaidEncryptorSlots(uint256 periodNumber) public view virtual returns (uint256); | ||
|
||
function getCurrentPeriodNumber() public view returns (uint256) { | ||
if (startOfSubscription == 0) { | ||
return 0; | ||
} | ||
return (block.timestamp - startOfSubscription) / subscriptionPeriodDuration; | ||
} | ||
|
||
function getEndOfSubscription() public view override returns (uint32 endOfSubscription) { | ||
if (startOfSubscription == 0) { | ||
return 0; | ||
} | ||
|
||
uint256 currentPeriodNumber = getCurrentPeriodNumber(); | ||
if (currentPeriodNumber == 0 && !isPeriodPaid(currentPeriodNumber)) { | ||
return 0; | ||
} | ||
|
||
if (isPeriodPaid(currentPeriodNumber)) { | ||
while (isPeriodPaid(currentPeriodNumber)) { | ||
currentPeriodNumber++; | ||
} | ||
} else { | ||
while (!isPeriodPaid(currentPeriodNumber)) { | ||
currentPeriodNumber--; | ||
} | ||
currentPeriodNumber++; | ||
} | ||
endOfSubscription = uint32( | ||
startOfSubscription + currentPeriodNumber * subscriptionPeriodDuration | ||
); | ||
} | ||
|
||
function beforeSetAuthorization( | ||
uint32 ritualId, | ||
address[] calldata addresses, | ||
bool value | ||
) public virtual override { | ||
super.beforeSetAuthorization(ritualId, addresses, value); | ||
if (value) { | ||
uint256 currentPeriodNumber = getCurrentPeriodNumber(); | ||
uint256 encryptorSlots = isPeriodPaid(currentPeriodNumber) | ||
? getPaidEncryptorSlots(currentPeriodNumber) | ||
: 0; | ||
usedEncryptorSlots += addresses.length; | ||
require(usedEncryptorSlots <= encryptorSlots, "Encryptors slots filled up"); | ||
} else { | ||
usedEncryptorSlots -= addresses.length; | ||
} | ||
} | ||
|
||
function beforeIsAuthorized(uint32 ritualId) public view virtual override { | ||
super.beforeIsAuthorized(ritualId); | ||
// used encryptor slots must be paid | ||
if (block.timestamp <= getEndOfSubscription()) { | ||
uint256 currentPeriodNumber = getCurrentPeriodNumber(); | ||
require( | ||
usedEncryptorSlots <= getPaidEncryptorSlots(currentPeriodNumber), | ||
"Encryptors slots filled up" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.