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

feat: add transaction stats entity [SF-1135] #96

Closed
wants to merge 3 commits into from

Conversation

leeshuzheng
Copy link
Contributor

No description provided.

Copy link

coderabbitai bot commented Jun 11, 2024

Walkthrough

The recent changes introduce new GraphQL types, TransactionStatsContainer and TransactionStats, with fields to track transaction statistics like currency, maturity, price change, and percentage change. Additionally, a new public function updateTransactionStats is added to calculate and update these statistics based on specified parameters. This enhances the system's ability to manage and analyze transaction data effectively.

Changes

File Change Summary
schema.graphql Introduced TransactionStatsContainer and TransactionStats types with fields for currency, maturity, price change, and percentage change.
src/helper/initializer.ts Added updateTransactionStats function to calculate and update transaction statistics.
src/mappings/lending-market.ts Included a call to updateTransactionStats within the handlePositionUnwound function.

Poem

In code's vast land, new stats arise,
With fields of change and currency ties.
Transactions now with stats so bright,
Calculations done, data takes flight.
A rabbit's joy in every byte,
Enhancing systems with delight.
🌟📊🐇


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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fa9b74a and c76aaab.

Files selected for processing (3)
  • schema.graphql (1 hunks)
  • src/helper/initializer.ts (2 hunks)
  • src/mappings/lending-market.ts (2 hunks)
Additional context used
Biome
src/mappings/lending-market.ts

[error] 1-1: Do not shadow the global "BigInt" property. (lint/suspicious/noShadowRestrictedNames)

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

src/helper/initializer.ts

[error] 4-4: Do not shadow the global "BigInt" property. (lint/suspicious/noShadowRestrictedNames)

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

Additional comments not posted (2)
schema.graphql (1)

160-163: LGTM! The TransactionStatsContainer type is well-defined with appropriate linking to TransactionStats.

src/mappings/lending-market.ts (1)

217-222: Consider verifying the necessity of updateTransactionStats in other event handlers to maintain consistency across the application.

Comment on lines +165 to +166
# TODO: add for APR
# TOOD: add for vol
Copy link

Choose a reason for hiding this comment

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

Please correct the spelling of "TODO" and provide more actionable details.

- # TOOD: add for vol
+ # TODO: Add functionality for calculating volume.

Also applies to: 166-166

Committable suggestion was skipped due to low confidence.

Comment on lines +281 to +339
export function updateTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);

if (currentCandleStick) {
const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400));
const previousCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
previousTimestamp.div(BigInt.fromI32(86400))
);
const previousCandleStick = TransactionCandleStick.load(
previousCandleStickId
);

if (previousCandleStick) {
const currentPrice = currentCandleStick.close;
const previousPrice = previousCandleStick.close;

const priceChange = currentPrice.minus(previousPrice);
const percentageChange = priceChange
.times(BigInt.fromI32(10000))
.div(previousPrice);

const containerId = 'CONTAINER';
let container = TransactionStatsContainer.load(containerId);

if (!container) {
container = new TransactionStatsContainer(containerId);
}

const statsId = currency.toHexString() + '-' + maturity.toString();
let stats = TransactionStats.load(statsId);

if (!stats) {
stats = new TransactionStats(statsId);
stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.save();
}

stats.priceChange = priceChange;
stats.percentageChange = percentageChange.toBigDecimal();
stats.save();
}
}
}
Copy link

Choose a reason for hiding this comment

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

Consider using a dynamic approach for generating containerId to handle multiple containers effectively.

- const containerId = 'CONTAINER';
+ const containerId = generateContainerId(currency, maturity); // Implement this function to generate unique IDs based on currency and maturity.
Committable suggestion

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

Suggested change
export function updateTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);
if (currentCandleStick) {
const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400));
const previousCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
previousTimestamp.div(BigInt.fromI32(86400))
);
const previousCandleStick = TransactionCandleStick.load(
previousCandleStickId
);
if (previousCandleStick) {
const currentPrice = currentCandleStick.close;
const previousPrice = previousCandleStick.close;
const priceChange = currentPrice.minus(previousPrice);
const percentageChange = priceChange
.times(BigInt.fromI32(10000))
.div(previousPrice);
const containerId = 'CONTAINER';
let container = TransactionStatsContainer.load(containerId);
if (!container) {
container = new TransactionStatsContainer(containerId);
}
const statsId = currency.toHexString() + '-' + maturity.toString();
let stats = TransactionStats.load(statsId);
if (!stats) {
stats = new TransactionStats(statsId);
stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.save();
}
stats.priceChange = priceChange;
stats.percentageChange = percentageChange.toBigDecimal();
stats.save();
}
}
}
export function updateTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);
if (currentCandleStick) {
const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400));
const previousCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
previousTimestamp.div(BigInt.fromI32(86400))
);
const previousCandleStick = TransactionCandleStick.load(
previousCandleStickId
);
if (previousCandleStick) {
const currentPrice = currentCandleStick.close;
const previousPrice = previousCandleStick.close;
const priceChange = currentPrice.minus(previousPrice);
const percentageChange = priceChange
.times(BigInt.fromI32(10000))
.div(previousPrice);
const containerId = generateContainerId(currency, maturity); // Implement this function to generate unique IDs based on currency and maturity.
let container = TransactionStatsContainer.load(containerId);
if (!container) {
container = new TransactionStatsContainer(containerId);
}
const statsId = currency.toHexString() + '-' + maturity.toString();
let stats = TransactionStats.load(statsId);
if (!stats) {
stats = new TransactionStats(statsId);
stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.save();
}
stats.priceChange = priceChange;
stats.percentageChange = percentageChange.toBigDecimal();
stats.save();
}
}
}

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c76aaab and 03845a8.

Files selected for processing (1)
  • schema.graphql (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • schema.graphql

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 03845a8 and 3b70d8f.

Files selected for processing (1)
  • schema.graphql (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • schema.graphql

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.

1 participant