Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: technical reference #28

Merged
merged 12 commits into from
Nov 14, 2023
Merged

feat: technical reference #28

merged 12 commits into from
Nov 14, 2023

Conversation

mfw78
Copy link
Contributor

@mfw78 mfw78 commented Nov 3, 2023

Description

This PR contains the canonical Technical Reference for CoW Protocol.

Changes

  • Implemented core / periphery methodology
  • Migrated previous documentation, and had proof-read
  • All contracts extensively documented
  • Typedoc generation for SDKs
  • Interactive swagger use for APIs

Related Issues

Fixes #6, #8

@mfw78 mfw78 added documentation Improvements or additions to documentation E:2.1: Documentation and Tutorials https://github.com/cowprotocol/pm/issues/10 labels Nov 3, 2023
@mfw78 mfw78 self-assigned this Nov 3, 2023
Copy link

vercel bot commented Nov 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2023 1:07pm

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good stuff! 👏
Only nits but didn't get through everything, yet.

docs/partials/_cancellation_offchain_versus_onchain.mdx Outdated Show resolved Hide resolved

:::caution

Be very careful when signing an intent to trade. All of the associated parameters are final and cannot be changed once the intent is signed and submitted to the API. If you make a mistake, you will need to cancel the intent and create a new one.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link on cancel would be nice.


## Cancel a limit order

Now we've been doing some thinking and realised we shouldn't be selling `COW`, so we want to cancel our limit order. We can do this by clicking the context menu on the order and selecting 'Cancel order':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit strange that we have a separate section for cancelling a market order but canceling a limit order is part of the limit order page.
Maybe it makes sense to document market and limit order cancelling on the same page to avoid repeating ourselves (e.g. off-chain cancellation not guaranteed and so on).


## Confirm swap

When you're ready to swap, click the "Swap" button. You will be prompted with a confirmation dialog to confirm the swap. This is where you can review the details of the swap (ie. the quote, the slippage tolerance, etc.). If you're happy with the details, click "Confirm Swap". You will be prompted to sign the intent to swap.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: New docs use ie. or ie whereas existing docs use i.e..


:::caution

Allowing interactions by solvers, creates the possibility of malicious solvers that can steal funds from the settlement contract. This is why the Protocol only allows interactions to be executed by whitelisted solvers, and requires that the solver post a bond to the Protocol before it can be whitelisted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a section explaining the process, reasoning and details for solver bonds in detail that we could link here?

Copy link
Contributor

@fedgiac fedgiac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review of the contracts section, core + ETH-flow. The core is basically spotless, really nice!

Comment on lines +89 to +100
| `sellToken` | `ETH` | `WETH` | |
| `buyToken` | any | same as user | |
| `receiver` | `!= address(0)` | same as user | Must **NOT** be the zero address as this has the meaning of `self` in CoW Protocol |
| `sellAmount` | any | same as user | |
| `buyAmount` | any | same as user | |
| `validTo` | any | `type(uint32).max` | Required to be fixed at the maximum point in the future as `filledAmount` in `GPv2Settlement` contract is relied upon which can be cleared by `freeFilledAmountStorage` |
| `appData` | any | same as user | |
| `feeAmount` | any | same as user | |
| `kind` | `sell` | `sell` | Limited to `sell` intents only as dust from `buy` intents left in the Eth-flow contract would not be economical for a user to withdraw |
| `partiallyFillable` | any | same as user | |
| `sellTokenBalance` | `external` | `external` | Only `external` implemented |
| `buyTokenBalance` | `external` | `external` | Only `external` implemented |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a user intent doesn't really "exist" as described onchain, the data used onchain is (sligtly) different. We could mention somewhere how this corresponds to the actual ETH-flow data.


:::note

Orders are not matched by the CoW Protocol infrastructure unless the `quoteId` refers to a valid and fresh quote in the API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still true?

docs/cow-protocol/reference/contracts/periphery/README.mdx Outdated Show resolved Hide resolved
Copy link
Contributor

@fedgiac fedgiac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I finished my review of the contract section. My understanding of ConditionalCoW is still a bit shallow, so I'm not sure all comments actually make sense.

Comment on lines +33 to +40
As there are many nested contracts, it's important for a callee to know some context from the caller. To achieve this, ComposableCoW passes a `bytes32` variable `ctx` to the callee, such that:

```
ctx = merkle root of orders: bytes32(0)
single order: H(ConditionalOrderParams)
```

Having this context also allows for conditional orders / merkle roots to use this as a key in a mapping, to store conditional order-specific data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execution context is zero if the order isn't a single order, right? Considering that single orders aren't recommended, I would clearly say that this is only used in this case.

Suggested change
As there are many nested contracts, it's important for a callee to know some context from the caller. To achieve this, ComposableCoW passes a `bytes32` variable `ctx` to the callee, such that:
```
ctx = merkle root of orders: bytes32(0)
single order: H(ConditionalOrderParams)
```
Having this context also allows for conditional orders / merkle roots to use this as a key in a mapping, to store conditional order-specific data.
Single orders allow the caller to specify additional context data.
As there are many nested contracts, it's important for a callee to know some context from the caller. To achieve this, ComposableCoW passes a `bytes32` variable `ctx` to the callee, such that:
```
ctx = H(ConditionalOrderParams)
```
Having this context also allows for conditional orders / merkle roots to use this as a key in a mapping, to store conditional order-specific data.
When using orders based on Merkle trees instead of single orders, then there is no execution context and `ctx` is set to `bytes32(0)`.

