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

Feat/min bet patch #24

Merged
merged 19 commits into from
Jun 17, 2024
Merged

Feat/min bet patch #24

merged 19 commits into from
Jun 17, 2024

Conversation

magiodev
Copy link
Owner

@magiodev magiodev commented Jun 16, 2024

The changes introduce Decimal for better precision in bid and prize calculations, adjust game configuration with new fields like game_duration_epoch, and adopt Decimal::from_str for validation. There's also refactoring and test adjustments, improving accuracy in managing bids, prizes, and game state, adhering to project coding standards.

Changes

File Path Change Summary
contracts/prudent-pots/src/execute.rs Enhanced bid and decay factor calculation using Decimal; Added game_duration_epoch handling.
contracts/prudent-pots/src/helpers/pot.rs Added Decimal and env parameter to functions; Refactored bid logic; Added calculate_max_bid.
contracts/prudent-pots/src/tests/calculate_total_losing_tokens.rs Updated test from setup_game_no_raffle_works to setup_game_works.
contracts/prudent-pots/src/tests/distribute_tokens.rs Changed test setup function to setup_game_works.
contracts/prudent-pots/src/tests/instantiate.rs Added Decimal; Updated initialization with game_duration_epoch and Decimal for decay_factor.
contracts/prudent-pots/src/tests/integration/allocate_tokens.rs Enhanced error handling with .unwrap() and .unwrap_err(); Simplified assertions.
contracts/prudent-pots/src/tests/integration/fixtures.rs Added Decimal; Introduced constants like GAME_EXTEND; Modified game_duration_epoch, decay_factor.
contracts/prudent-pots/src/tests/is_winning_pot.rs Updated test set-up to setup_game_works.
contracts/prudent-pots/src/tests/prepare_next_game.rs Adjusted end_time calculation without adding 1 to env.block.time.seconds().
contracts/prudent-pots/src/helpers/game_end.rs Replaced Uint128 with Decimal for prize_percentage; Adjusted calculation logic.

Copy link

vercel bot commented Jun 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
prudent-pots 🛑 Canceled (Inspect) Jun 17, 2024 10:07pm
prudent-pots-ms ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 17, 2024 10:07pm

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 7

Outside diff range and nitpick comments (3)
contracts/prudent-pots/src/state.rs (1)

[!TIP]
Codebase Verification

The script's output shows that OldGameConfig is still present in multiple files. This indicates that the migration to GameConfig is incomplete.

  • contracts/prudent-pots/src/state.rs
  • contracts/prudent-pots/src/bin/schema.rs
  • contracts/prudent-pots/src/contract.rs
  • contracts/prudent-pots/src/error.rs
  • contracts/prudent-pots/src/execute.rs
  • contracts/prudent-pots/src/helpers/game_end.rs
  • contracts/prudent-pots/src/helpers/mod.rs
  • contracts/prudent-pots/src/helpers/pot.rs
  • contracts/prudent-pots/src/helpers/validate.rs
  • contracts/prudent-pots/src/lib.rs
  • contracts/prudent-pots/src/msg.rs
  • contracts/prudent-pots/src/query.rs
  • contracts/prudent-pots/src/reply.rs
  • contracts/prudent-pots/src/tests/calculate_total_losing_tokens.rs
  • contracts/prudent-pots/src/tests/distribute_tokens.rs
  • contracts/prudent-pots/src/tests/instantiate.rs
  • contracts/prudent-pots/src/tests/integration/allocate_tokens.rs
  • contracts/prudent-pots/src/tests/integration/fixtures.rs
  • contracts/prudent-pots/src/tests/integration/game_end.rs
  • contracts/prudent-pots/src/tests/integration/helpers.rs
  • contracts/prudent-pots/src/tests/integration/instantiate.rs
  • contracts/prudent-pots/src/tests/integration/mod.rs
  • contracts/prudent-pots/src/tests/integration/reallocate_tokens.rs
  • contracts/prudent-pots/src/tests/integration/update_config.rs
  • contracts/prudent-pots/src/tests/is_winning_pot.rs
  • contracts/prudent-pots/src/tests/mod.rs
  • contracts/prudent-pots/src/tests/prepare_next_game.rs

