Skip to content

Commit

Permalink
Move panic catching into GetComparablePoolLiquidity
Browse files Browse the repository at this point in the history
- Panic catching was previously in CompareAndStorePool because that function did the multiplication.
- Since then, the multiplication logic that this panic catches overflows on has been separated into it's own function GetComparablePoolLiquidity
- This commit moves the panic catching logic to GetComparablePoolLiquidity
  • Loading branch information
NotJeremyLiu committed Jun 6, 2023
1 parent e5d17c6 commit 22d528e
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions x/protorev/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,23 @@ func (k Keeper) StoreSwap(ctx sdk.Context, poolId uint64, tokenIn, tokenOut stri
}

// GetComparablePoolLiquidity gets the comparable liquidity of a pool by multiplying the amounts of the pool coins.
func (k Keeper) GetComparablePoolLiquidity(ctx sdk.Context, poolId uint64) (sdk.Int, error) {
func (k Keeper) GetComparablePoolLiquidity(ctx sdk.Context, poolId uint64) (comparableLiquidity sdk.Int, err error) {
coins, err := k.poolmanagerKeeper.GetTotalPoolLiquidity(ctx, poolId)
if err != nil {
return sdk.Int{}, err
}

return coins[0].Amount.Mul(coins[1].Amount), nil
// Recover from overflow panic
defer func() {
if r := recover(); r != nil {
comparableLiquidity = sdk.Int{}
err = sdk.ErrIntOverflowAbci
}
}()

comparableLiquidity = coins[0].Amount.Mul(coins[1].Amount)

return comparableLiquidity, nil
}

// StoreJoinExitPoolSwaps stores the swaps associated with GAMM join/exit pool messages in the store, depending on if it is a join or exit.
Expand Down Expand Up @@ -184,12 +194,6 @@ func (k Keeper) CompareAndStorePool(ctx sdk.Context, poolId uint64, baseDenom, o
return
}

defer func() {
if r := recover(); r != nil {
ctx.Logger().Error("Protorev error recovering from panic in AfterCFMMPoolCreated hook, likely an overflow error", r)
}
}()

// Get comparable liquidity for the new pool
newPoolLiquidity, err := k.GetComparablePoolLiquidity(ctx, poolId)
if err != nil {
Expand Down

0 comments on commit 22d528e

Please sign in to comment.