Skip to content
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

* [#2016](https://github.com/osmosis-labs/osmosis/pull/2016) Add fixed 10000 gas cost for each Balancer swap

### Breaking Changes


Expand Down
1 change: 1 addition & 0 deletions x/gamm/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (k Keeper) swapExactAmountOut(
return sdk.Int{}, sdkerrors.Wrapf(types.ErrTooManyTokensOut,
"can't get more tokens out than there are tokens in the pool")
}

tokenIn, err := pool.SwapInAmtGivenOut(ctx, sdk.Coins{tokenOut}, tokenInDenom, swapFee)
if err != nil {
return sdk.Int{}, err
Expand Down
8 changes: 8 additions & 0 deletions x/gamm/keeper/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ func (suite *KeeperTestSuite) TestBalancerPoolSimpleSwapExactAmountIn() {
spotPriceBefore, err := keeper.CalculateSpotPrice(ctx, poolId, test.param.tokenIn.Denom, test.param.tokenOutDenom)
suite.NoError(err, "test: %v", test.name)

prevGasConsumed := suite.Ctx.GasMeter().GasConsumed()
tokenOutAmount, err := keeper.SwapExactAmountIn(ctx, suite.TestAccs[0], poolId, test.param.tokenIn, test.param.tokenOutDenom, test.param.tokenOutMinAmount)
suite.NoError(err, "test: %v", test.name)
suite.True(tokenOutAmount.Equal(test.param.expectedTokenOut), "test: %v", test.name)
gasConsumedForSwap := suite.Ctx.GasMeter().GasConsumed() - prevGasConsumed
// We consume `types.GasFeeForSwap` directly, so the extra I/O operation mean we end up consuming more.
suite.Assert().Greater(gasConsumedForSwap, uint64(types.BalancerGasFeeForSwap))

assertEventEmitted(suite, ctx, types.TypeEvtTokenSwapped, 1)

Expand Down Expand Up @@ -199,11 +203,15 @@ func (suite *KeeperTestSuite) TestBalancerPoolSimpleSwapExactAmountOut() {
spotPriceBefore, err := keeper.CalculateSpotPrice(ctx, poolId, test.param.tokenInDenom, test.param.tokenOut.Denom)
suite.NoError(err, "test: %v", test.name)

prevGasConsumed := suite.Ctx.GasMeter().GasConsumed()
tokenInAmount, err := keeper.SwapExactAmountOut(ctx, suite.TestAccs[0], poolId, test.param.tokenInDenom, test.param.tokenInMaxAmount, test.param.tokenOut)
suite.NoError(err, "test: %v", test.name)
suite.True(tokenInAmount.Equal(test.param.expectedTokenInAmount),
"test: %v\n expect_eq actual: %s, expected: %s",
test.name, tokenInAmount, test.param.expectedTokenInAmount)
gasConsumedForSwap := suite.Ctx.GasMeter().GasConsumed() - prevGasConsumed
// We consume `types.GasFeeForSwap` directly, so the extra I/O operation mean we end up consuming more.
suite.Assert().Greater(gasConsumedForSwap, uint64(types.BalancerGasFeeForSwap))

assertEventEmitted(suite, ctx, types.TypeEvtTokenSwapped, 1)

Expand Down
2 changes: 2 additions & 0 deletions x/gamm/pool-models/balancer/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ func (p *Pool) SwapInAmtGivenOut(

// ApplySwap.
func (p *Pool) applySwap(ctx sdk.Context, tokensIn sdk.Coins, tokensOut sdk.Coins) error {
// Fixed gas consumption per swap to prevent spam
ctx.GasMeter().ConsumeGas(types.BalancerGasFeeForSwap, "balancer swap computation")
// Also ensures that len(tokensIn) = 1 = len(tokensOut)
inPoolAsset, outPoolAsset, err := p.parsePoolAssetsCoins(tokensIn, tokensOut)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion x/gamm/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const (
OneShareExponent = 18
// Raise 10 to the power of SigFigsExponent to determine number of significant figures.
// i.e. SigFigExponent = 8 is 10^8 which is 100000000. This gives 8 significant figures.
SigFigsExponent = 8
SigFigsExponent = 8
BalancerGasFeeForSwap = 10_000
)

var (
Expand Down