Skip to content

fundingRateService

sequenceDiagram
    actor U as User
    participant FR as Funding Rate Service
    participant BQ as Big Query
    participant RD as Redis
    participant SQL as MySQL
    participant SEQ as Sequencer

    Note over FR: Minutes 1-59
    FR->>BQ: Store funding rate (1 min intervals)
    FR->>RD: Update cumulative 1hr window funding rate & timestamp

    Note over FR: Minute 60 - Account Update Cycle
    FR->>BQ: Store funding rate (1 min intervals)
    FR->>SQL: Begin account updates cycle
    loop For each user account
        FR->>SQL: Update accrued_funding (1hr lookback)
    end
    FR->>RD: Reset cumulative funding

    Note over U,SEQ: Trade Flow Paths
    U->>SEQ: Submit Trade

    alt Position Closure
        SEQ->>SQL: Reset accrued_funding to 0 (if applicable)
        SEQ->>SQL: Update realized_pnl (net of funding and fees)
        SEQ->>BQ: Process trade history
        SEQ->>SEQ: Update smart contract positions
    else Partial Close
        SEQ->>SQL: Reduce accrued_funding pro rata to closed size
        SEQ->>SQL: Update realized_pnl (net of funding and fees)
        SEQ->>BQ: Process trade history
        SEQ->>SEQ: Update smart contract positions
    else Position Flip
        SEQ->>SEQ: Follow Position Closure flow for existing position
    end