diff --git a/src/app/api/funds/[slug]/invest/route.ts b/src/app/api/funds/[slug]/invest/route.ts index ef0bfb3..1beff46 100644 --- a/src/app/api/funds/[slug]/invest/route.ts +++ b/src/app/api/funds/[slug]/invest/route.ts @@ -45,12 +45,12 @@ export async function POST(req: NextRequest, { params }: { params: { slug: strin const portfolioValue = fund.user.positions.reduce((sum, p) => { const val = p.positionType === 'LONG' ? p.shares * p.hashtag.currentPrice - : p.avgBuyPrice * p.shares - (p.hashtag.currentPrice - p.avgBuyPrice) * p.shares + : (2 * p.avgBuyPrice - p.hashtag.currentPrice) * p.shares return sum + val }, 0) const totalValue = fund.user.balance + portfolioValue const nav = calcFundNav(totalValue, fund.sharesOutstanding) - const sharesToMint = amount / nav + const sharesToMint = Math.round((amount / nav) * 1e6) / 1e6 // Weighted average NAV at buy for display const existingInvestment = await prisma.fundInvestment.findUnique({ diff --git a/src/app/api/funds/[slug]/redeem/route.ts b/src/app/api/funds/[slug]/redeem/route.ts index db39638..05d5c06 100644 --- a/src/app/api/funds/[slug]/redeem/route.ts +++ b/src/app/api/funds/[slug]/redeem/route.ts @@ -45,7 +45,7 @@ export async function POST(req: NextRequest, { params }: { params: { slug: strin const portfolioValue = fund.user.positions.reduce((sum, p) => { const val = p.positionType === 'LONG' ? p.shares * p.hashtag.currentPrice - : p.avgBuyPrice * p.shares - (p.hashtag.currentPrice - p.avgBuyPrice) * p.shares + : (2 * p.avgBuyPrice - p.hashtag.currentPrice) * p.shares return sum + val }, 0) const totalValue = fund.user.balance + portfolioValue @@ -56,7 +56,7 @@ export async function POST(req: NextRequest, { params }: { params: { slug: strin return NextResponse.json({ error: 'Fund has insufficient cash to redeem. Try a smaller amount.' }, { status: 400 }) } - const remainingShares = investment.shares - sharesToRedeem + const remainingShares = Math.round((investment.shares - sharesToRedeem) * 1e6) / 1e6 const [updatedInvestor] = await prisma.$transaction([ // Return cash to investor