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

[OTE-750] Affiliates new affiliate info table #2179

Merged
merged 4 commits into from
Sep 1, 2024

Conversation

jerryfan01234
Copy link
Contributor

@jerryfan01234 jerryfan01234 commented Aug 30, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Introduced a new affiliate_info table for managing affiliate data, including earnings and referral metrics.
    • Added constants for default affiliate information to streamline data handling.
    • Enhanced data model with new interfaces for affiliate information and referred users, improving data structure and usability.
    • Implemented CRUD operations for affiliate records, enhancing data interaction capabilities.
    • Added a new interface for querying affiliate information, improving query configuration.
  • Bug Fixes

    • Added comprehensive unit tests for the AffiliateInfoTable module to ensure reliability and prevent duplicate entries.
  • Documentation

    • Updated type definitions and interfaces for better clarity and type safety in managing affiliate information.

Copy link

linear bot commented Aug 30, 2024

Copy link
Contributor

coderabbitai bot commented Aug 30, 2024

Walkthrough

The changes introduce a new affiliate_info table in the PostgreSQL database, along with a corresponding model and helper functions for managing affiliate data. New constants and types are defined to facilitate the creation and querying of affiliate information. Additionally, unit tests are implemented to ensure the functionality of the AffiliateInfoTable module, enhancing the application's data management capabilities related to affiliates.

Changes

Files Change Summary
indexer/packages/postgres/__tests__/helpers/constants.ts, indexer/packages/postgres/src/types/affiliate-info-types.ts Added constants defaultAffiliateInfo and defaultAffiliateInfo1 for affiliate data, and defined the AffiliateInfoCreateObject interface for affiliate information structure.
indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts Introduced unit tests for the AffiliateInfoTable module, covering creation, duplication prevention, upsert functionality, retrieval of all records, and individual record access.
indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts Created migration script for the affiliate_info table, defining its schema and providing rollback functionality.
indexer/packages/postgres/src/models/affiliate-info-model.ts Introduced AffiliateInfoModel class to represent the affiliate_info table, including validation rules and JSON schema definitions.
indexer/packages/postgres/src/types/db-model-types.ts Added AffiliateInfoFromDatabase interface to represent affiliate information properties in the database.
indexer/packages/postgres/src/types/index.ts Exported types from affiliate-info-types for broader accessibility.
indexer/packages/postgres/src/types/query-types.ts Introduced AffiliateInfoQueryConfig interface extending QueryConfig to include optional address filtering for affiliate queries.
indexer/packages/postgres/src/models/affiliate-referred-users-model.ts, indexer/packages/postgres/src/types/affiliate-referred-users-types.ts Modified referredAtBlock property type from number to string in both model and types, indicating a shift in data representation.
indexer/packages/postgres/src/db/migrations/migration_files/20240830154741_create_affiliate_referred_users_table.ts Changed referredAtBlock column type from integer to bigInteger in the affiliate_referred_users table schema, allowing for larger block numbers.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API
    participant AffiliateInfoTable
    participant Database

    User->>API: Request to create affiliate info
    API->>AffiliateInfoTable: Call create function
    AffiliateInfoTable->>Database: Insert affiliate info
    Database-->>AffiliateInfoTable: Confirm insertion
    AffiliateInfoTable-->>API: Return created affiliate info
    API-->>User: Respond with created affiliate info
Loading
sequenceDiagram
    participant User
    participant API
    participant AffiliateInfoTable
    participant Database

    User->>API: Request to find all affiliate info
    API->>AffiliateInfoTable: Call findAll function
    AffiliateInfoTable->>Database: Query all affiliate info
    Database-->>AffiliateInfoTable: Return affiliate info records
    AffiliateInfoTable-->>API: Return records
    API-->>User: Respond with affiliate info records
Loading

Poem

🐰 In the meadow where data flows,
New tables sprout like blooming rose.
Affiliates gather, their earnings in sight,
With each little query, they take flight!
Hooray for the changes, let’s hop and cheer,
For better data, we hold dear! 🌼


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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

Copy link
Contributor

@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 ffde811 and bd53c2f.

