cowprotocol / cow-amm

An automated market maker running on top of CoW Protocol
17 stars 5 forks source link

Use the correct formula to compute surplus of a CoW AMM #91

Closed acanidio-econ closed 4 months ago

acanidio-econ commented 4 months ago

Following the discussions on Slack (here and here), we should use the correct math to compute surplus for a CoW AMM. This means that the buyAmount should be on the curve. The correct formula is: buyAmount = Math.mulDiv( sellAmount, selfReserve1, selfReserve0-sellAmount, Math.Rounding.Up ); so that the buyAmount corresponds to zero surplus for AMM given the sellAmount.

And similarly a few lines below

https://github.com/cowprotocol/cow-amm/blob/677e124edd33052c7c14c2e6b0891241b1a0ae4b/src/ConstantProduct.sol#L279C13-L284C15

mfw78 commented 4 months ago

@acanidio-econ given recent discussions on this topic, are you able to confirm if this remains an issue or not?

acanidio-econ commented 4 months ago

Yes: independently of the formula to compute surplus, we agreed that when a solver proposes to trade with the AMM, the "limit price" (buyAmount in the code) should be the minimum buy amount that the AMM would accept given the sell amount. The helper now instead produces a different limit price.

Having said that, I don't think it is a big issue: even with the current formula for the limit price, solvers competition should guarantee that the AMM then trades at the correct price (i.e., somewhere near the oracle price). The only drawback is that when two solvers propose an identical trade to the AMM, one via the order created using the helper and the other not, then the autopilot will compute different "surplus" for the two orders. Also, and perhaps more importantly, this implementation of the helper contract differs in its logic from Balancer's implementation, in a way that makes Balancer the reference for this specific aspect of the helper

mfw78 commented 4 months ago

The only drawback is that when two solvers propose an identical trade to the AMM, one via the order created using the helper and the other not, then the autopilot will compute different "surplus" for the two orders. Also, and perhaps more importantly, this implementation of the helper contract differs in its logic from Balancer's implementation, in a way that makes Balancer the reference for this specific aspect of the helper

As surplus calculation is (at least currently) the domain of solvers (optimisation) / backend (calculation) and post-processing (ebbo monitoring), with no specific ask having been put in place for surplus calculations, for the moment, I will flag this issue as wontfix, but leave it open to reflect an unsettled state.

mfw78 commented 4 months ago

Closed by #94