# 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="<https://216358192-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2K6E4Us9xYRIC0Tt0SIZ%2Fuploads%2FX2wF3hZAOhZkHeeXPnjx%2FERC20.json?alt=media&token=dc2027c7-9f6b-44de-b9e6-70e40b68e6e8>" %}
