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

update swap.sol #1618

Closed
wants to merge 3 commits into from
Closed

update swap.sol #1618

wants to merge 3 commits into from

Conversation

Tanisha0708
Copy link

The function is now marked as pure, indicating it does not modify the state of the contract. This could save gas costs when called, as it doesn’t involve storage operations. Direct Return: Instead of storing values in the state variables and then swapping, we return the swapped values directly. This avoids unnecessary storage updates.

Event Logging: The Swapped event allows anyone to track when swaps occur. You can expand this by logging the values before and after the swap for more context.

Input Validation: The require statement checks that the two numbers are not the same before proceeding with the swap. This prevents unnecessary operations and helps enforce a logical constraint in your function.

Indexed Parameters in Events: Using indexed in the event allows easier filtering of events in transaction logs, making it more efficient to search for specific swaps.
inshort this will be beneficial for larger bit numbers

User-Defined Precision for Decimal Places
In the original code, the average was calculated and stored as an integer, meaning any decimal part was lost due to Solidity’s lack of native floating-point support.
I added a decimals parameter to the function so the user can specify how many decimal places they want in the result. This gives more flexibility, as the user can define the precision they need.
2. Simulating Decimals in Solidity
Since Solidity doesn’t support floating-point numbers, I simulated decimals by multiplying the result by 10^decimals. This approach allows storing the average as an integer while retaining precision.
For example, if the average is 456 and the user wants four decimal places, the contract now stores 4560000 (which represents 456.0000).
This prevents loss of precision that would occur in calculations where the average isn’t a whole number.
3. Formatting Output as a String
I added a helper function, getFormattedAvg, to convert the integer result into a string with the specified number of decimal places. This formatting is more user-friendly and visually represents the average with decimals.
The function breaks down the avg into its integer and fractional parts based on the user-defined precision, then formats it as "integer.fractional".
4. Improved Precision Control
By letting the user define decimals, this contract can avoid excessive precision that isn’t needed, reducing computation complexity. For example, if the user wants only 2 decimal places, there’s no need to calculate 10 or more places, saving gas costs.

the user freindly access the avg value at till what he want a answer
Optimized for Gas Efficiency: Removed the need to store the cube result (cube_), saving gas. Instead, getCube is a pure function, which calculates and returns the result without modifying the contract’s state.
Input Validation: Added a check to ensure _number is small enough to prevent overflow during cube calculation.
Flexible Interface: getCube calculates the cube directly without modifying the state, and setAndCube can be used if the input needs to be stored in number.
 The function is now marked as pure, indicating it does not modify the state of the contract. This could save gas costs when called, as it doesn’t involve storage operations. Direct Return: Instead of storing values in the state variables and then swapping, we return the swapped values directly. This avoids unnecessary storage updates.

Event Logging: The Swapped event allows anyone to track when swaps occur. You can expand this by logging the values before and after the swap for more context.

Input Validation: The require statement checks that the two numbers are not the same before proceeding with the swap. This prevents unnecessary operations and helps enforce a logical constraint in your function.

Indexed Parameters in Events: Using indexed in the event allows easier filtering of events in transaction logs, making it more efficient to search for specific swaps.
inshort this will be beneficial for larger bit numbers
Copy link

github-actions bot commented Nov 5, 2024

@Tanisha0708

It's great having you contribute to this project

Thank you for opening a Pull Request 🙌 , Welcome to Project Guidance 💖 We will review everything and get back to you :)

@Tanisha0708
Copy link
Author

@Kushal997-das please once go through this pr

@Kushal997-das
Copy link
Owner

@Tanisha0708 Open issue before PR.

@Kushal997-das Kushal997-das added invalid You either opened a PR without an issue or the files aren't appropriate. gssoc-ext This level is for GSSOC-Extended. labels Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gssoc-ext This level is for GSSOC-Extended. invalid You either opened a PR without an issue or the files aren't appropriate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants