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

Gas related #1

Open
Magicianhax opened this issue Dec 8, 2024 · 1 comment
Open

Gas related #1

Magicianhax opened this issue Dec 8, 2024 · 1 comment

Comments

@Magicianhax
Copy link

I am here to seek a bit of help. I am trying to create a memecoin and a game contract where you send the memecoin I’ve created, and it will either double or result in nothing. The contract is deployed, but it needs some gas to transact. However, I just realized I can’t load gas into the contract because MITO is non-transferable. I’m seeking help because I can’t find any documentation related to this.

@pazdzioch78
Copy link

Proposal for Gas Optimization

I would like to suggest the following optimizations to improve gas efficiency in the smart contracts:

  1. Replace dynamic arrays with static arrays:

    • Dynamic arrays consume more gas compared to static arrays. Where possible, replace dynamic arrays with fixed-size arrays. Example:
      // Before:
      uint[] public dynamicArray;
      
      // After:
      uint[10] public staticArray; // Fixed size
  2. Use calldata instead of memory for public functions:

    • Parameters passed to public functions can be marked as calldata to reduce memory allocation. Example:
      // Before:
      function processData(uint[] memory data) public {
          // logic
      }
      
      // After:
      function processData(uint[] calldata data) public {
          // logic
      }
  3. Optimize loop logic:

    • Reduce the number of iterations in loops by precomputing values or simplifying logic. Example:
      // Before:
      for (uint i = 0; i < data.length; i++) {
          total += data[i];
      }
      
      // After:
      uint len = data.length;
      for (uint i = 0; i < len; i++) {
          total += data[i];
      }
  4. Refactor unnecessary storage reads:

    • Cache storage variables in memory to avoid repeated reads. Example:
      // Before:
      total += balances[user];
      total += balances[user];
      
      // After:
      uint balance = balances[user];
      total += balance;
      total += balance;

Request for Feedback

Please let me know if these changes align with your optimization goals. I am happy to assist further in implementing or testing these suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@Magicianhax @pazdzioch78 and others