feat(margin): implement leveraged margin trading with risk management (closes #383)#418
Merged
DevMuhdishaq merged 1 commit intoJun 28, 2026
Conversation
…closes StelTade#383) Add margin pair configuration, position management, automatic liquidation engine, daily interest accrual, and margin call notifications integrated with the existing insurance fund and queue infrastructure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements leveraged/margin trading with comprehensive risk management in SwapTrade-Backend. It adds per-asset-pair margin configuration, leveraged position management, maintenance margin enforcement, automatic liquidation for undercollateralized positions, volatility-based leverage caps, margin call notifications (email/SMS via queue), and daily interest accrual on borrowed funds.
Related Issue
Closes #383
Changes
⚙️ Margin Trading Core
[ADD]
src/margin/services/margin-calculator.service.tsAdded equity, margin ratio, liquidation price, and volatility-adjusted max leverage calculations.
Enforces maintenance margin requirements and margin-call threshold detection.
[ADD]
src/margin/services/margin-position.service.tsAdded open/close position flows with collateral locking against
UserBalance.Rejects positions when available margin is insufficient or leverage exceeds pair limits.
Integrates with the insurance fund via shortfall coverage on liquidation.
[ADD]
src/margin/services/margin-pair-config.service.tsAdded CRUD for per-pair margin parameters (max leverage, initial/maintenance margin, interest rate, volatility caps).
[ADD]
src/margin/services/liquidation-engine.service.tsAdded cron-based monitor (every 5s) that liquidates positions before equity goes negative.
Emits margin call events when positions approach the liquidation threshold.
[ADD]
src/margin/services/margin-interest.service.tsAdded daily interest accrual on borrowed funds with auditable records.
🗄️ Entities & Migration
src/margin/entities/margin-pair-config.entity.ts— per-pair configurable margin parameterssrc/margin/entities/margin-position.entity.ts— leveraged positions with margin, leverage, collateral, and borrowed amount fieldssrc/margin/entities/margin-interest-accrual.entity.ts— auditable daily interest recordssrc/database/migrations/1750200000000-CreateMarginTradingTables.ts— database migration for all margin tables🌐 API Surface
[ADD]
src/margin/margin.controller.tsAdded REST endpoints for pair config management and position open/close/query.
Added effective leverage lookup with volatility adjustment.
[ADD]
src/margin/margin.module.tsAdded dedicated NestJS module wiring entities, services, controller, and listener.
[MODIFY]
src/app.module.tsRegistered
MarginModuleand margin entities in TypeORM configuration.🔔 Notifications & Events
[ADD]
src/margin/listeners/margin.listener.tsDispatches email, SMS (via queue), and in-app notifications on margin calls and liquidations.
[MODIFY]
src/infrastructure/events/domain.events.tsAdded
PositionOpenedEvent,MarginCallEvent, andPositionLiquidatedEvent.[MODIFY]
src/common/enums/notification-event-type.enum.tsAdded
MARGIN_CALLandPOSITION_LIQUIDATEDnotification types.🧪 Tests
src/margin/services/margin-calculator.service.spec.tssrc/margin/services/margin-position.service.spec.tssrc/margin/services/liquidation-engine.service.spec.tssrc/margin/services/margin-interest.service.spec.tsVerification Results