This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(position-presenter): Add concept of position presenter (#1220)
- Loading branch information
Showing
18 changed files
with
263 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { applyDecorators, SetMetadata } from '@nestjs/common'; | ||
|
||
export const BALANCE_PRODUCT_META_SELECTOR = 'BALANCE_PRODUCT_META_SELECTOR'; | ||
|
||
export const BalanceProductMeta = (groupSelector: string) => { | ||
return applyDecorators(SetMetadata(BALANCE_PRODUCT_META_SELECTOR, groupSelector)); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
import { MetadataItemWithLabel } from '~balance/balance-fetcher.interface'; | ||
import { PositionBalance } from '~position/position-balance.interface'; | ||
import { ContractPositionBalance, TokenBalance } from '~position/position-balance.interface'; | ||
|
||
import { TokenBalanceResponse } from './balance-fetcher.interface'; | ||
|
||
export interface BalancePresenter { | ||
present(address: string, balances: PositionBalance[]): Promise<TokenBalanceResponse>; | ||
} | ||
|
||
export type ReadonlyBalances = ReadonlyArray<Readonly<Balance>>; | ||
export type GroupMeta = MetadataItemWithLabel[]; | ||
export type Balance = TokenBalance | ContractPositionBalance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,26 @@ | ||
import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core'; | ||
import { DiscoveryService } from '@nestjs/core'; | ||
|
||
import { BALANCE_PRESENTER_APP, BALANCE_PRESENTER_NETWORK } from '~app-toolkit/decorators/balance-presenter.decorator'; | ||
import { BALANCE_PRESENTER_GROUP_LABEL } from '~app-toolkit/decorators/group-meta.decorator'; | ||
import { Network } from '~types/network.interface'; | ||
import { buildRegistry } from '~utils/build-registry'; | ||
|
||
import { BalancePresenter, GroupMeta, ReadonlyBalances } from './balance-presenter.interface'; | ||
import { BalancePresenter } from './balance-presenter.interface'; | ||
|
||
@Injectable() | ||
export class BalancePresenterRegistry implements OnModuleInit { | ||
private registry = new Map<Network, Map<string, BalancePresenter>>(); | ||
private groupMetaResolverRegistry = new Map< | ||
Network, | ||
Map<string, Map<string, (balances: ReadonlyBalances) => Promise<GroupMeta>>> | ||
>(); | ||
|
||
constructor( | ||
@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService, | ||
@Inject(MetadataScanner) private readonly metadataScanner: MetadataScanner, | ||
@Inject(Reflector) private readonly reflector: Reflector, | ||
) {} | ||
constructor(@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService) {} | ||
|
||
onModuleInit() { | ||
this.registry = buildRegistry<[Network, string], BalancePresenter>(this.discoveryService, [ | ||
BALANCE_PRESENTER_NETWORK, | ||
BALANCE_PRESENTER_APP, | ||
]); | ||
|
||
// Build group meta registry | ||
this.registry.forEach((r, network) => { | ||
r.forEach((presenter, appId) => { | ||
this.metadataScanner.scanFromPrototype(presenter, Object.getPrototypeOf(presenter), (methodName: string) => { | ||
this.registerGroupMeta(presenter, methodName, { network, appId }); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
private registerGroupMeta(instance: any, methodName: string, ctx: { network: Network; appId: string }) { | ||
const methodRef = instance[methodName]; | ||
const groupLabel = this.reflector.get(BALANCE_PRESENTER_GROUP_LABEL, methodRef); | ||
if (!groupLabel) return; | ||
|
||
if (!this.groupMetaResolverRegistry.get(ctx.network)) this.groupMetaResolverRegistry.set(ctx.network, new Map()); | ||
if (!this.groupMetaResolverRegistry.get(ctx.network)?.get(ctx.appId)) | ||
this.groupMetaResolverRegistry.get(ctx.network)?.set(ctx.appId, new Map()); | ||
|
||
this.groupMetaResolverRegistry.get(ctx.network)?.get(ctx.appId)?.set(groupLabel, methodRef); | ||
} | ||
|
||
get(appId: string, network: Network) { | ||
return this.registry.get(network)?.get(appId) ?? null; | ||
} | ||
|
||
getMetaResolvers(appId: string, network: Network) { | ||
return this.groupMetaResolverRegistry.get(network)?.get(appId) ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.