Skip to content

Commit

Permalink
refactor: replace MinCoins with sdk coins.Min() (backport #7127) (#7276)
Browse files Browse the repository at this point in the history
* refactor: replace MinCoins with sdk coins.Min() (#7127)

* refactor: replace MinCoins with sdk coins.Min()

* fix: remove unused import

(cherry picked from commit ccd1865)

# Conflicts:
#	osmoutils/coin_helper.go

* Fix merge conflict

---------

Co-authored-by: levisyin <[email protected]>
Co-authored-by: mattverse <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2024
1 parent 219cee2 commit e5fc875
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
15 changes: 0 additions & 15 deletions osmoutils/coin_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// 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) {
Expand Down
9 changes: 4 additions & 5 deletions x/gamm/simulation/sim_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -36,7 +35,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)
Expand Down Expand Up @@ -195,7 +194,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))
Expand Down Expand Up @@ -241,7 +240,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))
Expand Down Expand Up @@ -274,7 +273,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))
Expand Down

0 comments on commit e5fc875

Please sign in to comment.