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

[PLA-2034] Support version 2 changes #106

Merged
merged 10 commits into from
Oct 13, 2024
Merged

Conversation

enjinabner
Copy link
Contributor

@enjinabner enjinabner commented Oct 11, 2024

PR Type

Enhancement, Tests


Description

  • Updated batch processing logic to handle tokens with new mint cap types and added additional parameters for token creation.
  • Modified SQL query logic in TokensDoNotExistInBeam to use updated token cap type constants.
  • Revised tests to align with changes in token cap types and event data structures.
  • Enhanced event tests to reflect new data structures for collection and token IDs.

Changes walkthrough 📝

Relevant files
Enhancement
BatchProcess.php
Update batch processing logic for token handling                 

src/Commands/BatchProcess.php

  • Changed condition check from constant comparison to variable
    comparison.
  • Updated token mint cap type logic to use new constants.
  • Added new parameters to the mint or create params array.
  • +5/-2     
    TokensDoNotExistInBeam.php
    Modify token existence rule with updated cap types             

    src/Rules/TokensDoNotExistInBeam.php

    • Updated SQL query logic to use new token cap type constants.
    +2/-2     
    Tests
    UpdateBeamTest.php
    Update test for token cap type changes                                     

    tests/Feature/GraphQL/Mutations/UpdateBeamTest.php

    • Changed token cap type in test setup to new constant.
    +1/-1     
    CreateCollectionData.php
    Adjust collection data creation for new cap types               

    tests/Feature/Traits/CreateCollectionData.php

    • Updated collection and token setup to use new cap type constants.
    +2/-2     
    EventTest.php
    Revise event tests for updated data structures                     

    tests/Unit/EventTest.php

  • Updated event mock data to use new structure for collection and token
    IDs.
  • Changed collection attributes to use new constants.
  • +14/-10 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    @enjinabner enjinabner added the enhancement New feature or request label Oct 11, 2024
    @enjinabner enjinabner self-assigned this Oct 11, 2024
    @github-actions github-actions bot added the tests label Oct 11, 2024
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Logic Consistency
    Ensure that the new conditional logic and parameters in the batch processing align with the expected business rules and data integrity is maintained.

    Query Accuracy
    Verify that the modified SQL conditions correctly reflect the new business rules without causing unintended side effects or performance issues.

    Test Coverage
    Check if the new test cases adequately cover the changes made, particularly the new token cap types and their effects on system behavior.

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Security
    Refactor the SQL condition to use a safer query building approach to prevent SQL injection

    Refactor the complex SQL condition to use parameterized queries or a query builder
    to prevent potential SQL injection vulnerabilities.

    src/Rules/TokensDoNotExistInBeam.php [63-72]

    -->whereRaw("
    -    (tokens.is_currency is false OR tokens.is_currency is NULL)
    -    AND (
    -        collections.max_token_supply = '1'
    -        OR (collections.force_collapsing_supply is true AND tokens.supply = '1')
    -        OR (tokens.cap='" . TokenMintCapType::SUPPLY->name . "' AND tokens.cap_supply = '1')
    -        OR (tokens.cap='" . TokenMintCapType::COLLAPSING_SUPPLY->name . "' AND tokens.cap_supply = '1')
    -    )
    -")
    +->where(function ($query) {
    +    $query->where('tokens.is_currency', false)
    +          ->orWhereNull('tokens.is_currency')
    +          ->where(function ($query) {
    +              $query->where('collections.max_token_supply', 1)
    +                    ->orWhere(function ($query) {
    +                        $query->where('collections.force_collapsing_supply', true)
    +                              ->where('tokens.supply', 1);
    +                    })
    +                    ->orWhere(function ($query) {
    +                        $query->where('tokens.cap', TokenMintCapType::SUPPLY->name)
    +                              ->where('tokens.cap_supply', 1);
    +                    })
    +                    ->orWhere(function ($query) {
    +                        $query->where('tokens.cap', TokenMintCapType::COLLAPSING_SUPPLY->name)
    +                              ->where('tokens.cap_supply', 1);
    +                    });
    +          });
    +})
    Suggestion importance[1-10]: 9

    Why: This suggestion significantly improves security by refactoring a complex SQL condition to use a query builder, reducing the risk of SQL injection vulnerabilities. It is a crucial enhancement for maintaining secure code.

    9
    Possible bug
    Add a check to ensure that beam_id is set before adding recipients to prevent null reference errors

    Ensure that the beam_id is set before adding recipients to avoid potential null
    reference issues.

    src/Commands/BatchProcess.php [153-159]

     $params[$collectionId]['beamId'] = $claim->beam_id;
     if ($type == BeamType::TRANSFER_TOKEN) {
    +    if (!isset($params[$collectionId]['beamId'])) {
    +        throw new \Exception("Beam ID is not set.");
    +    }
         $params[$collectionId]['recipients'][] = [
             'accountId' => $claim->wallet_public_key,
             'params' => $this->substrate->getTransferParams([
                 'tokenId' => ['integer' => $claim->token_chain_id],
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential null reference issue by ensuring that beam_id is set before adding recipients. This is a critical check that can prevent runtime errors, making it a valuable addition to the code.

    8
    Add validation for claim->quantity to prevent potential runtime errors due to invalid values

    Validate the claim->quantity to ensure it is not null or zero before using it in
    conditional logic to prevent division by zero errors or invalid type assignments.

    src/Commands/BatchProcess.php [186-187]

    +if ($claim->quantity === null || $claim->quantity === 0) {
    +    throw new \InvalidArgumentException("Invalid quantity.");
    +}
     'initialSupply' => $this->tokenCreatedCache[$key] ? null : $claim->quantity,
     'amount' => $this->tokenCreatedCache[$key] ? $claim->quantity : null,
    Suggestion importance[1-10]: 7

    Why: The suggestion adds validation for claim->quantity, which is important to prevent division by zero errors or invalid type assignments. This enhances the robustness of the code by ensuring that only valid quantities are processed.

    7
    Enhancement
    Set cap_supply appropriately when cap is TokenMintCapType::COLLAPSING_SUPPLY to maintain logical consistency

    Ensure that cap_supply is appropriately set when cap is
    TokenMintCapType::COLLAPSING_SUPPLY to avoid logical inconsistencies in token cap
    handling.

    tests/Feature/GraphQL/Mutations/UpdateBeamTest.php [216-217]

     'cap' => TokenMintCapType::COLLAPSING_SUPPLY->name,
    -'cap_supply' => null,
    +'cap_supply' => $claim->quantity,  # Assuming quantity is the intended cap supply
    Suggestion importance[1-10]: 6

    Why: The suggestion ensures logical consistency by setting cap_supply when cap is TokenMintCapType::COLLAPSING_SUPPLY. While it enhances logical clarity, the exact value for cap_supply should be verified, as it assumes claim->quantity is the intended value.

    6

    @leonardocustodio leonardocustodio self-requested a review October 11, 2024 14:18
    @enjinabner enjinabner merged commit 6e5d258 into master Oct 13, 2024
    5 checks passed
    @enjinabner enjinabner deleted the feature/pla-2034/support-v2 branch October 13, 2024 23:46
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Development

    Successfully merging this pull request may close these issues.

    2 participants