Files selected for processing (10)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/affiliate-info-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts (1 hunks)
  • indexer/packages/postgres/src/helpers/db-helpers.ts (1 hunks)
  • indexer/packages/postgres/src/models/affiliate-info-model.ts (1 hunks)
  • indexer/packages/postgres/src/stores/affiliate-info-table.ts (1 hunks)
  • indexer/packages/postgres/src/types/affiliate-info-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/index.ts (1 hunks)
  • indexer/packages/postgres/src/types/query-types.ts (1 hunks)
Additional comments not posted (13)
indexer/packages/postgres/src/types/affiliate-info-types.ts (2)

1-10: Well-defined TypeScript interface for affiliate information.

The AffiliateInfoCreateObject interface is well-defined, covering all necessary fields for the affiliate information with appropriate types. This ensures strong typing and helps prevent bugs related to incorrect data types.


12-21: Clear enumeration of database columns.

The AffiliateInfoColumns enum clearly lists all the database columns used in the affiliate info table, which enhances maintainability and readability of the code by using these enums instead of string literals throughout the codebase.

indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts (2)

3-14: Correct implementation of the migration up function.

The up function correctly sets up the affiliate_info table with all required columns and constraints. Each column is appropriately defined with notNullable() to ensure data integrity. The use of primary() for the address column is appropriate given the context that each affiliate's address should be unique.


16-18: Proper implementation of the migration down function.

The down function provides a clean way to revert the changes made by the up function by dropping the affiliate_info table. This ensures that the migration is fully reversible, which is a best practice in database schema management.

indexer/packages/postgres/src/types/index.ts (1)

33-33: Successful integration of new affiliate info types.

The addition of export * from './affiliate-info-types'; successfully integrates the new types defined for affiliate information into the broader application. This allows other parts of the application to utilize these types, enhancing modularity and reusability.

indexer/packages/postgres/src/models/affiliate-info-model.ts (1)

5-76: Comprehensive Review of AffiliateInfoModel Class

  1. Table Name and ID Column:

    • The tableName and idColumn are correctly defined. The use of 'address' as the primary key is unconventional but may be appropriate depending on the domain requirements.
  2. JSON Schema:

    • The schema is well-defined with required fields and types. Using NonNegativeNumericPattern ensures data integrity for numeric fields. However, the use of 'int' type for affiliateEarnings and similar fields might need clarification since typically, monetary values are better represented as decimals to avoid rounding errors.
  3. SQL to JSON Conversions:

    • The mapping is straightforward and matches the JSON schema. This consistency is crucial for data integrity between the database and application layers.
  4. Properties:

    • Properties are explicitly declared with TypeScript types, enhancing code readability and maintainability. This also helps with static type checking.
  5. TODO Comment:

    • The TODO comment about ensuring consistency between jsonSchema, sqlToJsonConversions, and model fields is vital. It suggests ongoing development and the need for a systematic review to ensure alignment.

Overall, the class is well-structured and adheres to best practices in defining ORM models. However, consider the implications of using 'int' for monetary values and ensure that all related fields across the application are consistent in their data type usage.

indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts (1)

6-68: Comprehensive Review of Test Cases in affiliate-info-table.test.ts

  1. Setup and Teardown:

    • Proper use of beforeAll, afterEach, and afterAll for database setup and cleanup. This ensures a clean state for each test, which is crucial for avoiding flaky tests.
  2. Test for Creation:

    • Tests the creation of affiliate info. It would be beneficial to also check the properties of the created object to ensure all fields are correctly stored and returned.
  3. Test for Duplicate Handling:

    • The test correctly checks that duplicate entries are not allowed. However, it would be more informative if the specific error expected is asserted rather than a general error.
  4. Test for Upsert Functionality:

    • The upsert test is well-implemented, checking both the initial insert and the update functionality. The use of objectContaining helps ensure that the relevant fields are tested without requiring a match for every field.
  5. Test for Retrieval:

    • Tests for finding all entries and specific entries are comprehensive. Using arrayContaining and objectContaining is appropriate for these tests.

