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: Add fixed gas cost for swaps #2016

Merged

Conversation

rrrliu
Copy link
Contributor

@rrrliu rrrliu commented Jul 11, 2022

Closes: #1903

What is the purpose of the change

To further prevent spam, adds fixed amount of gas we consume per swap in/out. Previously we had only been consuming gas for built-in I/O (store reads and writes), and in the future we might make

Brief Changelog

  • Added new BalancerGasFeeForSwap constant + TODO comment to turn into param
  • Have swapExactAmountIn and swapExactAmountOut consume gas
  • Test this in swap_test.go

Testing and Verifying

This change added tests and can be verified as follows:

  • Added lines to test gas consumed in swap_test.go

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (yes)
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? yes
  • How is the feature or change documented? (not applicable / specification (x/<module>/spec/) / Osmosis docs repo / not documented)

@rrrliu rrrliu requested a review from a team July 11, 2022 07:33
@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label Jul 11, 2022
@@ -12,6 +12,8 @@ const (
// 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
// TODO: Current fixed cost gas fee per swap -- turn this into a param in the future.
GasFeeForSwap = 10000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not directly just make this a param?

Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided to make it a fixed cost for now to avoid needing to set up migration code for a change this small (#1903 (comment))

@@ -49,6 +49,7 @@ func (k Keeper) swapExactAmountIn(
}
tokensIn := sdk.Coins{tokenIn}

ctx.GasMeter().ConsumeGas(types.GasFeeForSwap, "swap computation")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels safer to move the gas consumption to the top-most level of abstraction, i.e the msg handler for swapping.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be in each core loop here right, so that we scale by number of hops in a multi-hop?

(Also needs to be in swapExactAmountOut)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess really it should probably be defined per pool type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way we have it per Balancer swap, rather than per total swap. (And can then later charge a different gas amount for stableswap / next CFMM)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense, just updated! Should we update balancer pool docs with this or should we wait until it becomes a param?

x/gamm/types/constants.go Outdated Show resolved Hide resolved
x/gamm/keeper/swap_test.go Outdated Show resolved Hide resolved
Copy link
Member

@mattverse mattverse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Can we add a Changelog entry for this PR?

@github-actions github-actions bot added the C:docs Improvements or additions to documentation label Jul 13, 2022
@mattverse
Copy link
Member

Also out of curiousity, how was the constant 10000 decided?

@@ -12,6 +12,8 @@ const (
// 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
// TODO: Turn this into a param in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Can we make an issue for this please and remove the comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, issue please with a reference 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created one! #2058

CHANGELOG.md Show resolved Hide resolved
@@ -12,6 +12,8 @@ const (
// 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
// TODO: Turn this into a param in the future.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, issue please with a reference 👍

@rrrliu
Copy link
Contributor Author

rrrliu commented Jul 15, 2022

Also out of curiousity, how was the constant 10000 decided?

@mattverse oh I just benchmarked the swap i/o gas and it was around ~40000, so decided to have it be something around the same order of magnitude. No strong opinion on this though so open to changing.

@ValarDragon
Copy link
Member

Thanks for making this!

@mattverse
Copy link
Member

@rrrliu ah gotcha thanks for explaining! Nice work done here!

@ValarDragon ValarDragon merged commit e9907ce into osmosis-labs:main Jul 17, 2022
mattverse pushed a commit that referenced this pull request Jul 20, 2022
* 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 <[email protected]>

* rm TODO now that we have issue

* underscore for clarity

* gofmt

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
@czarcas7ic czarcas7ic mentioned this pull request Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x/gamm: Charge gas for computation in swaps instead of just I/O
6 participants