import { providers as ethersProviders } from 'ethers' ; import { GraphQLWrapper , QueryResult } from '@aragon/connect-thegraph' ; import { SubscriptionCallback , SubscriptionHandler } from '@aragon/connect-types' ; import { DisputableVotingData , IDisputableVotingConnector } from '../types' ; import Vote from '../models/Vote' ; import Voter from '../models/Voter' ; import ERC20 from '../models/ERC20' ; import Setting from '../models/Setting' ; import CastVote from '../models/CastVote' ; import ArbitratorFee from '../models/ArbitratorFee' ; import CollateralRequirement from '../models/CollateralRequirement' ; import * as queries from './queries' ; import { parseSetting , parseSettings , parseCurrentSetting , parseDisputableVoting , parseERC20 , parseVoter , parseVote , parseVotes , parseCastVote , parseCastVotes , parseArbitratorFee , parseCollateralRequirement , parseCurrentCollateralRequirement , } from './parsers' ; export function subgraphUrlFromChainId ( chainId ) { if ( chainId === 1 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-dvoting-mainnet' ; } if ( chainId === 4 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-dvoting-rinkeby' ; } if ( chainId === 100 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-dvoting-xdai' ; } return null ; } type DisputableVotingConnectorTheGraphConfig = { pollInterval ? : number ; subgraphUrl ? : string ; verbose ? : boolean ; } ; export default class DisputableVotingConnectorTheGraph implements IDisputableVotingConnector { #gql : GraphQLWrapper ; #ethersProvider : ethersProviders . Provider ; constructor ( config , provider ) { if ( ! config . subgraphUrl ) { throw new Error ( 'DisputableVotingConnectorTheGraph requires subgraphUrl to be passed.' ) ; } this . #gql = new GraphQLWrapper ( config . subgraphUrl , { pollInterval : config . pollInterval , verbose : config . verbose , } ) ; this . #ethersProvider = provider ; } async disconnect ( ) { this . #gql . close ( ) ; } async disputableVoting ( disputableVoting ) { return this . #gql . performQueryWithParser < DisputableVotingData > ( queries . GET_DISPUTABLE_VOTING ( 'query' ) , { disputableVoting } , ( result ) => parseDisputableVoting ( result ) ) ; } onDisputableVoting ( disputableVoting , callback ) { return this . #gql . subscribeToQueryWithParser < DisputableVotingData > ( queries . GET_DISPUTABLE_VOTING ( 'subscription' ) , { disputableVoting } , callback , ( result ) => parseDisputableVoting ( result ) ) ; } async currentSetting ( disputableVoting ) { return this . #gql . performQueryWithParser < Setting > ( queries . GET_CURRENT_SETTING ( 'query' ) , { disputableVoting } , ( result ) => parseCurrentSetting ( result ) ) ; } onCurrentSetting ( disputableVoting , callback ) { return this . #gql . subscribeToQueryWithParser < Setting > ( queries . GET_CURRENT_SETTING ( 'subscription' ) , { disputableVoting } , callback , ( result ) => parseCurrentSetting ( result ) ) ; } async setting ( settingId ) { return this . #gql . performQueryWithParser < Setting > ( queries . GET_SETTING ( 'query' ) , { settingId } , ( result ) => parseSetting ( result ) ) ; } onSetting ( settingId , callback ) { return this . #gql . subscribeToQueryWithParser < Setting > ( queries . GET_SETTING ( 'subscription' ) , { settingId } , callback , ( result ) => parseSetting ( result ) ) ; } async settings ( disputableVoting , first , skip ) { return this . #gql . performQueryWithParser < Setting [ ] > ( queries . ALL_SETTINGS ( 'query' ) , { disputableVoting , first , skip } , ( result ) => parseSettings ( result ) ) ; } onSettings ( disputableVoting , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < Setting [ ] > ( queries . ALL_SETTINGS ( 'subscription' ) , { disputableVoting , first , skip } , callback , ( result ) => parseSettings ( result ) ) ; } async currentCollateralRequirement ( disputableVoting ) { return this . #gql . performQueryWithParser < CollateralRequirement > ( queries . GET_CURRENT_COLLATERAL_REQUIREMENT ( 'query' ) , { disputableVoting } , ( result ) => parseCurrentCollateralRequirement ( result , this ) ) ; } onCurrentCollateralRequirement ( disputableVoting , callback ) { return this . #gql . subscribeToQueryWithParser < CollateralRequirement > ( queries . GET_CURRENT_COLLATERAL_REQUIREMENT ( 'subscription' ) , { disputableVoting } , callback , ( result ) => parseCurrentCollateralRequirement ( result , this ) ) ; } async vote ( voteId ) { return this . #gql . performQueryWithParser < Vote > ( queries . GET_VOTE ( 'query' ) , { voteId } , ( result ) => parseVote ( result , this , this . #ethersProvider ) ) ; } onVote ( voteId , callback ) { return this . #gql . subscribeToQueryWithParser < Vote > ( queries . GET_VOTE ( 'subscription' ) , { voteId } , callback , ( result ) => parseVote ( result , this , this . #ethersProvider ) ) ; } async votes ( disputableVoting , first , skip ) { return this . #gql . performQueryWithParser < Vote [ ] > ( queries . ALL_VOTES ( 'query' ) , { disputableVoting , first , skip } , ( result ) => parseVotes ( result , this , this . #ethersProvider ) ) ; } onVotes ( disputableVoting , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < Vote [ ] > ( queries . ALL_VOTES ( 'subscription' ) , { disputableVoting , first , skip } , callback , ( result ) => parseVotes ( result , this , this . #ethersProvider ) ) ; } async castVote ( castVoteId ) { return this . #gql . performQueryWithParser < CastVote | null > ( queries . GET_CAST_VOTE ( 'query' ) , { castVoteId } , ( result ) => parseCastVote ( result , this ) ) ; } onCastVote ( castVoteId , callback ) { return this . #gql . subscribeToQueryWithParser < CastVote | null > ( queries . GET_CAST_VOTE ( 'subscription' ) , { castVoteId } , callback , ( result ) => parseCastVote ( result , this ) ) ; } async castVotes ( voteId , first , skip ) { return this . #gql . performQueryWithParser < CastVote [ ] > ( queries . ALL_CAST_VOTES ( 'query' ) , { voteId , first , skip } , ( result ) => parseCastVotes ( result , this ) ) ; } onCastVotes ( voteId , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < CastVote [ ] > ( queries . ALL_CAST_VOTES ( 'subscription' ) , { voteId , first , skip } , callback , ( result ) => parseCastVotes ( result , this ) ) ; } async voter ( voterId ) { return this . #gql . performQueryWithParser < Voter > ( queries . GET_VOTER ( 'query' ) , { voterId } , ( result ) => parseVoter ( result , this ) ) ; } onVoter ( voterId , callback ) { return this . #gql . subscribeToQueryWithParser < Voter > ( queries . GET_VOTER ( 'subscription' ) , { voterId } , callback , ( result ) => parseVoter ( result , this ) ) ; } async collateralRequirement ( collateralRequirementId ) { return this . #gql . performQueryWithParser < CollateralRequirement > ( queries . GET_COLLATERAL_REQUIREMENT ( 'query' ) , { collateralRequirementId } , ( result ) => parseCollateralRequirement ( result , this ) ) ; } onCollateralRequirement ( collateralRequirementId , callback ) { return this . #gql . subscribeToQueryWithParser < CollateralRequirement > ( queries . GET_COLLATERAL_REQUIREMENT ( 'subscription' ) , { collateralRequirementId } , callback , ( result ) => parseCollateralRequirement ( result , this ) ) ; } async arbitratorFee ( arbitratorFeeId ) { return this . #gql . performQueryWithParser < ArbitratorFee | null > ( queries . GET_ARBITRATOR_FEE ( 'query' ) , { arbitratorFeeId } , ( result ) => parseArbitratorFee ( result , this ) ) ; } onArbitratorFee ( arbitratorFeeId , callback ) { return this . #gql . subscribeToQueryWithParser < ArbitratorFee | null > ( queries . GET_ARBITRATOR_FEE ( 'subscription' ) , { arbitratorFeeId } , callback , ( result ) => parseArbitratorFee ( result , this ) ) ; } async ERC20 ( tokenAddress ) { return this . #gql . performQueryWithParser ( queries . GET_ERC20 ( 'query' ) , { tokenAddress } , ( result ) => parseERC20 ( result , this . #ethersProvider ) ) ; } onERC20 ( tokenAddress , callback ) { return this . #gql . subscribeToQueryWithParser < ERC20 > ( queries . GET_ERC20 ( 'subscription' ) , { tokenAddress } , callback , ( result ) => parseERC20 ( result , this . #ethersProvider ) ) ; } }