Skip to content

Fix/settlement pnl calculation#247

Open
Sundayabel222 wants to merge 3 commits intoNetwalls:mainfrom
Sundayabel222:fix/settlement-pnl-calculation
Open

Fix/settlement pnl calculation#247
Sundayabel222 wants to merge 3 commits intoNetwalls:mainfrom
Sundayabel222:fix/settlement-pnl-calculation

Conversation

@Sundayabel222
Copy link

Closes #92

@GoSTEAN Pls Review Pr

i Just Fixed settle Predictions — hardcoded PnL calculation

- Add trading.service.ts with buyShares() method for acquiring shares
- Implement slippage protection with configurable min shares parameter (5% default)
- Create Trade entity records with CONFIRMED status for all buy transactions
- Add Share entity creation and updates with cost basis tracking
- Update market totalVolume on each buy transaction atomically
- Add trading.controller.ts with POST /buy endpoint for HTTP requests
- Implement trading routes for buy/sell/odds endpoints
- Integrate with Stellar AMM contract for price execution
- Use Prisma transactions for data consistency and atomicity
- Track user USDC balance changes and position updates
- Add comprehensive error handling (insufficient balance, closed markets, slippage)
- Add 17 passing integration tests covering all acceptance criteria
- All tests verified: buy flow, validation, database updates, share positions

Acceptance Criteria Met:
 Create trading.service.ts with buyShares() method
 Include slippage protection (min shares param)
 Record Trade entity in database
 Update/create Share entity
 Update market volume
 Create trading routes and controller
 Unit + integration tests (17 tests passing)
- Replace hardcoded 90% return with trade-based calculation
- Winners receive 1 USDC per share minus configurable platform fees
- PnL now reflects actual entry price from AMM odds at time of trade
- Add configurable PLATFORM_FEE_PERCENTAGE (default 2%)
- Include fallback mechanisms for predictions without trade data
- Update e2e test to match new calculation logic
- Add comprehensive documentation in SETTLEMENT_PNL_FIX.md

This ensures fair payouts based on actual risk/reward at entry time.
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.

[Backend] Fix settlePredictions — hardcoded PnL calculation

1 participant