From f0371455fac2f263d44fad495d8a8d2dad4aa39b Mon Sep 17 00:00:00 2001 From: levisyin Date: Fri, 15 Dec 2023 10:25:50 +0800 Subject: [PATCH 1/2] refactor: replace MinCoins with sdk coins.Min() --- osmoutils/coin_helper.go | 15 --------------- x/gamm/simulation/sim_msgs.go | 8 ++++---- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/osmoutils/coin_helper.go b/osmoutils/coin_helper.go index 3a60712a907..dc9b36cb363 100644 --- a/osmoutils/coin_helper.go +++ b/osmoutils/coin_helper.go @@ -16,21 +16,6 @@ func CoinsDenoms(coins sdk.Coins) []string { return denoms } -// MinCoins returns the minimum of each denom between both coins. -// For now it assumes they have the same denoms. -// TODO: Replace with method in SDK once we update our version -func MinCoins(coinsA sdk.Coins, coinsB sdk.Coins) sdk.Coins { - resCoins := sdk.Coins{} - for i, coin := range coinsA { - if coinsB[i].Amount.GT(coin.Amount) { - resCoins = append(resCoins, coin) - } else { - resCoins = append(resCoins, coinsB[i]) - } - } - return resCoins -} - // SubDecCoinArrays subtracts the contents of the second param from the first (decCoinsArrayA - decCoinsArrayB) // Note that this takes in two _arrays_ of DecCoins, meaning that each term itself is of type DecCoins (i.e. an array of DecCoin). func SubDecCoinArrays(decCoinsArrayA []sdk.DecCoins, decCoinsArrayB []sdk.DecCoins) ([]sdk.DecCoins, error) { diff --git a/x/gamm/simulation/sim_msgs.go b/x/gamm/simulation/sim_msgs.go index 6b8385efb31..718ef6f6893 100644 --- a/x/gamm/simulation/sim_msgs.go +++ b/x/gamm/simulation/sim_msgs.go @@ -36,7 +36,7 @@ func RandomJoinPoolMsg(k keeper.Keeper, sim *simtypes.SimCtx, ctx sdk.Context) ( } // cap joining pool to the pool liquidity - tokenIn = osmoutils.MinCoins(tokenIn, pool.GetTotalPoolLiquidity(ctx)) + tokenIn = tokenIn.Min(pool.GetTotalPoolLiquidity(ctx)) // TODO: Fix API so this is a one liner, pool.CalcJoinPoolNoSwapShares() minShareOutAmt, err := deriveRealMinShareOutAmt(ctx, tokenIn, pool) @@ -195,7 +195,7 @@ func RandomSwapExactAmountOut(k keeper.Keeper, sim *simtypes.SimCtx, ctx sdk.Con } // set the subset of coins to be upper-bound to the minimum between the address and the pool itself - randomCoinInSubset := osmoutils.MinCoins(sdk.NewCoins(coinIn), sdk.NewCoins(accCoin)) + randomCoinInSubset := sdk.NewCoins(coinIn).Min(sdk.NewCoins(accCoin)) // utilize CalcOutAmtGivenIn to calculate tokenOut and use tokenOut to calculate tokenInMax tokenOut, err := pool.CalcOutAmtGivenIn(ctx, randomCoinInSubset, coinOut.Denom, pool.GetSpreadFactor(ctx)) @@ -241,7 +241,7 @@ func RandomJoinSwapExternAmountIn(k keeper.Keeper, sim *simtypes.SimCtx, ctx sdk } // cap joining pool to the pool liquidity - newTokenIn := osmoutils.MinCoins(sdk.NewCoins(coinIn), sdk.NewCoins(tokenIn)) + newTokenIn := sdk.NewCoins(coinIn).Min(sdk.NewCoins(tokenIn)) // calc shares out with tokenIn minShareOutAmt, _, err := pool.CalcJoinPoolShares(ctx, newTokenIn, pool.GetSpreadFactor(ctx)) @@ -274,7 +274,7 @@ func RandomJoinSwapShareAmountOut(k keeper.Keeper, sim *simtypes.SimCtx, ctx sdk } // cap joining pool to the pool liquidity - newTokenIn := osmoutils.MinCoins(sdk.NewCoins(coinIn), sdk.NewCoins(tokenIn)) + newTokenIn := sdk.NewCoins(coinIn).Min(sdk.NewCoins(tokenIn)) // calc shares out with tokenIn minShareOutAmt, _, err := pool.CalcJoinPoolShares(ctx, newTokenIn, pool.GetSpreadFactor(ctx)) From 8fcb430a1b2378818301474bf00adbadf83c4c8b Mon Sep 17 00:00:00 2001 From: levisyin Date: Fri, 15 Dec 2023 16:07:01 +0800 Subject: [PATCH 2/2] fix: remove unused import --- x/gamm/simulation/sim_msgs.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/gamm/simulation/sim_msgs.go b/x/gamm/simulation/sim_msgs.go index aaa7f49a665..79d7a1661e7 100644 --- a/x/gamm/simulation/sim_msgs.go +++ b/x/gamm/simulation/sim_msgs.go @@ -9,7 +9,6 @@ import ( legacysimulationtype "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/osmosis-labs/osmosis/osmomath" - "github.com/osmosis-labs/osmosis/osmoutils" "github.com/osmosis-labs/osmosis/v21/simulation/simtypes" "github.com/osmosis-labs/osmosis/v21/x/gamm/keeper" balancertypes "github.com/osmosis-labs/osmosis/v21/x/gamm/pool-models/balancer"