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

[IND-494]: Create trading_rewards postgres table #822

Merged
merged 7 commits into from
Nov 30, 2023

Conversation

Christopher-Li
Copy link
Contributor

Changelist

Create trading_rewards postgres table

Test Plan

Test with unit tests

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.

Copy link

linear bot commented Nov 29, 2023

IND-494 Create trading_rewards postgres table

The trading_rewards table will be created with the following fields:

{
	required: [
    'id', // Generated from `address` and `createdAtHeight` [WIP]
    'address',
    'createdAt',
    'createdAtHeight',
		'amount', // amount of dYdX earned by address in that height
  ],
  properties: {
		id: { type: 'string', format: 'uuid' },
		address: { type: 'string' },
		createdAt: { type: 'string', format: 'date-time' },
		createdAtHeight: { type: 'string', pattern: IntegerPattern },
		amount: { type: 'string', pattern: NonNegativeNumericPattern },
	}
}

Indexes to be added:

  • ON "trading_rewards" ("address")
  • ON "trading_rewards" ("createdAtHeight")
  • ON "trading_rewards" ("createdAt")

Copy link
Contributor

coderabbitai bot commented Nov 29, 2023

Walkthrough

The changes involve the introduction of a new trading_rewards table in the database, along with the necessary models, stores, and tests to support it. This includes creating a new migration file for the table, updating the WalletModel to include relations to the new table, and adding a new TradingRewardModel. The TradingRewardTable store has been updated with functions to create, find, and retrieve trading rewards. Tests have been added to ensure the functionality of these new features.

Changes

File Path Change Summary
.../__tests__/helpers/constants.ts
.../__tests__/stores/trading-rewards-table.test.ts
Added TradingRewardCreateObject import and defaultTradingReward constant. New test suite for TradingReward store with setup and teardown hooks, and test cases for creating and finding trading rewards.
.../src/db/migrations/.../20231129153017_create_trading_rewards_table.ts New migration file to create trading_rewards table with necessary columns, foreign key, and indices.
.../src/helpers/db-helpers.ts Included 'trading_rewards' table in the layer1Tables array.
.../src/models/trading-reward-model.ts
.../src/models/wallet-model.ts
Introduced TradingRewardModel with properties and relation mappings. Updated WalletModel with new relation mappings and changed validation pattern for totalTradingRewards.
.../src/stores/trading-reward-table.ts Added functions and logic for querying (findAll, create, findById) the trading_rewards table.
.../src/types/... Added new types and interfaces for trading rewards, including TradingRewardFromDatabase, TradingRewardCreateObject, and TradingRewardQueryConfig.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

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.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3ffee7a and d41d94a.
Files selected for processing (11)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/trading-rewards-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20231129153017_create_trading_rewards_table.ts (1 hunks)
  • indexer/packages/postgres/src/helpers/db-helpers.ts (1 hunks)
  • indexer/packages/postgres/src/models/trading-reward-model.ts (1 hunks)
  • indexer/packages/postgres/src/models/wallet-model.ts (3 hunks)
  • indexer/packages/postgres/src/stores/trading-reward-table.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 (2 hunks)
  • indexer/packages/postgres/src/types/trading-reward-types.ts (1 hunks)
Files skipped from review due to trivial changes (3)
  • indexer/packages/postgres/tests/helpers/constants.ts
  • indexer/packages/postgres/src/helpers/db-helpers.ts
  • indexer/packages/postgres/src/types/index.ts
Additional comments: 14
indexer/packages/postgres/src/db/migrations/migration_files/20231129153017_create_trading_rewards_table.ts (2)
  • 14-14: Verify that the 'wallet' table exists and the 'address' column in the 'wallet' table is of type string to ensure the foreign key constraint is correctly established.

  • 7-19: The migration script looks good, but ensure that the 'id' column is set to be a UUID type in the 'wallet' table as well, to maintain consistency with the foreign key relationship.

indexer/packages/postgres/src/models/trading-reward-model.ts (1)
  • 42-42: The blockHeight property is defined as a string with an integer pattern in the JSON schema. Verify if this is the intended type, as block heights are typically integers and not strings.
indexer/packages/postgres/src/models/wallet-model.ts (3)
  • 1-7: The import changes reflect the summary accurately, with the addition of path and Model from 'objection' and NonNegativeNumericPattern from '../lib/validators'.

  • 18-43: The relationMappings have been updated to include new relations and use path.join for resolving model paths. Verify that the paths provided to path.join are correct and that the corresponding model files exist.

  • 51-57: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [45-57]

