@@ -16,6 +16,7 @@ import {
1616 isBeforeTime ,
1717 isInTimeRange ,
1818 toTimezoneDate ,
19+ AssetsUnderManagement ,
1920} from './utils'
2021
2122const logger = makeLogger ( 'Superstate' )
@@ -31,6 +32,8 @@ export interface ResponseSchema {
3132
3233const TZ = 'America/New_York'
3334
35+ type ReportValueType = typeof inputParameters . validated . reportValue
36+
3437// Custom transport implementation that takes incoming requests, adds them into a SET, and makes requests to DP
3538// on a specific time every day, after receiving a signal from scheduler.
3639export class NavTransport implements Transport < BaseEndpointTypes > {
@@ -39,7 +42,7 @@ export class NavTransport implements Transport<BaseEndpointTypes> {
3942 requester ! : Requester
4043 settings ! : BaseEndpointTypes [ 'Settings' ]
4144 endpointName ! : string
42- fundIdsSet ! : Set < number >
45+ fundsMap ! : Map < string , [ number , ReportValueType ] >
4346
4447 async initialize (
4548 dependencies : TransportDependencies < BaseEndpointTypes > ,
@@ -52,26 +55,27 @@ export class NavTransport implements Transport<BaseEndpointTypes> {
5255 this . requester = dependencies . requester
5356 this . settings = settings
5457 this . endpointName = endpointName
55- this . fundIdsSet = new Set ( )
58+ this . fundsMap = new Map ( )
5659 this . runScheduler ( )
5760 }
5861
5962 // registerRequest is invoked on every valid request to EA
6063 // Adds fundId in the request to a SET
6164 async registerRequest ( req : AdapterRequest < typeof inputParameters . validated > ) {
62- const { fundId } = req . requestContext . data
63- if ( ! this . fundIdsSet . has ( fundId ) ) {
64- this . fundIdsSet . add ( fundId )
65- logger . info ( `Added new fund id - ${ fundId } ` )
65+ const { fundId, reportValue } = req . requestContext . data
66+ const mapKey = `${ fundId } +${ reportValue } `
67+ if ( ! this . fundsMap . has ( mapKey ) ) {
68+ this . fundsMap . set ( mapKey , [ fundId , reportValue ] )
69+ logger . info ( `Added new fund id - ${ fundId } - reportValue ${ reportValue } ` )
6670 }
6771 }
6872
6973 // foregroundExecute is executed when there is a new request/fundId that is not in the cache
7074 async foregroundExecute (
7175 req : AdapterRequest < typeof inputParameters . validated > ,
7276 ) : Promise < AdapterResponse < BaseEndpointTypes [ 'Response' ] > | void > {
73- const { fundId } = req . requestContext . data
74- return this . execute ( fundId )
77+ const { fundId, reportValue } = req . requestContext . data
78+ return this . execute ( fundId , reportValue )
7579 }
7680
7781 // Runs 'execute' function every day at 9:09 AM ET (if fundIdsSet is not empty)
@@ -83,19 +87,17 @@ export class NavTransport implements Transport<BaseEndpointTypes> {
8387
8488 schedule . scheduleJob ( rule , ( ) => {
8589 logger . info (
86- `Scheduled execution started at ${ Date . now ( ) } . FundIdSet - ${ [ ...this . fundIdsSet ] . join (
87- ',' ,
88- ) } `,
90+ `Scheduled execution started at ${ Date . now ( ) } . FundsMap - ${ [ ...this . fundsMap ] . join ( ',' ) } ` ,
8991 )
90- ; [ ...this . fundIdsSet ] . map ( async ( fundId ) => this . execute ( fundId ) )
92+ ; [ ...this . fundsMap ] . map ( async ( entry ) => this . execute ( entry [ 1 ] [ 0 ] , entry [ 1 ] [ 1 ] ) )
9193 } )
9294 }
9395
9496 // execute is either called by scheduler or foregroundExecute.
9597 // Makes a request to DP and saves the response in the cache.
9698 // In case the DP returns stale data the function will be executed again several times
9799 // before finalizing and saving the last returned data to a cache.
98- async execute ( fundId : number , retryCount = 0 ) {
100+ async execute ( fundId : number , reportValue : ReportValueType , retryCount = 0 ) {
99101 const providerDataRequestedUnixMs = Date . now ( )
100102 const apiResponse = await this . makeRequest ( fundId )
101103 const providerDataReceivedUnixMs = Date . now ( )
@@ -110,12 +112,15 @@ export class NavTransport implements Transport<BaseEndpointTypes> {
110112 providerIndicatedTimeUnixMs : undefined ,
111113 } ,
112114 }
113- await this . responseCache . write ( this . name , [ { params : { fundId } , response } ] )
115+ await this . responseCache . write ( this . name , [ { params : { fundId, reportValue } , response } ] )
114116 return
115117 }
116118
117119 const data = apiResponse . data [ 0 ]
118- const result = Number ( data . net_asset_value )
120+ let result = Number ( data . net_asset_value )
121+ if ( reportValue == AssetsUnderManagement ) {
122+ result = Number ( data . assets_under_management )
123+ }
119124
120125 // DP updates previous working day's price on the next working day at 9:09 AM ET
121126 // If there is no fresh price data, we try to re-fetch the API until 10:30 AM ET
@@ -135,7 +140,10 @@ export class NavTransport implements Transport<BaseEndpointTypes> {
135140 } ms`,
136141 )
137142 retryCount ++
138- setTimeout ( ( ) => this . execute ( fundId , retryCount ) , this . settings . RETRY_INTERVAL_MS )
143+ setTimeout (
144+ ( ) => this . execute ( fundId , reportValue , retryCount ) ,
145+ this . settings . RETRY_INTERVAL_MS ,
146+ )
139147 // We don't `return` here and let the value be stored in cache on purpose.
140148 // This way the EA will respond with the latest value from DP (even though it's not the value that the EA expects),
141149 // while it tries to get a fresh update.
@@ -159,7 +167,7 @@ export class NavTransport implements Transport<BaseEndpointTypes> {
159167 providerIndicatedTimeUnixMs : undefined ,
160168 } ,
161169 }
162- await this . responseCache . write ( this . name , [ { params : { fundId } , response } ] )
170+ await this . responseCache . write ( this . name , [ { params : { fundId, reportValue } , response } ] )
163171 return response
164172 }
165173
0 commit comments