BitMEX and Hyperliquid have fairly detailed documentation about how they implement perps and there are probably open source projects out there.