Skip to content

Commit

Permalink
feat: rwa listener core logic
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed Mar 15, 2024
1 parent d9720d4 commit 62eb5d9
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 197 deletions.
2 changes: 2 additions & 0 deletions api/logger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LoggerConfig } from './src/types';
export const defaultLoggerConfig: LoggerConfig = {
// Filter by module name
moduleFilter: [],
debugModules: [],
// Filter by log prefix, e.g. adding
// `pref` to filter is going to correspond to showing `[PREF] ` logs
prefixFilter: [],
Expand All @@ -17,6 +18,7 @@ export const defaultLoggerConfig: LoggerConfig = {
export const debugLogConfig: LoggerConfig = {
// Filter by module name
moduleFilter: [],
debugModules: [],
// Filter by log prefix, e.g. adding
// `pref` to filter is going to correspond to showing `[PERF]` logs
prefixFilter: [],
Expand Down
130 changes: 71 additions & 59 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,36 @@ model Listener {

// RWA Operational Data
model RWAPortfolio {
id String
driveId String
accounts RWAAccountOnPortfolio[]
principalLenderAccountId String
spvs RWAPortfolioSpvOnPortfolio[]
feeTypes RWAPortfolioServiceProviderOnPortfolio[]
fixedIncomeTypes RWAPortfolioFixedIncomeTypeOnPortfolio[]
portfolio RWAPortfolioAsset[]
@@id([id, driveId])
id String @id @default(uuid())
driveId String
documentId String
accounts RWAAccountOnPortfolio[]
principalLenderAccountId String
spvs RWAPortfolioSpvOnPortfolio[]
feeTypes RWAPortfolioServiceProviderOnPortfolio[]
fixedIncomeTypes RWAPortfolioFixedIncomeTypeOnPortfolio[]
portfolio RWAPortfolioAsset[]
RWAPortfolioAccount RWAPortfolioAccount[]
RWAPortfolioFixedIncomeType RWAPortfolioFixedIncomeType[]
RWAPortfolioSpv RWAPortfolioSpv[]
RWAPortfolioServiceProvider RWAPortfolioServiceProvider[]
RWABaseTransaction RWABaseTransaction[]
RWAGroupTransaction RWAGroupTransaction[]
RWABaseTransactionOnGroupTransaction RWABaseTransactionOnGroupTransaction[]
@@unique([driveId, documentId])
}

model RWAPortfolioAccount {
id String
driveId String
id String @default(uuid())
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
reference String
label String?
RWAAccountOnPortfolio RWAAccountOnPortfolio[]
RWAPortfolioServiceProvider RWAPortfolioServiceProvider[]
@@id([id, driveId])
@@id([id, portfolioId])
}

enum RWAPortfolioAssetType {
Expand All @@ -152,9 +161,12 @@ enum RWAPortfolioAssetType {
}

model RWAPortfolioAsset {
driveId String
id String @default(uuid())
assetRefId String
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
fixedIncomeTypeId String?
fixedIncomeType RWAPortfolioFixedIncomeType? @relation(fields: [fixedIncomeTypeId, portfolioId], references: [id, portfolioId])
name String?
spvId String
maturity String?
Expand All @@ -167,89 +179,88 @@ model RWAPortfolioAsset {
ISIN String?
CUSIP String?
coupon Float?
portfolio RWAPortfolio @relation(fields: [portfolioId, driveId], references: [id, driveId])
portfolioId String
currency String?
assetType String
fixedIncomeType RWAPortfolioFixedIncomeType? @relation(fields: [fixedIncomeTypeId, driveId], references: [id, driveId])
@@id([id, driveId])
currency String?
assetType String
@@id([id, portfolioId]) // TODO: Change to id,
@@unique([assetRefId, portfolioId])
}

model RWAPortfolioFixedIncomeType {
id String
driveId String
id String @default(uuid())
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
name String
RWAPortfolioFixedIncomeTypeOnPortfolio RWAPortfolioFixedIncomeTypeOnPortfolio[]
RWAPortfolioAsset RWAPortfolioAsset[]
@@id([id, driveId])
@@id([id, portfolioId])
}

model RWAPortfolioSpv {
id String
driveId String
id String @default(uuid())
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
name String
RWAPortfolioSpvOnPortfolio RWAPortfolioSpvOnPortfolio[]
RWAPortfolioServiceProviderOnPortfolio RWAPortfolioServiceProviderOnPortfolio[]
@@id([id, driveId])
@@id([id, portfolioId])
}

model RWAPortfolioSpvOnPortfolio {
driveId String
portfolioId String
spvId String
portfolio RWAPortfolio @relation(fields: [portfolioId, driveId], references: [id, driveId])
spv RWAPortfolioSpv @relation(fields: [spvId, driveId], references: [id, driveId])
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
spv RWAPortfolioSpv @relation(fields: [spvId, portfolioId], references: [id, portfolioId])
@@id([spvId, portfolioId, driveId])
@@id([spvId, portfolioId])
}

model RWAAccountOnPortfolio {
driveId String
portfolioId String
accountId String
portfolio RWAPortfolio @relation(fields: [portfolioId, driveId], references: [id, driveId])
account RWAPortfolioAccount @relation(fields: [accountId, driveId], references: [id, driveId])
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
account RWAPortfolioAccount @relation(fields: [accountId, portfolioId], references: [id, portfolioId])
@@id([accountId, portfolioId, driveId])
@@id([accountId, portfolioId])
}

model RWAPortfolioServiceProvider {
id String @default(uuid())
driveId String
name String
feeType String
accountId String
account RWAPortfolioAccount @relation(fields: [accountId, driveId], references: [id, driveId])
id String @default(uuid())
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
name String
feeType String
accountId String
account RWAPortfolioAccount @relation(fields: [accountId, portfolioId], references: [id, portfolioId])
@@id([id, driveId])
@@id([id, portfolioId])
}

model RWAPortfolioServiceProviderOnPortfolio {
driveId String
portfolioId String
spvId String
portfolio RWAPortfolio @relation(fields: [portfolioId, driveId], references: [id, driveId])
spv RWAPortfolioSpv @relation(fields: [spvId, driveId], references: [id, driveId])
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
spv RWAPortfolioSpv @relation(fields: [spvId, portfolioId], references: [id, portfolioId])
@@id([spvId, portfolioId, driveId])
@@id([spvId, portfolioId])
}

model RWAPortfolioFixedIncomeTypeOnPortfolio {
driveId String
portfolioId String
fixedIncomeTypeId String
portfolio RWAPortfolio @relation(fields: [portfolioId, driveId], references: [id, driveId])
fixedIncome RWAPortfolioFixedIncomeType @relation(fields: [fixedIncomeTypeId, driveId], references: [id, driveId])
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
fixedIncome RWAPortfolioFixedIncomeType @relation(fields: [fixedIncomeTypeId, portfolioId], references: [id, portfolioId])
@@id([fixedIncomeTypeId, portfolioId, driveId])
@@id([fixedIncomeTypeId, portfolioId])
}

model RWABaseTransaction {
driveId String
id String
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
assetId String
amount Float
entryTime DateTime?
Expand All @@ -262,31 +273,32 @@ model RWABaseTransaction {
fixedIncomeTransactions RWAGroupTransaction[] @relation(name: "fixedIncomeTransaction")
feeTransactions RWABaseTransactionOnGroupTransaction[]
@@id([id, driveId])
@@id([id, portfolioId])
}

model RWAGroupTransaction {
driveId String
id String
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
type String
cashTransaction RWABaseTransaction? @relation(name: "cashTransaction", fields: [cashTransactionId, driveId], references: [id, driveId])
cashTransaction RWABaseTransaction? @relation(name: "cashTransaction", fields: [cashTransactionId, portfolioId], references: [id, portfolioId])
cashTransactionId String?
fixedIncomeTransaction RWABaseTransaction? @relation(name: "fixedIncomeTransaction", fields: [fixedTransactionId, driveId], references: [id, driveId])
fixedIncomeTransaction RWABaseTransaction? @relation(name: "fixedIncomeTransaction", fields: [fixedTransactionId, portfolioId], references: [id, portfolioId])
fixedTransactionId String?
feeTransactions RWABaseTransactionOnGroupTransaction[]
@@id([id, driveId])
@@id([id, portfolioId])
}

model RWABaseTransactionOnGroupTransaction {
driveId String
id String @default(uuid())
portfolioId String
portfolio RWAPortfolio @relation(fields: [portfolioId], references: [id], onDelete: Cascade)
groupTransactionId String
baseTransactionId String
groupTransaction RWAGroupTransaction @relation(fields: [groupTransactionId, driveId], references: [id, driveId])
baseTransaction RWABaseTransaction @relation(fields: [baseTransactionId, driveId], references: [id, driveId])
groupTransaction RWAGroupTransaction @relation(fields: [groupTransactionId, portfolioId], references: [id, portfolioId])
baseTransaction RWABaseTransaction @relation(fields: [baseTransactionId, portfolioId], references: [id, portfolioId])
@@id([id, driveId])
@@id([id, portfolioId])
}
3 changes: 2 additions & 1 deletion api/src/modules/document-drive/drive-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { OperationScope } from 'document-model/document';
import stringify from 'json-stringify-deterministic';
import { getChildLogger } from '../../logger';

const logger = getChildLogger({ msgPrefix: 'Drive' });
const logger = getChildLogger({ msgPrefix: 'Drive Resolver' });

export const Node = objectType({
name: 'Node',
Expand Down Expand Up @@ -275,6 +275,7 @@ export const pushUpdates = mutationField('pushUpdates', {
strands: list(nonNull(InputStrandUpdate)),
},
resolve: async (_parent, { strands }, ctx) => {
logger.info('pushUpdates')
if (!strands || strands?.length === 0) return [];

const listenerRevisions: IListenerRevision[] = await Promise.all(strands.map(async (s) => {
Expand Down
8 changes: 7 additions & 1 deletion api/src/modules/document/listenerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { DocumentDriveServer, IReceiver, InternalTransmitter, InternalTransmitte
import { Listener, DocumentDriveDocument } from 'document-model-libs/document-drive';
import { Document, OperationScope } from "document-model/document"
import { Prisma } from '@prisma/client';
import { getChildLogger } from '../../logger';

const logger = getChildLogger({ msgPrefix: 'Listener Manager' });

const listeners: Promise<any>[] = [];
function loadModules(startPath: string, filter: string): Promise<any>[] {
Expand All @@ -19,6 +22,7 @@ function loadModules(startPath: string, filter: string): Promise<any>[] {
if (stat.isDirectory()) {
loadModules(filename, filter); //recursive
} else if (filename.endsWith(filter)) {
logger.info(`Loading listener from ${filename}`);
listeners.push(import(filename));
};
};
Expand All @@ -40,6 +44,8 @@ async function registerListener(driveServer: DocumentDriveServer, driveId: strin
block: false,
label: listener.label!,
})

logger.info(`Listener ${listener.label}(${listener.listenerId}) registered for drive ${driveId}`);
}

export async function init(driveServer: DocumentDriveServer, prisma: Prisma.TransactionClient) {
Expand All @@ -59,12 +65,12 @@ export async function init(driveServer: DocumentDriveServer, prisma: Prisma.Tran

const transmitter = (await driveServer.getTransmitter(driveId, listener.listenerId));
if (transmitter instanceof InternalTransmitter) {
logger.info(`Setting receiver for ${listener.listenerId}`);
transmitter.setReceiver({
transmit: async (strands: InternalTransmitterUpdate<Document, OperationScope>[]) => {
transmit(strands, prisma)
}
})

}
}
}
Expand Down
11 changes: 7 additions & 4 deletions api/src/modules/document/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Prisma } from '@prisma/client';
import type { Prisma, PrismaClient } from '@prisma/client';
import {
DocumentDriveServer,
DriveInput,
Expand All @@ -23,10 +23,11 @@ import {


import { actions as rwaActions } from 'document-model-libs/dist/real-world-assets'
import logger from '../../logger';
import { init } from './listenerManager';

import { init } from './listenerManager';
import { getChildLogger } from '../../logger';

const logger = getChildLogger({ msgPrefix: 'Document Model' });
export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
const documentModels = [
DocumentModelLib,
Expand All @@ -35,7 +36,7 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {

const driveServer = new DocumentDriveServer(
documentModels,
new PrismaStorage(prisma),
new PrismaStorage(prisma as PrismaClient),
);

async function initialize() {
Expand Down Expand Up @@ -89,13 +90,15 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
documentId?: string,
) => {
if (!documentId) {
logger.info('adding drive operations')
const result = await driveServer.addDriveOperations(
driveId,
operations,
);

return result;
}
logger.info('adding operations to document')
const result = await driveServer.addOperations(
driveId,
documentId,
Expand Down
Loading

0 comments on commit 62eb5d9

Please sign in to comment.