Skip to content

Commit e9907ce

Browse files
rrrliualexanderbez
andauthored
x/gamm: Add fixed gas cost for swaps (#2016)
* add gas for swap + test * test file comments * move swap consumption to balancer only * fix one more test case * add to changelog * Update CHANGELOG.md Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com> * rm TODO now that we have issue * underscore for clarity * gofmt Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
1 parent 8e6e4de commit e9907ce

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4242

4343
## Unreleased
4444

45+
* [#2016](https://github.com/osmosis-labs/osmosis/pull/2016) Add fixed 10000 gas cost for each Balancer swap
46+
4547
### Breaking Changes
4648

4749

x/gamm/keeper/swap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (k Keeper) swapExactAmountOut(
114114
return sdk.Int{}, sdkerrors.Wrapf(types.ErrTooManyTokensOut,
115115
"can't get more tokens out than there are tokens in the pool")
116116
}
117+
117118
tokenIn, err := pool.SwapInAmtGivenOut(ctx, sdk.Coins{tokenOut}, tokenInDenom, swapFee)
118119
if err != nil {
119120
return sdk.Int{}, err

x/gamm/keeper/swap_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,13 @@ func (suite *KeeperTestSuite) TestBalancerPoolSimpleSwapExactAmountIn() {
9595
spotPriceBefore, err := keeper.CalculateSpotPrice(ctx, poolId, test.param.tokenIn.Denom, test.param.tokenOutDenom)
9696
suite.NoError(err, "test: %v", test.name)
9797

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

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

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

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

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

x/gamm/pool-models/balancer/pool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ func (p *Pool) SwapInAmtGivenOut(
584584

585585
// ApplySwap.
586586
func (p *Pool) applySwap(ctx sdk.Context, tokensIn sdk.Coins, tokensOut sdk.Coins) error {
587+
// Fixed gas consumption per swap to prevent spam
588+
ctx.GasMeter().ConsumeGas(types.BalancerGasFeeForSwap, "balancer swap computation")
587589
// Also ensures that len(tokensIn) = 1 = len(tokensOut)
588590
inPoolAsset, outPoolAsset, err := p.parsePoolAssetsCoins(tokensIn, tokensOut)
589591
if err != nil {

x/gamm/types/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const (
1111
OneShareExponent = 18
1212
// Raise 10 to the power of SigFigsExponent to determine number of significant figures.
1313
// i.e. SigFigExponent = 8 is 10^8 which is 100000000. This gives 8 significant figures.
14-
SigFigsExponent = 8
14+
SigFigsExponent = 8
15+
BalancerGasFeeForSwap = 10_000
1516
)
1617

1718
var (

0 commit comments

Comments
 (0)