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: made small changes to the refactor #241

Merged
merged 1 commit into from
Aug 29, 2024

Conversation

maxrobot
Copy link
Contributor

@maxrobot maxrobot commented Aug 29, 2024

  • See changelog but tl;dr just changed the import paths and added a new function

Summary by CodeRabbit

  • New Features

    • Introduced a new function, get_subaccount_total_value, for calculating the total value of a subaccount in a specific market.
    • Reintroduced the get_spot_market_id function for querying active spot markets.
  • Improvements

    • Enhanced module accessibility by making several modules public, improving usability across the codebase.
    • Restructured import statements for better organization and clarity.
  • Bug Fixes

    • Updated dependencies to newer versions, ensuring compatibility and stability.
  • Documentation

    • Updated changelog to reflect new version entries and changes made.

@maxrobot maxrobot requested a review from jbernal87 August 29, 2024 11:31
Copy link

coderabbitai bot commented Aug 29, 2024

Walkthrough

The updates involve modifications to the Cargo.toml file by changing dependency paths and version numbers, a Docker image upgrade in the build_release.sh script, import path adjustments in various Rust files, and the addition of new functions in the exchange.rs file. Additionally, several modules in the injective-testing package have been made public, enhancing their accessibility. The changelog has also been updated to reflect these changes.

Changes

File Change Summary
Cargo.toml Updated dependency paths for several packages and changed version numbers for injective-test-tube and injective-testing.
build_release.sh Updated Docker image tag from cosmwasm/workspace-optimizer:0.12.11 to cosmwasm/optimizer:0.16.0.
contracts/injective-cosmwasm-mock/src/utils.rs Modified import path for human_to_i64 function.
packages/injective-testing/CHANGELOG.md Added entry for version 1.1.0, noting re-organization of lib.rs.
packages/injective-testing/Cargo.toml Changed version from "1.0.0" to "1.1.0".
packages/injective-testing/src/lib.rs Changed visibility of mocks, multi_test, and test_tube modules from private to public.
packages/injective-testing/src/multi_test/chain_mock.rs Modified import path for InjectiveAddressGenerator.
packages/injective-testing/src/multi_test/mod.rs Changed module declarations to pub mod for address_generator and chain_mock.
packages/injective-testing/src/test_tube/authz.rs Restructured import statements for clarity.
packages/injective-testing/src/test_tube/bank.rs Restructured import statements to enhance organization.
packages/injective-testing/src/test_tube/exchange.rs Added get_subaccount_total_value function and reintroduced get_spot_market_id.
packages/injective-testing/src/test_tube/insurance.rs Restructured import statements for better organization.
packages/injective-testing/src/test_tube/mod.rs Changed module declarations to pub mod for authz, bank, exchange, insurance, oracle, and utils.
packages/injective-testing/src/test_tube/oracle.rs Restructured import statements for clarity and modularity.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Exchange
    participant Subaccount
    participant Market

    User->>Exchange: Query total value
    Exchange->>Subaccount: Retrieve trade deposits
    Subaccount->>Market: Query effective position
    Market-->>Subaccount: Return balance and margin
    Subaccount-->>Exchange: Return total value
    Exchange-->>User: Provide total value
Loading

🐰 In the meadow, changes bloom,
New paths and functions, dispelling gloom.
Modules now public, clear as day,
With each little hop, we find our way!
A dance of code, so bright and spry,
Hooray for updates, oh my, oh my! 🐇✨


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

/ Uint128::one();

let effective_position = exchange
.query_subaccount_effective_position_in_market(&QuerySubaccountEffectivePositionInMarketRequest { market_id, subaccount_id })
Copy link
Contributor

Choose a reason for hiding this comment

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

why dont a wrapper fuction for query_subaccount_effective_position_in_market ?

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 d669ef7 and fc58500.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (14)
  • Cargo.toml (1 hunks)
  • build_release.sh (1 hunks)
  • contracts/injective-cosmwasm-mock/src/utils.rs (1 hunks)
  • packages/injective-testing/CHANGELOG.md (1 hunks)
  • packages/injective-testing/Cargo.toml (1 hunks)
  • packages/injective-testing/src/lib.rs (1 hunks)
  • packages/injective-testing/src/multi_test/chain_mock.rs (1 hunks)
  • packages/injective-testing/src/multi_test/mod.rs (1 hunks)
  • packages/injective-testing/src/test_tube/authz.rs (1 hunks)
  • packages/injective-testing/src/test_tube/bank.rs (1 hunks)
  • packages/injective-testing/src/test_tube/exchange.rs (3 hunks)
  • packages/injective-testing/src/test_tube/insurance.rs (1 hunks)
  • packages/injective-testing/src/test_tube/mod.rs (1 hunks)
  • packages/injective-testing/src/test_tube/oracle.rs (1 hunks)