Please ensure that all references to OldGameConfig are replaced with GameConfig and validate the data migration logic in the migrate function.

Analysis chain

Line range hint 6-34: The transition from OldGameConfig to GameConfig with the inclusion of new fields like game_duration_epoch and using Decimal for decay_factor is a significant improvement for precision and future configuration flexibility. Ensure that all references to OldGameConfig are properly migrated to use GameConfig, and validate the data migration logic in the migrate function.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all references to `OldGameConfig` are replaced with `GameConfig`.
# Test: Search for references to `OldGameConfig`. Expect: No occurrences, only `GameConfig`.
rg --type rust $'OldGameConfig' ./src

Length of output: 82



Script:

#!/bin/bash
# Description: Locate Rust files and verify that all references to `OldGameConfig` are replaced with `GameConfig`.
# Step 1: Locate Rust files in the repository.
# Step 2: Search for references to `OldGameConfig` in located Rust files.

# Locate Rust files
fd -e rs > rust_files.txt

# Search for OldGameConfig references
while IFS= read -r file; do
  rg 'OldGameConfig' "$file"
done < rust_files.txt

Length of output: 2536

contracts/prudent-pots/src/helpers/pot.rs (1)

Line range hint 86-139: Consider adding error handling for potential panics from unwrap calls.

The use of unwrap in lines 97 and 101 assumes that subtraction and division will not fail. However, in a blockchain context, it's crucial to handle these potential errors gracefully to avoid panics that could halt the contract. Consider using pattern matching or conditional statements to handle these cases properly.

-        .unwrap();
+        .ok_or(ContractError::CalculationError)?;
contracts/prudent-pots/src/execute.rs (1)

Line range hint 70-88: Ensure the new configuration parameters are validated properly.

The validation logic for game_duration_epoch and decay_factor should ensure that these parameters are within reasonable bounds to prevent any logical errors or unexpected behavior in the contract.

+    if game_duration_epoch <= 0 {
+        return Err(ContractError::InvalidInput {});
+    }
+    if decay_factor <= Decimal::zero() || decay_factor >= Decimal::one() {
+        return Err(ContractError::InvalidInput {});
+    }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 750a803 and 03bf71a.

Files selected for processing (13)
  • contracts/prudent-pots/src/contract.rs (3 hunks)
  • contracts/prudent-pots/src/execute.rs (4 hunks)
  • contracts/prudent-pots/src/helpers/game_end.rs (3 hunks)
  • contracts/prudent-pots/src/helpers/pot.rs (4 hunks)
  • contracts/prudent-pots/src/msg.rs (3 hunks)
  • contracts/prudent-pots/src/query.rs (2 hunks)
  • contracts/prudent-pots/src/state.rs (3 hunks)
  • contracts/prudent-pots/src/tests/instantiate.rs (3 hunks)
  • contracts/prudent-pots/src/tests/integration/allocate_tokens.rs (2 hunks)
  • contracts/prudent-pots/src/tests/integration/fixtures.rs (5 hunks)
  • contracts/prudent-pots/src/tests/integration/game_end.rs (4 hunks)
  • contracts/prudent-pots/src/tests/integration/update_config.rs (3 hunks)
  • contracts/prudent-pots/src/tests/prepare_next_game.rs (1 hunks)
Files not reviewed due to errors (1)
  • contracts/prudent-pots/src/tests/integration/game_end.rs (no review received)
Files skipped from review due to trivial changes (1)
  • contracts/prudent-pots/src/tests/prepare_next_game.rs
Additional context used
GitHub Check: lint
contracts/prudent-pots/src/msg.rs

[failure] 29-29:
large size difference between variants

contracts/prudent-pots/src/helpers/pot.rs

[failure] 164-164:
useless conversion to the same type: cosmwasm_std::Uint128

contracts/prudent-pots/src/helpers/game_end.rs

[failure] 314-314:
manual implementation of an assign operation

Additional comments not posted (7)
contracts/prudent-pots/src/tests/integration/update_config.rs (1)

27-31: It's good to see the inclusion of game_duration_epoch and decay_factor using the Decimal type. However, ensure that all consuming functions and interfaces that interact with these variables can handle the Decimal type properly to avoid type mismatches or calculation errors.

