Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gamm code improvements #3657

Merged
merged 5 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -360,7 +360,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY for fixing this 🙏

}

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 @@ -17,9 +17,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 @@ -894,7 +894,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 @@ -921,7 +921,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 @@ -953,11 +953,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 @@ -14,6 +14,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