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

Add stellar into qf round #1835

Merged
merged 1 commit into from
Sep 24, 2024
Merged

Add stellar into qf round #1835

merged 1 commit into from
Sep 24, 2024

Conversation

HrithikSampson
Copy link
Collaborator

@HrithikSampson HrithikSampson commented Sep 24, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a new GraphQL query for retrieving allocated givbacks.
    • Introduced new migrations to enhance project records with banner images and ensure unique project addresses.
    • Expanded network options in the admin interface to include additional Solana networks and Stellar Mainnet.
  • Bug Fixes

    • Adjusted logic for validating project wallet addresses to include memo handling for Stellar chain types.
  • Documentation

    • Updated README to indicate the deprecation of the "Develop" CI/CD pipeline.
  • Tests

    • Added new test cases for donation handling and GraphQL queries to ensure functionality and accuracy.

Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Walkthrough

The changes encompass updates to various files, primarily focusing on database migrations, enhancements to donation handling, and adjustments to GraphQL queries and tests. Key modifications include the addition of new migration files that manage project banners and enforce unique project addresses for Stellar. Additionally, new functions and parameters have been introduced to improve the donation process and related queries, while test cases have been added or updated to ensure functionality and correctness.

Changes

File(s) Change Summary
README.md Added "(deprecated)" to the badge link for the "Develop" CI/CD pipeline.
migration/.../add_banner_endaoment_projects.ts Introduced a migration to update project records with banner images based on categories. Added class and methods for migration without rollback.
migration/.../UniqueProjectAdressWithMomoForStellar.ts Implemented a migration to create a unique index on the project_address table for Stellar addresses. Added class and methods for migration with rollback functionality.
migration/.../add_sponser_donations_to_givback_round_70.ts Added a migration to include sponsor donations in a specific Givback round, with transaction handling and user updates. Rollback logic not implemented.
src/entities/draftDonation.ts Updated the toWalletMemo property to allow null values in GraphQL.
src/repositories/donationRepository.test.ts Introduced a new test suite for getSumOfGivbackEligibleDonationsForSpecificRound, covering multiple scenarios.
src/repositories/donationRepository.ts Enhanced createDonation with new optional parameters and added a function to calculate eligible donations for Givback.
src/repositories/draftDonationRepository.ts Added findDraftDonationByMatchedDonationId function to retrieve draft donations by ID.
src/repositories/projectAddressRepository.ts Modified findRelatedAddressByWalletAddress to include an optional memo parameter for Stellar queries.
src/resolvers/donationResolver.test.ts Added a test suite for allocatedGivbacksQuery with two test cases.
src/resolvers/donationResolver.ts Introduced allocatedGivbacks query to return allocated givbacks data with caching logic.
src/resolvers/draftDonationResolver.test.ts Marked a test case as skipped for fixing later.
src/resolvers/draftDonationResolver.ts Adjusted logic for creating draft donations based on QR donations.
src/resolvers/projectResolver.allProject.test.ts Added networkId property to the project object in test cases.
src/resolvers/projectResolver.test.ts Modified test cases to manage execution and improve error messages.
src/resolvers/projectResolver.ts Updated validateProjectWalletAddress and walletAddressIsValid methods to include memo and chainType parameters.
src/server/adminJs/tabs/qfRoundTab.ts Added Stellar Mainnet to the availableNetworkValues array.
src/server/adminJs/tabs/tokenTab.ts Expanded generateTokenTab function with new Solana network entries and a chainType property.
src/services/chains/index.test.ts Added new test cases to validate transaction details for HNY and Solana tokens.
src/services/chains/index.ts Introduced importedFromDraftOrBackupService property and modified validation logic for transactions.
src/services/cronJobs/checkQRTransactionJob.ts Refactored getPendingDraftDonations function and updated transaction matching logic.
src/services/donationService.test.ts Added a test case for updating donation status based on transaction age.
src/services/donationService.ts Imported function to check draft donations and added logic for updating project statistics.
src/utils/tokenUtils.ts Updated findTokenByNetworkAndSymbol to include an optional coingeckoId parameter.
src/utils/validators/projectValidator.ts Modified validateProjectWalletAddress to handle memo and enhance error messaging.
test/graphqlQueries.ts Added a new GraphQL query allocatedGivbacksQuery for retrieving allocated givbacks data.
test/pre-test-scripts.ts Replaced hardcoded networkId with NETWORK_IDS.SEPOLIA and renamed variable for clarity.
test/testUtils.ts Updated logic for determining chainType and added new optional properties to CreateDonationData interface.

Possibly related PRs

🐇 In the garden, changes bloom,
New banners wave, dispel the gloom.
Donations tracked with care and grace,
Each project shines, finds its place.
With tests in line, all set to go,
A brighter path for us to sow! 🌼


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

@HrithikSampson HrithikSampson changed the base branch from staging to master September 24, 2024 10:43
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.

Caution

Inline review comments failed to post

Actionable comments posted: 31

Outside diff range and nitpick comments (24)
migration/1725188424424-UniqueProjectAdressWithMomoForStellar.ts (1)

3-5: Fix typo in class name

There's a typo in the class name: "Adress" should be "Address".

Please apply the following change:

-export class UniqueProjectAdressWithMomoForStellar1725188424424
+export class UniqueProjectAddressWithMemoForStellar1725188424424
   implements MigrationInterface
src/repositories/draftDonationRepository.ts (1)

72-80: LGTM! Consider adding error handling and documentation.

The new findDraftDonationByMatchedDonationId function is well-implemented and follows good practices. It's concise, uses appropriate TypeScript types, and leverages the ORM effectively.

To further improve the function:

  1. Consider adding error handling to be consistent with other functions in this file.
  2. Add a JSDoc comment to document the function's purpose and parameters.

Here's a suggested implementation with these improvements:

/**
 * Finds a draft donation by its matched donation ID.
 * @param matchedDonationId - The ID of the matched donation to search for.
 * @returns A Promise that resolves to the found DraftDonation or null if not found.
 */
export async function findDraftDonationByMatchedDonationId(
  matchedDonationId: number,
): Promise<DraftDonation | null> {
  try {
    return await DraftDonation.findOne({
      where: {
        matchedDonationId,
      },
    });
  } catch (e) {
    logger.error(
      `Error in findDraftDonationByMatchedDonationId - matchedDonationId: ${matchedDonationId} - error: ${e.message}`,
    );
    return null;
  }
}

