# IdeaTokenExchange

The `IdeaTokenExchange` handles the exchange (buy and sell) of `IdeaTokens` using a bonding curve for pricing. New`IdeaTokens` are minted on buy and existing `IdeaTokens` are burned on sell. The exchange also handles the distribution of generated interest aswell as trading and platform fees.

* `buyTokens`: Buys `IdeaTokens` in exchange for `Dai`. The cost for the purchase is determined using the bonding curve
  * `address ideaToken`: The address of the IdeaToken to be bought
  * `uint amount`: The amount of IdeaTokens to buy
  * `uint fallbackAmount`: The amount of `IdeaTokens` to buy if the cost to buy `amount` exceeds `cost` (slippage). This can happen if the price changes between submitting the transaction and it being executed
  * `uint cost`: The maximum cost in `Dai` the buyer is willing to pay
  * `address recipient`: The recipient of the bought `IdeaTokens`
* `sellTokens`: Sells `IdeaTokens` in exchange for `Dai`. The price is determined using the bonding curve
  * `address ideaToken`: The address of the `IdeaToken` to be sold
  * `uint amount`: The amount of `IdeaTokens` to sell
  * `uint minPrice`: The minimum price in `Dai` the seller wants for selling this `amount` of `IdeaTokens` (slippage)
  * `address recipient`: The recipient of the `Dai`

When tokens are bought the spent Dai are invested into an interest generating protocol using the `InterestManager`. Currently this is `InterestManagerCompound` which invests `Dai` into Compound. Trading and platform fees are also collected according to the parameters set in the `IdeaTokenFactory`.

When `allInterestToPlatform = false` then the interest which is generated by an `IdeaToken` can be withdrawn by the owner of the token using the `withdrawTokenInterest` method. The token owner must be previously set using `setTokenOwner` which is admin-only. If the owner is the zero address, i.e. an owner has not been set, a special account called the `authorizer` is allowed to initially set the token owner address. This allows for automated token owner verification without having to go through the timelock. Once the owner has been set the `authorizer` is not allowed to change the address anymore. Only the token owner and the admin (with timelock) can change it from now on.

When `allInterestToPlatform = true` then all interest generated by the tokens in a market can be withdrawn by the platform owner using `withdrawPlatformInterest`. The platform owner must be previously set using `setPlatformOwner` which is admin-only. The same logic regarding the `authorizer` account applies as for `withdrawTokenInterest`.

The collected platform fee can be withdrawn using `withdrawPlatformFee`. Again the platform owner must be set using `setPlatformOwner`.

The collected trading fee can be withdrawn using `withdrawTradingFee`. This method is not protected and can be called by anyone as it will always send the trading fee to the `tradingFeeRecipient` which is passed to the contract during initialization.

`setTokenFeeKillswitch` can be used to disable all trading and platform fee collection for a single `IdeaToken`.
