Skip to content

Commit f86c4fd

Browse files
ah, les mineur de cripto d'une entreprise doivent génrer de du bitcoin o
1 parent 77cd541 commit f86c4fd

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/app/companies/[companyId]/page.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { notFound } from 'next/navigation';
33
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card";
44
import { Badge } from '@/components/ui/badge';
55
import { Button } from '@/components/ui/button';
6-
import { ArrowLeft, Landmark, Users, DollarSign, LineChart, Briefcase, Percent, Package, Cpu, Settings } from 'lucide-react';
6+
import { ArrowLeft, Landmark, Users, DollarSign, LineChart, Briefcase, Percent, Package, Cpu, Settings, Server, Bitcoin } from 'lucide-react';
77
import Link from 'next/link';
88
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
99
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
@@ -70,7 +70,7 @@ export default async function CompanyDetailPage({ params }: { params: { companyI
7070
</div>
7171
</div>
7272

73-
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-4">
73+
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
7474
<Card>
7575
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
7676
<CardTitle className="text-sm font-medium">Trésorerie</CardTitle>
@@ -111,6 +111,26 @@ export default async function CompanyDetailPage({ params }: { params: { companyI
111111
<p className="text-xs text-muted-foreground">Personnes gérant l'entreprise</p>
112112
</CardContent>
113113
</Card>
114+
<Card>
115+
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
116+
<CardTitle className="text-sm font-medium">Valeur du Matériel de Minage</CardTitle>
117+
<Server className="h-4 w-4 text-muted-foreground" />
118+
</CardHeader>
119+
<CardContent>
120+
<div className="text-2xl font-bold">${company.miningRigsValue.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
121+
<p className="text-xs text-muted-foreground">Valeur totale du matériel détenu</p>
122+
</CardContent>
123+
</Card>
124+
<Card>
125+
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
126+
<CardTitle className="text-sm font-medium">Récompenses BTC non réclamées</CardTitle>
127+
<Bitcoin className="h-4 w-4 text-muted-foreground" />
128+
</CardHeader>
129+
<CardContent>
130+
<div className="text-2xl font-bold">{company.unclaimedBtc.toFixed(8)} BTC</div>
131+
<p className="text-xs text-muted-foreground">Généré par les opérations de minage</p>
132+
</CardContent>
133+
</Card>
114134
</div>
115135

116136
<div className="grid grid-cols-1 gap-6 lg:grid-cols-2">

src/lib/actions/companies.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,31 @@ export async function getCompanyById(companyId: number) {
188188
return null;
189189
}
190190

191+
// --- OFFLINE MINING CALCULATION FOR COMPANY ---
192+
const totalCompanyHashRate = company.miningRigs.reduce((total, ownedRig) => {
193+
const rigData = getRigById(ownedRig.rigId);
194+
return total + (rigData?.hashRateMhs || 0) * ownedRig.quantity;
195+
}, 0);
196+
197+
let finalUnclaimedBtc = parseFloat(company.unclaimedBtc);
198+
199+
if (totalCompanyHashRate > 0) {
200+
const now = new Date();
201+
const lastUpdate = new Date(company.lastMiningUpdateAt);
202+
const secondsElapsed = (now.getTime() - lastUpdate.getTime()) / 1000;
203+
204+
if (secondsElapsed > 1) {
205+
const BTC_PER_MHS_PER_SECOND = 7.7e-12; // Same as user's
206+
const earnedOffline = totalCompanyHashRate * BTC_PER_MHS_PER_SECOND * secondsElapsed;
207+
finalUnclaimedBtc += earnedOffline;
208+
209+
await db.update(companies)
210+
.set({ unclaimedBtc: finalUnclaimedBtc.toString(), lastMiningUpdateAt: now })
211+
.where(eq(companies.id, company.id));
212+
}
213+
}
214+
// --- END CALCULATION ---
215+
191216
// Fetch all asset prices for valuation
192217
const allAssets = await db.query.assets.findMany();
193218
const priceMap = allAssets.reduce((map, asset) => {
@@ -200,8 +225,15 @@ export async function getCompanyById(companyId: number) {
200225
return sum + (parseFloat(holding.quantity) * currentPrice);
201226
}, 0);
202227

228+
const miningRigsValue = company.miningRigs.reduce((total, ownedRig) => {
229+
const rigData = getRigById(ownedRig.rigId);
230+
return total + (rigData?.price || 0) * ownedRig.quantity;
231+
}, 0);
232+
233+
const unclaimedBtcValue = finalUnclaimedBtc * (priceMap['BTC'] || 0);
234+
203235
const companyCash = parseFloat(company.cash);
204-
const companyValue = companyCash + portfolioValue;
236+
const companyValue = companyCash + portfolioValue + miningRigsValue + unclaimedBtcValue;
205237
const totalShares = parseFloat(company.totalShares);
206238
const sharePrice = totalShares > 0 ? companyValue / totalShares : parseFloat(company.sharePrice);
207239

@@ -211,6 +243,8 @@ export async function getCompanyById(companyId: number) {
211243
sharePrice: sharePrice,
212244
totalShares: totalShares,
213245
marketCap: companyValue,
246+
miningRigsValue: miningRigsValue,
247+
unclaimedBtc: finalUnclaimedBtc,
214248
shares: company.shares.map(s => ({...s, quantity: parseFloat(s.quantity)})),
215249
holdings: company.holdings.map(h => ({
216250
...h,

src/lib/db/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ export const companies = pgTable('companies', {
189189
creatorId: integer('creator_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
190190
sharePrice: numeric('share_price', { precision: 10, scale: 2 }).default('0.10').notNull(),
191191
totalShares: numeric('total_shares', { precision: 20, scale: 8 }).default('10000.00').notNull(),
192+
unclaimedBtc: numeric('unclaimed_btc', { precision: 18, scale: 8 }).default('0').notNull(),
193+
lastMiningUpdateAt: timestamp('last_mining_update_at', { withTimezone: true }).defaultNow().notNull(),
192194
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
193195
});
194196

0 commit comments

Comments
 (0)