11import { EventFetchParams } from '../../BaseExchange' ;
22import { UnifiedEvent , UnifiedMarket } from '../../types' ;
3- import { GAMMA_API_URL , mapMarketToUnified , paginateParallel } from './utils' ;
3+ import { GAMMA_SEARCH_URL , mapMarketToUnified , paginateSearchParallel } from './utils' ;
44import { polymarketErrorMapper } from './errors' ;
55
66export async function fetchEvents ( params : EventFetchParams ) : Promise < UnifiedEvent [ ] > {
7- const searchLimit = 100000 ; // Fetch all events for comprehensive search
8-
97 try {
10- const status = params ?. status || 'active' ;
8+ if ( ! params . query ) {
9+ // If no query is provided, we can't use the search endpoint effectively.
10+ // However, the BaseExchange interface enforces query presence for fetchEvents.
11+ // Just in case, we return empty or throw.
12+ throw new Error ( "Query is required for Polymarket event search" ) ;
13+ }
14+
15+ const limit = params . limit || 10000 ;
16+ const status = params . status || 'active' ;
17+
1118 const queryParams : any = {
12- limit : searchLimit
19+ q : params . query ,
20+ limit_per_type : 50 , // Fetch 50 per page for better efficiency
21+ events_status : status === 'all' ? undefined : status ,
22+ sort : 'volume' ,
23+ ascending : false
1324 } ;
1425
26+ // If specific status requested
1527 if ( status === 'active' ) {
16- queryParams . active = 'true' ;
17- queryParams . closed = 'false' ;
28+ queryParams . events_status = 'active' ;
1829 } else if ( status === 'closed' ) {
19- queryParams . active = 'false' ;
20- queryParams . closed = 'true' ;
21- } else {
22- // 'all' - no filter, maybe handled by default or API behavior
30+ queryParams . events_status = 'closed' ;
2331 }
2432
25- // Fetch events from Gamma API using parallel pagination
26- const events = await paginateParallel ( GAMMA_API_URL , queryParams ) ;
33+ // Use parallel pagination to fetch all matching events
34+ const events = await paginateSearchParallel ( GAMMA_SEARCH_URL , queryParams , limit * 10 ) ;
2735
28- // Client-side text filtering
29- const lowerQuery = ( params ? .query || '' ) . toLowerCase ( ) ;
30- const searchIn = params ? .searchIn || 'title' ;
36+ // Client-side filtering to ensure title matches (API does fuzzy search)
37+ const lowerQuery = params . query . toLowerCase ( ) ;
38+ const searchIn = params . searchIn || 'title' ;
3139
32- const filtered = events . filter ( ( event : any ) => {
40+ const filteredEvents = events . filter ( ( event : any ) => {
3341 const titleMatch = ( event . title || '' ) . toLowerCase ( ) . includes ( lowerQuery ) ;
3442 const descMatch = ( event . description || '' ) . toLowerCase ( ) . includes ( lowerQuery ) ;
3543
3644 if ( searchIn === 'title' ) return titleMatch ;
3745 if ( searchIn === 'description' ) return descMatch ;
38- return titleMatch || descMatch ;
46+ return titleMatch || descMatch ; // 'both'
3947 } ) ;
4048
41- // Map to UnifiedEvent
42- const unifiedEvents : UnifiedEvent [ ] = filtered . map ( ( event : any ) => {
49+ // Map events to UnifiedEvent
50+ const unifiedEvents : UnifiedEvent [ ] = filteredEvents . map ( ( event : any ) => {
4351 const markets : UnifiedMarket [ ] = [ ] ;
4452
45- if ( event . markets ) {
53+ if ( event . markets && Array . isArray ( event . markets ) ) {
4654 for ( const market of event . markets ) {
4755 const unifiedMarket = mapMarketToUnified ( event , market , { useQuestionAsCandidateFallback : true } ) ;
4856 if ( unifiedMarket ) {
@@ -66,9 +74,7 @@ export async function fetchEvents(params: EventFetchParams): Promise<UnifiedEven
6674 return unifiedEvent ;
6775 } ) ;
6876
69- // Apply limit to filtered results
70- const limit = params ?. limit || 20 ;
71- return unifiedEvents . slice ( 0 , limit ) ;
77+ return unifiedEvents ;
7278
7379 } catch ( error : any ) {
7480 throw polymarketErrorMapper . mapError ( error ) ;
0 commit comments