The jsonSchema has been updated to use NonNegativeNumericPattern for the totalTradingRewards property. Ensure that the NonNegativeNumericPattern is correctly defined and enforces non-negative numeric values as intended.

indexer/packages/postgres/src/stores/trading-reward-table.ts (4)
  • 19-21: The uuid function is marked with a TODO comment indicating that there is an intention to refactor the string concatenation to use Array.join. This should be tracked to ensure it is addressed in the future.

  • 24-79: The findAll function correctly constructs a dynamic query based on the provided parameters and options. It's good to see that the function is using a base query setup and applying filters conditionally. This approach is maintainable and scalable.

  • 82-91: The create function correctly inserts a new trading reward entry into the database using the uuid function to generate a unique ID. The use of the spread operator to include the tradingRewardToCreate object is efficient and clean.

  • 94-104: The findById function is implemented correctly, using the setupBaseQuery helper to initialize the query and the findById method to retrieve the specific trading reward entry. The use of returning('*') ensures that all columns are returned.

indexer/packages/postgres/src/types/db-model-types.ts (1)
  • 226-232: The new TradingRewardFromDatabase interface is correctly defined and consistent with the rest of the file and the provided summary.
indexer/packages/postgres/src/types/query-types.ts (2)
  • 74-74: The addition of BLOCK_TIME_BEFORE_OR_AT to the QueryableField enum aligns with the summary provided.

  • 268-271: The TradingRewardQueryConfig interface has been correctly added, extending QueryConfig with the specified fields and types.

indexer/packages/postgres/src/types/trading-reward-types.ts (1)
  • 1-16: The changes to the trading-reward-types.ts file are correct and align with the summary provided. The import of IsoString, the definition of the TradingRewardCreateObject interface, and the TradingRewardColumns enum are all consistent with the intended use for the new trading_rewards table.

table.string('address').notNullable();
table.timestamp('blockTime').notNullable();
table.bigInteger('blockHeight').notNullable();
table.decimal('amount').notNullable();
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider specifying the precision and scale for the 'amount' column if the decimal values have a fixed format, to ensure data consistency and avoid potential issues with arithmetic operations on these values.

