> For the complete documentation index, see [llms.txt](https://docs.inverter.network/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.inverter.network/contracts/technical-reference/modules/logic-module/lm_pc_recurringpayments_v1.sol.md).

# LM\_PC\_RecurringPayments\_v1.sol

[Git Source](https://github.com/InverterNetwork/inverter-contracts/blob/649b450f02fc8b735c128ff0821467e71966c666/src/modules/logicModule/LM_PC_RecurringPayments_v1.sol)

**Inherits:** ILM\_PC\_RecurringPayments\_v1, ERC20PaymentClientBase\_v1

**Author:** Inverter Network

Facilitates the creation, management, and execution of scheduled recurring payments within the Inverter Network, allowing for systematic and timed financial commitments or subscriptions.

*Uses epochs to define the period of recurring payments and supports operations such as adding, removing, and triggering payments based on time cycles. Integrates with {ERC20PaymentClientBase\_v1} for handling actual payment transactions. Note that it will use the token type stored in the FundingManager for the payments.*

### State Variables

#### \_SENTINEL

*Marks the beginning of the list.*

```solidity
uint internal constant _SENTINEL = type(uint).max;
```

#### \_nextId

*Value for what the next id will be.*

```solidity
uint private _nextId;
```

#### epochLength

*length of an epoch.*

```solidity
uint private epochLength;
```

#### \_paymentRegistry

*Registry mapping ids to RecurringPayment structs id => RecurringPayment.*

```solidity
mapping(uint => RecurringPayment) private _paymentRegistry;
```

#### \_paymentList

*List of RecurringPayment id's.*

```solidity
LinkedIdList.List _paymentList;
```

#### \_\_gap

*Storage gap for future upgrades.*

```solidity
uint[50] private __gap;
```

### Functions

#### supportsInterface

*See {IERC165-supportsInterface}.*

```solidity
function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(ERC20PaymentClientBase_v1)
    returns (bool);
```

#### validId

*Checks if the given id is valid.*

```solidity
modifier validId(uint recurringPaymentId);
```

**Parameters**

| Name                 | Type      | Description                              |
| -------------------- | --------- | ---------------------------------------- |
| `recurringPaymentId` | `uint256` | The id of the RecurringPayment to check. |

#### validStartEpoch

*Checks if the given startEpoch is valid.*

```solidity
modifier validStartEpoch(uint startEpoch);
```

**Parameters**

| Name         | Type      | Description                                      |
| ------------ | --------- | ------------------------------------------------ |
| `startEpoch` | `uint256` | The startEpoch of the RecurringPayment to check. |

#### startIdBeforeEndId

*Checks if the startId is before the endId.*

```solidity
modifier startIdBeforeEndId(uint startId, uint endId);
```

**Parameters**

| Name      | Type      | Description                                   |
| --------- | --------- | --------------------------------------------- |
| `startId` | `uint256` | The startId of the RecurringPayment to check. |
| `endId`   | `uint256` | The endId of the RecurringPayment to check.   |

#### init

```solidity
function init(
    IOrchestrator_v1 orchestrator_,
    Metadata memory metadata,
    bytes memory configData
) external override(Module_v1) initializer;
```

#### getEpochLength

Returns the length of an epoch.

```solidity
function getEpochLength() external view returns (uint);
```

**Returns**

| Name     | Type      | Description                                         |
| -------- | --------- | --------------------------------------------------- |
| `<none>` | `uint256` | epochLength Length of an epoch in a uint timestamp. |

#### getRecurringPaymentInformation

Returns the RecurringPayment instance with id `id`.

```solidity
function getRecurringPaymentInformation(uint id)
    external
    view
    validId(id)
    returns (RecurringPayment memory);
```

**Parameters**

| Name | Type      | Description                               |
| ---- | --------- | ----------------------------------------- |
| `id` | `uint256` | The id of the RecurringPayment to return. |

**Returns**

| Name     | Type               | Description                    |
| -------- | ------------------ | ------------------------------ |
| `<none>` | `RecurringPayment` | RecurringPayment with id `id`. |

#### listRecurringPaymentIds

Returns total list of RecurringPayment ids.

*List is in ascending order.*

```solidity
function listRecurringPaymentIds() external view returns (uint[] memory);
```

**Returns**

| Name     | Type        | Description                   |
| -------- | ----------- | ----------------------------- |
| `<none>` | `uint256[]` | List of RecurringPayment ids. |

#### getPreviousPaymentId

Returns the id of previous RecurringPayment.

```solidity
function getPreviousPaymentId(uint id) external view returns (uint);
```

**Parameters**

| Name | Type      | Description                               |
| ---- | --------- | ----------------------------------------- |
| `id` | `uint256` | The id of the RecurringPayment to return. |

**Returns**

| Name     | Type      | Description                                 |
| -------- | --------- | ------------------------------------------- |
| `<none>` | `uint256` | prevId The id of previous RecurringPayment. |

#### isExistingRecurringPaymentId

Returns whether RecurringPayment with id `id` exists.

```solidity
function isExistingRecurringPaymentId(uint id) public view returns (bool);
```

**Parameters**

| Name | Type      | Description                             |
| ---- | --------- | --------------------------------------- |
| `id` | `uint256` | The id of the RecurringPayment to test. |

**Returns**

| Name     | Type   | Description                                                    |
| -------- | ------ | -------------------------------------------------------------- |
| `<none>` | `bool` | True if RecurringPayment with id `id` exists, false otherwise. |

#### getEpochFromTimestamp

Calculates the epoch from a given uint timestamp.

*Calculation is: timestamp divided by epochLength.*

```solidity
function getEpochFromTimestamp(uint timestamp)
    external
    view
    returns (uint epoch);
```

**Parameters**

| Name        | Type      | Description                   |
| ----------- | --------- | ----------------------------- |
| `timestamp` | `uint256` | A timestamp in a uint format. |

**Returns**

| Name    | Type      | Description                          |
| ------- | --------- | ------------------------------------ |
| `epoch` | `uint256` | Epoch in which timestamp belongs to. |

#### getCurrentEpoch

Calculates the current epoch.

*Calculation is: block.timestamp divided by epochLength.*

```solidity
function getCurrentEpoch() public view returns (uint epoch);
```

**Returns**

| Name    | Type      | Description                                                    |
| ------- | --------- | -------------------------------------------------------------- |
| `epoch` | `uint256` | Epoch in which current timestamp (block.timestamp) belongs to. |

#### getFutureEpoch

Calculates a future epoch x epochs from now.

*Calculation is: current epoch + X epochs in the future = futureEpoch.*

```solidity
function getFutureEpoch(uint xEpochsInTheFuture)
    external
    view
    returns (uint futureEpoch);
```

**Parameters**

| Name                 | Type      | Description                             |
| -------------------- | --------- | --------------------------------------- |
| `xEpochsInTheFuture` | `uint256` | How many epochs from the current epoch. |

**Returns**

| Name          | Type      | Description          |
| ------------- | --------- | -------------------- |
| `futureEpoch` | `uint256` | Epoch in the future. |

#### addRecurringPayment

Adds a recurring payment to the manager.

*a new id is created for each Payment.*

```solidity
function addRecurringPayment(uint amount, uint startEpoch, address recipient)
    external
    onlyOrchestratorAdmin
    validAmount(amount)
    validStartEpoch(startEpoch)
    validRecipient(recipient)
    returns (uint id);
```

**Parameters**

| Name         | Type      | Description                                                                                                       |
| ------------ | --------- | ----------------------------------------------------------------------------------------------------------------- |
| `amount`     | `uint256` | Amount of tokens send to the recipient address.                                                                   |
| `startEpoch` | `uint256` | Epoch in which the payment starts. Use getEpochFromTimestamp() or getCurrentEpoch() to get the appropriate epoch. |
| `recipient`  | `address` | Recipient address that should receive tokens.                                                                     |

**Returns**

| Name | Type      | Description                                |
| ---- | --------- | ------------------------------------------ |
| `id` | `uint256` | Id of the newly created recurring payment. |

#### removeRecurringPayment

Removes a recurring Payment.

```solidity
function removeRecurringPayment(uint prevId, uint id)
    external
    onlyOrchestratorAdmin;
```

**Parameters**

| Name     | Type      | Description                                               |
| -------- | --------- | --------------------------------------------------------- |
| `prevId` | `uint256` | Id of the previous recurring payment in the payment list. |
| `id`     | `uint256` | Id of the recurring payment that is to be removed.        |

#### trigger

Triggers the start of the due payments for all recurring payment orders.

```solidity
function trigger() external;
```

#### triggerFor

See trigger() but enables you to determine which ids you want to trigger payment ordes for.

*this is to being able to bypass the unlikely event of having a runOutOfGas error for the normal trigger function.*

```solidity
function triggerFor(uint startId, uint endId)
    external
    validId(startId)
    validId(endId)
    startIdBeforeEndId(startId, endId);
```

**Parameters**

| Name      | Type      | Description                                                                |
| --------- | --------- | -------------------------------------------------------------------------- |
| `startId` | `uint256` | : id of start position of the recurring payments that should be triggered. |
| `endId`   | `uint256` | : id of end position of the recurring payments that should be triggered.   |

#### \_triggerFor

*Triggers the given RecurringPayment.*

```solidity
function _triggerFor(uint startId, uint endId) private;
```

**Parameters**

| Name      | Type      | Description                                      |
| --------- | --------- | ------------------------------------------------ |
| `startId` | `uint256` | The id of the first RecurringPayment to trigger. |
| `endId`   | `uint256` | The id of the last RecurringPayment to trigger.  |
