---
# System prepended metadata

title: New APR reward system.

---

# New APR reward system.

## For all Lend, Borrow, and LP

$$ \text{reward_APR} = \frac{\text{reward_per_second}(\text{year})}{\text{total_principal_amount_converted_to_dollars}}$$




## Accounting Logic
- Have user positions dict
- This dict has the follwing structure - `key (user) value (list (tuple (tokenAmount, positionAmount, timestamp)))`
- When a user opens a position ordered by timetamp, logIndex:
    - Check user_positions has the user or not,
        - If yes: append the position as follows to the list (tuple (tokenAmount, positionAmount, timestamp))
        - Else: Create a list with the with a single entity [(tuple (tokenAmount, positionAmount, timestamp))]
- When a user closes a position ordered by timestamp, logIndex:
    - Get the list from the user positions dict
    - For the given position amount that is being closed, keep closing each tuple as follows:
        - If the close position amount is x, and the current position amount is y
        - if x> y, delete the current action tuple and continue
        - if x=y, delete the currrent action tuple
        - if x < y, update current action tuple as y-x


## Reward distribution logic
- Initialize a dict user_rewards with the following structure `key (user) value (reward amount)`
- Loop across timestamps from start/end timestamp
    - Get current user action from the user_positions dict 
    - Get the reward for the given timestamp - let this be r
    - Get the sum of all token amounts across all users in the dict - let this be U
    - Get the sum of all token amounts for the given user - let this be u
    - That implies the reward for user for the given timestamp is u*r/U
    - Update user_rewards as follows: user_rewards[user] = user_rewards.get(user,0) + u*r/U
