CoinAlpha / basket-protocol

A trust-less, non-custodial asset management protocol
Apache License 2.0
27 stars 18 forks source link

Rebalance #16

Closed fengtality closed 6 years ago

fengtality commented 6 years ago

Motivation

The top question we get from experienced asset managers and investors is how Basket handles rebalancing. While this is possible by buying the old Basket and selling a different quantity of the new Basket, it may be expensive since the Supplier has to double up in inventory to effectuate the transfer.

Since most rebalances only change a small subset of the tokens in the basket, we should be able to do a swap via that minimizes the amount of inventory utilized by the Supplier. The challenge is in doing so efficiently, i.e. minimizing the gas cost for a rebalance, especially for Baskets with a larger number of tokens.

Summary

This introduces a BasketDelta smart contract which intermediates a rebalance from one Basket to another. Also, it introduces a PermittedDeltas whitelist to validate BasketDelta contracts so that a new rebalance function in the Basket contract can be safely called without excess looping.

The Supplier injects the new tokens into BasketDelta. After a rebalance, the Supplier gets backs the excess tokens from the old Basket.

Definitions

How it works

BasketDelta

A special class that faciltates rebalances of Baskets. Like Basket tokens, BasketDelta tokens can be minted. However, when a BasketDelta token is consumed in a rebalance, it is automatically debundled. May also have an escrow function.

PermittedDeltas

PermittedDeltas is a whitelist in the BasketRegistry contract that defines a permitted BasketDelta for each (b0, b1) pair. To add a d1 token to the whitelist, someone needs to call a function that checks that the d1 token is valid, i.e. the property defined in d1 holds. This function may need to conduct a few loops and thus may be expensive, but once it is called, the presence of d1 in the whitelist prevents the need for redundant looping in each rebalance.

rebalance(address b1, address d1)

A function on the Basket contract. It does the following:

  1. Checks the PermittedDeltas whitelist to verify that address d1 exists for the (b0, b1) pair, whereb0` is the current Basket contract. If true, this means that the rebalance can be safely done in one loop.
  2. Checks msg.sender has a non-zero balance of b0.
  3. Checks d1 balance is sufficient.
  4. For each token t in b1, transfer r1 * b0[t:q] to b1, and transfer 1 - r1 * b0[t:q] to the owner of d1.
  5. Transfer the tokens in d1[t] to b1.
  6. Mint a b1 token belonging to the caller of the rebalance function.

Example

To come.