# Funding tick

Funding payments are part of the business logic of a perpetual application. A funding tick is a mechanism to credit or debit users' positions, according to a predefined logic, in order to incentivize certain strategies.

 Since StarkEx already supports periodic modification of users' balances, this support can be easily extended for different purposes, such as debt management or periodic rewards.

## 1. The operator sends a funding tick to StarkEx

The operator sends StarkEx funding ticks at a regular interval, at least once every twenty four hours, by calling the `add_transaction` API with the `FundingTick` transaction type. The API call specifies the exact date and time of the funding tick, in seconds since the Unix epoch, as well as a dictionary of global funding indices.

 The operator defines the interval in the general configuration, and StarkWare approves it. Although the minimum required frequency for sending funding ticks is once every twenty four hours, it is recommended to send them several times a day, or even more frequently, because perpetual systems have an internal system time that becomes increasingly out-of-synch as time passes. Funding ticks synchronize the system time.
 Every type of asset in the system should have a corresponding global funding index.

## 2. StarkEx verifies the validity of a funding tick

StarkEx checks the following conditions:

• The new system time is greater than the previous system time.

• For every asset type, represented by `asset_id`, the following condition holds:

|\text{prev_global_index} - \text{global_index}| \leq \text{config.max_funding_rate} \cdot (\text{system_time}-\text{prev_funding_time}) \cdot \text{current_price[asset_id]}

Where `config.max_funding_rate` is an on-chain configuration parameter, for more details see Global configuration. `current_price` is set according to the last oracle price tick. For more information, see Oracle price tick.

 In the above formula, the time since the previous funding tick directly affects the change allowed by the funding tick. In order to limit the permitted change, StarkEx verifies the following at the beginning of every transaction: \text{system_time} - \text{prev_funding_time} \leq \text{funding_valdity_period} For more information on `funding_validity_period`, see Global configuration

## 3. Funding tick effect

After the funding tick is accepted, an array called `global_indices` is updated with a new value for every type of asset.

The operator updates the positions that a user holds only when that user executes a transaction that affects their position. So not every funding tick affects every user’s positions.

When a user executes a transaction that affects a position that they hold, the operator adds funds to or subtracts funds from the position. Each position stores a value called `position_cached_indices` for every type of synthetic asset. `position_cached_indices` represents the most recent funding tick that affected it.

The following transaction types affect positions:

The execution of a transaction consists of two steps:

1. For every type of synthetic asset, reduce the funding debit from the balance and update the position indices, as follows:

\text{position_balances[collateral]} -= \text{position_balances[asset_id]} \cdot \\ (\text{global_indices[asset_id]} - \text{position_cached_indices[asset_id]})
\text{position_cached_indices[asset_id]}= \text{global_indices[asset_id]}
2. Apply the transaction-specific logic.

If a transaction is invalid then neither of these steps is executed.

## 4. Batch validity checks

If the funding tick is valid, it is included in a batch to be submitted on-chain along with a validity proof. For more information on on-chain state updates, see State Update.

As part of the batch validity checks, the StarkEx contract checks that the most recent funding tick’s timestamp in the batch was within the last week. If not, the batch reverts, with the error `SYSTEM_TIME_OUTDATED`.

This limits the time between two funding ticks and thus limits the funding debits.

## Implementing a funding tick

To send a funding tick, use the `add_transaction` API method with the `FundingTick` transaction type.

The StarkEx REST API reference includes all necessary information.