import axios , { AxiosResponse } from 'axios' ; import { DistributeToPayoutModEvent , DistributeToPayoutModEventJson , parseDistributeToPayoutModEvent , } from 'models/subgraph-entities/distribute-to-payout-mod-event copy' ; import { DistributeToTicketModEvent , DistributeToTicketModEventJson , parseDistributeToTicketModEvent , } from 'models/subgraph-entities/distribute-to-ticket-mod-event' ; import { parseParticipantJson , Participant , ParticipantJson , } from 'models/subgraph-entities/participant' ; import { parsePayEventJson , PayEvent , PayEventJson , } from 'models/subgraph-entities/pay-event' ; import { parsePrintReservesEventJson , PrintReservesEvent , PrintReservesEventJson , } from 'models/subgraph-entities/print-reserves-event' ; import { parseProjectJson , Project , ProjectJson , } from 'models/subgraph-entities/project' ; import { parseRedeemEventJson , RedeemEvent , RedeemEventJson , } from 'models/subgraph-entities/redeem-event' ; import { parseTapEventJson , TapEvent , TapEventJson , } from 'models/subgraph-entities/tap-event' ; export interface SubgraphEntities { project : Project ; projectSearch : Project ; payEvent : PayEvent ; redeemEvent : RedeemEvent ; participant : Participant ; tapEvent : TapEvent ; distributeToPayoutModEvent : DistributeToPayoutModEvent ; distributeToTicketModEvent : DistributeToTicketModEvent ; printReservesEvent : PrintReservesEvent ; } export interface SubgraphQueryReturnTypes { project : { projects : ProjectJson [ ] ; } ; projectSearch : { projectSearch : ProjectJson [ ] ; } ; payEvent : { payEvents : PayEventJson [ ] ; } ; redeemEvent : { redeemEvents : RedeemEventJson [ ] ; } ; participant : { participants : ParticipantJson [ ] ; } ; tapEvent : { tapEvents : TapEventJson [ ] ; } ; distributeToTicketModEvent : { distributeToTicketModEvents : DistributeToTicketModEventJson [ ] ; } ; distributeToPayoutModEvent : { distributeToPayoutModEvents : DistributeToPayoutModEventJson [ ] ; } ; printReservesEvent : { printReservesEvents : PrintReservesEventJson [ ] ; } ; } export type EntityKey = keyof SubgraphEntities ; export interface SubgraphError { locations : { column : number ; line : number ; } [ ] ; message : string ; } export type OrderDirection = 'asc' | 'desc' ; export type WhereConfig < E extends EntityKey > = { key : EntityKeys < E > ; value : string | number | boolean ; operator ? : 'not' | 'gt' | 'lt' | 'gte' | 'lte' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'starts_with' | 'ends_with' | 'not_starts_with' | 'not_ends_with' ; } ; export type BlockConfig = { number ? : number ; hash ? : string ; } ; export type EntityKeys < E extends EntityKey > = keyof SubgraphEntities [ E ] ; export interface GraphQueryOpts < E extends EntityKey , K extends EntityKeys < E > > { entity : E ; text ? : string ; first ? : number ; skip ? : number ; orderBy ? : keyof SubgraphEntities [ E ] ; block ? : BlockConfig ; url ? : string ; keys : ( K | { entity : EntityKey ; keys : string [ ] ; } ) [ ] ; orderDirection ? : OrderDirection ; where ? : WhereConfig < E > | WhereConfig < E > [ ] ; } export type InfiniteGraphQueryOpts < E extends EntityKey , K extends EntityKeys < E > > = Omit < GraphQueryOpts < E , K > , 'skip' > & { pageSize : number ; } ; export const formatGraphQuery = < E extends EntityKey , K extends EntityKeys < E > > ( opts ) => { if ( ! opts ) return ; let args = '' ; const addArg = ( name , value ? ) => { if ( value === undefined ) return ; args += ( args . length ? ', ' : '' ) + `${ name }: ` + value ; } ; addArg ( 'text' , opts . text ? `"${ opts . text }"` : undefined ) ; addArg ( 'first' , opts . first ) ; addArg ( 'skip' , opts . skip ) ; addArg ( 'orderBy' , opts . orderBy ) ; addArg ( 'orderDirection' , opts . orderDirection ) ; if ( opts . block ) { if ( opts . block . number ) { addArg ( 'block' , `{ number: ${ opts . block . number } }` ) ; } else if ( opts . block . hash ) { addArg ( 'block' , `{ hash: ${ opts . block . hash } }` ) ; } } addArg ( 'where' , opts . where ? Array . isArray ( opts . where ) ? `{${ opts . where . map ( w => ` ${ w . key }${ w . operator ? '_' + w . operator : '' }: "${ w . value }"` ) } }` : `{ ${ opts . where . key }${ opts . where . operator ? '_' + opts . where . operator : '' }: "${ opts . where . value }" }` : undefined ) ; return `{ ${ opts . entity }${ isPluralQuery ( opts . entity ) ? 's' : '' }${ args ? `(${ args })` : '' } {${ opts . keys . reduce ( ( acc , key ) => typeof key === 'string' || typeof key === 'number' || typeof key === 'symbol' ? acc + ' ' + key . toString ( ) : acc + ` ${ key . entity } { ${ key . keys . join ( ' ' ) } }` , '' ) } } }` ; } ; const subgraphUrl = process . env . REACT_APP_SUBGRAPH_URL ; export const querySubgraph = < E extends EntityKey , K extends EntityKeys < E > > ( opts , callback ) => { const url = opts . url || subgraphUrl ; if ( ! url ) return Promise . reject ( 'Missing url for subgraph query' ) ; return axios . post ( url , { query : formatGraphQuery ( opts ) , } , { headers : { 'Content-Type' : 'application/json' } } ) . then ( ( res ) => callback ( res . data ?. data ) ) . catch ( err => console . log ( 'Error getting ' + opts . entity + 's' , err ) ) ; } ; export const trimHexZero = ( hexStr ) => hexStr . replace ( '0x0' , '0x' ) ; export function formatGraphResponse < E extends EntityKey > ( entity , response ) { if ( ! response || typeof response !== 'object' ) { return [ ] ; } switch ( entity ) { case 'project' : if ( 'projects' in response ) { return response . projects . map ( parseProjectJson ) ; } break ; case 'projectSearch' : if ( 'projectSearch' in response ) { return response . projectSearch . map ( parseProjectJson ) ; } break ; case 'payEvent' : if ( 'payEvents' in response ) { return response . payEvents . map ( parsePayEventJson ) ; } break ; case 'redeemEvent' : if ( 'redeemEvents' in response ) { return response . redeemEvents . map ( parseRedeemEventJson ) ; } break ; case 'participant' : if ( 'participants' in response ) { return response . participants . map ( parseParticipantJson ) ; } break ; case 'tapEvent' : if ( 'tapEvents' in response ) { return response . tapEvents . map ( parseTapEventJson ) ; } break ; case 'distributeToPayoutModEvent' : if ( 'distributeToPayoutModEvents' in response ) { return response . distributeToPayoutModEvents . map ( parseDistributeToPayoutModEvent ) ; } break ; case 'distributeToTicketModEvent' : if ( 'distributeToTicketModEvents' in response ) { return response . distributeToTicketModEvents . map ( parseDistributeToTicketModEvent ) ; } break ; case 'printReservesEvent' : if ( 'printReservesEvents' in response ) { return response . printReservesEvents . map ( parsePrintReservesEventJson ) ; } break ; } return [ ] ; } const isPluralQuery = ( key ) : boolean => { if ( key === 'projectSearch' ) return false ; return true ; } ;