Verification successful

The review comment's concern about ensuring all consuming functions and interfaces handle the Decimal type for game_duration_epoch and decay_factor is valid. Based on the search results, it appears that the Decimal type is consistently used where expected. Here are the key points:

  • contracts/prudent-pots/src/msg.rs: game_duration_epoch is defined as u64, and decay_factor is defined as Decimal.
  • contracts/prudent-pots/src/contract.rs: Properly assigns msg.decay_factor and msg.game_duration_epoch.
  • contracts/prudent-pots/src/helpers/pot.rs: Uses decay_factor in calculations, ensuring proper handling of the Decimal type.
  • contracts/prudent-pots/src/helpers/game_end.rs: Uses decay_factor in a calculation, ensuring proper handling.
  • contracts/prudent-pots/src/tests/instantiate.rs, tests/integration/fixtures.rs, tests/integration/update_config.rs: Testing files show proper initialization and use of decay_factor as Decimal.
  • contracts/prudent-pots/src/state.rs: Defines decay_factor as Decimal.
  • contracts/prudent-pots/src/execute.rs: Properly updates and checks decay_factor as Decimal.

No issues were found regarding the handling of the Decimal type for these variables.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all functions consuming `game_duration_epoch` and `decay_factor` handle the Decimal type.
# Test: Search for the usage of these variables. Expect: Proper handling of Decimal type.
rg --type rust $'game_duration_epoch|decay_factor'

Length of output: 3827

contracts/prudent-pots/src/tests/integration/allocate_tokens.rs (1)

18-18: Using .unwrap() in test code is generally acceptable for simplifying error handling. However, be cautious about this approach as it might obscure the underlying issues during test failures. It's often more informative to use explicit error checks that can provide more context when a test fails.

contracts/prudent-pots/src/tests/instantiate.rs (1)

Line range hint 22-32: The setup for GameConfig in tests is comprehensive, covering a wide range of configuration options. It's crucial to ensure that these settings are reflective of realistic scenarios that the contract might face in production. Consider adding variability in the test setups to cover different configurations and edge cases.

contracts/prudent-pots/src/msg.rs (2)

20-24: The inclusion of game_duration_epoch, decay_factor, and other parameters in UpdateGameConfig aligns with the PR's objectives to enhance game configuration flexibility. Ensure that all new fields are properly validated in the handling code to prevent invalid configurations.


155-158: The MigrateMsg struct now includes game_duration_epoch and decay_factor. This change supports the migration logic to update the game configuration with new parameters. Ensure that the migration logic correctly handles these fields, especially the conversion and validation of the Decimal type for decay_factor.

contracts/prudent-pots/src/contract.rs (1)

121-146: The migration logic correctly loads the old configuration, integrates new parameters from MigrateMsg, and removes the old configuration storage. This is a key part of the update mechanism for the contract. Ensure that all new fields are validated before saving them to storage to prevent corrupt configurations.

contracts/prudent-pots/src/tests/integration/fixtures.rs (1)

124-128: The updates to the test setup functions to include new game configuration parameters like game_duration_epoch, game_extend, and decay_factor are necessary to ensure that the tests accurately reflect the updated contract logic. It's good practice to also update the test assertions to verify these new configurations are handled correctly.

Also applies to: 203-207

contracts/prudent-pots/src/helpers/game_end.rs Outdated Show resolved Hide resolved
contracts/prudent-pots/src/helpers/game_end.rs Outdated Show resolved Hide resolved
contracts/prudent-pots/src/query.rs Show resolved Hide resolved
contracts/prudent-pots/src/helpers/pot.rs Outdated Show resolved Hide resolved
contracts/prudent-pots/src/helpers/pot.rs Outdated Show resolved Hide resolved
contracts/prudent-pots/src/execute.rs Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 03bf71a and 2b98097.

