Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Add cross margin liquidation notification helper function#102

Merged
sudeepb02 merged 2 commits intodevelopfrom
add/crossMarginLiquidationFunction
Apr 12, 2025
Merged

Add cross margin liquidation notification helper function#102
sudeepb02 merged 2 commits intodevelopfrom
add/crossMarginLiquidationFunction

Conversation

@iamamitkumar0512
Copy link
Contributor

@iamamitkumar0512 iamamitkumar0512 commented Apr 12, 2025

Summary by CodeRabbit

  • Chores

    • Updated the SDK version to 2.2.15.
  • New Features

    • Introduced functionality to retrieve liquidated positions associated with SNX account identifiers, enhancing insights into account activity.
  • Tests

    • Improved test coverage by adding cases to verify the retrieval of liquidated positions for SNX account IDs and ensuring overall reliability.

@iamamitkumar0512 iamamitkumar0512 self-assigned this Apr 12, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 12, 2025

Walkthrough

This pull request updates the package version from "2.2.13" to "2.2.15" and modifies the Subgraph class by replacing the method getUserPositionsBySnxAccount with getUserLiquidatedPositionsBySnxAccount. The new method retrieves liquidated positions for a specific SNX account. Corresponding changes are made in the positions module and query file to reflect this shift. Additionally, test cases are updated to focus on the new functionality related to liquidated positions.

Changes

File(s) Change Summary
package.json Updated version from "2.2.13" to "2.2.15".
src/subgraph/index.ts Removed method getUserPositionsBySnxAccount and added getUserLiquidatedPositionsBySnxAccount(snxAccountId: string, lastRefresh: number).
src/subgraph/positions/index.ts, src/subgraph/positions/subgraphQueries.ts Renamed getUserPositionsBySnxAccount to getUserLiquidatedPositionsBySnxAccount and updated its signature. Renamed fetchPositionsBySnxAccount to fetchLiquidatedPositionsBySnxAccount and added a new method fetchCrossMarginPositionsBySnxAccount.
test/subgraph-tests/position.test.ts Removed the test case for getUserPositionsBySnxAccount and added a new test case for getUserLiquidatedPositionsBySnxAccount. Updated the execution context of an existing test case.

Suggested reviewers

  • sudeepb02

Poem

I’m a nimble rabbit in fields of code,
Hopping through functions on the digital road.
New methods bloom like carrots in spring,
With queries and tests that make my heart sing.
I crunch the bugs, then happily rest—
Code updates make my days the very best!

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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: 1

🧹 Nitpick comments (3)
src/subgraph/positions/subgraphQueries.ts (1)

392-404: New query for fetching cross-margin liquidated positions.

The query is well-structured for its specific purpose: fetching liquidated positions for a cross-margin account. However, it only retrieves minimal data (accountId, totalAmountLiquidated, and lastRefresh) compared to other position queries in this file that fetch comprehensive position details.

Consider enhancing the query to fetch more comprehensive position data if needed for notifications, such as liquidationTxHash, positionSize, etc., similar to other position queries in this file.

src/subgraph/index.ts (1)

234-241: New method for retrieving cross-margin liquidated positions.

This method extends the Subgraph class with the ability to retrieve cross-margin positions that have been liquidated after a specified timestamp.

The comment "Get open positions by user address" is inaccurate. It should be updated to reflect the actual purpose of retrieving liquidated cross-margin positions. Suggest changing to:

-  // Get open positions by user address
+  // Get liquidated cross-margin positions by SNX account ID and last refresh timestamp
src/subgraph/positions/index.ts (1)

195-216: New function to fetch cross-margin liquidated positions.

The function follows the established pattern in the codebase for fetching positions, including proper error handling and response mapping.

There's significant code duplication between this function and getUserPositionsBySnxAccount (lines 177-193). Consider extracting the common logic into a reusable helper function to improve maintainability:

+ // Helper function to format SNX account ID
+ const formatSnxAccountId = (snxAccountId: string): string => {
+   return !snxAccountId.includes('PERP') ? 'PERP-'.concat(snxAccountId) : snxAccountId;
+ };
+
+ // Generic function to fetch SNX account data with error handling
+ const fetchSnxAccountData = async (
+   subgraphEndpoint: string,
+   query: string,
+ ): Promise<SnxAccount | undefined> => {
+   try {
+     const subgraphResponse: any = await request(subgraphEndpoint, query);
+     return mapResponseToSnxAccount(subgraphResponse?.snxAccount);
+   } catch (error) {
+     console.error('Error fetching positions:', error);
+     return undefined;
+   }
+ };