Overall, the tests are well-structured and cover the main functionalities of the AffiliateInfoTable. Consider adding more detailed assertions for error cases and checking the exact properties of objects returned from the database to ensure complete coverage and data integrity.

indexer/packages/postgres/src/stores/affiliate-info-table.ts (1)

18-94: Comprehensive Review of Methods in affiliate-info-table.ts

  1. findAll Method:

    • Implements filtering, ordering, and limiting correctly. The use of default options and the handling of undefined values are well-implemented. However, consider adding explicit type checks or sanitization to prevent SQL injection through dynamic query parameters.
  2. create Method:

    • Properly uses transactions and inserts data. The returning of all fields is a good practice for confirming the creation. Ensure that all fields are validated before insertion to prevent database errors and potential security issues.
  3. upsert Method:

    • Handles the upsert logic, which is crucial for maintaining data integrity. The method assumes that only one record will be returned, which is a reasonable assumption given the context. However, adding error handling for unexpected cases (e.g., multiple records returned) would enhance robustness.
  4. findById Method:

    • Correctly retrieves a record by its ID. The method is straightforward and uses the base query setup, which is consistent with other methods. Adding a check to ensure the address is not null or malformed before executing the query would prevent potential runtime errors.

Overall, the methods are well-implemented with attention to transaction management and query construction. Adding additional checks for input validation and error handling would further improve the security and robustness of these operations.

indexer/packages/postgres/src/helpers/db-helpers.ts (1)

33-33: Verify integration of the new table in database operations.

The addition of 'affiliate_info' to the layer1Tables array is correctly implemented. However, ensure that the database schema and related operations are updated to handle this new table effectively.

Run the following script to verify the integration of the new table:

Verification successful

Integration of affiliate_info table is complete and correct.

The affiliate_info table is properly integrated into the database schema and ORM model. It is defined in a migration file and has a corresponding model, ensuring that it is handled correctly in the application.

  • Model Definition: Found in indexer/packages/postgres/src/models/affiliate-info-model.ts.
  • Migration File: Found in indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the integration of the `affiliate_info` table in database operations.

# Test: Search for the table usage in database operations. Expect: Proper integration in schema and operations.
rg --type sql -A 5 $'affiliate_info'

Length of output: 35


Script:

#!/bin/bash
# Description: Search for the `affiliate_info` table in TypeScript and JavaScript files to verify its integration.

# Search for references to `affiliate_info` in TypeScript files.
rg --type ts -A 5 'affiliate_info'

# Search for references to `affiliate_info` in JavaScript files.
rg --type js -A 5 'affiliate_info'

Length of output: 2221

indexer/packages/postgres/src/types/db-model-types.ts (1)

281-290: Verify usage of the new interface in the system.

The AffiliateInfoFromDatabase interface is correctly defined with appropriate properties and types. Ensure that this interface is properly utilized in the system and integrates well with existing structures.

Run the following script to verify the usage of the new interface:

Verification successful

The AffiliateInfoFromDatabase interface is properly integrated and utilized in the system.

The interface is used in the affiliate-info-table.ts file for database operations and is thoroughly tested in affiliate-info-table.test.ts. This indicates that it is well-integrated and its functionality is verified through tests.

  • Files:
    • indexer/packages/postgres/src/stores/affiliate-info-table.ts
    • indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `AffiliateInfoFromDatabase` interface in the system.

# Test: Search for the interface usage in the system. Expect: Proper integration and usage.
rg --type typescript -A 5 $'AffiliateInfoFromDatabase'

Length of output: 88


Script:

#!/bin/bash
# Description: Verify the usage of the `AffiliateInfoFromDatabase` interface in the system.

# Test: Search for the interface usage in the system. Expect: Proper integration and usage.
rg --type ts -A 5 $'AffiliateInfoFromDatabase'

Length of output: 5975

indexer/packages/postgres/src/types/query-types.ts (1)

339-341: Verify usage of the new query configuration interface in the system.

The AffiliateInfoQueryConfig interface is correctly defined and extends the QueryConfig interface with an optional address property. Ensure that this interface is properly utilized for configuring queries related to affiliate information.

Run the following script to verify the usage of the new interface:

Verification successful

