Skip to content

Commit

Permalink
feat: added rwa portfolio query
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed Mar 15, 2024
1 parent 9baf4aa commit 40a8442
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 52 deletions.
3 changes: 2 additions & 1 deletion api/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient, Prisma } from '@prisma/client';
import { Level as PinoLevel } from 'pino';
import { getChildLogger } from './logger';
import { getUserCrud, getSessionCrud, getChallengeCrud } from './modules';
import { getUserCrud, getSessionCrud, getChallengeCrud, getRWACRUD } from './modules';
import { getDocumentDriveCRUD } from './modules/document';

const dbLogger = getChildLogger({ msgPrefix: 'DATABASE' });
Expand Down Expand Up @@ -52,6 +52,7 @@ const prisma = prismaBase.$extends({
session: getSessionCrud(prismaBase),
challenge: getChallengeCrud(prismaBase),
document: getDocumentDriveCRUD(prismaBase),
rWAPortfolio: getRWACRUD(prismaBase),
},
});

Expand Down
11 changes: 7 additions & 4 deletions api/src/graphql/generated/drive/nexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export interface NexusGenInputs {
SetStartInput: { // input type
start: string; // String!
}
filterInput: { // input type
assetId?: string | null; // String
}
}

export interface NexusGenEnums {
Expand Down Expand Up @@ -907,7 +910,7 @@ export interface NexusGenFieldTypes {
coreUnits: Array<NexusGenRootTypes['CoreUnit'] | null> | null; // [CoreUnit]
document: NexusGenRootTypes['Document'] | null; // Document
drive: NexusGenRootTypes['DocumentDriveState'] | null; // DocumentDriveState
rwaPortfolio: NexusGenRootTypes['RealWorldAssets'] | null; // RealWorldAssets
rwaPortfolios: Array<NexusGenRootTypes['RealWorldAssetsState'] | null> | null; // [RealWorldAssetsState]
system: NexusGenRootTypes['SwitchboardDrive'] | null; // SwitchboardDrive
}
RealWorldAssets: { // field return type
Expand Down Expand Up @@ -1409,7 +1412,7 @@ export interface NexusGenFieldTypeNames {
coreUnits: 'CoreUnit'
document: 'Document'
drive: 'DocumentDriveState'
rwaPortfolio: 'RealWorldAssets'
rwaPortfolios: 'RealWorldAssetsState'
system: 'SwitchboardDrive'
}
RealWorldAssets: { // field return type name
Expand Down Expand Up @@ -1635,8 +1638,8 @@ export interface NexusGenArgTypes {
document: { // args
id: string; // String!
}
rwaPortfolio: { // args
id: string; // String!
rwaPortfolios: { // args
filter?: NexusGenInputs['filterInput'] | null; // filterInput
}
}
Sync: {
Expand Down
6 changes: 5 additions & 1 deletion api/src/graphql/generated/drive/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ type Query {
coreUnits: [CoreUnit]
document(id: String!): Document
drive: DocumentDriveState
rwaPortfolio(id: String!): RealWorldAssets
rwaPortfolios(filter: filterInput): [RealWorldAssetsState]
system: SwitchboardDrive
}

Expand Down Expand Up @@ -697,4 +697,8 @@ type Vesting {
date: String!
key: String!
vested: Boolean!
}

input filterInput {
assetId: String
}
1 change: 1 addition & 0 deletions api/src/modules/real-world-assets/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './resolvers';
export * from './model';
60 changes: 22 additions & 38 deletions api/src/modules/real-world-assets/listener.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { Prisma } from "@prisma/client";
import { InternalTransmitterUpdate } from "document-drive";
import logger from "../../logger";
import { DocumentDriveDocument, DocumentDriveState, ListenerFilter } from "document-model-libs/document-drive";
import { RealWorldAssets } from "document-model-libs/document-models";


type RWA = typeof RealWorldAssets.Document
const updateOperationalData = async (prisma: Prisma.TransactionClient, data: any) => {

import { ListenerFilter } from "document-model-libs/document-drive";
import { RealWorldAssetsDocument } from "document-model-libs/real-world-assets"
export interface IReceiverOptions {
listenerId: string;
label: string;
block: boolean;
filter: ListenerFilter;
}

const updateAnalyticalData = async (prisma: Prisma.TransactionClient, data: any) => {

export const listener: IReceiverOptions = {
listenerId: "real-world-assets",
filter: {
branch: ["main"],
documentId: ["*"],
documentType: ["makerdao/rwa-portfolio"],
scope: ["*"],
},
block: false,
label: "real-world-assets",
}

export async function transmit(strands: InternalTransmitterUpdate[], prisma: Prisma.TransactionClient) {


export async function transmit(strands: InternalTransmitterUpdate<RealWorldAssetsDocument, "global">[], prisma: Prisma.TransactionClient) {
//todo: work with strands instead of state
for (const strand of strands) {

const { transactions, principalLenderAccountId, fixedIncomeTypes, spvs, accounts, feeTypes, portfolio } = strand.state;
const driveId = strand.driveId;
const documentId = strand.documentId;

// console.log(portfolio)
//todo: check whether all operations can be applied otherwise delete database and insert everything new


// create portfolio document
await prisma.rWAPortfolio.upsert({
Expand All @@ -40,9 +47,7 @@ export async function transmit(strands: InternalTransmitterUpdate[], prisma: Pri
},
});



// // create spvs
// create spvs
await prisma.rWAPortfolioSpv.createMany({
data: spvs.map((spv) => ({ ...spv, driveId: driveId })),
skipDuplicates: true,
Expand Down Expand Up @@ -97,8 +102,6 @@ export async function transmit(strands: InternalTransmitterUpdate[], prisma: Pri
skipDuplicates: true,
});



// fetch portfolio with everything
const portfolioEntity = await prisma.rWAPortfolio.findUnique({
where: {
Expand Down Expand Up @@ -131,25 +134,6 @@ export async function transmit(strands: InternalTransmitterUpdate[], prisma: Pri
}

console.log(data);
// console.log(portfolioEntity.);
}
}

export interface IReceiverOptions {
listenerId: string;
label: string;
block: boolean;
filter: ListenerFilter;
}

export const listener: IReceiverOptions = {
listenerId: "real-world-assets",
filter: {
branch: ["main"],
documentId: ["*"],
documentType: ["makerdao/rwa-portfolio"],
scope: ["*"],
},
block: false,
label: "real-world-assets",
}
35 changes: 35 additions & 0 deletions api/src/modules/real-world-assets/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Prisma } from '@prisma/client';
import { transformPortfolioToState } from './utils';

export function getRWACRUD(prisma: Prisma.TransactionClient) {
return {
findRWAPortfolios: async (where?: Prisma.RWAPortfolioWhereInput) => {
const newWhere: Prisma.RWAPortfolioWhereInput = {
...where
}

try {
const portfolios = await prisma.rWAPortfolio.findMany({
where: newWhere, include: {
spvs: {
include: { spv: true }
},
feeTypes: true,
fixedIncomeTypes: true,
accounts: true,
portfolio: {
include: {
fixedIncomeType: true,
},
where: {}
}
}
})
return transformPortfolioToState(portfolios)
} catch (e) {
console.error(e)
return [];
}
},
};
}
30 changes: 22 additions & 8 deletions api/src/modules/real-world-assets/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { nonNull, objectType, queryField, unionType } from 'nexus';
import { arg, inputObjectType, list, nonNull, objectType, queryField, stringArg, unionType } from 'nexus';
import { documentModelInterface } from '../document';
import { GQLDateBase } from '../system';


export const Account = objectType({
name: 'Account',
definition(t) {
Expand Down Expand Up @@ -243,13 +244,26 @@ export const TransactionFee = objectType({
},
});

export const rwaQuery = queryField('rwaPortfolio', {
type: RealWorldAssetsDocument,
args: {
id: nonNull('String'),
},
resolve: async (_root, { id }, ctx) => {
const doc = await ctx.prisma.document.getDocument(ctx.driveId, id);
// export const filterInput = inputObjectType({
// name: 'filterInput',
// definition(t) {
// t.list.string("assetId_in");
// }
// });

export const rwaQuery = queryField('rwaPortfolios', {
type: list(RealWorldAssetsState),
// args: {
// filter: arg(
// {
// type: filterInput,
// }
// ),
// },
resolve: async (_root, args, ctx) => {
console.log(args);
const doc = await ctx.prisma.rWAPortfolio.findRWAPortfolios({ driveId: ctx.driveId });
return doc;
},
});

33 changes: 33 additions & 0 deletions api/src/modules/real-world-assets/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function transformPortfolioToState(portfolios) {
return portfolios.map(portfolio => ({
id: portfolio.id,
name: portfolio.name,
// spvs: [],
spvs: portfolio.spvs.map(spv => ({
id: spv.spv.id,
name: spv.spv.name
})),
feeTypes: portfolio.feeTypes.map(feeType => ({
id: feeType.id,
name: feeType.name
})),
portfolio: portfolio.portfolio.map(asset => ({
id: asset.id,
purchasePrice: asset.purchasePrice,
purchaseDate: asset.purchaseDate,
name: asset.name,
fixedIncomeType: {
id: asset.fixedIncomeType.id,
name: asset.fixedIncomeType.name
}
})),
// fixedIncomeTypes: portfolio.fixedIncomeTypes.map(fixedIncomeType => ({
// id: fixedIncomeType.id,
// name: fixedIncomeType.name
// })),
// accounts: portfolio.accounts.map(account => ({
// id: account.id,
// name: account.name
// }))
}));
}

0 comments on commit 40a8442

Please sign in to comment.