// Get positions by SNX Account id
export const getUserPositionsBySnxAccount = async (
  subgraphEndpoint: string,
  snxAccountId: string,
): Promise<SnxAccount | undefined> => {
-  let formattedSnxAccountId = snxAccountId;
-  try {
-    if (!snxAccountId.includes('PERP')) {
-      formattedSnxAccountId = 'PERP-'.concat(snxAccountId);
-    }
-    const subgraphResponse: any = await request(subgraphEndpoint, fetchPositionsBySnxAccount(formattedSnxAccountId));
-    const snxAccount = mapResponseToSnxAccount(subgraphResponse?.snxAccount);
-    return snxAccount;
-  } catch (error) {
-    console.error('Error fetching positions:', error);
-    return undefined;
-  }
+  const formattedSnxAccountId = formatSnxAccountId(snxAccountId);
+  return fetchSnxAccountData(subgraphEndpoint, fetchPositionsBySnxAccount(formattedSnxAccountId));
};

// Get liquidated cross-margin positions by SNX Account id
export const getCrossMarginPositionsBySnxAccount = async (
  subgraphEndpoint: string,
  snxAccountId: string,
  lastRefresh: number,
): Promise<SnxAccount | undefined> => {
-  let formattedSnxAccountId = snxAccountId;
-  try {
-    if (!snxAccountId.includes('PERP')) {
-      formattedSnxAccountId = 'PERP-'.concat(snxAccountId);
-    }
-    const subgraphResponse: any = await request(
-      subgraphEndpoint,
-      fetchCrossMarginPositionsBySnxAccount(formattedSnxAccountId, lastRefresh),
-    );
-    const snxAccount = mapResponseToSnxAccount(subgraphResponse?.snxAccount);
-    return snxAccount;
-  } catch (error) {
-    console.error('Error fetching positions:', error);
-    return undefined;
-  }
+  const formattedSnxAccountId = formatSnxAccountId(snxAccountId);
+  return fetchSnxAccountData(
+    subgraphEndpoint,
+    fetchCrossMarginPositionsBySnxAccount(formattedSnxAccountId, lastRefresh)
+  );
};

Update the comment to be more specific about the function's purpose:

- // Get positions by SNX Account id
+ // Get liquidated cross-margin positions by SNX Account id and lastRefresh timestamp
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b4f51d and 6931bb5.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (5)
  • package.json (1 hunks)
  • src/subgraph/index.ts (2 hunks)
  • src/subgraph/positions/index.ts (2 hunks)
  • src/subgraph/positions/subgraphQueries.ts (1 hunks)
  • test/subgraph-tests/position.test.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/subgraph/positions/index.ts (4)
src/subgraph/index.ts (1)
  • getCrossMarginPositionsBySnxAccount (235-241)
src/interfaces/sdkTypes.ts (1)
  • SnxAccount (17-37)
src/subgraph/positions/subgraphQueries.ts (1)
  • fetchCrossMarginPositionsBySnxAccount (392-404)
src/common/subgraphMapper.ts (1)
  • mapResponseToSnxAccount (37-68)
test/subgraph-tests/position.test.ts (1)
test/index.ts (1)
  • getParifiSdkInstanceForTesting (5-33)
src/subgraph/index.ts (2)
src/interfaces/sdkTypes.ts (1)
  • SnxAccount (17-37)
src/subgraph/positions/index.ts (1)
  • getCrossMarginPositionsBySnxAccount (196-216)
🪛 Biome (1.9.4)
test/subgraph-tests/position.test.ts

[error] 56-56: Don't focus the test.

The 'only' method is often used for debugging or during implementation. It should be removed before deploying to production.
Consider removing 'only' to ensure all tests are executed.
Unsafe fix: Remove focus from test.

(lint/suspicious/noFocusedTests)

🔇 Additional comments (4)
package.json (1)

3-3: Version bump for new functionality.

The package version has been incremented from 2.2.13 to 2.2.14, which correctly follows semantic versioning for the addition of the cross-margin liquidation notification helper function.

src/subgraph/index.ts (1)

27-27: Import for the new cross-margin positions function.

The import is correctly added to support the new functionality.

src/subgraph/positions/index.ts (1)

5-5: Import for new GraphQL query function.

Correctly added the import for the new GraphQL query function.

test/subgraph-tests/position.test.ts (1)