Files skipped from review due to trivial changes (9)
  • contracts/injective-cosmwasm-mock/src/utils.rs
  • packages/injective-testing/CHANGELOG.md
  • packages/injective-testing/Cargo.toml
  • packages/injective-testing/src/multi_test/chain_mock.rs
  • packages/injective-testing/src/test_tube/authz.rs
  • packages/injective-testing/src/test_tube/bank.rs
  • packages/injective-testing/src/test_tube/insurance.rs
  • packages/injective-testing/src/test_tube/mod.rs
  • packages/injective-testing/src/test_tube/oracle.rs
Additional comments not posted (12)
packages/injective-testing/src/multi_test/mod.rs (2)

1-1: LGTM!

Making the address_generator module public increases its accessibility.

The code changes are approved.


2-2: LGTM!

Making the chain_mock module public increases its accessibility.

The code changes are approved.

packages/injective-testing/src/lib.rs (3)

1-1: LGTM!

Making the mocks module public increases its accessibility.

The code changes are approved.


2-2: LGTM!

Making the multi_test module public increases its accessibility.

The code changes are approved.


3-3: LGTM!

Making the test_tube module public increases its accessibility.

The code changes are approved.

build_release.sh (1)

11-11: LGTM!

Updating the Docker image version may include enhancements, bug fixes, or new features.

The code changes are approved.

Cargo.toml (6)

23-23: LGTM!

The dependency injective-cosmwasm has been updated to include a local path, indicating a shift towards local development.


24-24: LGTM!

The dependency injective-math has been updated to include a local path, indicating a shift towards local development.


25-25: LGTM!

The dependency injective-std has been updated to include a local path, indicating a shift towards local development.


26-26: LGTM!

The dependency injective-std-derive has been updated to include a local path, indicating a shift towards local development.


27-27: LGTM!

The dependency injective-test-tube has been updated to a newer version, ensuring compatibility with newer versions of the dependency.


28-28: LGTM!

The dependency injective-testing has been updated to include a local path and a newer version, indicating a shift towards local development and ensuring compatibility with newer versions of the dependency.

Comment on lines +778 to +789
pub fn get_spot_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
let spot_markets = exchange
.query_spot_markets(&QuerySpotMarketsRequest {
status: "Active".to_string(),
market_ids: vec![],
})
.unwrap()
.markets;

let market = spot_markets.iter().find(|m| m.ticker == ticker).unwrap();

market.market_id.to_string()
Copy link

Choose a reason for hiding this comment

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

Enhance error handling.

The function is correctly implemented but can be improved for error handling. Apply this diff to enhance the function:

pub fn get_spot_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
    let spot_markets = exchange
        .query_spot_markets(&QuerySpotMarketsRequest {
            status: "Active".to_string(),
            market_ids: vec![],
        })
        .unwrap()
        .markets;

    let market = spot_markets.iter().find(|m| m.ticker == ticker).unwrap_or_else(|| {
        panic!("Failed to find spot market with ticker: {}", ticker);
    });

    market.market_id.to_string()
}
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
pub fn get_spot_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
let spot_markets = exchange
.query_spot_markets(&QuerySpotMarketsRequest {
status: "Active".to_string(),
market_ids: vec![],
})
.unwrap()
.markets;
let market = spot_markets.iter().find(|m| m.ticker == ticker).unwrap();
market.market_id.to_string()
pub fn get_spot_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
let spot_markets = exchange
.query_spot_markets(&QuerySpotMarketsRequest {
status: "Active".to_string(),
market_ids: vec![],
})
.unwrap()
.markets;
let market = spot_markets.iter().find(|m| m.ticker == ticker).unwrap_or_else(|| {
panic!("Failed to find spot market with ticker: {}", ticker);
});
market.market_id.to_string()
}

