primitivefinance / portfolio

Portfolio is an automated market making protocol for implementing custom strategies at the lowest cost possible.
https://www.primitive.xyz/
GNU Affero General Public License v3.0
113 stars 8 forks source link

Reserves above maximum not caught by swap trading function #442

Open Alexangelj opened 1 year ago

Alexangelj commented 1 year ago

Ref: https://github.com/primitivefinance/portfolio_simulations/blob/(bug-found)-invariant-pre-post-swap/bug_description.md

The Portfolio contracts calls validateSwap on the strategy contract and passes the adjusted reserves to the function. The adjusted reserves are the new reserves after the trade takes place without the fee. Therefore, the swap validation is checked on reserve values that could be less than what the actual reserve is because the fee could be large. This is not usually a problem, but it's a problem with the default strategy because any calls to the tradingFunction will now revert if using the Portfolio pool's reserves. This is because one of the reserves is at a bound (because of fees being added), while the other is not. This reverts now after making the fixes in https://github.com/primitivefinance/portfolio/tree/fix/trading-function-boundary

This is not as straightforward to fix because there's no place where we can run the additional verification the reserves have not eclipsed a bound, we would need to add some additional code to portfolio.

I am going to attempt to fix this on the temporary branch fix/arb-boundary-sims by not reverting, and instead overwriting the the reserves to the BOUND +/- 1 so the math ppf does not revert.

Alexangelj commented 1 year ago

See https://github.com/primitivefinance/portfolio/commit/1d0df553f939d643f522daad09e4817ee743f2d4 for attempted fix. Need to run the simulation with this.