The AffiliateInfoQueryConfig interface is correctly utilized in the system.

The interface is imported and used in the affiliate-info-table.ts file, confirming its proper integration for configuring queries related to affiliate information.

  • File: indexer/packages/postgres/src/stores/affiliate-info-table.ts
  • Usage: As a parameter type in a function, indicating its role in query configuration.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `AffiliateInfoQueryConfig` interface in the system.

# Test: Search for the interface usage in the system. Expect: Proper integration and usage.
rg --type typescript -A 5 $'AffiliateInfoQueryConfig'

Length of output: 87


Script:

#!/bin/bash
# Description: Verify the usage of the `AffiliateInfoQueryConfig` interface in the system.

# Test: Search for the interface usage in the system. Expect: Proper integration and usage.
rg --type ts -A 5 $'AffiliateInfoQueryConfig'

Length of output: 1323

indexer/packages/postgres/__tests__/helpers/constants.ts (2)

968-977: Review of defaultAffiliateInfo constant.

The constant defaultAffiliateInfo is well-defined and aligns with the expected structure of an AffiliateInfoCreateObject. However, ensure that the values provided (e.g., affiliateEarnings, referredMakerTrades) are realistic and align with the application's requirements.

Please verify the values with the business logic or data requirements to ensure they are appropriate.


979-988: Review of defaultAffiliateInfo1 constant.

The constant defaultAffiliateInfo1 is correctly implemented and provides a variant of the affiliate data with different values. Ensure that these values (e.g., affiliateEarnings, referredMakerTrades) are also realistic and serve the intended purpose, possibly representing a different scenario or test case.

Please verify the values with the business logic or data requirements to ensure they are appropriate and serve the intended purpose.

table.integer('referredTakerTrades').notNullable();
table.integer('totalReferredFees').notNullable();
table.integer('totalReferredUsers').notNullable();
table.integer('referredNetProtocolEarnings').notNullable();
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a decimal, since this will be denominated in USDC and we support up to 1e-6 precision

table.integer('affiliateEarnings').notNullable();
table.integer('referredMakerTrades').notNullable();
table.integer('referredTakerTrades').notNullable();
table.integer('totalReferredFees').notNullable();
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a decimal, since this will be denominated in USDC and we support up to 1e-6 precision