Comment on lines 46 to 56
```mermaid
flowchart TD
A[Extensible Fallback Handler: SignatureVerifierMuxer] -->|isValidSafeSignature| B[Check Authorization: MerkleRoot \n Proof & ConditionalOrderParams]
B -->|valid| S[SwapGuard:verify]
S -->|invalid| I[Revert]
S -->|valid| V[IConditionalOrder:verify]
B -->|invalid| C(Check Authorization: Single Order \n ConditionalOrderParams)
C -->|valid| V
C -->|invalid| I
V -->|valid| T[Return ERC1271 Magic]
V -->|invalid| I
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This graph seems inconsistent with the contract to me. Two things are weird:

  • Single order authorization is checked only if Merkle proof verification fails. Arguably not wrong, since size(proof) == 0 can be considered part of the verification, but I think it's better if we don't see it as a failure but as an option between two choices. (Otherwise, as a developer, I worry that if my signature is an invalid Merkle proof then it might somehow be interpreted as a valid single order signature).
  • In the diagram, SwapGuard:verify is only checked if it's a valid Merkle proof, but to my understanding it's also checked for single orders.
This is what I'd have expected.
flowchart TD
    A[Extensible Fallback Handler: SignatureVerifierMuxer] -->|isValidSafeSignature| B[Check Authorization]
    B -->|valid single order| S[SwapGuard:verify] 
    B -->|valid Merkle proof| S
    B -->|invalid| I[Revert]
    S -->|valid| V[IConditionalOrder:verify]
    S -->|invalid| I
    V -->|valid| T[Return ERC1271 Magic]
    V -->|invalid| I
Loading


:::note

All values **EXCLUDING** `offchainInput` are **verified** by ComposableCoW prior to calling an order type's `verify`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is key to the composable CoW framework and I'm not sure it's stressed enough.

My understanding of this framework is that the contract stores order information onchain (be it single orders or through a Merkle root) and all these parameters are exactly what is stored onchain for verification. A developer doesn't need to bother validating this data again, and this is why you'd use this framework rather than doing everything yourself.

Suggested change
All values **EXCLUDING** `offchainInput` are **verified** by ComposableCoW prior to calling an order type's `verify`.
ComposableCoW is responsible for checking that all values **EXCLUDING** `offchainInput` belong to an order that was previously registered on-chain.

However, this begs the question of how order creation/registration works, so maybe that should be handled earlier?


### Discrete order generators

A conditional order that generates discrete orders shall implement the `IConditionalOrderGenerator` interface.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the discrete order is the CoW Swap order that would be tradable, but when I was reading this I thought this was a way to create a valid order in composable CoW. I see, going back, that there is a distinction between that and a conditional order, but it's easier to forget, and also one expects first to create a conditional order and then to use it.

In general, it would be great in the architecture part to spell out that "discrete orders" is what will actually be executed on CoW Swap.

Comment on lines +296 to +309
| `SWARM` | `3` | `abi.encode(bytes32 swarmCac)` |
| `IPFS` | `5` | `abi.encode(bytes32 ipfsCid)` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there no 4?

Comment on lines +512 to +535
## Off-chain

### Watch-tower

As these orders are not automatically indexed by the CoW Protocol, there needs to be some method of relaying them to the Order Book API for inclusion in a batch.

This is the responsibility of a [watch-tower](https://github.com/cowprotocol/watch-tower). CoW Protocol runs a watch-tower that will monitor the `ConditionalOrderCreated` event, and relay the discrete orders to the Order Book API.

There is also a [DAppNode package for running a watch-tower](https://github.com/cowprotocol/dappnodepackage-cow-watch-tower).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already discussed the watchtower quite a bit before introducing it. Maybe we should move this to the architecture section? It's part of why we have conditional orders, so it makes sense to me, even if it isn't strictly contract-related.

Copy link
Contributor

@fhenneke fhenneke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through the rewards and slippage part of the docs. @harisang could you have a quick look as well?

docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/rewards.md Outdated Show resolved Hide resolved
docs/cow-protocol/reference/core/auctions/slippage.md Outdated Show resolved Hide resolved
Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
docusaurus-json-schema-plugin 1.7.0 None +110 107 MB jy95

@mfw78
Copy link
Contributor Author

mfw78 commented Nov 14, 2023

OK, I've addressed most of the above comments / integrated suggestions. I'm merging this, leaving the above unresolved, to come back to towards the top of the waterfall / CoW DAO-wide review of docs.

@mfw78 mfw78 merged commit ac8a9f0 into main Nov 14, 2023
4 checks passed
@mfw78 mfw78 deleted the technical-reference branch November 14, 2023 13:15
@github-actions github-actions bot locked and limited conversation to collaborators Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation E:2.1: Documentation and Tutorials https://github.com/cowprotocol/pm/issues/10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: composable-cow documentation
4 participants