primitivefinance / portfolio

Portfolio is an automated market making protocol for implementing custom strategies at the lowest cost possible.

Home Page:https://www.primitive.xyz/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix: Invariant emitted in swap event is incorrect

Alexangelj opened this issue · comments

Description

In swap, the next invariant is computed in the invariant check:

(validInvariant, nextInvariantWad) = checkInvariant(
args.poolId,
iteration.prevInvariant,
iteration.virtualX,
iteration.virtualY,
block.timestamp
);

However, while this nextInvariant is the valid invariant without the fees being considered, the actual invariant should include the fees that were added to the pool, which is not updated until after this amount is computed:

if (_state.sell) {
iteration.virtualX = nextIndependent;
} else {
iteration.virtualY = nextIndependent;
}

Update the nextInvariantWad to use the nextIndependent value for either X or Y reserves.

Closing for now. Emits the pre-fee invariant.