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

[x/gamm][Refactor] Follow-on cleanup to Gamm Refactor #1129

Merged
merged 6 commits into from
Mar 23, 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
6 changes: 3 additions & 3 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ func (keeperTestHelper *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(
defaultFutureGovernor = ""

// pool assets
defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{
defaultFooAsset balancer.PoolAsset = balancer.PoolAsset{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(bondDenom, uosmoAmount),
}
defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{
defaultBarAsset balancer.PoolAsset = balancer.PoolAsset{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(token, sdk.NewInt(10000)),
}
poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset}
poolAssets []balancer.PoolAsset = []balancer.PoolAsset{defaultFooAsset, defaultBarAsset}
)

poolId, err := keeperTestHelper.App.GAMMKeeper.CreateBalancerPool(keeperTestHelper.Ctx, acc1, balancer.PoolParams{
Expand Down
52 changes: 52 additions & 0 deletions go.sum

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions osmomath/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ var one_half sdk.Dec = sdk.MustNewDecFromStr("0.5")
var one sdk.Dec = sdk.OneDec()
var two sdk.Dec = sdk.MustNewDecFromStr("2")

// Returns the internal "power precision".
// All fractional exponentiation in osmosis is expected to be accurate up to
// powPrecision.
// *technically* the error term can be greater than this powPrecision,
// but for small bases this bound applies. See comments in the PowApprox function
// for more detail.
func GetPowPrecision() sdk.Dec {
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
return powPrecision.Clone()
}

/*********************************************************/

// AbsDifferenceWithSign returns | a - b |, (a - b).sign()
Expand Down
19 changes: 17 additions & 2 deletions proto/osmosis/gamm/pool-models/balancer/balancerPool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import "google/protobuf/timestamp.proto";
import "cosmos/auth/v1beta1/auth.proto";
import "cosmos/base/v1beta1/coin.proto";

import "osmosis/gamm/v1beta1/pool.proto";

option go_package = "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer";

// Parameters for changing the weights in a balancer pool smoothly from
Expand Down Expand Up @@ -89,6 +87,23 @@ message PoolParams {
];
}

// Pool asset is an internal struct that combines the amount of the
// token in the pool, and its balancer weight.
// This is an awkward packaging of data,
// and should be revisited in a future state migration.
message PoolAsset {
// Coins we are talking about,
// the denomination must be unique amongst all PoolAssets for this pool.
cosmos.base.v1beta1.Coin token = 1
[ (gogoproto.moretags) = "yaml:\"token\"", (gogoproto.nullable) = false ];
// Weight that is not normalized. This weight must be less than 2^50
string weight = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"weight\"",
(gogoproto.nullable) = false
];
}

message Pool {
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;
Expand Down
1 change: 0 additions & 1 deletion proto/osmosis/gamm/pool-models/balancer/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "osmosis/gamm/pool-models/balancer/balancerPool.proto";
import "osmosis/gamm/v1beta1/pool.proto";

option go_package = "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer";

Expand Down
30 changes: 0 additions & 30 deletions proto/osmosis/gamm/v1beta1/pool.proto

This file was deleted.

14 changes: 1 addition & 13 deletions proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "osmosis/gamm/v1beta1/tx.proto";
import "osmosis/gamm/v1beta1/pool.proto";

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
Expand Down Expand Up @@ -38,10 +37,7 @@ service Query {
option (google.api.http).get =
"/osmosis/gamm/v1beta1/pools/{poolId}/total_shares";
}
rpc PoolAssets(QueryPoolAssetsRequest) returns (QueryPoolAssetsResponse) {
option (google.api.http).get =
"/osmosis/gamm/v1beta1/pools/{poolId}/tokens";
}

rpc SpotPrice(QuerySpotPriceRequest) returns (QuerySpotPriceResponse) {
option (google.api.http).get =
"/osmosis/gamm/v1beta1/pools/{poolId}/prices";
Expand Down Expand Up @@ -104,14 +100,6 @@ message QueryTotalSharesResponse {
];
}

//=============================== PoolAssets
message QueryPoolAssetsRequest {
uint64 poolId = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
}
message QueryPoolAssetsResponse {
repeated PoolAsset poolAssets = 1 [ (gogoproto.nullable) = false ];
}

//=============================== SpotPrice
message QuerySpotPriceRequest {
uint64 poolId = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
Expand Down
43 changes: 0 additions & 43 deletions x/gamm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func GetQueryCmd() *cobra.Command {
GetCmdNumPools(),
GetCmdPoolParams(),
GetCmdTotalShares(),
GetCmdPoolAssets(),
GetCmdSpotPrice(),
GetCmdQueryTotalLiquidity(),
GetCmdEstimateSwapExactAmountIn(),
Expand Down Expand Up @@ -315,48 +314,6 @@ $ %s query gamm total-liquidity
return cmd
}

// GetCmdPoolAssets return pool-assets for a pool
func GetCmdPoolAssets() *cobra.Command {
cmd := &cobra.Command{
Use: "pool-assets <poolID>",
Short: "Query pool-assets",
Long: strings.TrimSpace(
fmt.Sprintf(`Query pool assets.
Example:
$ %s query gamm pool-assets 1
`,
version.AppName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

poolID, err := strconv.Atoi(args[0])
if err != nil {
return err
}

res, err := queryClient.PoolAssets(cmd.Context(), &types.QueryPoolAssetsRequest{
PoolId: uint64(poolID),
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdSpotPrice returns spot price
func GetCmdSpotPrice() *cobra.Command {
cmd := &cobra.Command{
Expand Down
8 changes: 4 additions & 4 deletions x/gamm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,14 @@ func NewBuildCreateBalancerPoolMsg(clientCtx client.Context, txf tx.Factory, fs
return txf, nil, err
}

var poolAssets []types.PoolAsset
var poolAssets []balancer.PoolAsset
for i := 0; i < len(poolAssetCoins); i++ {

if poolAssetCoins[i].Denom != deposit[i].Denom {
return txf, nil, errors.New("deposit tokens and token weights should have same denom order")
}

poolAssets = append(poolAssets, types.PoolAsset{
poolAssets = append(poolAssets, balancer.PoolAsset{
Weight: poolAssetCoins[i].Amount.RoundInt(),
Token: deposit[i],
})
Expand Down Expand Up @@ -399,14 +399,14 @@ func NewBuildCreateBalancerPoolMsg(clientCtx client.Context, txf tx.Factory, fs
return txf, nil, err
}

var targetPoolAssets []types.PoolAsset
var targetPoolAssets []balancer.PoolAsset
for i := 0; i < len(targetPoolAssetCoins); i++ {

if targetPoolAssetCoins[i].Denom != poolAssetCoins[i].Denom {
return txf, nil, errors.New("initial pool weights and target pool weights should have same denom order")
}

targetPoolAssets = append(targetPoolAssets, types.PoolAsset{
targetPoolAssets = append(targetPoolAssets, balancer.PoolAsset{
Weight: targetPoolAssetCoins[i].Amount.RoundInt(),
Token: deposit[i],
// TODO: This doesn't make sense. Should only use denom, not an sdk.Coin
Expand Down
9 changes: 4 additions & 5 deletions x/gamm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
osmoapp "github.com/osmosis-labs/osmosis/v7/app"
"github.com/osmosis-labs/osmosis/v7/x/gamm"
"github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v7/x/gamm/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
Expand All @@ -22,7 +21,7 @@ import (
// balancerPool, err := balancer.NewBalancerPool(1, balancer.PoolParams{
// SwapFee: sdk.NewDecWithPrec(1, 2),
// ExitFee: sdk.NewDecWithPrec(1, 2),
// }, []types.PoolAsset{
// }, []balancerbalancer.PoolAsset{
// {
// Weight: sdk.NewInt(1),
// Token: sdk.NewInt64Coin(sdk.DefaultBondDenom, 10),
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestGammExportGenesis(t *testing.T) {
_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, balancer.PoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{{
}, []balancer.PoolAsset{{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin("foo", sdk.NewInt(10000)),
}, {
Expand All @@ -91,7 +90,7 @@ func TestGammExportGenesis(t *testing.T) {
_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, balancer.PoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{{
}, []balancer.PoolAsset{{
Weight: sdk.NewInt(70),
Token: sdk.NewCoin("foo", sdk.NewInt(10000)),
}, {
Expand Down Expand Up @@ -123,7 +122,7 @@ func TestMarshalUnmarshalGenesis(t *testing.T) {
_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, balancer.PoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{{
}, []balancer.PoolAsset{{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin("foo", sdk.NewInt(10000)),
}, {
Expand Down
6 changes: 3 additions & 3 deletions x/gamm/keeper/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
balanacertypes "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer"
balancertypes "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v7/x/gamm/types"
)

Expand Down Expand Up @@ -103,7 +103,7 @@ func (suite *KeeperTestSuite) TestRepeatedJoinPoolDistinctDenom() {
err = simapp.FundAccount(suite.app.BankKeeper, suite.ctx, defaultAddr, coins)
suite.Require().NoError(err)

defaultPoolParams := balanacertypes.PoolParams{
defaultPoolParams := balancertypes.PoolParams{
SwapFee: sdk.NewDec(0),
ExitFee: sdk.NewDec(0),
}
Expand All @@ -115,7 +115,7 @@ func (suite *KeeperTestSuite) TestRepeatedJoinPoolDistinctDenom() {
err = simapp.FundAccount(suite.app.BankKeeper, suite.ctx, defaultAddr, coins)
suite.Require().NoError(err)

poolAssets := []types.PoolAsset{
poolAssets := []balancertypes.PoolAsset{
{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(prevRandToken, sdk.NewInt(10)),
Expand Down
19 changes: 0 additions & 19 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,6 @@ func (k Keeper) TotalShares(ctx context.Context, req *types.QueryTotalSharesRequ
}, nil
}

// TODO: Fix
func (k Keeper) PoolAssets(ctx context.Context, req *types.QueryPoolAssetsRequest) (*types.QueryPoolAssetsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

return nil, status.Error(codes.Unimplemented, "unimplemented")

// sdkCtx := sdk.UnwrapSDKContext(ctx)

// pool, err := k.GetPool(sdkCtx, req.PoolId)
// if err != nil {
// return nil, status.Error(codes.Internal, err.Error())
// }
// return &types.QueryPoolAssetsResponse{
// PoolAssets: pool.GetAllPoolAssets(),
// }, nil
}

func (k Keeper) SpotPrice(ctx context.Context, req *types.QuerySpotPriceRequest) (*types.QuerySpotPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func genericPow(base, exp sdk.Dec) sdk.Dec {
if !base.GTE(sdk.NewDec(2)) {
return osmomath.Pow(base, exp)
}
return osmomath.PowApprox(sdk.OneDec().Quo(base), exp.Neg(), powPrecision)
return osmomath.PowApprox(sdk.OneDec().Quo(base), exp.Neg(), osmomath.GetPowPrecision())
}

// constantChange returns the multiplicative factor difference in the pool constant, between two different pools.
Expand Down
38 changes: 0 additions & 38 deletions x/gamm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -60,43 +59,6 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSpace paramtyp
}
}

func (k *Keeper) createSwapEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, input sdk.Coins, output sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtTokenSwapped,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTokensIn, input.String()),
sdk.NewAttribute(types.AttributeKeyTokensOut, output.String()),
),
})
}

func (k *Keeper) createAddLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtPoolJoined,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTokensIn, liquidity.String()),
),
})
}

func (k *Keeper) createRemoveLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtPoolExited,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTokensOut, liquidity.String()),
),
})
}

// Set the gamm hooks
func (k *Keeper) SetHooks(gh types.GammHooks) *Keeper {
if k.hooks != nil {
Expand Down
Loading