cowprotocol / services

Off-chain services for CoW Protocol
https://cow.fi/
Other
203 stars 79 forks source link

chore: Enable quoting with JIT orders liquidity #3082

Open squadgazzz opened 1 month ago

squadgazzz commented 1 month ago

Background

From https://github.com/cowprotocol/services/issues/3027:

Currently CoW Protocol is in an awkward spot because it can not be the only source of liquidity yet. If all liquidity for a token rests in CoW Protocol's orderbook or some CoW AMM solvers would have no way of using it to quote a price for a given token.

Details

Previous attempts to implement it(https://github.com/cowprotocol/services/pull/3033 and https://github.com/cowprotocol/services/pull/3038) showed that this is a more involved change than anticipated, which also requires breaking API changes.

Currently, JIT orders get simulated on limit prices which means they will not get any surplus. Any surplus that can be generated by using a CoW AMM would, therefore, go to the user quote. In reality, the user order that comes out of the quote would be settled at a worse price because if it gets settled against a CoW AMM order surplus will have to be shared. So, the solvers should be able to tell us at which price things should get settled so that we can reason about distributing surplus even in quotes.

To fix that we should allow solvers to return pre-interactions, JIT orders and uniform clearing prices with their quote. That way solvers could quote with CoW AMMs (require JIT orders, pre-interactions and UCP). They could also index the orderbook and return resting limit orders as JIT orders.

Tasks

Quote:
  type: object
  properties:
    ..existing fields
Quote:
  oneOf:
    - $ref: "#/components/schemas/LegacyQuote"
    - $ref: "#/components/schemas/QuoteWithJitOrders"

LegacyQuote:
  type: object
  properties:
    ..existing fields

QuoteWithJitOrders:
  type: object
  properties:
    ..existing fields, where out_amount is replaced with the clearingPrices

    clearingPrices:
      type: array
      items:
        $ref: "#/components/schemas/Price"
      nullable: true
    jitOrders:
      type: array
      items:
        $ref: "#/components/schemas/JITOrder"
      nullable: true
    preInteractions:
      type: array
      items:
        $ref: "#/components/schemas/Interaction"
      nullable: true

MartinquaXD commented 1 month ago

nit: The new quote struct should have the uniform clearing prices instead of out_amount because the out_amount can be computed based on the prices. Other than that the plan looks good. Also clearingPrices currently contains JITOrders in your suggested openapispec. But I assume this was more for demonstration purposes anyway.

squadgazzz commented 1 month ago

The new quote struct should have the uniform clearing prices instead of out_amount because the out_amount can be computed based on the prices. Other than that the plan looks good.

Ah, right. Updated the example.

Also clearingPrices currently contains JITOrders in your suggested openapispec. But I assume this was more for demonstration purposes anyway.

Thanks, it was a typo, replaced with a Price object.