Skip to content

Commit

Permalink
[x/gamm][stableswap]: Increase max post-scaled amount to 10^34 (#3290)
Browse files Browse the repository at this point in the history
* increase max post-scaled amount to 10^34

* tighten tests around max pool assets

* change big.int to sdk int pow

Co-authored-by: Dev Ojha <[email protected]>

* fix power calculation and clean up comments

* minor comment cleanup

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
AlpinYukseloglu and ValarDragon authored Nov 7, 2022
1 parent 31c75cf commit d9c8942
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
28 changes: 14 additions & 14 deletions x/gamm/pool-models/stableswap/amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,34 +878,34 @@ func TestJoinPoolSharesInternal(t *testing.T) {
sdk.NewInt64Coin("bar", 1),
),
poolAssets: sdk.NewCoins(
sdk.NewInt64Coin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewInt64Coin("bar", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("bar", types.StableswapMaxScaledAmtPerAsset),
),
scalingFactors: defaultTwoAssetScalingFactors,
swapFee: sdk.ZeroDec(),
expNumShare: sdk.ZeroInt(),
expTokensJoined: sdk.Coins{},
expPoolAssets: sdk.NewCoins(
sdk.NewInt64Coin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewInt64Coin("bar", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("bar", types.StableswapMaxScaledAmtPerAsset),
),
expectPass: false,
},
"single-asset pool join exceeds hits max scaled asset amount": {
tokensIn: sdk.NewCoins(
sdk.NewInt64Coin("foo", 1),
sdk.NewInt64Coin("foo", 2),
),
poolAssets: sdk.NewCoins(
sdk.NewInt64Coin("foo", 10_000_000_000),
sdk.NewInt64Coin("bar", 10_000_000_000),
sdk.NewCoin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("bar", types.StableswapMaxScaledAmtPerAsset),
),
scalingFactors: defaultTwoAssetScalingFactors,
swapFee: sdk.ZeroDec(),
expNumShare: sdk.ZeroInt(),
expTokensJoined: sdk.Coins{},
expPoolAssets: sdk.NewCoins(
sdk.NewInt64Coin("foo", 10_000_000_000),
sdk.NewInt64Coin("bar", 10_000_000_000),
sdk.NewCoin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("bar", types.StableswapMaxScaledAmtPerAsset),
),
expectPass: false,
},
Expand All @@ -915,19 +915,19 @@ func TestJoinPoolSharesInternal(t *testing.T) {
sdk.NewInt64Coin("bar", 1),
),
poolAssets: sdk.NewCoins(
sdk.NewInt64Coin("foo", types.StableswapMaxScaledAmtPerAsset-1),
sdk.NewInt64Coin("bar", types.StableswapMaxScaledAmtPerAsset-1),
sdk.NewCoin("foo", types.StableswapMaxScaledAmtPerAsset.Sub(sdk.NewInt(1))),
sdk.NewCoin("bar", types.StableswapMaxScaledAmtPerAsset.Sub(sdk.NewInt(1))),
),
scalingFactors: defaultTwoAssetScalingFactors,
swapFee: sdk.ZeroDec(),
expNumShare: types.InitPoolSharesSupply.QuoRaw(int64(types.StableswapMaxScaledAmtPerAsset)),
expNumShare: types.InitPoolSharesSupply.Quo(types.StableswapMaxScaledAmtPerAsset),
expTokensJoined: sdk.NewCoins(
sdk.NewInt64Coin("foo", 1),
sdk.NewInt64Coin("bar", 1),
),
expPoolAssets: sdk.NewCoins(
sdk.NewInt64Coin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewInt64Coin("bar", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("foo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("bar", types.StableswapMaxScaledAmtPerAsset),
),
expectPass: true,
},
Expand Down
34 changes: 17 additions & 17 deletions x/gamm/pool-models/stableswap/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ func TestMsgCreateStableswapPoolValidateBasic(t *testing.T) {
name: "max asset amounts",
msg: updateMsg(func(msg stableswap.MsgCreateStableswapPool) stableswap.MsgCreateStableswapPool {
msg.InitialPoolLiquidity = sdk.Coins{
sdk.NewCoin("osmo", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("atom", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("usdt", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("usdc", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("juno", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("akt", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("regen", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("band", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("osmo", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("atom", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("usdt", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("usdc", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("juno", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("akt", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("regen", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("band", types.StableswapMaxScaledAmtPerAsset),
}
msg.ScalingFactors = []uint64{1, 1, 1, 1, 1, 1, 1, 1}
return msg
Expand All @@ -246,22 +246,22 @@ func TestMsgCreateStableswapPoolValidateBasic(t *testing.T) {
name: "greater than max post-scaled amount with regular scaling factors",
msg: updateMsg(func(msg stableswap.MsgCreateStableswapPool) stableswap.MsgCreateStableswapPool {
msg.InitialPoolLiquidity = sdk.Coins{
sdk.NewCoin("osmo", sdk.NewInt(1+10_000_000_000)),
sdk.NewCoin("atom", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("usdt", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("usdc", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("juno", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("akt", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("regen", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("band", sdk.NewInt(10_000_000_000)),
sdk.NewCoin("osmo", types.StableswapMaxScaledAmtPerAsset.Add(sdk.OneInt())),
sdk.NewCoin("atom", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("usdt", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("usdc", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("juno", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("akt", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("regen", types.StableswapMaxScaledAmtPerAsset),
sdk.NewCoin("band", types.StableswapMaxScaledAmtPerAsset),
}
msg.ScalingFactors = []uint64{1, 1, 1, 1, 1, 1, 1, 1}
return msg
}),
expectPass: false,
},
{
name: "100B token 8-asset pool using large scaling factors",
name: "100B token 8-asset pool using large scaling factors (6 decimal precision per asset)",
msg: updateMsg(func(msg stableswap.MsgCreateStableswapPool) stableswap.MsgCreateStableswapPool {
msg.InitialPoolLiquidity = sdk.Coins{
sdk.NewCoin("osmo", sdk.NewInt(100_000_000_000_000_000)),
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/pool-models/stableswap/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func validatePoolLiquidity(liquidity sdk.Coins, scalingFactors []uint64) error {

for i, asset := range liquidity {
scaledAmount := asset.Amount.Quo(sdk.NewInt(int64(scalingFactors[i])))
if scaledAmount.GT(sdk.NewInt(types.StableswapMaxScaledAmtPerAsset)) {
if scaledAmount.GT(types.StableswapMaxScaledAmtPerAsset) {
return types.ErrHitMaxScaledAssets
} else if scaledAmount.LT(sdk.NewInt(types.StableswapMinScaledAmtPerAsset)) {
return types.ErrHitMinScaledAssets
Expand Down
4 changes: 3 additions & 1 deletion x/gamm/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (
SigFigsExponent = 8
BalancerGasFeeForSwap = 10_000

StableswapMaxScaledAmtPerAsset = 10_000_000_000
StableswapMinScaledAmtPerAsset = 1
// We keep this multiplier at 1, but can increase if needed in the unlikely scenario where default scaling factors of 1 cannot accommodate enough assets
ScalingFactorMultiplier = 1
Expand All @@ -34,4 +33,7 @@ var (

// MultihopSwapFeeMultiplierForOsmoPools if a swap fees multiplier for trades consists of just two OSMO pools during a single transaction.
MultihopSwapFeeMultiplierForOsmoPools = sdk.NewDecWithPrec(5, 1) // 0.5

// Maximum amount per asset after the application of scaling factors should be 10e34.
StableswapMaxScaledAmtPerAsset = sdk.NewDec(10).Power(34).TruncateInt()
)
2 changes: 1 addition & 1 deletion x/gamm/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ var (
ErrInvalidStableswapScalingFactors = sdkerrors.Register(ModuleName, 62, "length between liquidity and scaling factors mismatch")
ErrNotScalingFactorGovernor = sdkerrors.Register(ModuleName, 63, "not scaling factor governor")
ErrInvalidScalingFactors = sdkerrors.Register(ModuleName, 64, "invalid scaling factor")
ErrHitMaxScaledAssets = sdkerrors.Register(ModuleName, 65, "post-scaled pool assets can not exceed 10B")
ErrHitMaxScaledAssets = sdkerrors.Register(ModuleName, 65, "post-scaled pool assets can not exceed 10e34")
ErrHitMinScaledAssets = sdkerrors.Register(ModuleName, 66, "post-scaled pool assets can not be less than 1")
)

0 comments on commit d9c8942

Please sign in to comment.