if staking a tokenX the contract needs to set the state variables accordingly, and if staking a tokenY set the other state variables. i have various helper functions for this but was wondering what the most gas efficient way of doing this was. option 1: two separate functions for each specific call option 2: one function that takes the param of the token type (X | Y) and has an if X ... else ...

If option 2 is more gas efficient what is the best way to store X and Y? bool: true | false, int: 0 | else string: 'x'|else ? just curious and am looking at various ways for optimizing gas

1 Answer 1


This kind of things are usually easiest to just try out, and measure the gas usage.

As a rule of thumb, use the smallest possible variable type (for example booleans). There are lots of different optimizations that can be tried (for example tight packing (Efficient Bit Packing)).

On the other hand, you should also consider code readability. Clear and easy to understand code increases trust, since more people are able to read what it does. So it's always a trade off between efficiency and readability.

Also, you'll need to consider whether you want to optimize for deployment or usage (or both).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.