import { Address , SubscriptionCallback , SubscriptionHandler , } from '@aragon/connect-types' ; import { ErrorException } from '@aragon/connect-core' ; import { GraphQLWrapper } from '@aragon/connect-thegraph' ; import { ITokensConnector } from '../types' ; import * as queries from './queries' ; import Token from '../models/Token' ; import TokenHolder from '../models/TokenHolder' ; import { parseToken , parseTokenHolders } from './parsers' ; export function subgraphUrlFromChainId ( chainId ) { if ( chainId === 1 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-tokens-mainnet' ; } if ( chainId === 4 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-tokens-rinkeby' ; } if ( chainId === 100 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-tokens-xdai' ; } return null ; } type TokensConnectorTheGraphConfig = { appAddress ? : Address ; pollInterval ? : number ; subgraphUrl ? : string ; verbose ? : boolean ; } ; export default class TokensConnectorTheGraph implements ITokensConnector { #gql : GraphQLWrapper ; #token : Token ; constructor ( gql , token ) { this . #gql = gql ; this . #token = token ; } static async create ( config ) { if ( ! config . subgraphUrl ) { throw new ErrorException ( 'TokensConnectorTheGraph requires subgraphUrl to be passed.' ) ; } if ( ! config . appAddress ) { throw new ErrorException ( 'TokensConnectorTheGraph requires appAddress to be passed.' ) ; } const gql = new GraphQLWrapper ( config . subgraphUrl , { pollInterval : config . pollInterval , verbose : config . verbose , } ) ; const token = await gql . performQueryWithParser < Token > ( queries . TOKEN ( 'query' ) , { tokenManagerAddress : config . appAddress } , ( result ) => parseToken ( result ) ) ; return new TokensConnectorTheGraph ( gql , token ) ; } async disconnect ( ) { this . #gql . close ( ) ; } async token ( ) { return this . #token ; } async tokenHolders ( tokenAddress , first , skip ) { return this . #gql . performQueryWithParser < TokenHolder [ ] > ( queries . TOKEN_HOLDERS ( 'query' ) , { tokenAddress , first , skip } , ( result ) => parseTokenHolders ( result ) ) ; } onTokenHolders ( tokenAddress , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < TokenHolder [ ] > ( queries . TOKEN_HOLDERS ( 'subscription' ) , { tokenAddress , first , skip } , callback , ( result ) => parseTokenHolders ( result ) ) ; } }