Skip to content

Commit

Permalink
switch sansSwapFee for withSwapFee (osmosis-labs#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored May 14, 2021
1 parent 81c0815 commit 7539deb
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 104 deletions.
2 changes: 1 addition & 1 deletion proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ message QuerySpotPriceRequest {
string tokenInDenom = 2 [ (gogoproto.moretags) = "yaml:\"token_in_denom\"" ];
string tokenOutDenom = 3
[ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ];
bool sansSwapFee = 4 [ (gogoproto.moretags) = "yaml:\"sans_swap_fee\"" ];
bool withSwapFee = 4 [ (gogoproto.moretags) = "yaml:\"with_swap_fee\"" ];
}
message QuerySpotPriceResponse {
// String of the Dec. Ex) 10.203uatom
Expand Down
4 changes: 2 additions & 2 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func (k Keeper) SpotPrice(ctx context.Context, req *types.QuerySpotPriceRequest)

var sp sdk.Dec
var err error
if req.SansSwapFee {
sp, err = k.CalculateSpotPriceSansSwapFee(sdkCtx, req.PoolId, req.TokenInDenom, req.TokenOutDenom)
if req.WithSwapFee {
sp, err = k.CalculateSpotPriceWithSwapFee(sdkCtx, req.PoolId, req.TokenInDenom, req.TokenOutDenom)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions x/gamm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func (suite *KeeperTestSuite) preparePool() uint64 {
ExitFee: sdk.NewDec(0),
})

spotPrice, err := suite.app.GAMMKeeper.CalculateSpotPrice(suite.ctx, poolId, "foo", "bar")
spotPrice, err := suite.app.GAMMKeeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, "foo", "bar")
suite.NoError(err)
suite.Equal(sdk.NewDec(2).String(), spotPrice.String())
spotPrice, err = suite.app.GAMMKeeper.CalculateSpotPrice(suite.ctx, poolId, "bar", "baz")
spotPrice, err = suite.app.GAMMKeeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, "bar", "baz")
suite.NoError(err)
suite.Equal(sdk.NewDecWithPrec(15, 1).String(), spotPrice.String())
spotPrice, err = suite.app.GAMMKeeper.CalculateSpotPrice(suite.ctx, poolId, "baz", "foo")
spotPrice, err = suite.app.GAMMKeeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, "baz", "foo")
suite.NoError(err)
suite.Equal(sdk.NewDec(1).Quo(sdk.NewDec(3)).String(), spotPrice.String())

Expand Down
12 changes: 6 additions & 6 deletions x/gamm/keeper/multihop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (suite *KeeperTestSuite) TestSimpleMultihopSwapExactAmountIn() {
tokenInDenom = test.param.routes[i-1].TokenOutDenom
}

sp, err := keeper.CalculateSpotPrice(suite.ctx, route.PoolId, tokenInDenom, route.TokenOutDenom)
sp, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, route.PoolId, tokenInDenom, route.TokenOutDenom)
suite.NoError(err, "test: %v", test.name)
dec = dec.Mul(sp)
}
Expand All @@ -77,14 +77,14 @@ func (suite *KeeperTestSuite) TestSimpleMultihopSwapExactAmountIn() {
tokenInDenom = test.param.routes[i-1].TokenOutDenom
}

sp, err := keeper.CalculateSpotPrice(suite.ctx, route.PoolId, tokenInDenom, route.TokenOutDenom)
sp, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, route.PoolId, tokenInDenom, route.TokenOutDenom)
suite.NoError(err, "test: %v", test.name)
dec = dec.Mul(sp)
}
return dec
}()

// Ratio of the oken out should be between the before spot price and after spot price.
// Ratio of the token out should be between the before spot price and after spot price.
sp := test.param.tokenIn.Amount.ToDec().Quo(tokenOutAmount.ToDec())
suite.True(sp.GT(spotPriceBefore) && sp.LT(spotPriceAfter), "test: %v", test.name)
} else {
Expand Down Expand Up @@ -146,7 +146,7 @@ func (suite *KeeperTestSuite) TestSimpleMultihopSwapExactAmountOut() {
tokenOutDenom = test.param.routes[i+1].TokenInDenom
}

sp, err := keeper.CalculateSpotPrice(suite.ctx, route.PoolId, route.TokenInDenom, tokenOutDenom)
sp, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, route.PoolId, route.TokenInDenom, tokenOutDenom)
suite.NoError(err, "test: %v", test.name)
dec = dec.Mul(sp)
}
Expand All @@ -165,14 +165,14 @@ func (suite *KeeperTestSuite) TestSimpleMultihopSwapExactAmountOut() {
tokenOutDenom = test.param.routes[i+1].TokenInDenom
}

