Skip to content

Trade optimizer specification

Thibauld Favre edited this page Jan 3, 2020 · 5 revisions

Trade optimizer specification

Status: DRAFT

Context

The c-org smart-contract provides a primary market for FAIRs: Anyone can buy and anyone can sell to the bonding curve. However, there is a significant spread between the buy price and sell price, leaving ample space for secondary market trading. Of course, this secondary market trading could happen on exchanges but we'd like to provide a more integrated and convenient solution. We can do it by creating a Uniswap market with a pair DAI/FAIR specific to the organization. FAIR holders could then decide to act as liquidity providers for this Uniswap market and earn Uniswap fees for doing it.

However, the existence of 2 markets in parallels (the primary market materialized by the c-org smart-contract and the secondary market materialized by the Uniswap smart-contract) poses a challenge for the investor as he/she nows needs to optimize his trade between the 2 markets.

Indeed, buying on the primary market might not be optimal if you can buy FAIRs cheaper on the secondary market. Likewise, buying on the secondary market might not be optimal if the price slippage is such that the price on the secondary market now surpasses the price of FAIR on the primary market.

The goal of this contract is to create an on-chain trade optimizer that, for a given trade (buy or sell), will optimize the trade between the primary and secondary market.

Specifications

Variables

  • market1. Address of the c-org smart-contract (Primary Market).
  • market2. Address of the Uniswap exchange contract (Secondary Market).

Methods

The contract will expose 2 functions: buy and sell.

buy(amount,minimum,to)

  • input variables:

    • amount. the amount of currency (i.e. DAI) the investor is sending to buy FAIRs.
    • minimum. the minimum amount of FAIRs the investor wants to have for amount of currency. Transaction should fail otherwise.
    • to. the address to which the FAIRs should be sent.
  • algorithm:

    1. if not(to) then to=caller_address
    2. read X=latest issuance price on market1
    3. calculate Y=future price on market2 if amount is bought on market2 [calculus to be provided]
    4. if Y<=X then
      • P1=market1.buy(amount,minimum)
      • P2=0
    5. if Y>X then
      • calculate Z=amount to buy on market2 so that
      • Y=X [calculus to be provided]
      • P1=market1.buy(amount-Z,minimum*(amount-Z)/amount)
      • P2=market2.buy(Z)
    6. transfer(P1+P2,to)

Note1: the calls to market1.buy() and market2.buy() could fail due to a lack of authorizations. Typically, if the address of the caller is not KYCed, which is verified via the TPL contract at the c-org contract level and at the FAIR token contract level.

Note2: It seems hard to enforce strong guarantees regarding the minimum amount of FAIRs that will be returned when Y>X because market2.buy() could return a amount of FAIRs much lower than anticipated when the calculation was done. What do you think?

sell(amount,minimum)

  • input variables:
    • amount. the amount of FAIRs the investor is sending to redeem currency (i.e. DAI).
    • minimum. the minimum amount of currency the investor wants to have for the amount of FAIRs he's sending. Transaction should fail otherwise.

[TO BE FINISHED 👇]

  • algorithm:
    1. read X=latest issuance price on market1
    2. calculate Y=future price on market2 if amount is bought on market2 [calculus to be provided]
    3. if Y<=X then
      • P1=market1.buy(amount,minimum)
      • P2=0
    4. if Y>X then
      • calculate Z=amount to buy on market2 so that
      • Y=X [calculus to be provided]
      • P2=market2.buy(Z)
      • P1=market1.buy(amount-Z,minimum-P)
    5. transfer(P1+P2,to)

[/TO BE FINISHED ☝]

Task and expections

  • Implement the trade-optimizer smart-contract in Solidity.
  • Test it extensively with a c-org contract and the associated uniswap exchange.
  • Be available to answer questions from the auditors and to fix what needs to be fixed.