1+ const fs = require ( "fs" )
2+ const { get } = require ( "../projects/helper/http" )
3+ // const { setCache, getCache } = require("../projects/helper/cache")
4+
5+ async function run ( ) {
6+ // await getCache('defi-configs', 'tvlModules')
7+ const configs = await get ( 'https://api.llama.fi/_fe/static/configs' )
8+
9+ const moduleMap = { }
10+ const protocols = configs . protocols . concat ( configs . treasuries ) . concat ( configs . entities )
11+
12+ console . log ( '# of protocols/treasuries/entities:' , protocols . length )
13+
14+ for ( const protocol of protocols ) {
15+ try {
16+
17+ if ( moduleMap [ protocol . module ] ) continue ; // already imported
18+
19+ const modulePath = `../projects/${ protocol . module } `
20+ const importedModule = mockFunctions ( require ( modulePath ) )
21+
22+ if ( importedModule . hallmarks )
23+ importedModule . hallmarks = convertHallmarkStrings ( importedModule . hallmarks )
24+
25+ moduleMap [ protocol . module ] = importedModule
26+ } catch ( e ) {
27+ console . error ( `Error importing ${ protocol . module } :` , e )
28+ }
29+ }
30+
31+ fs . writeFileSync ( 'scripts/tvlModules.json' , JSON . stringify ( moduleMap ) )
32+ // await setCache('defi-configs', 'tvlModules', moduleMap, {
33+ // skipCompression: true,
34+ // })
35+
36+ process . exit ( 0 )
37+ }
38+
39+ run ( ) . catch ( ( e ) => {
40+ console . error ( e )
41+ process . exit ( 1 )
42+ } )
43+
44+
45+ function convertHallmarkStrings ( hallmarks ) {
46+ if ( ! Array . isArray ( hallmarks ) ) return hallmarks
47+ return hallmarks . map ( ( item ) => {
48+ if ( typeof item ?. [ 0 ] === 'string' ) {
49+ item [ 0 ] = dateStringToTimestamp ( item [ 0 ] )
50+ }
51+ if ( Array . isArray ( item ?. [ 0 ] ) ) {
52+ item [ 0 ] . forEach ( ( subItem , index ) => {
53+ if ( typeof subItem === 'string' ) {
54+ item [ 0 ] [ index ] = dateStringToTimestamp ( subItem )
55+ }
56+ } )
57+ }
58+ return item
59+ } ) . filter ( ( item ) => {
60+ if ( typeof item ?. [ 0 ] === 'number' ) return true
61+ // if it is a range hallmark
62+ if ( Array . isArray ( item ?. [ 0 ] && typeof item [ 0 ] [ 0 ] === 'number' && typeof item [ 0 ] [ 1 ] === 'number' ) ) {
63+ return true
64+ }
65+ return false
66+ } )
67+ }
68+
69+ //Replace all fuctions with mock functions in an object all the way down
70+ function mockFunctions ( obj ) {
71+ if ( typeof obj === "function" ) {
72+ return '_f' // llamaMockedTVLFunction
73+ } else if ( typeof obj === "object" ) {
74+ Object . keys ( obj ) . forEach ( ( key ) => obj [ key ] = mockFunctions ( obj [ key ] ) )
75+ }
76+ return obj
77+ }
78+
79+ function dateStringToTimestamp ( dateString ) {
80+
81+ let timestamp = Math . floor ( + new Date ( dateString ) / 1e3 )
82+ if ( ! isNaN ( timestamp ) )
83+ return timestamp
84+ return dateString
85+ }
0 commit comments