import { Address , SubscriptionCallback , SubscriptionHandler , } from '@aragon/connect-types' ; import { ErrorException } from '@aragon/connect-core' ; import { GraphQLWrapper , QueryResult } from '@aragon/connect-thegraph' ; import * as queries from './queries' ; import Signer from '../models/Signer' ; import Signature from '../models/Signature' ; import Action from '../models/Action' ; import Staking from '../models/Staking' ; import StakingMovement from '../models/StakingMovement' ; import Version from '../models/Version' ; import DisputableApp from '../models/DisputableApp' ; import CollateralRequirement from '../models/CollateralRequirement' ; import ERC20 from '../models/ERC20' ; import { AgreementData , IAgreementConnector } from '../types' ; import { parseAgreement , parseSigner , parseSignatures , parseCurrentVersion , parseVersions , parseVersion , parseDisputableApps , parseCollateralRequirement , parseAction , parseStaking , parseStakingMovements , parseERC20 , } from './parsers' ; export function subgraphUrlFromChainId ( chainId ) { if ( chainId === 1 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-agreement-mainnet' ; } if ( chainId === 4 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-agreement-rinkeby' ; } if ( chainId === 100 ) { return 'https://api.thegraph.com/subgraphs/name/aragon/aragon-agreement-xdai' ; } return null ; } type AgreementConnectorTheGraphConfig = { appAddress ? : Address ; pollInterval ? : number ; subgraphUrl ? : string ; verbose ? : boolean ; } ; export default class AgreementConnectorTheGraph implements IAgreementConnector { #gql : GraphQLWrapper ; constructor ( gql ) { this . #gql = gql ; } static async create ( config ) { if ( ! config . subgraphUrl ) { throw new ErrorException ( 'AgreementConnectorTheGraph requires subgraphUrl to be passed.' ) ; } if ( ! config . appAddress ) { throw new ErrorException ( 'AgreementConnectorTheGraph requires appAddress to be passed.' ) ; } const gql = new GraphQLWrapper ( config . subgraphUrl , { pollInterval : config . pollInterval , verbose : config . verbose , } ) ; return new AgreementConnectorTheGraph ( gql ) ; } async disconnect ( ) { this . #gql . close ( ) ; } async agreement ( agreement ) { return this . #gql . performQueryWithParser < AgreementData > ( queries . GET_AGREEMENT ( 'query' ) , { agreement } , ( result ) => parseAgreement ( result ) ) ; } onAgreement ( agreement , callback ) { return this . #gql . subscribeToQueryWithParser < AgreementData > ( queries . GET_AGREEMENT ( 'subscription' ) , { agreement } , callback , ( result ) => parseAgreement ( result ) ) ; } async currentVersion ( agreement ) { return this . #gql . performQueryWithParser < Version > ( queries . GET_CURRENT_VERSION ( 'query' ) , { agreement } , ( result ) => parseCurrentVersion ( result , this ) ) ; } onCurrentVersion ( agreement , callback ) { return this . #gql . subscribeToQueryWithParser < Version > ( queries . GET_CURRENT_VERSION ( 'subscription' ) , { agreement } , callback , ( result ) => parseCurrentVersion ( result , this ) ) ; } async version ( versionId ) { return this . #gql . performQueryWithParser < Version > ( queries . GET_VERSION ( 'query' ) , { versionId } , ( result ) => parseVersion ( result , this ) ) ; } onVersion ( versionId , callback ) { return this . #gql . subscribeToQueryWithParser < Version > ( queries . GET_VERSION ( 'subscription' ) , { versionId } , callback , ( result ) => parseVersion ( result , this ) ) ; } async versions ( agreement , first , skip ) { return this . #gql . performQueryWithParser < Version [ ] > ( queries . ALL_VERSIONS ( 'query' ) , { agreement , first , skip } , ( result ) => parseVersions ( result , this ) ) ; } onVersions ( agreement , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < Version [ ] > ( queries . ALL_VERSIONS ( 'subscription' ) , { agreement , first , skip } , callback , ( result ) => parseVersions ( result , this ) ) ; } async disputableApps ( agreement , first , skip ) { return this . #gql . performQueryWithParser < DisputableApp [ ] > ( queries . ALL_DISPUTABLE_APPS ( 'query' ) , { agreement , first , skip } , ( result ) => parseDisputableApps ( result , this ) ) ; } onDisputableApps ( agreement , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < DisputableApp [ ] > ( queries . ALL_DISPUTABLE_APPS ( 'subscription' ) , { agreement , first , skip } , callback , ( result ) => parseDisputableApps ( result , this ) ) ; } async signer ( signerId ) { return this . #gql . performQueryWithParser < Signer | null > ( queries . GET_SIGNER ( 'query' ) , { signerId } , ( result ) => parseSigner ( result , this ) ) ; } onSigner ( signerId , callback ) { return this . #gql . subscribeToQueryWithParser < Signer | null > ( queries . GET_SIGNER ( 'query' ) , { signerId } , callback , ( result ) => parseSigner ( result , this ) ) ; } async signatures ( signerId , first , skip ) { return this . #gql . performQueryWithParser < Signature [ ] > ( queries . GET_SIGNATURES ( 'query' ) , { signerId , first , skip } , ( result ) => parseSignatures ( result , this ) ) ; } onSignatures ( signerId , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < Signature [ ] > ( queries . GET_SIGNATURES ( 'query' ) , { signerId , first , skip } , callback , ( result ) => parseSignatures ( 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 staking ( stakingId ) { return this . #gql . performQueryWithParser < Staking > ( queries . GET_STAKING ( 'query' ) , { stakingId } , ( result ) => parseStaking ( result , this ) ) ; } onStaking ( stakingId , callback ) { return this . #gql . subscribeToQueryWithParser < Staking | null > ( queries . GET_STAKING ( 'query' ) , { stakingId } , callback , ( result ) => parseStaking ( result , this ) ) ; } async stakingMovements ( stakingId , agreementId , first , skip ) { return this . #gql . performQueryWithParser < StakingMovement [ ] > ( queries . GET_STAKING_MOVEMENTS ( 'query' ) , { stakingId , agreementId , first , skip } , ( result ) => parseStakingMovements ( result , this ) ) ; } onStakingMovements ( stakingId , agreementId , first , skip , callback ) { return this . #gql . subscribeToQueryWithParser < StakingMovement [ ] > ( queries . GET_STAKING_MOVEMENTS ( 'query' ) , { stakingId , agreementId , first , skip } , callback , ( result ) => parseStakingMovements ( result , this ) ) ; } async action ( actionId ) { return this . #gql . performQueryWithParser < Action | null > ( queries . GET_ACTION ( 'query' ) , { actionId } , ( result ) => parseAction ( result , this ) ) ; } onAction ( actionId , callback ) { return this . #gql . subscribeToQueryWithParser < Action | null > ( queries . GET_ACTION ( 'query' ) , { actionId } , callback , ( result ) => parseAction ( result , this ) ) ; } async ERC20 ( tokenAddress ) { return this . #gql . performQueryWithParser < ERC20 > ( queries . GET_ERC20 ( 'query' ) , { tokenAddress } , ( result ) => parseERC20 ( result ) ) ; } onERC20 ( tokenAddress , callback ) { return this . #gql . subscribeToQueryWithParser < ERC20 > ( queries . GET_ERC20 ( 'subscription' ) , { tokenAddress } , callback , ( result ) => parseERC20 ( result ) ) ; } }