Skip to content

Latest commit

 

History

History
271 lines (191 loc) · 10.8 KB

File metadata and controls

271 lines (191 loc) · 10.8 KB

client-rust-ws

Rust[1.74.1, stable] Cargo Test Cargo ClippyLicense: MITMSRV

Rust client for Power.Trade WS endpoints (v0.1.7)

Power.Trade WebSocket API documents

See here for WS API documents

Power.Trade Authentication

See here for an explanation of the WS API authentication process @ power.trade.

Content provided (see endpoint links below)

Sample Requests

See this page for sample data feed requests

Setup

  1. Download code from this repo

  2. Install Rust using instructions here OR here

  3. Cd to the folder containing the code downloaded from repo

  4. Run 'build' command:

    cargo build
    

    to update linked libraries and compile code. The version built by default is 'dev'

  5. To build the 'release' version which is more optimized reduced in size run this command

    cargo build --release
    
  6. Before running the code configuration should be added to files (one per env)

    => Running app on Development env requires env file in root folder named ".env.dev". Can be created by copying "env.example" sample file and renaming to ".env.dev" to setup dev env

    ```
    cp .env.example .env.dev
    ```
    

    => Running app on Test env requires env file in root folder named ".env.test". Can be created by copying "env.example" sample file and renaming to ".env.test" to setup test env

    ```
    cp .env.example .env.test
    ```
    
    Once the env file is created add values for:
    * PT_API_KEY - API Key for authentication
    * PT_API_SECRET - API Secret for authentication
    * PT_SERVER_URL - WebSocket server address & port
    * PT_EPOCH_COUNT - Number of cycles the app will run
    * PT_WS_SLEEP - Sleep duration between message checks (in seconds)
    
    API key to be used must be one of the Read-Only ones issued under '' dropdown for API type.
    See example of UI fopr generating API Key below with correct settings for the WS Balance/Position API
    

    image

    => Running app on Prod env requires a .env file in root folder named ".env.prod". This file can be created by copying "env.example" sample file and and renaming to ".env.prod"

    ```
    cp .env.example .env.prod
    ```
    
    Once the env file is created add the same values as mentioned above.
    
  7. Run the code by this command "cargo run -- --env ENV" where ENV is either 'development', 'test' or 'production'

    cargo run -- --env development
    

    or

    cargo run -- --env test
    

    or

    cargo run -- --env production
    
  8. Some debug messages are printed at console, but most are copied to a logging file named "app.log" which is located in the root folder.

  9. App runs for the number of cycles specified in PT_EPOCH_COUNT environment variable as it runs a loop sleeping and checking messages.

Change the 'PT_EPOCH_COUNT' environment value in .env files to make the app run longer or shorter duration.

  1. Step 8 above can be replaced by running the generated binary which will be copied to the "target" folder under project root/home folder.

There will be a "debug" folder generated by running "cargo build" command and a "release" folder created when "cargo build --release" command is run

To run "debug" version execute command

./target/debug/client-rust-ws --env test

in the project root/home folder. The environment can be changed to "development" or "production" also as follows

./target/debug/client-rust-ws --env development

or

./target/debug/client-rust-ws --env production

To run "release" version: execute command

./target/release/client-rust-ws --env test

in project root/home folder. The environment can be changed to "development" or "production" also.

n.b. to view command line options e.g. available settings for environment ("--env") flag run the command without any settings ("./target/debug/client-rust-ws") or with no value added for environment flag ("./target/debug/client-rust-ws --env")

  1. Running the Rust WS Client against various power.trade endpoints

The rust client can be configured to listen for any of the three content types below by setting the environment variable named "PT_SERVER_URL" to the URLs below in the relevant configuration file for development (".env.dev"), test (".env.test") or production (".env.prod").

N.B. Current Rust client only supports one of the three content types per installed Rust code (under /target/release/ folder). Listening for more than one set of content from list below requires multiple copies of the Rust runtime files and a custom configuration per instance.

Testing

The project includes comprehensive unit tests with 19.18% code coverage (47/245 lines covered).

Running Tests

Run all tests with proper test isolation:

cargo test -- --test-threads=1

Run tests with verbose output:

cargo test -- --test-threads=1 --nocapture

Run a specific test:

cargo test test_from_env_success -- --nocapture

Run tests for a specific module:

cargo test config::tests -- --test-threads=1

Test Coverage

Generate a detailed coverage report using cargo-tarpaulin:

  1. Install cargo-tarpaulin (one-time setup):

    cargo install cargo-tarpaulin
  2. Generate coverage report:

    cargo tarpaulin --verbose --timeout 120 -- --test-threads=1
  3. Generate HTML coverage report:

    cargo tarpaulin --out Html --output-dir coverage -- --test-threads=1

    Then open coverage/index.html in your browser.

Current Test Coverage

Module Coverage Lines Covered
src/config.rs 96.7% 29/30
src/utils/mod.rs 22.2% 4/18
src/websocket.rs 23.3% 14/60
src/error.rs 0% 0/13
src/logging.rs 0% 0/17
src/main.rs 0% 0/107
Overall 19.18% 47/245

Test Suites

Configuration Tests (13 tests)

  • Environment variable parsing and validation
  • Credential masking for security
  • Default value handling
  • Error handling for missing/invalid configuration

JWT Generation Tests (3 tests)

  • Invalid private key handling
  • Empty key validation
  • Malformed PEM format detection

WebSocket Client Tests (8 tests)

  • Configuration validation
  • URL format validation
  • Parameter validation (epoch count, sleep duration)

Code Quality

Run linting checks:

cargo clippy --all-targets --all-features -- -D warnings

Format code:

cargo fmt

Check formatting without making changes:

cargo fmt -- --check

Endpoint Links

Balances & Positions

see here for power.trade API docs on balance and position data available from the following test & production API endpoints

see here for details on power.trade's balance & positions response message

Env Link Notes
Test wss://api.wss.test.power.trade/v1/position_summary both balances & positions returned in one response
Production wss://api.wss.prod.power.trade/v1/position_summary both balances & positions returned in one response

Single Leg Orders/RFQs

n.b. to customize or filter the data feed content see parameters documented at this page

Env Link Notes
Test wss://api.wss.test.power.trade/v1/feeds/?type[]=mbp_snapshot&tradeable_type[]=all_single_leg&mbp_period=1&mbo_period=0 link returns all tradeable types with full price book snapshot sent but no order book snapshot
Production wss://api.wss.prod.power.trade/v1/feeds/?type[]=mbp_snapshot&tradeable_type[]=all_single_leg&mbp_period=1&mbo_period=0 link returns all tradeable types with full price book snapshot sent but no order book snapshot

Multi Leg orders/RFQs

n.b. to customize or filter the data feed content see parameters documented at this page

Env Link Notes
Test wss://api.wss.test.power.trade/v1/feeds/multi_leg?type[]=all_multi_leg,multi_leg_mbp_snapshot&mbp_period=1&mbo_period=0 all multi-leg showing price book snapshot but not order book snapshot. add "market_id[]=none" to retireve RFQs
Production wss://api.wss.prod.power.trade/v1/feeds/multi_leg?type[]=all_multi_leg,multi_leg_mbp_snapshot&mbp_period=1&mbo_period=0 all multi-leg showing price book snapshot but not order book snapshot. add "market_id[]=none" to retireve RFQs