Skip to content

x/gamm: Add fixed gas cost for swaps#2016

Merged
ValarDragon merged 10 commits intoosmosis-labs:mainfrom
rrrliu:rrrliu/1903-charge-gas-for-computation
Jul 17, 2022
Merged

x/gamm: Add fixed gas cost for swaps#2016
ValarDragon merged 10 commits intoosmosis-labs:mainfrom
rrrliu:rrrliu/1903-charge-gas-for-computation

Conversation

@rrrliu
Copy link
Copy Markdown
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
// 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
Copy Markdown
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
Copy Markdown
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))

}
tokensIn := sdk.Coins{tokenIn}

ctx.GasMeter().ConsumeGas(types.GasFeeForSwap, "swap computation")
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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?

Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

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

// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Thanks for making this!

@mattverse
Copy link
Copy Markdown
Contributor

@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 <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>
@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