This implementation adds error logging consistent with other functions in the file and includes a JSDoc comment for better documentation.

src/services/chains/index.ts (2)

63-67: LGTM: Updated timestamp validation logic

The modification to bypass the timestamp check for imported donations is well-implemented and clearly commented. This change aligns with the new importedFromDraftOrBackupService property.

Consider extracting the condition into a named variable for improved readability:

+  const shouldCheckTimestamp = !input.importedFromDraftOrBackupService;
+
-  if (
-    // We bypass checking tx and donation time for imported donations from backup service or draft donation
-    !input.importedFromDraftOrBackupService &&
-    input.timestamp - transaction.timestamp > ONE_HOUR
-  ) {
+  if (shouldCheckTimestamp && input.timestamp - transaction.timestamp > ONE_HOUR) {
    // because we first create donation, then transaction will be mined, the transaction always should be greater than
    // donation created time, but we set one hour because maybe our server time is different with blockchain time server
    logger.debug(
      'i18n.__(translationErrorMessagesKeys.TRANSACTION_CANT_BE_OLDER_THAN_DONATION)',
      {
        transaction,
        input,
      },
    );
    throw new Error(
      i18n.__(
        translationErrorMessagesKeys.TRANSACTION_CANT_BE_OLDER_THAN_DONATION,
      ),
    );
  }

This change would make the condition more explicit and easier to understand at a glance.


115-115: LGTM: Improved closeTo function implementation

The modification to use a ratio-based comparison in the closeTo function is a good improvement. This approach is more robust when comparing numbers of different magnitudes.

Consider the following enhancements to make the function more robust and flexible:

  1. Add input validation to handle edge cases:
export const closeTo = (a: number, b: number, delta = 0.001): boolean => {
  if (!Number.isFinite(a) || !Number.isFinite(b)) {
    return false;
  }
  if (a === b) {
    return true;
  }
  if (b === 0) {
    return Math.abs(a) < delta;
  }
  return Math.abs(1 - a / b) < delta;
};
  1. Add JSDoc comments to improve documentation:
/**
 * Checks if two numbers are close to each other within a specified relative tolerance.
 * @param a - The first number to compare.
 * @param b - The second number to compare.
 * @param delta - The relative tolerance (default is 0.001).
 * @returns True if the numbers are considered close, false otherwise.
 */
export const closeTo = (a: number, b: number, delta = 0.001): boolean => {
  // ... implementation ...
};

These changes would make the function more robust and self-documenting.

src/server/adminJs/tabs/tokenTab.ts (1)

214-225: LGTM: New Solana network entries added.

The new entries for Solana networks (MAINNET, TESTNET, and DEVNET) are correctly added to the availableValues array. This expands the functionality of the token tab to include Solana networks.

For consistency with other network labels, consider using all uppercase for the Solana network labels:

-              label: 'SOLANA MAINNET',
+              label: 'SOLANA MAINNET',
-              label: 'SOLANA TESTNET',
+              label: 'SOLANA TESTNET',
-              label: 'SOLANA DEVNET',
+              label: 'SOLANA DEVNET',
src/server/adminJs/tabs/qfRoundTab.ts (1)

214-214: LGTM! Consider a minor formatting adjustment.

The addition of the Stellar Mainnet to the availableNetworkValues array is correct and aligns with the PR objective. The structure and naming convention are consistent with other entries.

For consistency with other multi-word labels in the array, consider adding parentheses around "Mainnet":

-  { value: NETWORK_IDS.STELLAR_MAINNET, label: 'STELLAR MAINNET'},
+  { value: NETWORK_IDS.STELLAR_MAINNET, label: 'STELLAR (MAINNET)'},
src/services/donationService.ts (2)

232-233: LGTM: New functionality to find relevant draft donation.

The addition of relevantDraftDonation enhances the function by incorporating draft donation data. This is a good improvement to the donation synchronization process.

Consider adding error handling for the findDraftDonationByMatchedDonationId call to gracefully handle any potential errors during the asynchronous operation.

You might want to add a try-catch block around the findDraftDonationByMatchedDonationId call:

let relevantDraftDonation;
try {
  relevantDraftDonation = await findDraftDonationByMatchedDonationId(donationId);
} catch (error) {
  logger.error('Error finding draft donation', { error, donationId });
  // Decide how to proceed if finding the draft donation fails
}

267-269: LGTM: Added context for imported donations.

The new importedFromDraftOrBackupService property provides valuable information about the donation's origin to the getTransactionInfoFromNetwork function. This addition improves the context available during transaction processing.

To enhance readability, consider extracting the boolean logic into a separate variable with a descriptive name.

You could improve code readability by extracting the boolean logic:

const isImportedDonation = Boolean(donation.importDate || relevantDraftDonation);

// ...

importedFromDraftOrBackupService: isImportedDonation,

This makes the code more self-documenting and easier to understand at a glance.

src/services/chains/index.test.ts (1)

1068-1079: Modifications to closeTo test cases are appropriate, but could use more context

The changes to the existing test cases and the addition of a new test case for the closeTo function look good. They provide better coverage and seem to align with updated expectations for the function's behavior.

However, it would be helpful to add a comment explaining the reasoning behind these changes, especially for the modified assertions.

Consider adding a brief comment above these test cases explaining why the behavior of closeTo has changed for smaller numbers but remains the same for larger numbers. This will help future developers understand the intent behind these modifications.

test/graphqlQueries.ts (1)

Line range hint 1-2266: File structure and organization are maintained.

The addition of the allocatedGivbacksQuery maintains the overall structure and organization of the file. The file continues to be a well-organized collection of GraphQL queries and mutations, which is good for maintainability.

Consider adding a table of contents or grouping comments to help navigate this large file, as it contains numerous queries and mutations.

src/resolvers/projectResolver.allProject.test.ts (1)

Line range hint 1-1824: Consider reviewing test coverage for new scenarios.

The test file provides comprehensive coverage for various filtering, sorting, and pagination scenarios. With the recent additions of new networks and criteria, it would be beneficial to review the test suite to ensure all edge cases are covered.

Consider the following suggestions:

  1. Review if there are any new edge cases introduced by the inclusion of SEPOLIA in the mainnet filter that need specific tests.
  2. Ensure there are sufficient tests for scenarios involving multiple network filters simultaneously.
  3. Consider adding tests for error cases, such as invalid network IDs or conflicting filter combinations.

These additions would further strengthen the robustness of the test suite.

src/resolvers/projectResolver.test.ts (3)

Line range hint 12-179: Consider splitting this test file for better maintainability.

While the imports themselves are correct, the large number of imports suggests that this test file might be covering too many areas. Consider splitting it into smaller, more focused test files. This would improve maintainability and make it easier to locate and run specific tests.


Line range hint 180-216: Address TODO and implement missing test cases.

There are several commented-out test cases and a TODO comment indicating incomplete test coverage. Consider implementing these missing test cases to ensure comprehensive testing of all queries and mutations.

Would you like me to help create a GitHub issue to track the implementation of these missing test cases?


Line range hint 218-264: Enhance date range test for projectsPerDate.

The test case for projectsPerDate is well-structured and covers the basic functionality. However, it could be improved by adding assertions to verify that the returned projects' creation dates actually fall within the specified date range. This would ensure that the query is correctly filtering projects based on the date parameters.

Consider adding the following assertions after the existing checks:

const projects = projectsResponse.data.data.projectsPerDate.projects;
projects.forEach(project => {
  const projectDate = new Date(project.creationDate);
  assert.isTrue(projectDate >= new Date(variables.fromDate));
  assert.isTrue(projectDate <= new Date(variables.toDate));
});
migration/1724368995904-add_banner_endaoment_projects.ts (1)

61-62: Use a proper logging mechanism instead of console.log

Using console.log is not recommended in production code. Consider using a logging library to handle logs more effectively.

migration/1726377580626-add_sponser_donations_to_givback_round_70.ts (3)

56-56: Replace 'console.log' with a structured logging mechanism

Using console.log for logging at lines 56 and 68 is not recommended in production code. It bypasses configured logging levels and formats, making log management and monitoring more difficult.

Consider using a logging library like winston or the application's existing logging utility:

import logger from '../src/logger';

// Replace console.log with:
logger.info('We just want to create these donations in production DB');

And similarly at line 68.

Also applies to: 68-68


20-22: Remove unnecessary 'millisecondTimestampToDate' function

The function millisecondTimestampToDate simply wraps new Date(timestamp) without adding significant value. You can use new Date(timestamp) directly where needed.

Unless there is a specific reason to keep this helper function, consider removing it for conciseness.

-const millisecondTimestampToDate = (timestamp: number): Date => {
-  return new Date(timestamp);
-};

...

-  createdAt: millisecondTimestampToDate(1725354026000),
+  createdAt: new Date(1725354026000),

79-79: Clarify comment regarding GIVbacks eligibility

The comment on line 79 could be more precise. It's crucial to maintain clear documentation, especially when dealing with financial transactions like GIVbacks.

Consider rephrasing for clarity:

-// Set true for isTokenEligibleForGivback, isProjectVerified because Ashley mentioned we want to pay givback for them
+// Set 'isTokenEligibleForGivback' and 'isProjectVerified' to true to ensure GIVbacks are issued for these donations, as per Ashley's instructions.
src/utils/validators/projectValidator.ts (1)

47-55: Review the conditional error handling logic

The added conditional block checks if chainType === ChainType.STELLAR && memo, and throws a specific error message if true. Otherwise, it throws a general error message.

  • Verify that the logic accurately reflects the intended behavior for Stellar addresses with memos.
  • Ensure that the error messages are clear and user-friendly.

Consider enhancing the error messages for clarity:

-`Address ${walletAddress} is already being used for a project with the same MEMO. Please enter a different address or a different MEMO`
+`The Stellar address ${walletAddress} with memo "${memo}" is already associated with another project. Please use a different address or memo.`

-`Address ${walletAddress} is already being used for a project`
+`The address ${walletAddress} is already associated with another project.`
src/repositories/donationRepository.ts (1)

485-523: Consider adding database indexes to improve query performance.

The SQL query in getSumOfGivbackEligibleDonationsForSpecificRound involves multiple conditions and a subquery, which could impact performance on large datasets. Adding indexes on frequently queried columns like "status", "isProjectVerified", "powerRound", and "fromWalletAddress" may enhance query performance.

src/resolvers/donationResolver.ts (1)

95-95: Instance-level caching may lead to inconsistencies in a distributed system.

Storing allocatedGivbacksCache at the instance level can cause inconsistent behavior when running multiple instances of the application (e.g., in a load-balanced environment). Each instance will maintain its own cache, leading to potential discrepancies in the data returned to clients.

Consider using a distributed caching solution like Redis or an in-memory cache with shared state to synchronize the cache across all instances.

src/repositories/donationRepository.test.ts (2)

1465-1466: Add a descriptive comment for the test suite function.

Consider adding a brief comment explaining the purpose of the getSumOfGivbackEligibleDonationsForSpecificRoundTestCases function to enhance readability and maintainability.


1468-1754: Use descriptive constants for powerRound values.

Numeric values are directly assigned to the powerRound variable (e.g., powerRound = 3232;). To improve code readability, consider defining these values as constants with meaningful names.

src/services/cronJobs/checkQRTransactionJob.ts (1)

28-33: Remove unnecessary await in return statement

In the getPendingDraftDonations function, using return await is redundant since the function is already async. You can return the promise directly.

Apply this diff to simplify the code:

-const getPendingDraftDonations = async () => {
-  return await DraftDonation.createQueryBuilder('draftDonation')
+const getPendingDraftDonations = async () => {
+  return DraftDonation.createQueryBuilder('draftDonation')
     .where('draftDonation.status = :status', { status: 'pending' })
     .andWhere('draftDonation.isQRDonation = true')
     .getMany();
-};
+};
Comments failed to post (31)
src/repositories/draftDonationRepository.ts (1)

72-80: 💡 Codebase verification

Inconsistent error handling detected.

The exported async functions in draftDonationRepository.ts exhibit inconsistent error handling:

  • markDraftDonationStatusMatched includes a try-catch block.
  • findDraftDonationByMatchedDonationId lacks a try-catch block.

For consistency and improved error management, consider standardizing the error handling approach across all exported async functions.

  • Option A: Add try-catch blocks with appropriate error logging to all exported async functions.
  • Option B: Remove try-catch blocks from all functions and handle errors at a higher level.
Analysis chain

Consider usage context and error handling consistency.

The new findDraftDonationByMatchedDonationId function is a valuable addition to the repository. It complements existing functionality and adheres to the established patterns in this file.

However, consider the following points:

  1. Usage Context: It would be helpful to understand where and how this function will be used. Consider adding a brief comment explaining its intended use case.

  2. Error Handling Consistency: The error handling approach in this function differs slightly from others in the file (e.g., markDraftDonationStatusMatched). For consistency, consider either:
    a) Adding similar try-catch blocks with error logging to all functions, or
    b) Removing try-catch blocks from all functions and handling errors at a higher level.

