Skip to content

Conversation

abretonc7s
Copy link
Contributor

@abretonc7s abretonc7s commented Sep 1, 2025

Description

Fixed incorrect hardcoded fee display in HyperLiquid trading by implementing dynamic fee calculation that reflects user-specific discounts and fee tiers.

Problem:
HyperLiquid fees were hardcoded at 0.045% for both market and limit orders, causing incorrect fee estimates for users. This didn't account for:

  • Different maker/taker rates (market vs limit orders)
  • Volume-based fee tiers (0-6 tiers based on 14-day volume)
  • Referral code discounts (up to 40%)
  • HYPE token staking discounts (5-40% based on tier)

Solution:
Updated the calculateFees method in HyperLiquidProvider.ts to:

  1. Use correct API fields: Changed from deprecated userFees.feeSchedule.* to userFees.userCrossRate/userAddRate
  2. Apply discounts manually: HyperLiquid API returns base rates, so we manually apply referral + staking discounts (capped at 40%)
  3. Enhanced logging: Added comprehensive DevLogger statements for debugging fee calculations
  4. Better validation: Added fee rate validation before caching

This now provides accurate fee estimates that match what users see in the HyperLiquid UI.

Changelog

CHANGELOG entry: Fixed HyperLiquid fee calculation to show dynamic rates instead of hardcoded 0.045%

Related issues

Fixes: TAT-1440

Manual testing steps

Feature: Dynamic HyperLiquid Fee Calculation

  Scenario: User with no discounts sees base rates
    Given user has no referral code set
    And user has no HYPE staked
    When user views market order fee estimate
    Then fee should show base taker rate (0.045%)
    When user views limit order fee estimate  
    Then fee should show base maker rate (0.015%)

  Scenario: User with referral discount sees reduced rates
    Given user has MetaMask referral code set
    When user views order fee estimate
    Then fee should show reduced rate based on referral discount
    And discount should be visible in logs

  Scenario: User with HYPE staking sees additional discounts
    Given user has HYPE tokens staked
    When user views order fee estimate
    Then fee should show combined referral + staking discounts
    But total discount should not exceed 40%

  Scenario: High volume trader sees better rates
    Given user has high 14-day trading volume
    When user views order fee estimate
    Then fee should show volume-tier based rates
    And rates should be lower than base rates

  Scenario: API error fallback works correctly
    Given HyperLiquid API is unavailable
    When user views order fee estimate
    Then fee should fallback to base rates (0.045%/0.015%)
    And error should be logged but not shown to user

Screenshots/Recordings

Before

  • All orders showed hardcoded 0.045% HyperLiquid fee regardless of order type or user discounts
  • No consideration for referral codes or staking rewards
  • Inaccurate fee estimates compared to actual HyperLiquid UI

After

  • Market orders show dynamic taker rates (e.g., 0.0432% with MetaMask referral)
  • Limit orders show dynamic maker rates (e.g., 0.0144% with MetaMask referral)
  • Fees accurately reflect user's volume tier, referral discounts, and staking rewards
  • Fee estimates match HyperLiquid UI calculations
  • Comprehensive logging for debugging fee calculations
image

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

github-actions bot commented Sep 1, 2025

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@github-actions github-actions bot added the size-M label Sep 1, 2025
@abretonc7s abretonc7s marked this pull request as ready for review September 1, 2025 15:18
@abretonc7s abretonc7s requested a review from a team as a code owner September 1, 2025 15:18
cursor[bot]

This comment was marked as outdated.

Copy link

sonarqubecloud bot commented Sep 1, 2025

@nickewansmith nickewansmith added No QA Needed Apply this label when your PR does not need any QA effort. No E2E Smoke Needed If the PR does not need E2E smoke test run labels Sep 2, 2025
@nickewansmith nickewansmith added this pull request to the merge queue Sep 2, 2025
Merged via the queue into main with commit 54ef9ea Sep 2, 2025
76 of 79 checks passed
@nickewansmith nickewansmith deleted the perps/fix/incorrectfee-tat-1440 branch September 2, 2025 02:25
@github-actions github-actions bot locked and limited conversation to collaborators Sep 2, 2025
@metamaskbot metamaskbot added the release-7.56.0 Issue or pull request that will be included in release 7.56.0 label Sep 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
No E2E Smoke Needed If the PR does not need E2E smoke test run No QA Needed Apply this label when your PR does not need any QA effort. release-7.56.0 Issue or pull request that will be included in release 7.56.0 size-M team-perps
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants