> For the complete documentation index, see [llms.txt](https://docs.idle.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.idle.finance/developers/best-yield/get-user-earnings.md).

# Get user earnings

Tracking earnings on Idle is easy, you just need the avg buy price of the IdleTokens, the amount of IdleTokens held by a user and the current price.

IdleToken contract saves and updates the `userAvgPrices` mapping which contains the avg price paid for each IdleToken of a user, so to calculate earnings (in underlying) one can do the following:

```javascript
address user = <address_of_user>;
IIdleToken idleDAI = IIdleToken(<address_idle_token>);
uint256 userBalance = idleDAI.balanceOf(user);
uint256 avgCost = userBalance.times(idleDAI.userAvgPrices(user)).div(10**18);
uint256 currentValue = userBalance.times(idleDAI.tokenPriceWithFee()).div(10**18);
uint256 earnings = currentValue.minus(avgCost);
```

{% hint style="info" %}
Note: `tokenPriceWithFee` is available only for IdleToken of the Best yield strategy. For the Risk adjusted strategy fee must be accounted on the integrators side or by using [this helper](https://etherscan.io/address/0x04Ce60ed10F6D2CfF3AA015fc7b950D13c113be5#code).\
\
[Here](https://twitter.com/emilianobonassi/status/1344419709597458432) you can find more info and also a [follow up here](https://twitter.com/emilianobonassi/status/1344550511538802688).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.idle.finance/developers/best-yield/get-user-earnings.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