To ensure consistent usage of error handling across the file, you can run the following command:

This script will help identify if there's a mix of error handling approaches in the file.



</details>

<details>
<summary>Scripts executed</summary>

The following scripts were executed for the analysis:

Script:

```shell
#!/bin/bash
# Description: Check for consistent error handling in exported functions

# Test: Count the number of exported functions with and without try-catch blocks
with_try_catch=$(rg --type typescript '^\s*export\s+async\s+function.*\{.*try\s*\{' src/repositories/draftDonationRepository.ts | wc -l)
without_try_catch=$(rg --type typescript '^\s*export\s+async\s+function.*\{(?!.*try\s*\{)' src/repositories/draftDonationRepository.ts | wc -l)

echo "Functions with try-catch: $with_try_catch"
echo "Functions without try-catch: $without_try_catch"

if [ "$with_try_catch" -ne 0 ] && [ "$without_try_catch" -ne 0 ]; then
  echo "Inconsistent error handling detected. Consider standardizing the approach."
else
  echo "Error handling appears to be consistent."
fi

Length of output: 854


Script:

#!/bin/bash
# Description: Verify consistent error handling in exported async functions using ast-grep

# Extract all exported async functions
exported_functions=$(ast-grep --lang typescript --pattern 'export async function $_($_): Promise<$_> { $$$ }' src/repositories/draftDonationRepository.ts)

# Initialize counters
with_try_catch=0
without_try_catch=0

# Iterate over each exported function to check for try-catch blocks
echo "$exported_functions" | while read -r function; do
  # Check if the function contains a try-catch block
  if echo "$function" | grep -q "try {"; then
    with_try_catch=$((with_try_catch + 1))
  else
    without_try_catch=$((without_try_catch + 1))
  fi
done

echo "Functions with try-catch: $with_try_catch"
echo "Functions without try-catch: $without_try_catch"

if [ "$with_try_catch" -ne 0 ] && [ "$without_try_catch" -ne 0 ]; then
  echo "Inconsistent error handling detected. Consider standardizing the approach."
else
  echo "Error handling appears to be consistent."
fi

Length of output: 13206


Script:

#!/bin/bash
# Description: Verify consistent error handling in exported async functions using ast-grep

# Extract all exported async functions
exported_functions=$(ast-grep --lang typescript --pattern 'export async function $_($_): Promise<$_> { $$$ }' src/repositories/draftDonationRepository.ts)

# Initialize counters
with_try_catch=0
without_try_catch=0

# Split the functions by the closing brace of each function
IFS=$'\n}' read -rd '' -a functions <<< "$exported_functions"

# Iterate over each function to check for try-catch blocks
for func in "${functions[@]}"; do
  if echo "$func" | grep -q "try {"; then
    with_try_catch=$((with_try_catch + 1))
  elif [[ -n "$func" ]]; then
    without_try_catch=$((without_try_catch + 1))
  fi
done

echo "Functions with try-catch: $with_try_catch"
echo "Functions without try-catch: $without_try_catch"

if [ "$with_try_catch" -ne 0 ] && [ "$without_try_catch" -ne 0 ]; then
  echo "Inconsistent error handling detected. Consider standardizing the approach."
else
  echo "Error handling appears to be consistent."
fi

Length of output: 15162


Script:

#!/bin/bash
# Description: Verify consistent error handling in exported async functions using ast-grep

# Extract all exported async functions
all_functions=$(ast-grep --lang typescript --pattern 'export async function $_($_): Promise<$_> { $$$ }' src/repositories/draftDonationRepository.ts)

# Extract exported async functions that contain a try-catch block
functions_with_try=$(ast-grep --lang typescript --pattern 'export async function $_($_): Promise<$_> { try { $$$ } catch (e) { $$$ } $$$ }' src/repositories/draftDonationRepository.ts)

# Count total exported async functions
total_functions=$(echo "$all_functions" | grep -c 'export async function')

# Count exported async functions with try-catch
total_with_try=$(echo "$functions_with_try" | grep -c 'export async function')

# Calculate functions without try-catch
total_without_try=$((total_functions - total_with_try))

echo "Total exported async functions: $total_functions"
echo "Functions with try-catch: $total_with_try"
echo "Functions without try-catch: $total_without_try"

if [ "$total_with_try" -ne "$total_functions" ] && [ "$total_without_try" -ne "$total_functions" ]; then
  echo "Inconsistent error handling detected. Consider standardizing the approach."
else
  echo "Error handling appears to be consistent."
fi

Length of output: 12399

src/server/adminJs/tabs/tokenTab.ts (1)

241-248: 🛠️ Refactor suggestion

LGTM: New chainType property added.

The chainType property is correctly implemented with appropriate availableValues for EVM, SOLANA, and STELLAR. This addition enhances the token categorization based on the underlying blockchain technology and aligns with the PR objective of adding Stellar support.

Consider using an enum or constant for the chain type values to improve maintainability and reduce the risk of typos:

const CHAIN_TYPES = {
  EVM: 'EVM',
  SOLANA: 'SOLANA',
  STELLAR: 'STELLAR',
} as const;

// Then in the availableValues array:
availableValues: Object.entries(CHAIN_TYPES).map(([key, value]) => ({
  value,
  label: key,
})),

This approach would make it easier to add new chain types in the future and ensure consistency throughout the codebase.

src/resolvers/draftDonationResolver.test.ts (1)

558-559: 💡 Codebase verification

⚠️ Potential issue

Multiple skipped tests detected in draftDonationResolver.test.ts

The test file src/resolvers/draftDonationResolver.test.ts contains the following skipped tests:

  1. should return the same draft recurring donation id if the same donation is created twice
  2. should renew the expiration date of the draft donation

Skipping tests can lead to reduced test coverage and potential bugs going unnoticed.

Consider the following improvements:

  1. Provide Detailed Explanations: Update the TODO comments for each skipped test to explain why they are skipped and what needs to be addressed.

    // TODO: Fix this test (JIRA-1234). It's currently failing due to [specific reason].
    // Assigned to Meriem. Target resolution by [date].
  2. Add Ticket References or Timelines: Include references to relevant tickets or set deadlines to ensure that these tests are revisited and resolved in a timely manner.

  3. Avoid Skipping Tests When Possible: If feasible, address the reasons causing the tests to fail instead of skipping them to maintain comprehensive test coverage.

Analysis chain

Avoid skipping tests without proper documentation

The test case for renewing the expiration date of draft donations has been skipped without a clear explanation. Skipping tests can lead to reduced test coverage and potential bugs going unnoticed.

Consider the following improvements:

  1. Provide a more detailed explanation in the TODO comment about why the test is being skipped and what specifically needs to be fixed.
  2. Add a ticket reference or timeline for when this test will be addressed.
  3. If possible, fix the test now instead of skipping it to maintain test coverage.

Example of a more informative TODO comment:

// TODO: Fix this test (JIRA-1234). It's currently failing due to [specific reason].
// Meriem to address by [date].

To ensure this is the only skipped test in the file, run the following command:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any skipped tests in the file
rg --type typescript 'it\.skip' src/resolvers/draftDonationResolver.test.ts

Length of output: 112


Script:

#!/bin/bash
# Description: Check for any skipped tests in the TypeScript test file
rg 'it\.skip' src/resolvers/draftDonationResolver.test.ts

Length of output: 257

test/graphqlQueries.ts (1)

2252-2266: 💡 Codebase verification

Backend schema does not support allocatedGivbacks query and it's unused in frontend.

  • The allocatedGivbacks query is not found in the backend GraphQL schema.
  • Frontend references to allocatedGivbacksQuery exist only in test files and are not utilized in the actual application code.

Please ensure that the backend schema includes the allocatedGivbacks query and that the frontend components are updated to use it appropriately.

Analysis chain

Verify backend support and frontend usage.

While the new allocatedGivbacksQuery is well-structured, it's important to ensure:

  1. The backend GraphQL schema supports this new query and can handle the refreshCache parameter.
  2. The frontend components are updated to use this new query where appropriate.

Could you confirm that these aspects have been addressed in the corresponding backend and frontend changes?


[/run_scripts]

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for backend schema updates and frontend usage of the new query

# Test 1: Check if the backend schema has been updated
echo "Checking backend schema for allocatedGivbacks query:"
rg --type graphql "type Query" -A 20 | rg "allocatedGivbacks"

# Test 2: Check for frontend usage of the new query
echo "Checking frontend usage of allocatedGivbacksQuery:"
rg "allocatedGivbacksQuery" --type typescript --type javascript

Length of output: 418


Script:

#!/bin/bash
# Description: Check for backend schema updates and frontend usage of the new query using file extensions

# Test 1: Check if the backend schema has been updated
echo "Checking backend schema for allocatedGivbacks query in .graphql and .gql files:"
fd --extension graphql --extension gq | xargs rg "type Query" -A 20 | rg "allocatedGivbacks"

# Test 2: Check for frontend usage of allocatedGivbacksQuery in .ts, .tsx, .js, and .jsx files
echo "Checking frontend usage of allocatedGivbacksQuery in TypeScript and JavaScript files:"
fd --extension ts --extension tsx --extension js --extension jsx | xargs rg "allocatedGivbacksQuery"

Length of output: 1227

test/testUtils.ts (1)

276-291: 💡 Codebase verification

Action Required: Update Affected Test Files

The following test files use the saveProjectDirectlyToDb function or have hardcoded ChainType.EVM and may need updates to accommodate recent changes:

  • test/pre-test-scripts.ts
  • test/testUtils.ts

Please update these tests to handle the dynamic determination of chainType and ensure they remain consistent with the updated implementation.

Analysis chain

LGTM! Consider verifying the impact on existing tests.

The changes to saveProjectDirectlyToDb function look good. The introduction of SolanaNetworkIds and dynamic chainType determination improves the flexibility of the function by supporting Solana chain types.

To ensure these changes don't break existing functionality, please run the following script:


Shell Script Correction Needed

The previous verification script encountered an error. Please run the following corrected script to check for any test files that might need updates due to the changes in the saveProjectDirectlyToDb function:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any test files that might need updates due to the changes in saveProjectDirectlyToDb function

# Search for test files that use saveProjectDirectlyToDb
echo "Test files potentially affected by changes in saveProjectDirectlyToDb:"
grep -R "saveProjectDirectlyToDb" test/

# Search for any hardcoded ChainType.EVM in test files
echo "\nTest files with hardcoded ChainType.EVM that might need updates:"
grep -R "ChainType.EVM" test/

Length of output: 19650


Script:

#!/bin/bash
# Description: Check for any test files that might need updates due to the changes in saveProjectDirectlyToDb function

# Search for test files that use saveProjectDirectlyToDb
echo "Test files potentially affected by changes in saveProjectDirectlyToDb:"
grep -R "saveProjectDirectlyToDb" test/

# Search for any hardcoded ChainType.EVM in test files
echo -e "\nTest files with hardcoded ChainType.EVM that might need updates:"
grep -R "ChainType.EVM" test/

Length of output: 1271

src/resolvers/projectResolver.ts (1)

1789-1795: 💡 Codebase verification

Potential Issue Detected: Update Required for walletAddressIsValid Calls

The call to walletAddressIsValid in test/graphqlQueries.ts is missing the new chainType and memo parameters.

  • test/graphqlQueries.ts: walletAddressIsValid(address: $address)

Please update this call to include the new parameters to ensure compatibility with the updated method signature.

Analysis chain

LGTM! Verify impact on existing code.

The addition of chainType and memo parameters to the walletAddressIsValid method enhances its flexibility for different blockchain types. This is a good improvement that allows for more comprehensive wallet address validation.

To ensure this change doesn't break existing functionality, please run the following script to check for any calls to walletAddressIsValid that might need updating:


To resolve the error with the previous script, please run the following corrected script to find all calls to walletAddressIsValid:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all calls to walletAddressIsValid and verify if they need updating

# Search for calls to walletAddressIsValid
rg --type typescript -e 'walletAddressIsValid\s*\(' -A 3

Length of output: 93


Script:

#!/bin/bash
# Description: Find all calls to walletAddressIsValid and verify if they need updating

# Search for calls to walletAddressIsValid in TypeScript files
rg -g "*.ts" -g "*.tsx" -e 'walletAddressIsValid\s*\(' -A 3

Length of output: 533

migration/1724368995904-add_banner_endaoment_projects.ts (4)

47-47: ⚠️ Potential issue

Correct the typo in 'Registred Non-profits'

There's a typo in the key 'Registred Non-profits'; it should be 'Registered Non-profits'.

Apply this diff to fix the typo:

-          'Registred Non-profits': 'non-profit',
+          'Registered Non-profits': 'non-profit',
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.

      'Registered Non-profits': 'non-profit',

67-79: 🛠️ Refactor suggestion

Move getCategoryNames function definition outside the loop

Defining the getCategoryNames function inside the loop causes it to be redefined on every iteration, which is inefficient. Move the function definition outside the loop to improve performance and readability.

Apply this diff to move the function definition:

+  // Move the function definition before the loop
+  const getCategoryNames = (nteeCode: string): string[] => {
+    const mapping = endaomentProjectCategoryMapping.find(
+      category => category.nteeCode === nteeCode,
+    );
+    return mapping
+      ? [
+          mapping.category1,
+          mapping.category2,
+          mapping.category3 || '',
+          mapping.category4 || '',
+        ].filter(Boolean)
+      : [];
+  };

   for (const project of endaomentProjects) {
-     const getCategoryNames = (nteeCode: string): string[] => {
-       const mapping = endaomentProjectCategoryMapping.find(
-         category => category.nteeCode === nteeCode,
-       );
-       return mapping
-         ? [
-             mapping.category1,
-             mapping.category2,
-             mapping.category3 || '',
-             mapping.category4 || '',
-           ].filter(Boolean)
-         : [];
-     };
     // Rest of the loop body...

Committable suggestion was skipped due to low confidence.


86-86: ⚠️ Potential issue

Handle potential undefined categoryNames[1] to avoid runtime errors

Accessing categoryNames[1] may result in undefined if there are fewer than two categories returned. This could lead to an incorrect bannerImage path. Ensure that categoryNames[1] exists before using it.

Apply this diff to handle missing categories:

-          const bannerImage = `/images/defaultProjectImages/${imageCategoryMapping[categoryNames[1]] || '1'}.png`;
+          const categoryKey = categoryNames[1] || categoryNames[0] || '1';
+          const bannerImage = `/images/defaultProjectImages/${imageCategoryMapping[categoryKey] || '1'}.png`;
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.

      const categoryKey = categoryNames[1] || categoryNames[0] || '1';
      const bannerImage = `/images/defaultProjectImages/${imageCategoryMapping[categoryKey] || '1'}.png`;

98-100: ⚠️ Potential issue

Implement a rollback mechanism in the down method

The down method is currently empty, which means this migration cannot be rolled back. Implementing the down method is important for reversing changes made by the up method, ensuring database integrity.

Consider resetting the image field to its previous value or setting it to NULL in the down method.

   public async down(queryRunner: QueryRunner): Promise<void> {
-    // No down migration
+    await queryRunner.query(`UPDATE project SET image = NULL WHERE image LIKE '/images/defaultProjectImages/%'`);
   }

Committable suggestion was skipped due to low confidence.

migration/1726377580626-add_sponser_donations_to_givback_round_70.ts (6)

63-65: ⚠️ Potential issue

Prevent SQL injection by using parameterized queries

The query at lines 63-65 interpolates user input directly into the SQL statement, which can lead to SQL injection vulnerabilities. Use parameterized queries to ensure that inputs are properly escaped.

Refactor the query to use parameterized parameters:

let user = (
  await queryRunner.query(
    `SELECT * FROM public.user WHERE lower("walletAddress") = lower($1)`,
    [tx.donorAddress]
  )
)[0];

70-76: ⚠️ Potential issue

Use parameterized queries for INSERT statement to enhance security

The INSERT statement at lines 70-76 directly includes user-provided values, posing a risk of SQL injection. Utilize parameterized queries to securely insert data into the database.

Refactor the INSERT query as follows:

await queryRunner.query(
  `
  INSERT INTO public.user ("walletAddress", role, "loginType", name)
  VALUES (lower($1), 'restricted', 'wallet', $2)
  ON CONFLICT DO NOTHING;
  `,
  [tx.donorAddress, tx.donorName]
);

88-97: ⚠️ Potential issue

Secure the INSERT INTO 'donation' statement with parameterized queries

The INSERT statement at lines 88-97 directly interpolates variables into the SQL query, which can lead to SQL injection attacks. Replace string interpolation with parameterized queries to enhance security.

Refactor the INSERT query to use parameters:

await queryRunner.query(
  `
  INSERT INTO donation (
    "toWalletAddress",
    "projectId",
    "fromWalletAddress",
    "userId",
    amount,
    currency,
    "transactionId",
    "transactionNetworkId",
    anonymous,
    "valueUsd",
    status,
    "segmentNotified",
    "isTokenEligibleForGivback",
    "isProjectVerified",
    "createdAt",
    "givbackFactor",
    "powerRound",
    "projectRank",
    "bottomRankInRound",
    "qfRoundId",
    "tokenAddress"
  )
  VALUES (
    lower($1),
    $2,
    lower($3),
    $4,
    $5,
    $6,
    lower($7),
    $8,
    false,
    $9,
    'verified',
    true,
    true,
    true,
    $10,
    $11,
    $12,
    $13,
    $14,
    $15,
    $16
  )
  ON CONFLICT DO NOTHING;
  `,
  [
    tx.toWalletAddress,
    tx.projectId,
    tx.fromWalletAddress,
    user.id,
    tx.amount,
    tx.currency,
    tx.transactionId,
    tx.transactionNetworkId,
    tx.valueUsd,
    createdAt,
    givbackFactor,
    powerRound,
    projectRank,
    bottomRankInRound,
    tx.qfRoundId || null,
    tx.tokenAddress
  ]
);

108-109: 🛠️ Refactor suggestion

Implement the 'down' method for proper migration rollback

The down method is currently empty, meaning this migration cannot be rolled back. Implementing the down method ensures that changes can be reverted if necessary, aiding in database maintainability.

Consider adding logic to reverse the changes made in the up method, such as deleting the inserted donations and any newly created users.


48-50: ⚠️ Potential issue

Correct the typo in 'Sponser' to 'Sponsor' in class name and filename

The word "Sponsor" is misspelled as "Sponser" in both the class name AddSponserDonationsToGivbackRound701726377580626 and the filename 1726377580626-add_sponser_donations_to_givback_round_70.ts. Correcting this will improve code readability and maintain consistency.

Apply this diff to fix the typo in the class name:

-export class AddSponserDonationsToGivbackRound701726377580626
+export class AddSponsorDonationsToGivbackRound701726377580626

Rename the file to:

1726377580626-add_sponsor_donations_to_givback_round_70.ts
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.

export class AddSponsorDonationsToGivbackRound701726377580626
  implements MigrationInterface
{

54-58: 🛠️ Refactor suggestion

Allow migration to run in non-production environments for testing

The migration is currently set to run only in the production environment. This prevents testing and validating the migration in development or staging environments, which is crucial for identifying potential issues before deployment.

Modify the condition to allow the migration to run in non-production environments, or provide a mechanism to enable testing when required.

-if (environment !== 'production') {
+if (environment === 'production' || process.env.ALLOW_MIGRATION_IN_DEV) {

You can set ALLOW_MIGRATION_IN_DEV to true in your development environment when you need to test the migration.

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.

    if (environment === 'production' || process.env.ALLOW_MIGRATION_IN_DEV) {
      // eslint-disable-next-line no-console
      console.log('We just want to create these donations in production DB');
      return;
    }
src/repositories/projectAddressRepository.ts (2)

54-71: ⚠️ Potential issue

Potential Issue with Handling Empty Strings for memo Parameter

The current condition if (memo) treats an empty string ('') as falsy. If an empty string is a valid memo value for Stellar transactions, this could lead to incorrect query results, as the code would incorrectly treat memo as not provided.

Consider changing the condition to explicitly check for memo !== undefined && memo !== null to correctly handle empty strings.

Apply this diff to fix the condition:

- if (memo) {
+ if (memo !== undefined && memo !== null) {
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.

      // If a memo is provided, check for both address and memo
      if (memo !== undefined && memo !== null) {
        query = query.where(
          'UPPER(address) = :walletAddress AND memo = :memo',
          {
            walletAddress: walletAddress.toUpperCase(),
            memo,
          },
        );
      } else {
        // If no memo is provided, check only the address
        query = query.where(
          'UPPER(address) = :walletAddress AND memo IS NULL',
          {
            walletAddress: walletAddress.toUpperCase(),
          },
        );
      }

43-44: 💡 Codebase verification

Some calls to findRelatedAddressByWalletAddress are missing the optional memo parameter required for Stellar addresses. Please update these call sites to include the memo parameter where necessary:

  • test/testUtils.ts
  • src/utils/validators/projectValidator.ts
  • src/repositories/projectAddressRepository.test.ts
  • migration/1680507337701-add_some_donations_to_db.ts
Analysis chain

Ensure All Calls to Updated Function Include the Optional memo Parameter

The function findRelatedAddressByWalletAddress now accepts an optional memo?: string parameter. Please verify that all calls to this function in the codebase have been updated accordingly, and that the memo parameter is provided where necessary for Stellar addresses.

Run the following script to find all usages of findRelatedAddressByWalletAddress:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all call sites of `findRelatedAddressByWalletAddress` to ensure correct usage.

# Expected: Review each call to ensure it handles the `memo` parameter appropriately.

rg --type ts 'findRelatedAddressByWalletAddress\(' -A 2 -B 2

Length of output: 2434

src/utils/validators/projectValidator.ts (2)

44-44: ⚠️ Potential issue

Confirm findRelatedAddressByWalletAddress accepts the memo parameter

The function findRelatedAddressByWalletAddress is now called with an additional memo argument. Ensure that this function's definition has been updated to accept the memo parameter and that it handles it correctly.

If findRelatedAddressByWalletAddress does not accept memo as a parameter, this could lead to unexpected behavior or runtime errors. Please confirm that its signature and implementation have been updated accordingly.


26-26: 💡 Codebase verification

Action Required: Update Function Calls to Include memo Parameter

Several calls to validateProjectWalletAddress are missing the memo parameter:

  • src/utils/validators/projectValidator.ts
  • src/utils/validators/projectValidator.test.ts

Please update these calls to include the memo parameter and ensure the function handles cases where memo is undefined appropriately.

Analysis chain

Ensure all function calls include the new memo parameter

The validateProjectWalletAddress function now includes an optional memo?: string parameter. Please verify that all calls to this function throughout the codebase have been updated to include the memo parameter where necessary, and that the function correctly handles cases where memo is undefined.

You can run the following script to find all usages of the function:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all calls to `validateProjectWalletAddress` and check for the `memo` parameter.

# Test: Search for function calls. Expect: All calls should handle the `memo` parameter appropriately.
rg --type ts 'validateProjectWalletAddress\(' -A 2

Length of output: 3412

src/repositories/donationRepository.ts (2)

501-501: ⚠️ Potential issue

Handle possible NULL values in 'valueUsd' and 'givbackFactor'.

In the SQL query, the calculation SUM("donation"."valueUsd" * "donation"."givbackFactor") may result in NULL if either valueUsd or givbackFactor is NULL, potentially leading to inaccurate totals. Consider using COALESCE to handle NULL values and ensure accurate aggregation.

Apply this diff to handle NULL values:

-              SELECT 
-                SUM("donation"."valueUsd" * "donation"."givbackFactor") AS "totalUsdWithGivbackFactor"
+              SELECT 
+                SUM(COALESCE("donation"."valueUsd", 0) * COALESCE("donation"."givbackFactor", 0)) AS "totalUsdWithGivbackFactor"
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.

            SUM(COALESCE("donation"."valueUsd", 0) * COALESCE("donation"."givbackFactor", 0)) AS "totalUsdWithGivbackFactor"

519-522: ⚠️ Potential issue

Avoid returning a default value on error without notification.

Returning 0 in the catch block may mask errors and cause the calling functions to misinterpret the result as no eligible donations. Instead, consider rethrowing the error or returning a specific error value to indicate that an error occurred.

Apply this diff to rethrow the error:

    } catch (e) {
        logger.error('getSumOfGivbackEligibleDonationsForSpecificRound() error', e);
-       return 0;
+       throw e;
    }

Alternatively, handle the error in a way that ensures the calling function is aware of the failure and can respond appropriately.

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.

  } catch (e) {
    logger.error('getSumOfGivbackEligibleDonationsForSpecificRound() error', e);
    throw e;
  }
src/resolvers/donationResolver.ts (2)

464-469: ⚠️ Potential issue

Ensure 'givPrice' is valid to prevent division by zero errors.

There's a risk of a division by zero when calculating allocatedGivTokens if givPrice is zero or undefined. This would cause a runtime error.

Apply this diff to validate givPrice before performing the calculation:

+if (!givPrice || givPrice <= 0) {
+  throw new Error('Invalid GIV price: cannot be zero or undefined.');
+}
 const allocatedGivTokens = Math.ceil(
   Math.min(maxSentGivInRound, usdValueSentAmountInPowerRound / givPrice),
 );
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.

    const givPrice = await getTokenPrice(NETWORK_IDS.MAIN_NET, givToken);

    const maxSentGivInRound = 1_000_000;
    if (!givPrice || givPrice <= 0) {
      throw new Error('Invalid GIV price: cannot be zero or undefined.');
    }
    const allocatedGivTokens = Math.ceil(
      Math.min(maxSentGivInRound, usdValueSentAmountInPowerRound / givPrice),
    );

479-485: 🛠️ Refactor suggestion

Use a robust caching mechanism instead of 'setTimeout'.

Utilizing setTimeout for cache invalidation may lead to memory leaks over time, especially if this method is called frequently. This approach can also be unreliable in a production environment where the application might restart, causing the timeout to reset.

Consider using a dedicated caching library like node-cache or lru-cache that handles expiration more efficiently. These libraries provide built-in methods for setting time-to-live (TTL) values and automatic cache invalidation.

Apply this diff to integrate a caching library:

+import NodeCache from 'node-cache';
+const cache = new NodeCache({ stdTTL: 3600 }); // Cache expires after 1 hour

 @Query(_returns => AllocatedGivbacks, { nullable: true })
 async allocatedGivbacks(
   @Arg('refreshCache', { nullable: true }) refreshCache?: boolean,
 ): Promise<AllocatedGivbacks> {
-  if (allocatedGivbacksCache && !refreshCache) {
-    return allocatedGivbacksCache;
-  }
+  const cacheKey = 'allocatedGivbacks';
+  if (!refreshCache) {
+    const cachedData = cache.get(cacheKey);
+    if (cachedData) {
+      return cachedData;
+    }
+  }

   // ... [existing code to calculate allocatedGivTokens] ...

-  allocatedGivbacksCache = {
+  const result = {
     allocatedGivTokens,
     givPrice,
     usdValueSentAmountInPowerRound,
     date,
   };
-  // 60 minute cache
-  const sentGivbackCacheTimeout = 1000 * 60 * 60;
-
-  setTimeout(() => {
-    allocatedGivbacksCache = null;
-  }, sentGivbackCacheTimeout);
+  cache.set(cacheKey, result);

   return result;
 }

Committable suggestion was skipped due to low confidence.

src/repositories/donationRepository.test.ts (4)

1468-1754: 🛠️ Refactor suggestion

Refactor repeated code into helper functions.

The setup code for creating projects, donors, and donations is repeated across multiple test cases. Extracting this code into reusable helper functions will reduce duplication and enhance maintainability.


1468-1754: 🛠️ Refactor suggestion

Use constants for donation status values.

Throughout the test cases, the status property is assigned string literals like 'verified' and 'pending'. To improve consistency and reduce the risk of typos, consider using the DONATION_STATUS constants imported from '../entities/donation'.

Apply this diff to use status constants:

-            status: 'verified',
+            status: DONATION_STATUS.VERIFIED,

Similarly, replace all instances where status is set with string literals.

Committable suggestion was skipped due to low confidence.


1729-1730: ⚠️ Potential issue

Check for null or undefined walletAddress.

When creating donor3, the walletAddress of verifiedProject is used. Ensure that walletAddress is not undefined or null before using it to prevent potential runtime errors.

Apply this diff to handle potential null values:

- const donor3 = await saveUserDirectlyToDb(
-   verifiedProject!.walletAddress as string,
- );
+ if (verifiedProject.walletAddress) {
+   const donor3 = await saveUserDirectlyToDb(verifiedProject.walletAddress);
+ } else {
+   throw new Error('verifiedProject.walletAddress is undefined');
+ }

This adds a check to ensure walletAddress is available.

Committable suggestion was skipped due to low confidence.


1755-1824: ⚠️ Potential issue

Avoid side effects from global state in tests.

The test relies on setting a global powerRound value using setPowerRound. If tests are executed concurrently, this can cause unpredictable results due to shared state. Consider mocking or isolating the powerRound state within the test to prevent interference with other tests.

Apply this refactor to isolate the powerRound state:

- await setPowerRound(powerRound);
+ // Mock the powerRound value within the function or use dependency injection

This ensures that the test remains self-contained and does not affect global state.

Committable suggestion was skipped due to low confidence.

src/resolvers/donationResolver.test.ts (2)

4973-5165: 🛠️ Refactor suggestion

Refactor duplicate setup code in test cases

Both test cases within allocatedGivbacksQueryTestCases contain similar setup code for creating projects, donors, and donations. Consider refactoring this common code into a shared helper function or using beforeEach to reduce duplication and enhance maintainability.


4972-4972: ⚠️ Potential issue

Avoid using async with describe functions

The allocatedGivbacksQueryTestCases function is used as a callback in describe, and using the async keyword on describe callbacks is generally discouraged as it may cause unexpected behavior in Mocha tests. It's recommended to remove async from the function declaration.

Apply this diff to fix the issue:

-async function allocatedGivbacksQueryTestCases() {
+function allocatedGivbacksQueryTestCases() {
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.

function allocatedGivbacksQueryTestCases() {
src/services/cronJobs/checkQRTransactionJob.ts (1)

151-153: ⚠️ Potential issue

Handle potential errors from calculateGivbackFactor

Since calculateGivbackFactor is an asynchronous operation that may fail, it's advisable to handle potential errors to prevent unhandled exceptions.

Apply this diff to add error handling:

 try {
   const { givbackFactor, projectRank, bottomRankInRound, powerRound } =
     await calculateGivbackFactor(project.id);
+} catch (error) {
+  logger.error(
+    `Error calculating givback factors for project ID ${project.id}:`,
+    error,
+  );
+  // Decide how to handle the error, e.g., set default values or abort the operation
+  return;
 }

Committable suggestion was skipped due to low confidence.

Copy link
Collaborator

@RamRamez RamRamez left a comment

Choose a reason for hiding this comment

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

@RamRamez RamRamez merged commit cd4de51 into master Sep 24, 2024
@RamRamez RamRamez deleted the add-stellar-into-qf-round branch September 24, 2024 10:56
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.

2 participants