Skip to content

feat: calculate spread-based fees for Scrypt trades#3472

Open
TaprootFreak wants to merge 2 commits intodevelopfrom
feature/scrypt-spread-fee
Open

feat: calculate spread-based fees for Scrypt trades#3472
TaprootFreak wants to merge 2 commits intodevelopfrom
feature/scrypt-spread-fee

Conversation

@TaprootFreak
Copy link
Collaborator

Summary

  • Scrypt reports feeAmount=0 for all trades, but the actual cost is the spread between execution price and market rate
  • Adds automatic spread fee calculation using historical prices via getPriceAt() during exchange TX sync
  • Preserves calculated fees across re-syncs (prevents Object.assign from overwriting with DTO's feeAmount=0)

Details

  • New calculateSpreadFee() method in ExchangeTxService
  • Uses AssetPrice table for historical market rates at exact trade date
  • Supports all Scrypt pairs: USDT/CHF, USDT/EUR, BTC/USDT (buy & sell)
  • Fee = amount × (executionPrice − marketRate) in quote currency, converted to CHF

After deploy

One-time resync needed for existing ~741 trades:

await exchangeTxService.syncExchanges(new Date('2026-01-19'), ExchangeName.SCRYPT);

Test plan

  • Verify new Scrypt trades get feeAmount and feeAmountChf populated
  • Verify re-sync does not overwrite calculated fees
  • Run backfill for existing trades and verify values match analysis (~13'800 CHF total)
  • Verify non-Scrypt exchanges are unaffected

Scrypt reports feeAmount=0 for all trades, but the real cost is hidden
in the price spread between execution price and market rate. This adds
automatic spread fee calculation using historical asset prices from the
AssetPrice table via getPriceAt().

After deploy, a one-time resync is needed for existing trades:
  syncExchanges(new Date('2026-01-19'), ExchangeName.SCRYPT)
@TaprootFreak TaprootFreak marked this pull request as ready for review March 20, 2026 07:44
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.

1 participant