import { Address , SubscriptionCallback , SubscriptionHandler , } from '@aragon/connect-types' ; import { ErrorException } from '@aragon/connect-core' ; import { GraphQLWrapper , QueryResult } from '@aragon/connect-thegraph' ; import { IFinanceConnector } from '../types' ; import Transaction from '../models/Transaction' ; import TokenBalance from '../models/TokenBalance' ; import * as queries from './queries' ; import { parseTransactions , parseTokenBalance } from './parsers' ; export function subgraphUrlFromChainId ( chainId ) { if ( chainId === 1 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-finance-mainnet' ; } if ( chainId === 4 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-finance-rinkeby' ; } if ( chainId === 100 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-finance-xdai' ; } return null ; } type FinanceConnectorTheGraphConfig = { pollInterval ? : number ; subgraphUrl ? : string ; verbose ? : boolean ; } ; export default class FinanceConnectorTheGraph implements IFinanceConnector { #gql : GraphQLWrapper ; constructor ( config ) { if ( ! config . subgraphUrl ) { throw new ErrorException ( 'FinanceConnectorTheGraph requires subgraphUrl to be passed.' ) ; } this . #gql = new GraphQLWrapper ( config . subgraphUrl , { pollInterval : config . pollInterval , verbose : config . verbose , } ) ; } async disconnect ( ) { this . #gql . close ( ) ; } async transactionsForApp ( appAddress , first , skip ) { return this . #gql . performQueryWithParser < Transaction [ ] > ( queries . ALL_TRANSACTIONS ( 'query' ) , { appAddress , first , skip } , ( result ) => parseTransactions ( result ) ) ; } onTransactionsForApp ( appAddress , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < Transaction [ ] > ( queries . ALL_TRANSACTIONS ( 'subscription' ) , { appAddress , first , skip } , callback , ( result ) => parseTransactions ( result ) ) ; } async balanceForToken ( appAddress , tokenAddress , first , skip ) { return this . #gql . performQueryWithParser < TokenBalance > ( queries . BALANCE_FOR_TOKEN ( 'query' ) , { appAddress , tokenAddress , first , skip } , ( result ) => parseTokenBalance ( result ) ) ; } onBalanceForToken ( appAddress , tokenAddress , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < TokenBalance > ( queries . BALANCE_FOR_TOKEN ( 'subscription' ) , { appAddress , tokenAddress , first , skip } , callback , ( result ) => parseTransactions ( result ) ) ; } }