export async function up(knex: Knex): Promise<void> {
return knex.schema.createTable('affiliate_info', (table) => {
table.string('address').primary().notNullable();
table.integer('affiliateEarnings').notNullable();
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a decimal, since this will be denominated in USDC and we support up to 1e-6 precision

table.integer('totalReferredFees').notNullable();
table.integer('totalReferredUsers').notNullable();
table.integer('referredNetProtocolEarnings').notNullable();
table.integer('firstReferralBlockHeight').notNullable();
Copy link
Contributor

Choose a reason for hiding this comment

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

we typicall use biginteger for blockheight, because integer max's ~2billion, and we're worried about it eventually hitting the limit. If block height wasn't correctly set in the previous PRs, you should change to biginteger. See here

totalReferredFees: { type: 'int', pattern: NonNegativeNumericPattern },
totalReferredUsers: { type: 'int', pattern: NonNegativeNumericPattern },
referredNetProtocolEarnings: { type: 'int', pattern: NonNegativeNumericPattern },
firstReferralBlockHeight: { type: 'int', pattern: NonNegativeNumericPattern },
Copy link
Contributor

Choose a reason for hiding this comment

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

CleanShot 2024-08-30 at 21 58 46@2x
Only the commented values should be NonNegativeNumericPattern, and I believe the patterns have only been used with strings, I don't know if they can be used with an int

Copy link
Contributor

@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: 1

Outside diff range, codebase verification and nitpick comments (1)
indexer/packages/postgres/src/types/db-model-types.ts (1)

281-290: LGTM! New AffiliateInfoFromDatabase interface added.

The new interface provides a structured way to store and retrieve affiliate information from the database, which aligns with the PR objectives.

Consider adding JSDoc comments to describe the purpose of the interface and each of its properties. This will improve code documentation and maintainability. For example:

/**
 * Represents affiliate information stored in the database.
 */
export interface AffiliateInfoFromDatabase {
  /** Unique address of the affiliate */
  address: string;
  /** Total earnings of the affiliate */
  affiliateEarnings: string;
  /** Number of maker trades referred by the affiliate */
  referredMakerTrades: number;
  // ... (add comments for other properties)
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bd53c2f and 434e252.

Files selected for processing (10)
  • indexer/packages/postgres/tests/helpers/constants.ts (3 hunks)
  • indexer/packages/postgres/tests/stores/affiliate-info-table.test.ts (1 hunks)
  • indexer/packages/postgres/tests/stores/affiliate-referred-users-table.test.ts (2 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830154741_create_affiliate_referred_users_table.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts (1 hunks)
  • indexer/packages/postgres/src/models/affiliate-info-model.ts (1 hunks)
  • indexer/packages/postgres/src/models/affiliate-referred-users-model.ts (3 hunks)
  • indexer/packages/postgres/src/types/affiliate-info-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/affiliate-referred-users-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
Files skipped from review as they are similar to previous changes (5)
  • indexer/packages/postgres/tests/helpers/constants.ts
  • indexer/packages/postgres/tests/stores/affiliate-info-table.test.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts
  • indexer/packages/postgres/src/models/affiliate-info-model.ts
  • indexer/packages/postgres/src/types/affiliate-info-types.ts
Additional comments not posted (6)
indexer/packages/postgres/src/types/affiliate-referred-users-types.ts (1)

4-4: Type change for referredAtBlock: Ensure consistency across the codebase

The type of referredAtBlock has been changed from number to string. This change aligns with the modifications in other files and potentially allows for greater flexibility in representing block numbers.

To ensure consistency, please verify that all usages of this type have been updated accordingly:

Additionally, consider adding a comment explaining the rationale behind using a string instead of a number for block representation.

Verification successful

Type change for referredAtBlock is consistent across the codebase

The type change of referredAtBlock from number to string is consistently applied across the codebase. It is treated as a string in both the test files and within the database operations in affiliate-referred-users-table.ts. No inconsistencies were found.

  • indexer/packages/postgres/src/types/affiliate-referred-users-types.ts: referredAtBlock is defined as a string.
  • indexer/packages/postgres/__tests__/helpers/constants.ts: referredAtBlock is used as a string.
  • indexer/packages/postgres/src/stores/affiliate-referred-users-table.ts: referredAtBlock is used in an orderBy clause, which is compatible with its string type.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usages of AffiliateReferredUsersCreateObject and check if referredAtBlock is treated as a string
rg --type typescript -A 5 "AffiliateReferredUsersCreateObject"

Length of output: 97


Script:

#!/bin/bash
# Correct the file type and search for usages of AffiliateReferredUsersCreateObject
rg --type ts -A 5 "AffiliateReferredUsersCreateObject"

Length of output: 7505


Script:

#!/bin/bash
# Check how referredAtBlock is handled in affiliate-referred-users-table.ts
rg "referredAtBlock" indexer/packages/postgres/src/stores/affiliate-referred-users-table.ts -A 5 -B 5

Length of output: 277

indexer/packages/postgres/src/db/migrations/migration_files/20240830154741_create_affiliate_referred_users_table.ts (1)

7-7: Column type change for referredAtBlock: Consider data migration and performance

The referredAtBlock column type has been changed from integer to bigInteger. This change allows for storing larger block numbers, which is consistent with the type changes in other files.

Please ensure that:

  1. Existing data (if any) is migrated correctly to the new type.
  2. Any queries or indexes using this column are still performant with the new type.

You can use the following script to check for existing queries on this column:

Also, consider adding a comment in the migration file explaining the reason for this change, to provide context for future developers.

indexer/packages/postgres/src/models/affiliate-referred-users-model.ts (2)

1-1: New import: Ensure proper error handling for invalid input

The addition of NonNegativeNumericPattern import is good for input validation.

Please ensure that proper error handling is in place for cases where the input doesn't match the pattern. You can check for error handling implementations with:

If no results are found, consider adding appropriate error handling.


39-39: Type changes: Ensure consistent handling of referredAtBlock as string

The sqlToJsonConversions and class property for referredAtBlock have been updated to use string type. This is consistent with the changes in other parts of the codebase.

To ensure that all usages of referredAtBlock in this model are treating it as a string, run:

Review the results to confirm that all occurrences are handling it as a string type.

Also applies to: 47-47

Verification successful

Verification successful: referredAtBlock is consistently handled as a string

The referredAtBlock is correctly defined and used as a string in the file indexer/packages/postgres/src/models/affiliate-referred-users-model.ts. All occurrences align with the expected type change.

  • JSON schema: { type: 'string', pattern: NonNegativeNumericPattern }
  • Class property: referredAtBlock!: string;
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usages of referredAtBlock within this file
rg --type typescript -A 5 "referredAtBlock" indexer/packages/postgres/src/models/affiliate-referred-users-model.ts

Length of output: 149


Script:

#!/bin/bash
# Search for usages of referredAtBlock within this file without specifying file type
rg -A 5 "referredAtBlock" indexer/packages/postgres/src/models/affiliate-referred-users-model.ts

Length of output: 461

indexer/packages/postgres/__tests__/stores/affiliate-referred-users-table.test.ts (1)

76-76: LGTM! Type consistency maintained for referredAtBlock.

The changes to referredAtBlock from number to string are consistent with the updated type definition in the database model. This ensures that the test data matches the new expected format.

To ensure all instances have been updated, run the following script:

Also applies to: 81-81, 106-106, 111-111

Verification successful

Verification Successful: referredAtBlock is consistently a string.

All instances of referredAtBlock in the test file are strings, confirming the type consistency as mentioned in the review comment. No numeric instances were found.

  • Lines verified: 76, 81, 106, 111
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all instances of referredAtBlock are strings in the test file

# Test: Search for any remaining numeric referredAtBlock. Expect: No results.
rg --type typescript 'referredAtBlock:\s*\d+' indexer/packages/postgres/__tests__/stores/affiliate-referred-users-table.test.ts

# Test: Confirm all referredAtBlock are strings. Expect: All instances are strings.
rg --type typescript "referredAtBlock:\s*'\\d+'" indexer/packages/postgres/__tests__/stores/affiliate-referred-users-table.test.ts

Length of output: 337


Script:

#!/bin/bash
# Description: Verify all instances of referredAtBlock are strings in the test file

# Test: Search for any remaining numeric referredAtBlock. Expect: No results.
rg 'referredAtBlock:\s*\d+' indexer/packages/postgres/__tests__/stores/affiliate-referred-users-table.test.ts

# Test: Confirm all referredAtBlock are strings. Expect: All instances are strings.
rg "referredAtBlock:\s*'\\d+'" indexer/packages/postgres/__tests__/stores/affiliate-referred-users-table.test.ts

Length of output: 345

indexer/packages/postgres/src/types/db-model-types.ts (1)

295-295: LGTM! Type consistency maintained for referredAtBlock.

The change of referredAtBlock from number to string in the AffiliateReferredUserFromDatabase interface is consistent with the changes observed in the test file. This ensures type consistency across the codebase.

@@ -20,7 +21,7 @@ export default class AffiliateReferredUsersModel extends BaseModel {
properties: {
affiliateAddress: { type: 'string' },
refereeAddress: { type: 'string' },
referredAtBlock: { type: 'integer' },
referredAtBlock: { type: 'string', pattern: NonNegativeNumericPattern },
Copy link
Contributor

Choose a reason for hiding this comment

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

Updated jsonSchema: Consider adding a maximum length constraint

The jsonSchema for referredAtBlock has been updated to use a string type with a pattern validation. This is good for ensuring the input is a non-negative numeric string.

Consider adding a maximum length constraint to prevent excessively long inputs:

- referredAtBlock: { type: 'string', pattern: NonNegativeNumericPattern },
+ referredAtBlock: { type: 'string', pattern: NonNegativeNumericPattern, maxLength: 256 },

Adjust the maxLength value based on your specific requirements.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
referredAtBlock: { type: 'string', pattern: NonNegativeNumericPattern },
referredAtBlock: { type: 'string', pattern: NonNegativeNumericPattern, maxLength: 256 },

@jerryfan01234 jerryfan01234 force-pushed the affiliates-new-affiliate-info-table branch from 434e252 to 6375aa9 Compare August 31, 2024 22:39
Copy link
Contributor

@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 434e252 and 6375aa9.

Files selected for processing (10)
  • indexer/packages/postgres/tests/helpers/constants.ts (3 hunks)
  • indexer/packages/postgres/tests/stores/affiliate-info-table.test.ts (1 hunks)
  • indexer/packages/postgres/tests/stores/affiliate-referred-users-table.test.ts (2 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830154741_create_affiliate_referred_users_table.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts (1 hunks)
  • indexer/packages/postgres/src/models/affiliate-info-model.ts (1 hunks)
  • indexer/packages/postgres/src/models/affiliate-referred-users-model.ts (3 hunks)
  • indexer/packages/postgres/src/types/affiliate-info-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/affiliate-referred-users-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
Files skipped from review as they are similar to previous changes (8)
  • indexer/packages/postgres/tests/stores/affiliate-info-table.test.ts
  • indexer/packages/postgres/tests/stores/affiliate-referred-users-table.test.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830154741_create_affiliate_referred_users_table.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts
  • indexer/packages/postgres/src/models/affiliate-info-model.ts
  • indexer/packages/postgres/src/models/affiliate-referred-users-model.ts
  • indexer/packages/postgres/src/types/affiliate-info-types.ts
  • indexer/packages/postgres/src/types/db-model-types.ts
Additional comments not posted (4)
indexer/packages/postgres/src/types/affiliate-referred-users-types.ts (1)

4-4: LGTM! Verify impact on related code.

The change from number to string for referredAtBlock is approved. This change allows for greater precision and avoids potential issues with large block numbers.

Please ensure that all code interacting with referredAtBlock is updated to handle it as a string. Run the following script to find potentially affected areas:

indexer/packages/postgres/__tests__/helpers/constants.ts (3)

20-20: LGTM! New import added.

The addition of AffiliateInfoCreateObject to the import statement is approved. This import is necessary for the new constants being added.


951-951: LGTM! Type change applied.

The change of referredAtBlock to string type in defaultAffiliateReferredUser is consistent with the changes made in the types file. This ensures consistency across the codebase.


965-988: LGTM! New affiliate info constants added.

The addition of defaultAffiliateInfo and defaultAffiliateInfo1 constants is approved. These constants will be useful for testing affiliate-related functionality.

For consistency with other constants in this file, consider adding a comment header for this section, similar to the other sections:

+// ============== Affiliate Info Data ==============

 export const defaultAffiliateInfo: AffiliateInfoCreateObject = {
   // ... (rest of the constant definition)
 };

Please ensure that these new constants are being used in the appropriate test files. Run the following script to check their usage:

Verification successful

Constants Usage Verified in Test Files

The constants defaultAffiliateInfo and defaultAffiliateInfo1 are being used in the test file indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts. This confirms that they are integrated into the testing framework as intended.

  • File: indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts
    • The constants are imported and used multiple times within this test file.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of defaultAffiliateInfo and defaultAffiliateInfo1 in test files
rg -t typescript -t javascript "defaultAffiliateInfo|defaultAffiliateInfo1" __tests__

Length of output: 122


Script:

#!/bin/bash
# Corrected search for usage of defaultAffiliateInfo and defaultAffiliateInfo1 in test files
rg -t ts -t js "defaultAffiliateInfo|defaultAffiliateInfo1" __tests__

Length of output: 121


Script:

#!/bin/bash
# Search for usage of defaultAffiliateInfo and defaultAffiliateInfo1 in the entire repository
rg -t ts -t js "defaultAffiliateInfo|defaultAffiliateInfo1"

Length of output: 2587

@jerryfan01234 jerryfan01234 merged commit f4bb592 into main Sep 1, 2024
16 checks passed
@jerryfan01234 jerryfan01234 deleted the affiliates-new-affiliate-info-table branch September 1, 2024 19:33
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