Comment on lines +19 to +21
it('Successfully creates a TradingReward', async () => {
await TradingRewardTable.create(defaultTradingReward);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

The test case 'Successfully creates a TradingReward' does not assert the result of the create operation. It is important to verify that the create method returns the expected result to ensure that the TradingReward was created successfully.

Comment on lines +46 to +53
it('Successfully finds a TradingReward', async () => {
await TradingRewardTable.create(defaultTradingReward);

const tradingReward: TradingRewardFromDatabase | undefined = await TradingRewardTable.findById(
TradingRewardTable.uuid(defaultTradingReward.address, defaultTradingReward.blockHeight),
);

expect(tradingReward).toEqual(expect.objectContaining(defaultTradingReward));
Copy link
Contributor

Choose a reason for hiding this comment

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

The test case 'Successfully finds a TradingReward' correctly asserts the expected result. However, it would be beneficial to also verify that the findById method returns undefined when a non-existent ID is passed. This would ensure that the method behaves correctly in scenarios where the TradingReward does not exist.

* A mapping from column name to JSON conversion expected.
* See getSqlConversionForDydxModelTypes for valid conversions.
*
* TODO(IND-239): Ensure that jsonSchema() / sqlToJsonConversions() / model fields match.
Copy link
Contributor

Choose a reason for hiding this comment

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

The TODO comment indicates a need to ensure that jsonSchema(), sqlToJsonConversions(), and model fields match. If this has been addressed, the comment should be removed to avoid confusion.

],
properties: {
id: { type: 'string', format: 'uuid' },
address: { type: 'string' },
Copy link
Contributor

Choose a reason for hiding this comment

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

The address property in the JSON schema does not specify a format or pattern. Consider adding validation to ensure that addresses are stored in a consistent and valid format.

Comment on lines 69 to 73
baseQuery = baseQuery.orderBy(
TradingRewardColumns.blockHeight,
Ordering.ASC,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: order by block time and address by default for a deterministic ordering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did block height and address which should be the same

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d41d94a and 972bfe3.
Files selected for processing (2)
  • indexer/packages/postgres/src/models/wallet-model.ts (3 hunks)
  • indexer/packages/postgres/src/stores/trading-reward-table.ts (1 hunks)
Additional comments: 7
indexer/packages/postgres/src/models/wallet-model.ts (3)
  • 1-1: The import of the path module is correctly added to support the new relation mapping in the WalletModel.

  • 38-38: The jsonSchema definition for totalTradingRewards correctly uses NonNegativeNumericPattern to ensure that the value is a non-negative number.

  • 18-26: The new relation mapping for tradingRewards is correctly defined, with the modelClass property using path.join to resolve the path to the trading-reward-model.

indexer/packages/postgres/src/stores/trading-reward-table.ts (4)
  • 34-42: The verifyAllRequiredFields function is called with a constructed object and requiredFields. Ensure that the requiredFields parameter is being used correctly and that it is necessary for the function's operation. If requiredFields are meant to be a subset of the fields in TradingRewardQueryConfig, then the typecasting to QueryConfig might not be necessary.

  • 68-75: The default ordering is set to descending for both blockHeight and address. Verify that this ordering is intentional and aligns with the expected behavior of the findAll function. If not, consider adjusting the default ordering or providing a rationale for this choice.

  • 87-87: The default value for options in the create function is { txId: undefined }. Verify that this default is intentional and that it is safe to proceed with database operations without a transaction ID. If a transaction ID is required for certain operations, consider handling the case where it is undefined.

  • 97-107: The findById function is being used to retrieve a trading reward by its address. Typically, findById is used with a primary key, which is usually an id field. Verify that the address field is being used as a unique identifier for trading rewards and that there is no confusion with the primary key of the table.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 972bfe3 and 69004a0.
Files selected for processing (1)
  • indexer/packages/postgres/src/models/wallet-model.ts (2 hunks)
Additional comments: 4
indexer/packages/postgres/src/models/wallet-model.ts (4)
  • 1-7: The import statements have been updated correctly to reflect the changes in the summary.

  • 26-31: The jsonSchema for totalTradingRewards has been correctly updated to use NonNegativeNumericPattern for validation.

  • 26-32: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [32-40]

Verify if totalTradingRewards requires a conversion in sqlToJsonConversions similar to the address field, and if so, ensure it is added.

  • 1-8: > Note: This review was outside the patches, and no patch overlapping with it was found. Original lines [9-11]

The relationMappings object is empty, but the summary indicates that there should be new relation mappings for tradingRewards. Ensure that the relation mappings are correctly implemented if they are part of this change.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 69004a0 and 41f2954.
Files selected for processing (2)
  • indexer/packages/postgres/tests/stores/trading-rewards-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/models/wallet-model.ts (2 hunks)
Additional comments: 3
indexer/packages/postgres/src/models/wallet-model.ts (3)
  • 1-1: The import statement has been correctly updated to reflect the use of NonNegativeNumericPattern in the jsonSchema.

  • 22-27: The jsonSchema for 'totalTradingRewards' has been correctly updated to use NonNegativeNumericPattern to ensure that the value is non-negative as intended.

  • 1-4: > Note: This review was outside the patches, and no patch overlapping with it was found. Original lines [8-11]

The summary indicates that there should be updates to relation mappings for tradingRewards, but these are not present in the provided code. Please verify if the relation mappings have been implemented elsewhere or if they were mistakenly omitted.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 41f2954 and cc6c573.
Files selected for processing (1)
  • indexer/packages/postgres/src/db/migrations/migration_files/20231129153017_create_trading_rewards_table.ts (1 hunks)
Additional comments: 3
indexer/packages/postgres/src/db/migrations/migration_files/20231129153017_create_trading_rewards_table.ts (3)
  • 11-11: Consider specifying the precision and scale for the 'amount' column if the decimal values have a fixed format, to ensure data consistency and avoid potential issues with arithmetic operations on these values.

  • 14-14: Verify that the 'wallets' table and the 'address' column exist before setting up the foreign key relationship to avoid migration failures.

  • 17-19: Ensure that the addition of indices on 'address', 'blockTime', and 'blockHeight' does not significantly impact write performance, especially if the table is expected to have a high write volume.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cc6c573 and 560e615.
Files selected for processing (1)
  • indexer/packages/postgres/tests/stores/trading-rewards-table.test.ts (1 hunks)

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 560e615 and 2c989d9.
Files selected for processing (1)
  • indexer/packages/postgres/tests/stores/trading-rewards-table.test.ts (1 hunks)

@Christopher-Li Christopher-Li merged commit 66073c1 into main Nov 30, 2023
11 checks passed
@Christopher-Li Christopher-Li deleted the cl_trading_rewards branch November 30, 2023 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants