Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions microservices/donation-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:20-alpine
WORKDIR /app
COPY package.json tsconfig.json ./
COPY src ./src
RUN npm ci --production=false || true
RUN npm run build || true
EXPOSE 3011
CMD ["node", "dist/main.js"]
22 changes: 22 additions & 0 deletions microservices/donation-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "donation-service",
"version": "0.1.0",
"private": true,
"scripts": {
"start:dev": "ts-node -r tsconfig-paths/register src/main.ts",
"start": "node dist/main.js",
"build": "tsc -p tsconfig.json",
"test": "echo \"No tests\" && exit 0"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
},
"devDependencies": {
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.6"
}
}
9 changes: 9 additions & 0 deletions microservices/donation-service/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { DonationController } from './donation/donation.controller';
import { DonationService } from './donation/donation.service';

@Module({
controllers: [DonationController],
providers: [DonationService],
})
export class AppModule {}
47 changes: 47 additions & 0 deletions microservices/donation-service/src/donation/donation.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { DonationService } from './donation.service';

@Controller()
export class DonationController {
constructor(private readonly svc: DonationService) {}

@Post('charities')
registerCharity(@Body() body: { name: string; impactDescription: string }) {
return this.svc.registerCharity(body.name, body.impactDescription);
}

@Post('charities/:id/verify')
verifyCharity(@Param('id') id: string) {
return this.svc.verifyCharity(id);
}

@Get('charities')
listCharities() {
return this.svc.listCharities();
}

@Post('donations')
donate(@Body() body: { donorId: string; charityId: string; amount: number; currency?: string; recurring?: boolean; intervalDays?: number }) {
return this.svc.donate(body.donorId, body.charityId, body.amount, body.currency, body.recurring, body.intervalDays);
}

@Post('donations/:id/receipt')
taxReceipt(@Param('id') id: string) {
return this.svc.generateTaxReceipt(id);
}

@Get('donors/:donorId/history')
donorHistory(@Param('donorId') donorId: string) {
return this.svc.getDonorHistory(donorId);
}

@Get('charities/:id/impact')
impact(@Param('id') id: string) {
return this.svc.getImpactReport(id);
}

@Get('charities/:id/quadratic-match')
quadraticMatch(@Param('id') id: string) {
return this.svc.quadraticMatch(id);
}
}
27 changes: 27 additions & 0 deletions microservices/donation-service/src/donation/donation.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface Charity {
id: string;
name: string;
verified: boolean;
totalReceived: number;
impactDescription: string;
}

export interface Donation {
id: string;
donorId: string;
charityId: string;
amount: number;
currency: string;
recurring: boolean;
intervalDays?: number;
nextDueAt?: number;
taxReceiptGenerated: boolean;
createdAt: number;
}

export interface ImpactReport {
charityId: string;
totalDonations: number;
totalAmount: number;
donors: number;
}
76 changes: 76 additions & 0 deletions microservices/donation-service/src/donation/donation.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Charity, Donation, ImpactReport } from './donation.entity';

let idSeq = 1;
const uid = () => String(idSeq++);

@Injectable()
export class DonationService {
private charities = new Map<string, Charity>();
private donations = new Map<string, Donation>();

// --- Charities ---
registerCharity(name: string, impactDescription: string): Charity {
const id = uid();
const charity: Charity = { id, name, verified: false, totalReceived: 0, impactDescription };
this.charities.set(id, charity);
return charity;
}

verifyCharity(id: string): Charity {
const c = this.charities.get(id);
if (!c) throw new NotFoundException('Charity not found');
c.verified = true;
return c;
}

listCharities(): Charity[] {
return [...this.charities.values()];
}

// --- Donations ---
donate(donorId: string, charityId: string, amount: number, currency = 'USD', recurring = false, intervalDays?: number): Donation {
const charity = this.charities.get(charityId);
if (!charity) throw new NotFoundException('Charity not found');
if (!charity.verified) throw new NotFoundException('Charity not verified');

const id = uid();
const now = Date.now();
const donation: Donation = {
id, donorId, charityId, amount, currency, recurring,
intervalDays,
nextDueAt: recurring && intervalDays ? now + intervalDays * 86400_000 : undefined,
taxReceiptGenerated: false,
createdAt: now,
};
this.donations.set(id, donation);
charity.totalReceived += amount;
return donation;
}

generateTaxReceipt(donationId: string): { receipt: string; donation: Donation } {
const d = this.donations.get(donationId);
if (!d) throw new NotFoundException('Donation not found');
d.taxReceiptGenerated = true;
const receipt = `TAX-RECEIPT-${donationId}-${d.amount}${d.currency}-${new Date(d.createdAt).toISOString()}`;
return { receipt, donation: d };
}

getDonorHistory(donorId: string): Donation[] {
return [...this.donations.values()].filter(d => d.donorId === donorId);
}

getImpactReport(charityId: string): ImpactReport {
const all = [...this.donations.values()].filter(d => d.charityId === charityId);
const donors = new Set(all.map(d => d.donorId)).size;
const totalAmount = all.reduce((s, d) => s + d.amount, 0);
return { charityId, totalDonations: all.length, totalAmount, donors };
}

// Quadratic funding: match = sqrt(sum of sqrt(amounts))^2
quadraticMatch(charityId: string): { matched: number } {
const all = [...this.donations.values()].filter(d => d.charityId === charityId);
const sumSqrt = all.reduce((s, d) => s + Math.sqrt(d.amount), 0);
return { matched: Math.round(sumSqrt * sumSqrt * 100) / 100 };
}
}
13 changes: 13 additions & 0 deletions microservices/donation-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: ['error', 'warn', 'log'] });
app.enableShutdownHooks();
const port = process.env.PORT ? Number(process.env.PORT) : 3011;
await app.listen(port);
console.log(`Donation Service listening on port ${port}`);
}

