@@ -5,6 +5,60 @@ import { sendMessage, withTimeout } from "./serverUtils";
55import { init , processSingleProtocol } from "./utils" ;
66import { readdirSync } from "fs" ;
77
8+ async function validateAdapters ( emissionsAdapters : any ) {
9+ const errors = [ ] as string [ ] ;
10+ const tokens = new Set < string > ( ) , protocolIds = new Set < string > ( ) , notes = new Set < string > ( ) , sources = new Set < string > ( ) ;
11+ for ( const [ protocolName , protocolFile ] of Object . entries ( emissionsAdapters ) ) {
12+ try {
13+
14+ if ( protocolName === "daomaker" || protocolName === "streamflow" ) {
15+ continue
16+ }
17+ const rawProtocol = ( protocolFile as any ) . default
18+ const protocol = rawProtocol . meta ;
19+ if ( ! protocol . token ) errors . push ( `Error in ${ protocolName } : missing token field` ) ;
20+ if ( tokens . has ( protocol . token ! ) ) errors . push ( `Error in ${ protocolName } : duplicate token ${ protocol . token } ` ) ;
21+
22+ tokens . add ( protocol . token ! ) ;
23+
24+ if ( ! Array . isArray ( protocol . protocolIds ) || protocol . protocolIds . length === 0 )
25+ errors . push ( `Error in ${ protocolName } : protocolIds should be an array` )
26+ else {
27+ protocol . protocolIds . forEach ( ( id : string ) => {
28+ if ( protocolIds . has ( id ) ) errors . push ( `Error in ${ protocolName } : duplicate protocolId ${ id } ` ) ;
29+ protocolIds . add ( id ) ;
30+ } ) ;
31+ }
32+
33+ /*
34+ // Why was the test checking if the notes was duplicated and the unit test had a bug by comparing objects instead of strings?
35+ if (Array.isArray(protocol.notes)) {
36+ protocol.notes.forEach((note: string) => {
37+ if (notes.has(note)) errors.push(`Error in ${protocolName}: duplicate note ${note}`);
38+ notes.add(note);
39+ });
40+ }
41+ */
42+
43+ if ( Array . isArray ( protocol . sources ) ) {
44+ protocol . sources . forEach ( ( source : string ) => {
45+ if ( sources . has ( source ) ) errors . push ( `Error in ${ protocolName } : duplicate source ${ source } ` ) ;
46+ sources . add ( source ) ;
47+ } ) ;
48+ }
49+
50+
51+ } catch ( e : any ) {
52+ errors . push ( `Error in ${ protocolName } : ${ e ?. message } ` ) ;
53+ }
54+ }
55+
56+ if ( errors . length > 0 ) {
57+ console . table ( errors ) ;
58+ await sendMessage ( `Emissions Adapters Validation Errors: \n${ errors . join ( "\n" ) } ` , process . env . DIM_ERROR_CHANNEL_WEBHOOK ! ) ;
59+ }
60+ }
61+
862export async function processProtocolList ( ) {
963 await init ( )
1064
@@ -16,6 +70,8 @@ export async function processProtocolList() {
1670
1771 const protocolAdapters = Object . entries ( adapters )
1872
73+ await validateAdapters ( adapters )
74+
1975 // randomize array
2076 protocolAdapters . sort ( ( ) => Math . random ( ) - 0.5 ) ;
2177
@@ -71,17 +127,6 @@ async function handlerErrors(errors: string[]) {
71127 }
72128}
73129
74- export async function handler ( ) {
75- try {
76- await withTimeout ( 8400000 , processProtocolList ( ) ) ;
77- } catch ( e ) {
78- process . env . UNLOCKS_WEBHOOK ? await sendMessage ( `${ e } ` , process . env . UNLOCKS_WEBHOOK ! ) : console . log ( e ) ;
79- }
80- process . exit ( ) ;
81- }
82-
83- // export default wrapScheduledLambda(handler);
84- handler ( ) ; // ts-node defi/src/storeEmissions.ts
85130
86131
87132const extensions = [ 'ts' , 'md' , 'js' ]
@@ -114,3 +159,15 @@ function removeDotTs(s: string) {
114159 splitted . pop ( )
115160 return splitted . join ( '.' )
116161}
162+
163+ async function run ( ) {
164+ try {
165+ await withTimeout ( 8400000 , processProtocolList ( ) ) ;
166+ } catch ( e ) {
167+ process . env . DIM_ERROR_CHANNEL_WEBHOOK ? await sendMessage ( `${ e } ` , process . env . DIM_ERROR_CHANNEL_WEBHOOK ! ) : console . log ( e ) ;
168+ }
169+ process . exit ( ) ;
170+ }
171+
172+
173+ run ( )
0 commit comments