Cooler

This contract handles the cooldown required before withdrawal of eTokens

For each withdrawal position it mints an NFT. The owner of the NFT is who receives the funds when the withdrawal is executed. The value of the eTokens at the execution time can be higher or lower than the value of the eTokens when the withdrawal was scheduled, due to earnings and losses during the cooldown period. If the resulting amount is lower, the LP (owner of the NFT) will receive less. If the value is higher than the value at the schedule period, the LP will receive ONLY the value at the schedule time, and the difference will be distributed to the remaining LPs of the token.

Types

WithdrawalRequest

Struct to store info about the withdrawal request

There's one of this for each request (each NFT). It contains the request amount and the request time and expiration.

struct WithdrawalRequest {
  ETKLib.Scale scaleAtRequest;
  contract IEToken etk;
  uint128 requestedAmount;
  uint40 requestedAt;
  uint40 expiration;
}

Variables

_withdrawalRequests

mapping(uint256 => struct Cooler.WithdrawalRequest) _withdrawalRequests

Mapping with the WithdrawalRequest info

_cooldownPeriods

mapping(contract IEToken => uint40) _cooldownPeriods

Mapping with the cooldown period (in seconds) for each eToken

_pendingWithdrawals

mapping(contract IEToken => uint256) _pendingWithdrawals

Mapping with the aggregate amount of pending withdrawals per eToken

_nextTokenId

uint256 _nextTokenId

It used for the withdrawal NFTs, starts at 1.

Events

CooldownPeriodChanged

event CooldownPeriodChanged(contract IEToken eToken, uint40 oldCooldownPeriod, uint40 newCooldownPeriod)

Event emitted when the cooldown period is changed

Parameters

Name Type Description
eToken contract IEToken The EToken contract address for which the cooldown period was modified
oldCooldownPeriod uint40 The previous cooldown period value (in seconds)
newCooldownPeriod uint40 The new cooldown period value (in seconds)

WithdrawalRequested

event WithdrawalRequested(contract IEToken eToken, uint256 tokenId, address owner, uint40 when, ETKLib.Scale scaleAtRequest, uint256 amount)

Event emitted when a withdrawal is requested

Parameters

Name Type Description
eToken contract IEToken The EToken contract from which the withdrawal is being requested
tokenId uint256 The NFT id of the withdrawal position created
owner address The owner initiating the withdrawal request
when uint40 The timestamp when the withdrawal can be executed
scaleAtRequest ETKLib.Scale The token scale (see {EToken.getCurrentScale(true)}) at the time of the withdrawal request
amount uint256 The amount of eTokens being requested for withdrawal

WithdrawalExecuted

event WithdrawalExecuted(contract IEToken eToken, uint256 tokenId, address receiver, uint256 amountRequested, uint256 amountWithdrawn)

Event emitted when a withdrawal is executed

Parameters

Name Type Description
eToken contract IEToken The EToken contract from which the withdrawal was processed
tokenId uint256 The unique identifier of the token position that was withdrawn
receiver address The address that received the withdrawn funds
amountRequested uint256 The original amount of eTokens requested for withdrawal
amountWithdrawn uint256 The actual amount withdrawn to the receiver

Errors

WithdrawalRequestEarlierThanMin

error WithdrawalRequestEarlierThanMin(uint40 minRequestTime, uint40 timeRequested)

Error produced when requesting a withdrawal earlier than the minimum withdrawal period

InvalidEToken

error InvalidEToken(contract IEToken eToken)

Error produced when requesting a withdrawal from an eToken that doesn't have address(this) as cooler

InvalidWithdrawalRequest

error InvalidWithdrawalRequest(uint256 tokenId)

Error produced when trying to execute a withdrawal of an non-existent or already used NFT

WithdrawalNotReady

error WithdrawalNotReady(uint256 tokenId, uint40 expiration)

Error produced when trying to execute a withdrawal ahead of time (WithdrawalRequest.when)

CannotDoZeroWithdrawals

error CannotDoZeroWithdrawals()

Error produced when trying to schedule a withdrawal with zero amount

Public Functions

constructor

constructor(contract IPolicyPool policyPool_) public

initialize

function initialize(string name_, string symbol_) public virtual

Initializes the Cooler contract

Parameters

Name Type Description
name_ string ERC721 name attribute of the NFT collection
symbol_ string ERC721 symbol attribute of the NFT collection

