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

refactor: updated apr formula #40

Merged
merged 2 commits into from
Jun 24, 2024
Merged

Conversation

yevhen-burkovskyi
Copy link
Contributor

No description provided.

Copy link

coderabbitai bot commented Jun 24, 2024

Walkthrough

The update introduces significant changes to the handling of APR calculations by integrating new endpoints and restructuring service dependencies. Key components like Okp4Service now incorporate methods for APR computation, leveraging new response interfaces and removing the reliance on OsmosisService. These adjustments streamline APR fetching and provide more efficient service interactions within the token and staking modules.

Changes

File/Path Change Summary
.../endpoints.enum.ts Added new endpoints INFLATION and DISTRIBUTION_PARAMS to Endpoints enum.
.../responses/inflation.response.ts Introduced InflationResponse interface with inflation property of type string.
.../responses/distribution-params.response.ts Added DistributionParamsResponse interface defining parameters related to community tax, proposer rewards, and withdrawal address enabling.
.../okp4.service.ts Modified Okp4Service to import Big, InflationResponse, and DistributionParamsResponse, and added a new method getApr() for making async calls and calculations.
.../staking/services/staking.service.ts Removed OsmosisService dependency from StakingService constructor. Updated method fetchAndCacheGlobalStakedOverview to use okp4Service.getApr() instead of osmosisService.getStakingApr().
.../staking/staking.module.ts Removed OsmosisService from the providers array. Adjusted formatting in the controllers array.
.../token/services/token.service.ts Added okp4Service dependency to TokenService. Updated fetchAndCacheTokenInfo method to use okp4Service.getApr() instead of osmosisService.getStakingApr().
.../token/token.module.ts Imported Okp4Service and included it in the providers array. Reformatted the controllers array.

Sequence Diagram(s)

sequenceDiagram
    participant StakingService
    participant Okp4Service
    participant TokenService
    participant CosmosNetwork

    StakingService->>Okp4Service: getApr()
    Okp4Service->>CosmosNetwork: fetch(INFLATION)
    CosmosNetwork-->>Okp4Service: INFLATION Response
    Okp4Service->>CosmosNetwork: fetch(DISTRIBUTION_PARAMS)
    CosmosNetwork-->>Okp4Service: DISTRIBUTION_PARAMS Response
    Okp4Service-->>StakingService: Calculated APR

    TokenService->>Okp4Service: getApr()
    Okp4Service->>CosmosNetwork: fetch(INFLATION)
    CosmosNetwork-->>Okp4Service: INFLATION Response
    Okp4Service->>CosmosNetwork: fetch(DISTRIBUTION_PARAMS)
    CosmosNetwork-->>Okp4Service: DISTRIBUTION_PARAMS Response
    Okp4Service-->>TokenService: Calculated APR
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
src/core/lib/okp4/okp4.service.ts (1)

30-30: Consider using a more precise import for Big.js.

Importing the entire Big.js library globally might lead to larger bundle sizes if this service is used on the client side. Consider importing only the necessary functions if Big.js supports tree-shaking.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e458959 and 257b500.

Files selected for processing (8)
  • src/core/lib/okp4/enums/endpoints.enum.ts (1 hunks)
  • src/core/lib/okp4/okp4.service.ts (2 hunks)
  • src/core/lib/okp4/responses/distribution-params.response.ts (1 hunks)
  • src/core/lib/okp4/responses/inflation.response.ts (1 hunks)
  • src/modules/staking/services/staking.service.ts (3 hunks)
  • src/modules/staking/staking.module.ts (2 hunks)
  • src/modules/token/services/token.service.ts (2 hunks)
  • src/modules/token/token.module.ts (1 hunks)
Files skipped from review due to trivial changes (2)
  • src/core/lib/okp4/enums/endpoints.enum.ts
  • src/core/lib/okp4/responses/distribution-params.response.ts
Additional context used
Biome
src/core/lib/okp4/okp4.service.ts

[error] 175-176: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.


[error] 266-266: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

src/modules/staking/services/staking.service.ts

[error] 477-477: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