Files selected for processing (9)
  • contracts/prudent-pots/src/execute.rs (5 hunks)
  • contracts/prudent-pots/src/helpers/pot.rs (4 hunks)
  • contracts/prudent-pots/src/tests/calculate_total_losing_tokens.rs (6 hunks)
  • contracts/prudent-pots/src/tests/distribute_tokens.rs (2 hunks)
  • contracts/prudent-pots/src/tests/instantiate.rs (3 hunks)
  • contracts/prudent-pots/src/tests/integration/allocate_tokens.rs (4 hunks)
  • contracts/prudent-pots/src/tests/integration/fixtures.rs (6 hunks)
  • contracts/prudent-pots/src/tests/is_winning_pot.rs (5 hunks)
  • contracts/prudent-pots/src/tests/prepare_next_game.rs (3 hunks)
Files skipped from review due to trivial changes (2)
  • contracts/prudent-pots/src/tests/distribute_tokens.rs
  • contracts/prudent-pots/src/tests/instantiate.rs
Files skipped from review as they are similar to previous changes (4)
  • contracts/prudent-pots/src/execute.rs
  • contracts/prudent-pots/src/tests/integration/allocate_tokens.rs
  • contracts/prudent-pots/src/tests/integration/fixtures.rs
  • contracts/prudent-pots/src/tests/prepare_next_game.rs
Additional context used
GitHub Check: lint
contracts/prudent-pots/src/helpers/pot.rs

[failure] 164-164:
useless conversion to the same type: cosmwasm_std::Uint128

Additional comments not posted (8)
contracts/prudent-pots/src/tests/calculate_total_losing_tokens.rs (2)

11-11: Refactored test setup function name improves semantic clarity.


20-20: Ensure the setup_game_works function correctly initializes game state for testing scenarios.

Also applies to: 48-48, 77-77, 105-105, 134-134

contracts/prudent-pots/src/tests/is_winning_pot.rs (2)

9-9: Refactored test setup function name improves semantic clarity.


17-17: Ensure the setup_game_works function correctly initializes game state for testing scenarios.

Also applies to: 54-54, 91-91, 128-128, 165-165

Verification successful

The setup_game_works function is defined in contracts/prudent-pots/src/tests/instantiate.rs and is used in multiple test files such as is_winning_pot.rs, prepare_next_game.rs, distribute_tokens.rs, and calculate_total_losing_tokens.rs. The function initializes the game state by setting up the game configuration, calling the instantiate function, and configuring pot allocations. The instantiate function itself validates the game configuration and prepares the game state.

Based on the complete implementation and the extensive usage of setup_game_works in various test scenarios, it can be confirmed that setup_game_works correctly initializes the game state for testing.

  • contracts/prudent-pots/src/tests/instantiate.rs: Definition of setup_game_works
  • contracts/prudent-pots/src/tests/is_winning_pot.rs: Multiple usages
  • contracts/prudent-pots/src/tests/prepare_next_game.rs: Usage
  • contracts/prudent-pots/src/tests/distribute_tokens.rs: Usage
  • contracts/prudent-pots/src/tests/calculate_total_losing_tokens.rs: Multiple usages
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `setup_game_works` function correctly sets up the game state as expected.
rg --type rust $'setup_game_works' -- ./contracts/prudent-pots/src/tests/instantiate/tests.rs

Length of output: 188



Script:

#!/bin/bash
# Locate the definition of `setup_game_works` function in the repository.
rg 'fn setup_game_works' --type rust

Length of output: 115



Script:

#!/bin/bash
# Extract the implementation of `setup_game_works` from `contracts/prudent-pots/src/tests/instantiate.rs`.
rg -A 30 'pub fn setup_game_works' contracts/prudent-pots/src/tests/instantiate.rs

Length of output: 1128



Script:

#!/bin/bash
# Extract more lines to get the complete implementation of `setup_game_works`.
rg -A 50 'pub fn setup_game_works' contracts/prudent-pots/src/tests/instantiate.rs

Length of output: 1920



Script:

#!/bin/bash
# Extract more lines to get the complete implementation of `setup_game_works` and `setup_pot_allocation`.
rg -A 70 'pub fn setup_game_works' contracts/prudent-pots/src/tests/instantiate.rs

Length of output: 2442



Script:

#!/bin/bash
# Locate the definition of the `instantiate` function in the repository.
rg 'fn instantiate' --type rust

Length of output: 395



Script:

