Skip to content

Commit

Permalink
feat/fix(stableswap): scaling factors CLI, fix bug with not saving po…
Browse files Browse the repository at this point in the history
…ol to state (#3332)

* feat/fix(stableswap): scaling factors CLI, fix bug with not saving pool, fix pools query

* lint
  • Loading branch information
p0mvn authored Nov 10, 2022
1 parent bd3719f commit a355026
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
11 changes: 11 additions & 0 deletions x/gamm/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
FlagSwapRouteAmounts = "swap-route-amounts"
// Will be parsed to []string.
FlagSwapRouteDenoms = "swap-route-denoms"
// FlagScalingFactors represents the flag name for the scaling factors.
FlagScalingFactors = "scaling-factors"
)

type createBalancerPoolInputs struct {
Expand Down Expand Up @@ -116,3 +118,12 @@ func FlagSetJoinSwapExternAmount() *flag.FlagSet {

return fs
}

func FlagSetAdjustScalingFactors() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)

fs.Uint64(FlagPoolId, 0, "The id of pool")
fs.String(FlagScalingFactors, "", "The scaling factors")

return fs
}
61 changes: 61 additions & 0 deletions x/gamm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewTxCmd() *cobra.Command {
NewJoinSwapShareAmountOut(),
NewExitSwapExternAmountOut(),
NewExitSwapShareAmountIn(),
NewStableSwapAdjustScalingFactorsCmd(),
)

return txCmd
Expand Down Expand Up @@ -348,6 +349,35 @@ func NewExitSwapShareAmountIn() *cobra.Command {
return cmd
}

func NewStableSwapAdjustScalingFactorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "adjust-scaling-factors --pool-id=[pool-id] --scaling-factors=[scaling-factors]",
Short: "adjust scaling factors",
Example: "osmosisd adjust-scaling-factors --pool-id=1 --scaling-factors=\"100, 100\"",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)

txf, msg, err := NewStableSwapAdjustScalingFactorsMsg(clientCtx, txf, cmd.Flags())
if err != nil {
return err
}

return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}

cmd.Flags().AddFlagSet(FlagSetAdjustScalingFactors())
flags.AddTxFlagsToCmd(cmd)
_ = cmd.MarkFlagRequired(FlagPoolId)

return cmd
}

func NewBuildCreateBalancerPoolMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) {
pool, err := parseCreateBalancerPoolFlags(fs)
if err != nil {
Expand Down Expand Up @@ -794,3 +824,34 @@ func NewBuildExitSwapShareAmountInMsg(clientCtx client.Context, tokenOutDenom, s

return txf, msg, nil
}

func NewStableSwapAdjustScalingFactorsMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) {
poolID, err := fs.GetUint64(FlagPoolId)
if err != nil {
return txf, nil, err
}

scalingFactorsStr, err := fs.GetString(FlagScalingFactors)
if err != nil {
return txf, nil, err
}

scalingFactorsStrSlice := strings.Split(scalingFactorsStr, ",")

scalingFactors := make([]uint64, len(scalingFactorsStrSlice))
for i, scalingFactorStr := range scalingFactorsStrSlice {
scalingFactor, err := strconv.ParseUint(scalingFactorStr, 10, 64)
if err != nil {
return txf, nil, err
}
scalingFactors[i] = scalingFactor
}

msg := &stableswap.MsgStableSwapAdjustScalingFactors{
Sender: clientCtx.GetFromAddress().String(),
PoolID: poolID,
ScalingFactors: scalingFactors,
}

return txf, msg, nil
}
8 changes: 1 addition & 7 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ func (q Querier) Pools(
return err
}

// TODO: pools query should not be balancer specific
pool, ok := poolI.(*balancer.Pool)
if !ok {
return fmt.Errorf("pool (%d) is not basic pool", pool.GetId())
}

any, err := codectypes.NewAnyWithValue(pool)
any, err := codectypes.NewAnyWithValue(poolI)
if err != nil {
return err
}
Expand Down
11 changes: 1 addition & 10 deletions x/gamm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -57,15 +56,7 @@ func (server msgServer) CreateStableswapPool(goCtx context.Context, msg *stables
func (server msgServer) StableSwapAdjustScalingFactors(goCtx context.Context, msg *stableswap.MsgStableSwapAdjustScalingFactors) (*stableswap.MsgStableSwapAdjustScalingFactorsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

pool, err := server.keeper.GetPoolAndPoke(ctx, msg.PoolID)
if err != nil {
return nil, err
}
stableswapPool, ok := pool.(*stableswap.Pool)
if !ok {
return nil, fmt.Errorf("pool id %d is not of type stableswap pool", msg.PoolID)
}
if err := stableswapPool.SetScalingFactors(ctx, msg.ScalingFactors, msg.Sender); err != nil {
if err := server.keeper.setStableSwapScalingFactors(ctx, msg.PoolID, msg.ScalingFactors, msg.Sender); err != nil {
return nil, err
}

Expand Down
20 changes: 20 additions & 0 deletions x/gamm/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/osmosis-labs/osmosis/v12/osmoutils"
"github.com/osmosis-labs/osmosis/v12/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v12/x/gamm/pool-models/stableswap"
"github.com/osmosis-labs/osmosis/v12/x/gamm/types"
)

Expand Down Expand Up @@ -262,3 +263,22 @@ func (k Keeper) getNextPoolIdAndIncrement(ctx sdk.Context) uint64 {
k.setNextPoolId(ctx, nextPoolId+1)
return nextPoolId
}

// setStableSwapScalingFactors sets the stable swap scaling factors.
// errors if the pool does not exist, the sender is not the scaling factor controller, or due to other
// internal errors.
func (k Keeper) setStableSwapScalingFactors(ctx sdk.Context, poolId uint64, scalingFactors []uint64, sender string) error {
pool, err := k.GetPoolAndPoke(ctx, poolId)
if err != nil {
return err
}
stableswapPool, ok := pool.(*stableswap.Pool)
if !ok {
return fmt.Errorf("pool id %d is not of type stableswap pool", poolId)
}
if err := stableswapPool.SetScalingFactors(ctx, scalingFactors, sender); err != nil {
return err
}

return k.setPool(ctx, stableswapPool)
}

0 comments on commit a355026

Please sign in to comment.