Additional comments not posted (7)
src/core/lib/okp4/responses/inflation.response.ts (1)

1-3: Interface definition is correct and aligns with the requirements.

The InflationResponse interface correctly defines the inflation property as a string, which is consistent with the expected data type for inflation values.

src/modules/staking/staking.module.ts (1)

22-22: Simplified controller array is appropriate.

The update to the controllers array to include only StakingController simplifies and clarifies the module's structure.

src/modules/token/token.module.ts (1)

10-10: Correct addition of Okp4Service to imports and providers.

The inclusion of Okp4Service in both imports and providers is essential for enabling the new APR fetching functionality in the TokenModule.

Also applies to: 18-18

src/modules/token/services/token.service.ts (2)

19-19: Appropriate addition of Okp4Service as a dependency.

The inclusion of Okp4Service as a dependency is crucial for enabling APR fetching functionality in the TokenService.

Also applies to: 27-27


100-100: Updated APR fetching logic is correct.

The update to use okp4Service.getApr() for fetching APR in fetchAndCacheTokenInfo correctly reflects the new dependency and functionality.

src/core/lib/okp4/okp4.service.ts (1)

28-30: New imports for handling APR calculations.

The new imports InflationResponse and DistributionParamsResponse are correctly added to handle the responses from the new endpoints. This aligns with the PR's objectives to integrate new endpoints for APR calculations.

src/modules/staking/services/staking.service.ts (1)

156-156: Updated method call to use okp4Service.getApr().

The change to use okp4Service.getApr() instead of osmosisService.getStakingApr() is aligned with the PR's objectives to centralize APR calculations within the Okp4Service. This should ensure that APR values are consistent across different modules.

@yevhen-burkovskyi yevhen-burkovskyi requested a review from DraugS June 24, 2024 10:47
@yevhen-burkovskyi yevhen-burkovskyi self-assigned this Jun 24, 2024
Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 257b500 and e4b5897.

Files selected for processing (1)
  • src/core/lib/okp4/okp4.service.ts (2 hunks)
Additional context used
Biome
src/core/lib/okp4/okp4.service.ts

[error] 175-176: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

Additional comments not posted (1)
src/core/lib/okp4/okp4.service.ts (1)

28-30: Imports for new classes and libraries added.

The addition of InflationResponse, DistributionParamsResponse, and the Big library from "big.js" align with the new functionality introduced in the getApr() method. This ensures proper data handling and calculations.

Comment on lines +252 to +273
async getApr() {
const promises = [
this.getWithErrorHandling(this.constructUrl(Endpoints.INFLATION)),
this.getWithErrorHandling(
this.constructUrl(Endpoints.DISTRIBUTION_PARAMS)
),
this.getStakingPool(),
];
const res = (await Promise.all(promises)) as [
InflationResponse,
DistributionParamsResponse,
StakingPoolResponse
];

if (res[2]?.pool?.bonded_tokens) {
Big(res[0].inflation)
.mul(Big(1).minus(res[1].params.community_tax))
.div(res[2].pool.bonded_tokens)
.toString();
}
return "0";
}
Copy link

Choose a reason for hiding this comment

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

Review of the getApr() method.

The method correctly uses the new endpoints to fetch inflation and distribution parameters. However, there's a potential improvement in how the results are handled:

  1. The method always returns "0" as a string, which might be incorrect if the calculations in lines 266-270 are supposed to affect the return value. This could be a logic error.
  2. Use optional chaining as suggested by static analysis to safely access nested properties.
-    if (res[2] && res[2].pool.bonded_tokens) {
+    if (res[2]?.pool?.bonded_tokens) {
      Big(res[0].inflation)
        .mul(Big(1).minus(res[1].params.community_tax))
        .div(res[2].pool.bonded_tokens)
        .toString();
    }
    return "0";

Committable suggestion was skipped due to low confidence.

@yevhen-burkovskyi yevhen-burkovskyi merged commit df96ca1 into main Jun 24, 2024
11 checks passed
@yevhen-burkovskyi yevhen-burkovskyi deleted the refactor/apr-formula branch June 24, 2024 11:57
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