sp, err := keeper.CalculateSpotPrice(suite.ctx, route.PoolId, route.TokenInDenom, tokenOutDenom)
sp, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, route.PoolId, route.TokenInDenom, tokenOutDenom)
suite.NoError(err, "test: %v", test.name)
dec = dec.Mul(sp)
}
return dec
}()

// Ratio of the oken out should be between the before spot price and after spot price.
// Ratio of the token out should be between the before spot price and after spot price.
sp := tokenInAmount.ToDec().Quo(test.param.tokenOut.Amount.ToDec())
suite.True(sp.GT(spotPriceBefore) && sp.LT(spotPriceAfter), "test: %v", test.name)
} else {
Expand Down
4 changes: 2 additions & 2 deletions x/gamm/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (k Keeper) updatePoolForSwap(
return err
}

func (k Keeper) CalculateSpotPrice(ctx sdk.Context, poolId uint64, tokenInDenom, tokenOutDenom string) (sdk.Dec, error) {
func (k Keeper) CalculateSpotPriceWithSwapFee(ctx sdk.Context, poolId uint64, tokenInDenom, tokenOutDenom string) (sdk.Dec, error) {
pool, inPoolAsset, outPoolAsset, err :=
k.getPoolAndInOutAssets(ctx, poolId, tokenInDenom, tokenOutDenom)
if err != nil {
Expand All @@ -164,7 +164,7 @@ func (k Keeper) CalculateSpotPrice(ctx sdk.Context, poolId uint64, tokenInDenom,
), nil
}

func (k Keeper) CalculateSpotPriceSansSwapFee(ctx sdk.Context, poolId uint64, tokenInDenom, tokenOutDenom string) (sdk.Dec, error) {
func (k Keeper) CalculateSpotPrice(ctx sdk.Context, poolId uint64, tokenInDenom, tokenOutDenom string) (sdk.Dec, error) {
_, inPoolAsset, outPoolAsset, err :=
k.getPoolAndInOutAssets(ctx, poolId, tokenInDenom, tokenOutDenom)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions x/gamm/keeper/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func (suite *KeeperTestSuite) TestSimpleSwapExactAmountIn() {
keeper := suite.app.GAMMKeeper

if test.expectPass {
spotPriceBefore, err := keeper.CalculateSpotPrice(suite.ctx, poolId, test.param.tokenIn.Denom, test.param.tokenOutDenom)
spotPriceBefore, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, test.param.tokenIn.Denom, test.param.tokenOutDenom)
suite.NoError(err, "test: %v", test.name)

tokenOutAmount, _, err := keeper.SwapExactAmountIn(suite.ctx, acc1, poolId, test.param.tokenIn, test.param.tokenOutDenom, test.param.tokenOutMinAmount)
suite.NoError(err, "test: %v", test.name)

spotPriceAfter, err := keeper.CalculateSpotPrice(suite.ctx, poolId, test.param.tokenIn.Denom, test.param.tokenOutDenom)
spotPriceAfter, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, test.param.tokenIn.Denom, test.param.tokenOutDenom)
suite.NoError(err, "test: %v", test.name)

// Ratio of the token out should be between the before spot price and after spot price.
Expand Down Expand Up @@ -177,16 +177,16 @@ func (suite *KeeperTestSuite) TestSimpleSwapExactAmountOut() {
keeper := suite.app.GAMMKeeper

if test.expectPass {
spotPriceBefore, err := keeper.CalculateSpotPrice(suite.ctx, poolId, test.param.tokenInDenom, test.param.tokenOut.Denom)
spotPriceBefore, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, test.param.tokenInDenom, test.param.tokenOut.Denom)
suite.NoError(err, "test: %v", test.name)

tokenInAmount, _, err := keeper.SwapExactAmountOut(suite.ctx, acc1, poolId, test.param.tokenInDenom, test.param.tokenInMaxAmount, test.param.tokenOut)
suite.NoError(err, "test: %v", test.name)

spotPriceAfter, err := keeper.CalculateSpotPrice(suite.ctx, poolId, test.param.tokenInDenom, test.param.tokenOut.Denom)
spotPriceAfter, err := keeper.CalculateSpotPriceWithSwapFee(suite.ctx, poolId, test.param.tokenInDenom, test.param.tokenOut.Denom)
suite.NoError(err, "test: %v", test.name)

// Ratio of the oken out should be between the before spot price and after spot price.
// Ratio of the token out should be between the before spot price and after spot price.
tradeAvgPrice := tokenInAmount.ToDec().Quo(test.param.tokenOut.Amount.ToDec())
suite.True(tradeAvgPrice.GT(spotPriceBefore) && tradeAvgPrice.LT(spotPriceAfter), "test: %v", test.name)
} else {
Expand Down
Loading

0 comments on commit 7539deb

Please sign in to comment.