Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Akhtariev authored and Ruslan Akhtariev committed Nov 10, 2022
1 parent 21507ac commit 8c1842a
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 256 deletions.
21 changes: 11 additions & 10 deletions proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ service Query {
option (google.api.http).get = "/osmosis/gamm/v1beta1/pool_type/{pool_id}";
}

// Simulates joining pool without a swap. Returns the amount of shares you'd get
// and tokens needed to provide
rpc CalcJoinPoolNoSwap(QueryJoinPoolNoSwapRequest)
returns (QueryJoinPoolNoSwapResponse) {}
// Simulates joining pool without a swap. Returns the amount of shares you'd
// get and tokens needed to provide
rpc CalcJoinPoolNoSwap(QueryCalcJoinPoolNoSwapRequest)
returns (QueryCalcJoinPoolNoSwapResponse) {}

rpc CalcJoinPoolShares(QueryCalcJoinPoolSharesRequest)
returns (QueryCalcJoinPoolSharesResponse) {
Expand Down Expand Up @@ -191,17 +191,18 @@ message QueryTotalSharesResponse {
];
}
//=============================== SimJoinPoolNoSwap
message QueryJoinPoolNoSwapRequest {
message QueryCalcJoinPoolNoSwapRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string shares_out_amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
string tokens_in = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coins",
(gogoproto.moretags) = "yaml:\"tokens_in\"",
(gogoproto.nullable) = false
];
}
message QueryJoinPoolNoSwapResponse {
repeated cosmos.base.v1beta1.Coin tokens_in = 1 [
message QueryCalcJoinPoolNoSwapResponse {
repeated cosmos.base.v1beta1.Coin tokens_out = 1 [
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
(gogoproto.moretags) = "yaml:\"tokens_in\"",
(gogoproto.moretags) = "yaml:\"tokens_out\"",
(gogoproto.nullable) = false
];
string shares_out = 2 [
Expand Down
4 changes: 1 addition & 3 deletions x/gamm/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ func (k Keeper) GetNextPoolIdAndIncrement(ctx sdk.Context) uint64 {
return k.getNextPoolIdAndIncrement(ctx)
}

func (_ Keeper) GetMaximalNoSwapLPAmount(ctx sdk.Context, pool types.PoolI, numShares sdk.Int) (sdk.Coins, error) {
return getMaximalNoSwapLPAmount(ctx, pool, numShares)
}
//
19 changes: 4 additions & 15 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (q Querier) CalcExitPoolCoinsFromShares(ctx context.Context, req *types.Que
}

// SimJoinPoolNoSwap returns the amount of shares you'd get if joined a pool without a swap and tokens which need to be provided
func (q Querier) CalcJoinPoolNoSwap(ctx context.Context, req *types.QueryJoinPoolNoSwapRequest) (*types.QueryJoinPoolNoSwapResponse, error) {
func (q Querier) CalcJoinPoolNoSwap(ctx context.Context, req *types.QueryCalcJoinPoolNoSwapRequest) (*types.QueryCalcJoinPoolNoSwapResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
Expand All @@ -205,24 +205,13 @@ func (q Querier) CalcJoinPoolNoSwap(ctx context.Context, req *types.QueryJoinPoo
return nil, err
}

neededLpLiquidity, err := getMaximalNoSwapLPAmount(sdkCtx, pool, req.SharesOutAmount)
sharesOut, tokensJoined, err := pool.CalcJoinPoolNoSwapShares(sdkCtx, req.TokensIn, pool.GetSwapFee(sdkCtx))
if err != nil {
return nil, err
}

sharesOut, _, err := pool.CalcJoinPoolNoSwapShares(sdkCtx, neededLpLiquidity, pool.GetSwapFee(sdkCtx))
if err != nil {
return nil, err
}

// sanity check
if sharesOut.LT(req.SharesOutAmount) {
return nil, fmt.Errorf("Expected to JoinPoolNoSwap >= %s shares, actually did %s shares",
req.SharesOutAmount, sharesOut)
}

return &types.QueryJoinPoolNoSwapResponse{
TokensIn: neededLpLiquidity,
return &types.QueryCalcJoinPoolNoSwapResponse{
TokensOut: tokensJoined,
SharesOut: sharesOut,
}, nil
}
Expand Down
89 changes: 42 additions & 47 deletions x/gamm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,63 +87,63 @@ func (suite *KeeperTestSuite) TestCalcExitPoolCoinsFromShares() {
}
}

func (suite *KeeperTestSuite) TestCalcJoinPoolNoSwap() {
func (suite *KeeperTestSuite) TestCalcJoinPoolNoSwapShares() {
queryClient := suite.queryClient
poolId := suite.PrepareBalancerPool()
ctx := suite.Ctx
poolId := suite.PrepareBalancerPool()
swapFee := sdk.ZeroDec()

testCases := []struct {
name string
sharesOutAmount sdk.Int
poolId uint64
expectingErr bool
name string
poolId uint64
tokensIn sdk.Coins
expectedErr error
}{
{
name: "valid test case",
sharesOutAmount: types.OneShare.MulRaw(50),
poolId: poolId,

expectingErr: false,
"valid uneven multi asset join test case",
poolId,
sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(5000000)), sdk.NewCoin("bar", sdk.NewInt(5000000)), sdk.NewCoin("baz", sdk.NewInt(5000000)), sdk.NewCoin("uosmo", sdk.NewInt(5000000))),
nil,
},
{
name: "invalid pool id",
sharesOutAmount: types.OneShare.MulRaw(50),
poolId: poolId + 1,

expectingErr: true,
"valid even multi asset join test case",
poolId,
sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(500000)), sdk.NewCoin("bar", sdk.NewInt(1000000)), sdk.NewCoin("baz", sdk.NewInt(1500000)), sdk.NewCoin("uosmo", sdk.NewInt(2000000))),
nil,
},
{
name: "negative shares out",
sharesOutAmount: sdk.NewInt(-1),
poolId: poolId,

expectingErr: true,
"valid single asset join test case",
poolId,
sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(1000000))),
nil,
},
{
name: "no pool id",
sharesOutAmount: sdk.NewInt(-1),

expectingErr: true,
"pool id does not exist",
poolId + 1,
sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(1000000))),
types.PoolDoesNotExistError{PoolId: poolId + 1},
},
{
name: "zero shares",
sharesOutAmount: sdk.ZeroInt(),
poolId: poolId,

expectingErr: true,
"token in denom does not exist",
poolId,
sdk.NewCoins(sdk.NewCoin("random", sdk.NewInt(10000))),
sdkerrors.Wrapf(types.ErrDenomNotFoundInPool, "input denoms must already exist in the pool (%s)", "random"),
},
{
"join pool with incorrect amount of assets",
poolId,
sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(10000)), sdk.NewCoin("bar", sdk.NewInt(10000))),
errors.New("balancer pool only supports LP'ing with one asset or all assets in pool"),
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
out, err := queryClient.CalcJoinPoolNoSwap(gocontext.Background(), &types.QueryJoinPoolNoSwapRequest{
PoolId: tc.poolId,
SharesOutAmount: tc.sharesOutAmount,
out, err := queryClient.CalcJoinPoolNoSwap(gocontext.Background(), &types.QueryCalcJoinPoolNoSwapRequest{
PoolId: tc.poolId,
TokensIn: tc.tokensIn,
})

if !tc.expectingErr {
suite.Require().NoError(err)

if tc.expectedErr == nil {
poolRes, err := queryClient.Pool(gocontext.Background(), &types.QueryPoolRequest{
PoolId: tc.poolId,
})
Expand All @@ -153,21 +153,16 @@ func (suite *KeeperTestSuite) TestCalcJoinPoolNoSwap() {
err = suite.App.InterfaceRegistry().UnpackAny(poolRes.Pool, &pool)
suite.Require().NoError(err)

liquidityBefore := pool.GetTotalPoolLiquidity(ctx)

neededLpLiquidity, err := suite.App.GAMMKeeper.GetMaximalNoSwapLPAmount(ctx, pool, tc.sharesOutAmount)
suite.Require().NoError(err)
expectedShares, _, err := pool.CalcJoinPoolNoSwapShares(ctx, neededLpLiquidity, pool.GetSwapFee(ctx))
numShares, numLiquidity, err := pool.CalcJoinPoolNoSwapShares(ctx, tc.tokensIn, swapFee)
suite.Require().NoError(err)

suite.Require().Equal(out.SharesOut, expectedShares)
suite.Require().Equal(out.TokensIn, neededLpLiquidity)
suite.Require().Equal(liquidityBefore, pool.GetTotalPoolLiquidity(ctx))
suite.Require().Equal(numShares, out.SharesOut)
suite.Require().Equal(numLiquidity, out.TokensOut)
} else {
suite.Require().Error(err)
suite.Require().EqualError(err, tc.expectedErr.Error())
}
})
}

}
func (suite *KeeperTestSuite) TestCalcJoinPoolShares() {
queryClient := suite.queryClient
Expand Down
Loading

0 comments on commit 8c1842a

Please sign in to comment.