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
4 changes: 4 additions & 0 deletions src/repositories/wallet.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class WalletRepository {
return this.db.wallet.findUnique({ where: { userId } });
}

async findByPublicKey(publicKey) {
return this.db.wallet.findUnique({ where: { publicKey } });
}

async create(data) {
return this.db.wallet.create({ data });
}
Expand Down
56 changes: 36 additions & 20 deletions src/routes/relayer.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,44 @@ const createRelayerRoutes = ({ escrowService, stellarService, horizonService })
try {
const { actionType, params } = req.body;

if (!params || !params.id) {
return res.status(400).json({ message: 'Escrow ID is required in params' });
}

let unsignedXdr;
let escrowIntentId;
const escrowId = params.id;

switch (actionType) {
case 'LOCK':
({ unsignedXdr, escrowIntentId } = await escrowService.lockEscrow({ escrowId }));
break;
case 'RELEASE':
({ unsignedXdr, escrowIntentId } = await escrowService.releaseEscrow({ escrowId }));
break;
case 'REFUND':
({ unsignedXdr, escrowIntentId } = await escrowService.refundEscrow({ escrowId }));
break;
case 'DISPUTE':
throw new Error('DISPUTE action not yet implemented in service layer.');
default:
throw new Error(`Unsupported actionType: ${actionType}`);

if (actionType === 'CREATE') {
if (!params || !params.buyer || !params.seller || !params.amount) {
return res.status(400).json({ message: 'buyer, seller, and amount are required for CREATE' });
}

const buyerWallet = await walletRepository.findByPublicKey(params.buyer);
const sellerWallet = await walletRepository.findByPublicKey(params.seller);

if (!buyerWallet || !sellerWallet) {
return res.status(400).json({ message: 'Buyer or seller not found in database' });
}

({ unsignedXdr, escrowIntentId } = await escrowService.createEscrow(params));
} else {
if (!params || !params.id) {
return res.status(400).json({ message: 'Escrow ID is required in params' });
}

const escrowId = params.id;

switch (actionType) {
case 'LOCK':
({ unsignedXdr, escrowIntentId } = await escrowService.lockEscrow({ escrowId }));
break;
case 'RELEASE':
({ unsignedXdr, escrowIntentId } = await escrowService.releaseEscrow({ escrowId }));
break;
case 'REFUND':
({ unsignedXdr, escrowIntentId } = await escrowService.refundEscrow({ escrowId }));
break;
case 'DISPUTE':
throw new Error('DISPUTE action not yet implemented in service layer.');
default:
throw new Error(`Unsupported actionType: ${actionType}`);
}
}

const signedXdr = stellarService.signTransaction(unsignedXdr);
Expand Down
2 changes: 1 addition & 1 deletion src/validation/schemas/escrow.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { z } = require('zod');

const submitEscrowSchema = z.object({
body: z.object({
actionType: z.enum(['LOCK', 'RELEASE', 'DISPUTE', 'REFUND']),
actionType: z.enum(['CREATE', 'LOCK', 'RELEASE', 'DISPUTE', 'REFUND']),
params: z.record(z.any()).optional(),
}),
});
Expand Down
49 changes: 48 additions & 1 deletion tests/routes/relayer.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ jest.mock('../../src/services/escrow-funding.service', () => ({
}),
}));

const mockFindByPublicKey = jest.fn();

jest.mock('../../src/repositories/wallet.repository', () => {
return {
WalletRepository: jest.fn().mockImplementation(() => ({
findByPublicKey: mockFindByPublicKey,
})),
};
});

const { createEscrowFundingService } = require('../../src/services/escrow-funding.service');
const escrowFundingServiceMock = createEscrowFundingService();

Expand All @@ -38,7 +48,6 @@ describe('Relayer Routes', () => {
let mockEscrowService;
let mockStellarService;
let mockHorizonService;

beforeEach(() => {
mockEscrowService = {
createEscrow: jest.fn(),
Expand Down Expand Up @@ -72,6 +81,7 @@ describe('Relayer Routes', () => {
});

beforeEach(() => {
mockFindByPublicKey.mockReset();
jest.clearAllMocks();
});

Expand Down Expand Up @@ -139,6 +149,43 @@ describe('Relayer Routes', () => {
expect(mockEscrowService.lockEscrow).toHaveBeenCalledWith({ escrowId: 'intent-123' });
expect(mockEscrowService.recordTransaction).toHaveBeenCalledWith('intent-123', 'tx-456', 'SUCCESS');
});

it('should fail CREATE action if buyer or seller are not in DB', async () => {
const payload = { actionType: 'CREATE', params: { buyer: 'G_BUYER', seller: 'G_SELLER', amount: '100' } };
mockFindByPublicKey.mockResolvedValueOnce(null);

const res = await request(app).post('/api/relayer/submit-escrow').send(payload);

expect(res.status).toBe(400);
expect(res.body.message).toBe('Buyer or seller not found in database');
});

it('should fail CREATE action if params are missing', async () => {
const payload = { actionType: 'CREATE', params: { buyer: 'G_BUYER' } };
const res = await request(app).post('/api/relayer/submit-escrow').send(payload);

expect(res.status).toBe(400);
expect(res.body.message).toBe('buyer, seller, and amount are required for CREATE');
});

it('should call createEscrow and submit for CREATE action', async () => {
const payload = { actionType: 'CREATE', params: { buyer: 'G_BUYER', seller: 'G_SELLER', amount: '100' } };

mockFindByPublicKey.mockResolvedValue({ id: 'w-1', publicKey: 'G_SOMETHING' });

mockEscrowService.createEscrow.mockResolvedValue({ unsignedXdr: 'unsigned_create', escrowIntentId: 'intent-999' });
mockStellarService.signTransaction.mockReturnValue('signed_create');
mockStellarService.submitTransaction.mockResolvedValue({ hash: 'tx-789' });

const res = await request(app).post('/api/relayer/submit-escrow').send(payload);

expect(res.status).toBe(200);
expect(res.body.message).toBe('Escrow CREATE action submitted successfully');
expect(res.body.result).toEqual({ hash: 'tx-789' });

expect(mockEscrowService.createEscrow).toHaveBeenCalledWith(payload.params);
expect(mockEscrowService.recordTransaction).toHaveBeenCalledWith('intent-999', 'tx-789', 'SUCCESS');
});
});

describe('POST /api/relayer/fund', () => {
Expand Down
Loading