supportsInterface

function supportsInterface(bytes4 interfaceId) public view virtual returns (bool)

pendingWithdrawals

function pendingWithdrawals(contract IEToken eToken) external view returns (uint256)

Returns the amount of pending (scheduled) withdrawals for a given eToken

Parameters

Name Type Description
eToken contract IEToken The eToken (see {EToken})

Return Values

Name Type Description
[0] uint256 The amount in currency that is pending

cooldownPeriod

function cooldownPeriod(contract IEToken eToken, address, uint256) public view returns (uint40)

Returns the cooldown period in seconds required for withdrawals in a given eToken

Parameters

Name Type Description
eToken contract IEToken The eToken (see {EToken})
address
uint256

Return Values

Name Type Description
[0] uint40 The cooldown period in seconds

setCooldownPeriod

function setCooldownPeriod(contract IEToken eToken, uint40 newCooldownPeriod) external

Sets the cooldown period for a specific EToken

Parameters

Name Type Description
eToken contract IEToken The EToken contract address to configure the cooldown period for
newCooldownPeriod uint40 The new cooldown period duration in seconds

Emits

CooldownPeriodChanged
event with the old and new cooldown periods

scheduleWithdrawalWithPermit

function scheduleWithdrawalWithPermit(contract IEToken eToken, uint40 when, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external returns (uint256 tokenId)

Schedules a withdrawal using EIP-2612 permit for gasless approval

This function allows users to schedule withdrawals without prior ERC20 approvals by using EIP-2612 permit signatures for gasless transactions

Parameters

Name Type Description
eToken contract IEToken The EToken contract from which to withdraw
when uint40 The timestamp when the withdrawal should be executable (when =0 uses the minimum cooldown period)
amount uint256 The amount of eTokens to withdraw
deadline uint256 The expiration timestamp for the permit signature
v uint8 The recovery byte of the signature
r bytes32 The R component of the signature
s bytes32 The S component of the signature

Return Values

Name Type Description
tokenId uint256 The NFT ID of the token representing the withdrawal position

Emits

WithdrawalRequested

scheduleWithdrawal

function scheduleWithdrawal(contract IEToken eToken, uint40 when, uint256 amount) external returns (uint256 tokenId)

Schedules a withdrawal

Parameters

Name Type Description
eToken contract IEToken The EToken contract from which to withdraw
when uint40 The timestamp when the withdrawal should be executable (when =0 uses the minimum cooldown period)
amount uint256 The amount of eTokens to withdraw

Return Values

Name Type Description
tokenId uint256 The NFT ID of the token representing the withdrawal position

Emits

WithdrawalRequested

executeWithdrawal

function executeWithdrawal(uint256 tokenId) external

Executes a previously scheduled withdrawal after the cooldown period has elapsed

This function processes a withdrawal request that has completed its cooldown period, transferring the underlying tokens to the token owner and cleaning up the withdrawal state.

Parameters

Name Type Description
tokenId uint256 The ID of the token representing the withdrawal position to execute

Pre-conditions

  • The withdrawal request must exist (request.requestedAt != 0)
  • The cooldown period must have elapsed (block.timestamp >= request.expiration)

Emits

WithdrawalExecuted
with the requested and actual withdrawn amounts

getCurrentValue

function getCurrentValue(uint256 tokenId) external view returns (uint256)

Returns the current withdrawable value for a given withdrawal position

Parameters

Name Type Description
tokenId uint256 The ID of the token representing the withdrawal position

Return Values

Name Type Description
[0] uint256 The current withdrawable amount in underlying tokens

getWithdrawalRequestInfo

function getWithdrawalRequestInfo(uint256 tokenId) external view returns (struct Cooler.WithdrawalRequest)

Returns the information of a pending withdrawal request

Parameters

Name Type Description
tokenId uint256 The ID of the token representing the withdrawal position

Return Values

Name Type Description
[0] struct Cooler.WithdrawalRequest The information about the withdrawal request

Private Functions

__Cooler_init

function __Cooler_init(string name_, string symbol_) internal

_scheduleWithdrawal

function _scheduleWithdrawal(contract IEToken eToken, uint40 when, uint256 amount) internal returns (uint256 tokenId)

_computeCurrentValue

function _computeCurrentValue(struct Cooler.WithdrawalRequest request) internal view returns (uint256)