Closed acanidio-econ closed 4 months ago
@acanidio-econ given recent discussions on this topic, are you able to confirm if this remains an issue or not?
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
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.
Closed by #94
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 thebuyAmount
corresponds to zero surplus for AMM given thesellAmount
.And similarly a few lines below
https://github.com/cowprotocol/cow-amm/blob/677e124edd33052c7c14c2e6b0891241b1a0ae4b/src/ConstantProduct.sol#L279C13-L284C15