@@ -7,69 +7,19 @@ import {
77 HttpClientRequest ,
88 HttpClientResponse ,
99} from "@effect/platform" ;
10+ import { Array as _Array , Config , Effect , Layer , Logger } from "effect" ;
1011import {
11- Array as _Array ,
12- Config ,
13- Data ,
14- Effect ,
15- Layer ,
16- Logger ,
17- Order ,
18- Schema ,
19- } from "effect" ;
20-
21- const DEFAULT_LIMIT = 50 ;
22-
23- const exchangePriorityRecord : Record < string , number > = {
24- Coinbase : 0 ,
25- Binance : 1 ,
26- CRYPTOCAP : 2 ,
27- "Crypto.com" : 3 ,
28- } ;
29-
30- // -----------------------------------------------------------------------------
31- // Schemas
32- // -----------------------------------------------------------------------------
33- const TokenDto = Schema . Struct ( { symbol : Schema . String } ) ;
34-
35- const ProviderDto = Schema . Struct ( {
36- id : Schema . String ,
37- name : Schema . optionalWith ( Schema . String , { nullable : true } ) ,
38- } ) ;
39-
40- const MarketDto = Schema . Struct ( {
41- id : Schema . String ,
42- providerId : Schema . String ,
43- baseAsset : TokenDto ,
44- quoteAsset : TokenDto ,
45- } ) ;
46-
47- const MarketsResponse = Schema . Struct ( {
48- total : Schema . Number ,
49- offset : Schema . Number ,
50- limit : Schema . Number ,
51- items : Schema . optionalWith ( Schema . Array ( MarketDto ) , { nullable : true } ) ,
52- } ) ;
53-
54- const ProvidersResponse = Schema . Array ( ProviderDto ) ;
55-
56- const TradingViewSearchResponse = Schema . Struct ( {
57- symbols : Schema . Array (
58- Schema . Struct ( {
59- symbol : Schema . String ,
60- exchange : Schema . String ,
61- provider_id : Schema . String ,
62- } ) ,
63- ) ,
64- } ) ;
65-
66- // -----------------------------------------------------------------------------
67- // Error Types
68- // -----------------------------------------------------------------------------
69- class HttpError extends Data . TaggedError ( "HttpError" ) < {
70- message : string ;
71- cause ?: unknown ;
72- } > { }
12+ type BaseSymbolSchema ,
13+ byProviderAndCurrency ,
14+ compareSymbolsFromBaseSymbol ,
15+ DEFAULT_LIMIT ,
16+ HttpError ,
17+ MarketsResponse ,
18+ makeResult ,
19+ normalizeSymbol ,
20+ ProvidersResponse ,
21+ TradingViewSearchResponse ,
22+ } from "scripts/generate-tradingview-symbols/utils" ;
7323
7424// -----------------------------------------------------------------------------
7525// HttpClient Services
@@ -123,11 +73,6 @@ class TradingViewClient extends Effect.Service<TradingViewClient>()(
12373// -----------------------------------------------------------------------------
12474// API Functions
12575// -----------------------------------------------------------------------------
126- const normalizeSymbol = ( symbol : string ) =>
127- symbol
128- . replace ( / < \/ ? e m > / g, "" )
129- . trim ( )
130- . toUpperCase ( ) ;
13176
13277const getProviders = Effect . gen ( function * ( ) {
13378 const perpsClient = yield * PerpsClient ;
@@ -226,50 +171,23 @@ const searchTradingViewSymbol = (
226171 ) ,
227172 ) ;
228173
229- const CheckTradingViewSymbolResult = Schema . Union (
230- Schema . Struct ( {
231- status : Schema . Literal ( "match" ) ,
232- perpsSymbol : Schema . String ,
233- tradingViewSymbol : Schema . String ,
234- providerId : Schema . String ,
235- } ) ,
236- Schema . Struct ( {
237- status : Schema . Literal ( "noMatch" ) ,
238- perpsSymbol : Schema . String ,
239- } ) ,
240- Schema . Struct ( {
241- status : Schema . Literal ( "error" ) ,
242- perpsSymbol : Schema . String ,
243- } ) ,
244- ) ;
245-
246- const makeResult = Schema . decodeSync ( CheckTradingViewSymbolResult ) ;
247-
248- const checkTradingViewSymbol = Effect . fn ( function * ( baseSymbol : string ) {
174+ const checkTradingViewSymbol = Effect . fn ( function * (
175+ baseSymbol : typeof BaseSymbolSchema . Type ,
176+ ) {
249177 const tvClient = yield * TradingViewClient ;
250178
251179 const normalizedBase = normalizeSymbol ( baseSymbol ) ;
252180
253181 return yield * searchTradingViewSymbol ( tvClient , normalizedBase ) . pipe (
254182 Effect . map ( ( response ) => {
255- const results = _Array . sort (
256- response . symbols ,
257- Order . mapInput (
258- Order . number ,
259- ( v : ( typeof response . symbols ) [ number ] ) =>
260- exchangePriorityRecord [ v . exchange ] ?? 999 ,
261- ) ,
262- ) ;
183+ const results = _Array . sort ( response . symbols , byProviderAndCurrency ) ;
184+
185+ const compareSymbols = compareSymbolsFromBaseSymbol ( normalizedBase ) ;
263186
264187 const matchedResult = _Array . findFirst ( results , ( result ) => {
265188 const resultSymbol = normalizeSymbol ( result . symbol ) ;
266189
267- return [
268- normalizedBase ,
269- `${ normalizedBase } USD` ,
270- `${ normalizedBase } USDC` ,
271- `${ normalizedBase } USDT` ,
272- ] . some ( ( symbol ) => resultSymbol === symbol ) ;
190+ return _Array . some ( compareSymbols , ( symbol ) => resultSymbol === symbol ) ;
273191 } ) ;
274192
275193 if ( matchedResult . _tag === "Some" ) {
@@ -324,11 +242,12 @@ const program = Effect.gen(function* () {
324242
325243 const nonMatched = results . filter ( ( result ) => result . status !== "match" ) ;
326244
327- yield * Effect . log ( "Non matched: " , JSON . stringify ( nonMatched , null , 2 ) ) ;
245+ yield * Effect . log ( "Not matched: " , JSON . stringify ( nonMatched , null , 2 ) ) ;
328246
329247 const path = join (
330248 dirname ( fileURLToPath ( import . meta. url ) ) ,
331249 ".." ,
250+ ".." ,
332251 "src" ,
333252 "assets" ,
334253 "tradingview-symbols.json" ,
0 commit comments