Comment on lines +755 to +775
pub fn get_subaccount_total_value(exchange: &Exchange<InjectiveTestApp>, market_id: String, subaccount_id: String, denom: String) -> Uint128 {
let trade_deposits_during = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
subaccount_id: subaccount_id.clone(),
subaccount: None,
})
.unwrap();

let total_balance = Uint128::from_str(&trade_deposits_during.deposits[&denom].total_balance)
.unwrap_or(Uint128::zero()) // Use zero if the result is an Err
/ Uint128::one();

let effective_position = exchange
.query_subaccount_effective_position_in_market(&QuerySubaccountEffectivePositionInMarketRequest { market_id, subaccount_id })
.unwrap();

let effective_margin = effective_position.state.as_ref().map_or(Uint128::zero(), |state| {
Uint128::from_str(&state.effective_margin).unwrap_or(Uint128::zero())
}) / Uint128::one();

total_balance + effective_margin
Copy link

Choose a reason for hiding this comment

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

Improve readability and error handling.

The function is correctly implemented but can be improved for readability and error handling. Apply this diff to enhance the function:

pub fn get_subaccount_total_value(exchange: &Exchange<InjectiveTestApp>, market_id: String, subaccount_id: String, denom: String) -> Uint128 {
    let trade_deposits_during = exchange
        .query_subaccount_deposits(&QuerySubaccountDepositsRequest {
            subaccount_id: subaccount_id.clone(),
            subaccount: None,
        })
        .unwrap();

    let total_balance = Uint128::from_str(&trade_deposits_during.deposits[&denom].total_balance)
        .unwrap_or_else(|_| {
            eprintln!("Failed to parse total balance for denom: {}", denom);
            Uint128::zero()
        });

    let effective_position = exchange
        .query_subaccount_effective_position_in_market(&QuerySubaccountEffectivePositionInMarketRequest { market_id, subaccount_id })
        .unwrap();

    let effective_margin = effective_position.state.as_ref().map_or(Uint128::zero(), |state| {
        Uint128::from_str(&state.effective_margin).unwrap_or_else(|_| {
            eprintln!("Failed to parse effective margin for market_id: {}", market_id);
            Uint128::zero()
        })
    });

    total_balance + effective_margin
}
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
pub fn get_subaccount_total_value(exchange: &Exchange<InjectiveTestApp>, market_id: String, subaccount_id: String, denom: String) -> Uint128 {
let trade_deposits_during = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
subaccount_id: subaccount_id.clone(),
subaccount: None,
})
.unwrap();
let total_balance = Uint128::from_str(&trade_deposits_during.deposits[&denom].total_balance)
.unwrap_or(Uint128::zero()) // Use zero if the result is an Err
/ Uint128::one();
let effective_position = exchange
.query_subaccount_effective_position_in_market(&QuerySubaccountEffectivePositionInMarketRequest { market_id, subaccount_id })
.unwrap();
let effective_margin = effective_position.state.as_ref().map_or(Uint128::zero(), |state| {
Uint128::from_str(&state.effective_margin).unwrap_or(Uint128::zero())
}) / Uint128::one();
total_balance + effective_margin
pub fn get_subaccount_total_value(exchange: &Exchange<InjectiveTestApp>, market_id: String, subaccount_id: String, denom: String) -> Uint128 {
let trade_deposits_during = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
subaccount_id: subaccount_id.clone(),
subaccount: None,
})
.unwrap();
let total_balance = Uint128::from_str(&trade_deposits_during.deposits[&denom].total_balance)
.unwrap_or_else(|_| {
eprintln!("Failed to parse total balance for denom: {}", denom);
Uint128::zero()
});
let effective_position = exchange
.query_subaccount_effective_position_in_market(&QuerySubaccountEffectivePositionInMarketRequest { market_id, subaccount_id })
.unwrap();
let effective_margin = effective_position.state.as_ref().map_or(Uint128::zero(), |state| {
Uint128::from_str(&state.effective_margin).unwrap_or_else(|_| {
eprintln!("Failed to parse effective margin for market_id: {}", market_id);
Uint128::zero()
})
});
total_balance + effective_margin
}

Copy link
Contributor

@jbernal87 jbernal87 left a comment

Choose a reason for hiding this comment

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

just small comment, but LGTM

@maxrobot maxrobot merged commit b442784 into dev Aug 29, 2024
4 checks passed
@maxrobot maxrobot deleted the f/injective-testing-refactor-2.0 branch August 29, 2024 12:41
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