# Tokens rebalancing

The goal of this function is to rebalance a three-token portfolio to reach specific target weights (measured in basis points), using USD valuations and slippage-protected swaps.

For example, suppose the targets are:

* 50% BTC
* 30% ETH
* 20% DAI

If the portfolio currently holds:

* $6,500 in BTC (65%)
* $2,500 in ETH (25%)
* $1,000 in DAI (10%)

Since BTC is overweight while ETH and DAI are underweight, the function will swap $500 worth of BTC into ETH and $1,000 worth of BTC into DAI to restore the target balance.

### Function

{% @github-files/github-code-block url="<https://github.com/mimic-protocol/examples/blob/main/examples/10-rebalancing-tokens/src/function.ts>" visible="false" %}

[Github link](https://github.com/mimic-protocol/examples/blob/main/examples/10-rebalancing-tokens/src/function.ts)

### Manifest

{% @github-files/github-code-block url="<https://github.com/mimic-protocol/examples/blob/main/examples/10-rebalancing-tokens/manifest.yaml>" visible="false" %}

[Github link](https://github.com/mimic-protocol/examples/blob/main/examples/10-rebalancing-tokens/manifest.yaml)

The ABI used for this example can be downloaded below:

{% file src="/files/RVbks3Xm1ai4RZWygXNX" %}


---

# 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://docs.mimic.fi/use-cases/tokens-rebalancing.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.
