> For the complete documentation index, see [llms.txt](https://docs.mimic.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mimic.fi/use-cases/tokens-rebalancing.md).

# 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" %}