47-47: The exclusive test marker has been properly removed.

Good change. Removing the .only ensures this test will run along with other tests in the suite, which is necessary for comprehensive test coverage.

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

♻️ Duplicate comments (1)
test/subgraph-tests/position.test.ts (1)

37-37: ⚠️ Potential issue

Remove the .only test marker before merging to production.

The .only method is used for focusing on a specific test during development, but it should be removed before merging to ensure all tests in the suite are executed.

-it.only('should return positions for a snx account id', async () => {
+it('should return positions for a snx account id', async () => {
🧰 Tools
🪛 Biome (1.9.4)

[error] 37-37: Don't focus the test.

The 'only' method is often used for debugging or during implementation. It should be removed before deploying to production.
Consider removing 'only' to ensure all tests are executed.
Unsafe fix: Remove focus from test.

(lint/suspicious/noFocusedTests)

🧹 Nitpick comments (3)
src/subgraph/index.ts (2)

35-35: Remove import from test directory

The import statement from a test directory into source code is unconventional. The imported status is not used in this file.

-import { status } from '../../test/common/constants';

228-235: Update function comment to match its purpose

The comment above this function incorrectly states "Get open positions by user address" but the function actually retrieves liquidated positions by SNX account.

-  // Get open positions by user address
+  // Get liquidated positions by SNX account
   public async getUserLiquidatedPositionsBySnxAccount(
     snxAccountId: string,
     lastRefresh: number,
   ): Promise<SnxAccount | undefined> {
test/subgraph-tests/position.test.ts (1)

40-47: Test structure could be improved

The test wraps the actual test code in an unnecessary code block. Consider simplifying the structure:

it('should return positions for a snx account id', async () => {
  const parifiSdk = await getParifiSdkInstanceForTesting();

- {
-   // It should return position data just with the snx account id
    const snxAccountId = '12868688093503149326';
    const lastRefresh = 1741189060;
    const positionData = await parifiSdk.subgraph.getUserLiquidatedPositionsBySnxAccount(snxAccountId, lastRefresh);
    
    // Add assertions to verify liquidated positions data
    expect(positionData).toBeDefined();
    expect(positionData?.id).toEqual(`PERP-${snxAccountId}`);
    expect(positionData?.positions?.length).toEqual(1);
- }
});
🧰 Tools
🪛 GitHub Actions: Test contracts sdk

[error] 44-44: GraphQL Error (Code: 403): Error fetching positions: ClientError: GraphQL Error (Code: 403)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6931bb5 and d8d0a72.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (5)
  • package.json (1 hunks)
  • src/subgraph/index.ts (2 hunks)
  • src/subgraph/positions/index.ts (2 hunks)
  • src/subgraph/positions/subgraphQueries.ts (3 hunks)
  • test/subgraph-tests/position.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • package.json
  • src/subgraph/positions/subgraphQueries.ts
  • src/subgraph/positions/index.ts
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/subgraph/index.ts (2)
src/interfaces/sdkTypes.ts (1)
  • SnxAccount (17-37)
src/subgraph/positions/index.ts (1)
  • getUserLiquidatedPositionsBySnxAccount (176-196)
test/subgraph-tests/position.test.ts (1)
test/index.ts (1)
  • getParifiSdkInstanceForTesting (5-33)
🪛 Biome (1.9.4)
test/subgraph-tests/position.test.ts

[error] 37-37: Don't focus the test.

The 'only' method is often used for debugging or during implementation. It should be removed before deploying to production.
Consider removing 'only' to ensure all tests are executed.
Unsafe fix: Remove focus from test.

(lint/suspicious/noFocusedTests)

🪛 GitHub Actions: Test contracts sdk
test/subgraph-tests/position.test.ts

[error] 44-44: GraphQL Error (Code: 403): Error fetching positions: ClientError: GraphQL Error (Code: 403)

🔇 Additional comments (2)
src/subgraph/index.ts (1)

229-234: Function implementation looks good

The implementation correctly retrieves liquidated positions for a specific SNX account ID with the given lastRefresh parameter, matching the interface defined in the positions module.

test/subgraph-tests/position.test.ts (1)

28-28: Good: Removed test focus from closed positions test

Removing .only from this test is the right approach to ensure all tests run in the suite.

@sudeepb02 sudeepb02 merged commit 7cf82fd into develop Apr 12, 2025
2 checks passed
@sudeepb02 sudeepb02 deleted the add/crossMarginLiquidationFunction branch April 12, 2025 19:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants