# Premiums

When a position is first opened, a trader requires a non-zero amount of premiums in his deposit, indexed by his position id.&#x20;

<pre class="language-solidity"><code class="lang-solidity"><strong>mapping(PositionId => uint256) public PremiumDeposit;
</strong></code></pre>

Premiums are computed and paid from this deposit every time a trader adds or reduces a position.&#x20;

An off-chain bot will continuously monitor positions and close positions that satisfy the following condition:&#x20;

<pre class="language-solidity"><code class="lang-solidity">// Get premiums quoted this block
// This will be automatically paid when a trader is interacting with the protocol
<strong>uint256 premiumQuoted = computePremiums(PositionId); 
</strong><strong>
</strong><strong>// can force close if deposit is less than the instantaneous premiums owed 
</strong>bool canForceClose = PremiumDeposit[PositionId] &#x3C; premiumQuoted; 
</code></pre>

## Computing Premiums

A pseudocode for computing premiums is presented below.&#x20;

```solidity
function computePremiums( PositionId positionId) public view returns(uint256 premium){
    
    (uint lastPremiumPaymentTime, BorrowedTick[] memory borrowedTicks)
        = getPositionInfo(PositionId); 
        
    // get the time-weighted averaged tick between current time and last payment time
    // The 'distance' between the twat and the borrowed ticks will determine the 
    // multiplier
    int24 twat = getTimeWeightedAverageTick(block.timestamp - lastPremiumPaymentTime); 
    
    for(uint i; i<borrowedTicks.length; i++){
        // the closer the twat to the borrowed tick, the higher the multiplier
        uint multiplier = getMultiplier(twat, borrowedTicks[i].tick); 
        
        // get the interest accumulator at the given tick
        uint interestGrowthInTick = getInterestGrowth(borrowedTicks[i].tick); 
        
        // since interestGrowth is an accumulator, need to get actually owed interest
        uint interest = (interestGrowthInTick/borrowedTicks[i].lastInterestGrowth); 
        
        // scale interest by multiplier
        interest = interest * multiplier; 
        
        // add to total premiums for all borrowed ticks
        premium += interest * toTokenAmounts(borrowedTicks[i].liquidity)
    }
    
    return premium; 
}

function getInterestGrowth(int24 tick) public view returns(uint256){
    UtilizationGrowth memory growth = UtilizationGrowths[tick];
    
    // interest rate per second is proportional to utilization rate, 
    // where the utilization rate is recorded whenever a user provides,withdraws,borrows, or repays
    // the tick
    uint percentageIncreaseFromLastGrowth = getInterestRate(growth.lastURate) 
            ** (block.timestamp- growth.lastUpdateTime); 
            
    // the higher the last recorded utilization rate of the tick, the faster
    // the rate of growth
    uint totalAccumulatedGrowthForTick = growth.lastGrowth * percentageIncreaseFromLastGrowth;
    return totalAccumulatedGrowthTick;
}


```

Since the growth object of a given tick is updated every time an LP provides or withdraws or when a trader borrows and repays, the accumulator will be reflective of all debt originated from that tick.&#x20;

#### Interest Rate

The `getInterestRate` function above is a piecewise function with a pivot rate. Each tick is characterized by its rate function.&#x20;

<figure><img src="https://20107516-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhzaQpygyckQc1GpfmDWt%2Fuploads%2FO4AWRl8aEa3XuY8dcp1M%2FKakaoTalk_Photo_2023-12-29-23-11-02%20003.png?alt=media&#x26;token=61018fed-f202-424a-9625-b75eb81e7997" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://limitless.gitbook.io/limitless/advanced/premiums.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
