From 4349c0fd9b3f088588cf2ed53a1bdb0b520d1b35 Mon Sep 17 00:00:00 2001 From: Adarsh Kumar Date: Mon, 9 Dec 2024 16:56:30 +0530 Subject: [PATCH] add div by zero check (#1051) --- x/amm/types/pool_calc_join_pool_no_swap_shares.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/amm/types/pool_calc_join_pool_no_swap_shares.go b/x/amm/types/pool_calc_join_pool_no_swap_shares.go index 900113413..54690ed8c 100644 --- a/x/amm/types/pool_calc_join_pool_no_swap_shares.go +++ b/x/amm/types/pool_calc_join_pool_no_swap_shares.go @@ -31,6 +31,10 @@ func MaximalExactRatioJoin(p *Pool, tokensIn sdk.Coins) (numShares sdkmath.Int, // Note: QuoInt implements floor division, unlike Quo // This is because it calls the native golang routine big.Int.Quo // https://pkg.go.dev/math/big#Int.Quo + // Division by zero check + if poolLiquidity.AmountOfNoDenomValidation(coin.Denom).IsZero() { + return numShares, remCoins, errors.New("pool liquidity is zero for denom: " + coin.Denom) + } shareRatio := sdkmath.LegacyNewDecFromBigInt(coin.Amount.BigInt()).QuoInt(poolLiquidity.AmountOfNoDenomValidation(coin.Denom)) if shareRatio.LT(minShareRatio) { minShareRatio = shareRatio