#!/bin/bash
# Extract the implementation of `instantiate` from `contracts/prudent-pots/src/contract.rs`.
rg -A 50 'pub fn instantiate' contracts/prudent-pots/src/contract.rs

Length of output: 1975



Script:

#!/bin/bash
# Search for the usage of `setup_game_works` in the repository.
rg 'setup_game_works' --type rust

Length of output: 1591

contracts/prudent-pots/src/helpers/pot.rs (4)

1-1: Import of Decimal aligns with the new requirement to handle more complex numeric calculations.

Also applies to: 6-7


Line range hint 86-139: Refactored calculate_min_bid function to incorporate new game dynamics and decay calculations.


176-185: The logic for calculating the maximum bid is clear and maintains the integrity of the bidding process by ensuring the max bid is not less than twice the original minimum bid.


164-164: Remove unnecessary conversion to the same type.

Tools
GitHub Check: lint

[failure] 164-164:
useless conversion to the same type: cosmwasm_std::Uint128

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2b98097 and 8d2d9c9.

Files selected for processing (2)
  • contracts/prudent-pots/src/helpers/game_end.rs (3 hunks)
  • contracts/prudent-pots/src/tests/integration/allocate_tokens.rs (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • contracts/prudent-pots/src/helpers/game_end.rs
  • contracts/prudent-pots/src/tests/integration/allocate_tokens.rs

Copy link
Owner Author

@magiodev magiodev left a comment

Choose a reason for hiding this comment

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

lg

Repository owner deleted a comment from coderabbitai bot Jun 16, 2024
Copy link

coderabbitai bot commented Jun 17, 2024

Warning

Review failed

The pull request is closed.

Walkthrough

The recent changes across various files enhance the functionality and flexibility of the prudent-pots smart contract. They introduce new configurations for game parameters, improved error handling, and refined calculation methods for bids and token distributions. Additionally, the frontend components now include better timer displays and new interactive features. Overall, these updates enrich the user experience and streamline game operations.

Changes

Files/Modules Change Summary
contracts/prudent-pots/src/contract.rs Introduced GameConfig struct, updated query and migrate functions to align with new game configurations.
contracts/prudent-pots/src/error.rs Added new error variants GameNotStarted and InvalidNextGameStart to improve game state error handling.
contracts/prudent-pots/src/execute.rs Updated game configuration logic, token allocation calculations, and added validation for new parameters.
contracts/prudent-pots/src/helpers/*.rs Various files updated to use Decimal type for calculations, refined game state management, and introduced new bid calculation functions.
contracts/prudent-pots/src/msg.rs Added fields to UpdateGameConfig and MigrateMsg. Removed commented-out fields in InstantiateMsg.
contracts/prudent-pots/src/qu... Updated to include the Env parameter in query_bid_range for accurate bid calculations.
contracts/prudent-pots/src/state.rs Replaced OldGameConfig with an enhanced GameConfig struct, renamed GAME_CONFIG to GAME_CONFIG_V2.
contracts/prudent-pots/src/tests/*.rs Updated tests to reflect new game configuration, adjusted to use Decimal, and improved mock setups.
frontend-common/mixin/game.js Introduced isCountingDownToStart computed property and modified time calculations for different game states.
frontend-common/store/index.js Removed redundant checks for uninitialized querier before querying contract data.
frontend-ms/src/components/Common/TimerComponent.vue Updated timer visualization logic to differentiate countdowns to game start and end.
frontend-ms/src/components/Layout/FooterComponent.vue Added a new list item with a Discord icon linking to a Discord server.
frontend-ms/src/views/Home.vue Added conditional rendering based on isCountingDownToStart to toggle visibility of game and bet sections.

Poem

🐇 In the pot, the prudent bid,
New configs all securely hid.
Errors now more finely spun,
For games that start and swiftly run.
Decimal points now guide the way,
Ensuring fair games, night or day.
🎮🎲 Play on, with joy we say!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@magiodev magiodev merged commit 4785b8d into main Jun 17, 2024
6 of 7 checks passed
@vercel vercel bot temporarily deployed to Preview – prudent-pots June 17, 2024 22:07 Inactive
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

Successfully merging this pull request may close these issues.

1 participant