Skip to content

Commit

Permalink
Gamm code improvements (#3657) (#3674)
Browse files Browse the repository at this point in the history
* Added target and init weights length check on CLI

* gamm utils conv helpers moved to osmoutils

* gamm CLI typo fix

* gamm num of pool assets defined constants

* sdk.Int to Int64 cast removed from err wrapping

(cherry picked from commit c659f20)

Co-authored-by: Mirel Dalčeković <97894739+dalmirel@users.noreply.github.com>
  • Loading branch information
mergify[bot] and dalmirel authored Dec 9, 2022
1 parent 175a6b6 commit cd82e24
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion x/gamm/utils/conv.go → osmoutils/conv_helper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package osmoutils

import (
"encoding/binary"
Expand Down
4 changes: 4 additions & 0 deletions x/gamm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ func NewBuildCreateBalancerPoolMsg(clientCtx client.Context, fs *flag.FlagSet) (
return nil, err
}

if len(targetPoolAssetCoins) != len(poolAssetCoins) {
return nil, errors.New("initial pool weights and target pool weights should have same length")
}

var targetPoolAssets []balancer.PoolAsset
for i := 0; i < len(targetPoolAssetCoins); i++ {
if targetPoolAssetCoins[i].Denom != poolAssetCoins[i].Denom {
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/keeper/pool_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (k Keeper) JoinSwapShareAmountOut(
}

if tokenInAmount.GT(tokenInMaxAmount) {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrLimitMaxAmount, "%d resulted tokens is larger than the max amount of %d", tokenInAmount.Int64(), tokenInMaxAmount.Int64())
return sdk.Int{}, sdkerrors.Wrapf(types.ErrLimitMaxAmount, "%s resulted tokens is larger than the max amount of %s", tokenInAmount, tokenInMaxAmount)
}

tokenIn := sdk.NewCoins(sdk.NewCoin(tokenInDenom, tokenInAmount))
Expand Down
14 changes: 7 additions & 7 deletions x/gamm/pool-models/balancer/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (

//nolint:deadcode
const (
nonPostiveSharesAmountErrFormat = "shares amount must be positive, was %d"
nonPostiveTokenAmountErrFormat = "token amount must be positive, was %d"
sharesLargerThanMaxErrFormat = "%d resulted shares is larger than the max amount of %d"
nonPostiveSharesAmountErrFormat = "shares amount must be positive, was %s"
nonPostiveTokenAmountErrFormat = "token amount must be positive, was %s"
sharesLargerThanMaxErrFormat = "%s resulted shares is larger than the max amount of %s"
invalidInputDenomsErrFormat = "input denoms must already exist in the pool (%s)"

failedInterimLiquidityUpdateErrFormat = "failed to update interim liquidity - pool asset %s does not exist"
Expand Down Expand Up @@ -892,7 +892,7 @@ func (p *Pool) CalcTokenInShareAmountOut(
).Ceil().TruncateInt()

if !tokenInAmount.IsPositive() {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrNotPositiveRequireAmount, nonPostiveTokenAmountErrFormat, tokenInAmount.Int64())
return sdk.Int{}, sdkerrors.Wrapf(types.ErrNotPositiveRequireAmount, nonPostiveTokenAmountErrFormat, tokenInAmount)
}

return tokenInAmount, nil
Expand All @@ -919,7 +919,7 @@ func (p *Pool) JoinPoolTokenInMaxShareAmountOut(
).TruncateInt()

if !tokenInAmount.IsPositive() {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrNotPositiveRequireAmount, nonPostiveTokenAmountErrFormat, tokenInAmount.Int64())
return sdk.Int{}, sdkerrors.Wrapf(types.ErrNotPositiveRequireAmount, nonPostiveTokenAmountErrFormat, tokenInAmount)
}

poolAssetIn.Token.Amount = poolAssetIn.Token.Amount.Add(tokenInAmount)
Expand Down Expand Up @@ -951,11 +951,11 @@ func (p *Pool) ExitSwapExactAmountOut(
).TruncateInt()

if !sharesIn.IsPositive() {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrNotPositiveRequireAmount, nonPostiveSharesAmountErrFormat, sharesIn.Int64())
return sdk.Int{}, sdkerrors.Wrapf(types.ErrNotPositiveRequireAmount, nonPostiveSharesAmountErrFormat, sharesIn)
}

if sharesIn.GT(shareInMaxAmount) {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrLimitMaxAmount, sharesLargerThanMaxErrFormat, sharesIn.Int64(), shareInMaxAmount.Uint64())
return sdk.Int{}, sdkerrors.Wrapf(types.ErrLimitMaxAmount, sharesLargerThanMaxErrFormat, sharesIn, shareInMaxAmount)
}

if err := p.exitPool(ctx, sdk.NewCoins(tokenOut), sharesIn); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions x/gamm/pool-models/balancer/pool_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"sort"
"strings"

"github.com/osmosis-labs/osmosis/v13/x/gamm/types"
"gopkg.in/yaml.v2"

"github.com/osmosis-labs/osmosis/v13/x/gamm/types"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -69,11 +70,11 @@ func sortPoolAssetsByDenom(assets []PoolAsset) {

func validateUserSpecifiedPoolAssets(assets []PoolAsset) error {
// The pool must be swapping between at least two assets
if len(assets) < 2 {
if len(assets) < types.MinNumOfAssetsInPool {
return types.ErrTooFewPoolAssets
}

if len(assets) > 8 {
if len(assets) > types.MaxNumOfAssetsInPool {
return sdkerrors.Wrapf(types.ErrTooManyPoolAssets, "%d", len(assets))
}

Expand Down
4 changes: 4 additions & 0 deletions x/gamm/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
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

// pools can be created with min and max number of assets defined with this constants
MinNumOfAssetsInPool = 2
MaxNumOfAssetsInPool = 8
)

var (
Expand Down
6 changes: 3 additions & 3 deletions x/incentives/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keeper
import (
"context"

"github.com/osmosis-labs/osmosis/v13/x/gamm/utils"
"github.com/osmosis-labs/osmosis/v13/osmoutils"
"github.com/osmosis-labs/osmosis/v13/x/incentives/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (server msgServer) CreateGauge(goCtx context.Context, msg *types.MsgCreateG
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtCreateGauge,
sdk.NewAttribute(types.AttributeGaugeID, utils.Uint64ToString(gaugeID)),
sdk.NewAttribute(types.AttributeGaugeID, osmoutils.Uint64ToString(gaugeID)),
),
})

Expand All @@ -72,7 +72,7 @@ func (server msgServer) AddToGauge(goCtx context.Context, msg *types.MsgAddToGau
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtAddToGauge,
sdk.NewAttribute(types.AttributeGaugeID, utils.Uint64ToString(msg.GaugeId)),
sdk.NewAttribute(types.AttributeGaugeID, osmoutils.Uint64ToString(msg.GaugeId)),
),
})

Expand Down
10 changes: 5 additions & 5 deletions x/lockup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/osmosis-labs/osmosis/v13/x/gamm/utils"
"github.com/osmosis-labs/osmosis/v13/osmoutils"
"github.com/osmosis-labs/osmosis/v13/x/lockup/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -49,7 +49,7 @@ func (server msgServer) LockTokens(goCtx context.Context, msg *types.MsgLockToke
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtAddTokensToLock,
sdk.NewAttribute(types.AttributePeriodLockID, utils.Uint64ToString(lockID)),
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lockID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, msg.Owner),
sdk.NewAttribute(types.AttributePeriodLockAmount, msg.Coins.String()),
),
Expand All @@ -66,7 +66,7 @@ func (server msgServer) LockTokens(goCtx context.Context, msg *types.MsgLockToke
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtLockTokens,
sdk.NewAttribute(types.AttributePeriodLockID, utils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockAmount, lock.Coins.String()),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
Expand Down Expand Up @@ -136,7 +136,7 @@ func (server msgServer) BeginUnlockingAll(goCtx context.Context, msg *types.MsgB
func createBeginUnlockEvent(lock *types.PeriodLock) sdk.Event {
return sdk.NewEvent(
types.TypeEvtBeginUnlock,
sdk.NewAttribute(types.AttributePeriodLockID, utils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
sdk.NewAttribute(types.AttributePeriodLockUnlockTime, lock.EndTime.String()),
Expand Down Expand Up @@ -167,7 +167,7 @@ func (server msgServer) ExtendLockup(goCtx context.Context, msg *types.MsgExtend
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtLockTokens,
sdk.NewAttribute(types.AttributePeriodLockID, utils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
),
Expand Down
4 changes: 2 additions & 2 deletions x/superfluid/keeper/internal/events/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/v13/x/gamm/utils"
"github.com/osmosis-labs/osmosis/v13/osmoutils"
"github.com/osmosis-labs/osmosis/v13/x/superfluid/types"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ func EmitSuperfluidDelegateEvent(ctx sdk.Context, lockId uint64, valAddress stri
func newSuperfluidDelegateEvent(lockId uint64, valAddress string) sdk.Event {
return sdk.NewEvent(
types.TypeEvtSuperfluidDelegate,
sdk.NewAttribute(types.AttributeLockId, utils.Uint64ToString(lockId)),
sdk.NewAttribute(types.AttributeLockId, osmoutils.Uint64ToString(lockId)),
sdk.NewAttribute(types.AttributeValidator, valAddress),
)
}
Expand Down

0 comments on commit cd82e24

Please sign in to comment.