bootstrap();
15 changes: 15 additions & 0 deletions microservices/donation-service/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
"declaration": false,
"sourceMap": true,
"outDir": "dist",
"rootDir": "src",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": ["src/**/*"]
}
8 changes: 8 additions & 0 deletions microservices/marketplace-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:20-alpine
WORKDIR /app
COPY package.json tsconfig.json ./
COPY src ./src
RUN npm ci --production=false || true
RUN npm run build || true
EXPOSE 3012
CMD ["node", "dist/main.js"]
22 changes: 22 additions & 0 deletions microservices/marketplace-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "marketplace-service",
"version": "0.1.0",
"private": true,
"scripts": {
"start:dev": "ts-node -r tsconfig-paths/register src/main.ts",
"start": "node dist/main.js",
"build": "tsc -p tsconfig.json",
"test": "echo \"No tests\" && exit 0"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
},
"devDependencies": {
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.6"
}
}
9 changes: 9 additions & 0 deletions microservices/marketplace-service/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { MarketplaceController } from './marketplace/marketplace.controller';
import { MarketplaceService } from './marketplace/marketplace.service';

@Module({
controllers: [MarketplaceController],
providers: [MarketplaceService],
})
export class AppModule {}
13 changes: 13 additions & 0 deletions microservices/marketplace-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: ['error', 'warn', 'log'] });
app.enableShutdownHooks();
const port = process.env.PORT ? Number(process.env.PORT) : 3012;
await app.listen(port);
console.log(`Marketplace Service listening on port ${port}`);
}

bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { MarketplaceService } from './marketplace.service';

@Controller()
export class MarketplaceController {
constructor(private readonly svc: MarketplaceService) {}

@Post('listings')
createListing(@Body() body: { nftId: string; sellerId: string; price: number; currency?: string; royaltyPercent?: number; creatorId: string; auctionDurationMs?: number }) {
return this.svc.createListing(body.nftId, body.sellerId, body.price, body.currency, body.royaltyPercent, body.creatorId, body.auctionDurationMs);
}

@Get('listings')
listActive() {
return this.svc.listActive();
}

@Get('listings/:id')
getListing(@Param('id') id: string) {
return this.svc.getListing(id);
}

@Post('listings/:id/cancel')
cancelListing(@Param('id') id: string, @Body() body: { sellerId: string }) {
return this.svc.cancelListing(id, body.sellerId);
}

@Post('listings/:id/buy')
buy(@Param('id') id: string, @Body() body: { buyerId: string }) {
return this.svc.buy(id, body.buyerId);
}

@Post('offers')
makeOffer(@Body() body: { listingId: string; buyerId: string; amount: number }) {
return this.svc.makeOffer(body.listingId, body.buyerId, body.amount);
}

@Post('offers/:id/respond')
respondOffer(@Param('id') id: string, @Body() body: { action: 'accept' | 'reject' | 'counter'; counterAmount?: number }) {
return this.svc.respondOffer(id, body.action, body.counterAmount);
}

@Post('listings/:id/bid')
placeBid(@Param('id') id: string, @Body() body: { bidderId: string; amount: number }) {
return this.svc.placeBid(id, body.bidderId, body.amount);
}

@Post('listings/:id/settle')
settleAuction(@Param('id') id: string) {
return this.svc.settleAuction(id);
}

@Get('nfts/:nftId/price-history')
priceHistory(@Param('nftId') nftId: string) {
return this.svc.getPriceHistory(nftId);
}

@Get('collections/:creatorId/stats')
collectionStats(@Param('creatorId') creatorId: string) {
return this.svc.getCollectionStats(creatorId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export interface Listing {
id: string;
nftId: string;
sellerId: string;
price: number;
currency: string;
royaltyPercent: number;
creatorId: string;
status: 'active' | 'sold' | 'cancelled';
auctionEndsAt?: number;
highestBid?: number;
highestBidder?: string;
createdAt: number;
}

export interface Offer {
id: string;
listingId: string;
buyerId: string;
amount: number;
status: 'pending' | 'accepted' | 'rejected' | 'countered';
counterAmount?: number;
createdAt: number;
}

export interface Transaction {
id: string;
listingId: string;
buyerId: string;
sellerId: string;
nftId: string;
price: number;
royaltyPaid: number;
creatorId: string;
executedAt: number;
}

export interface PriceHistory {
nftId: string;
price: number;
at: number;
}
Loading
Loading