import * as React from "react" ; import { Civil , UniswapService , FeatureFlagService , EthersProviderResult , makeEthersProvider } from "@joincivil/core" ; import { AuthService } from "./AuthService" ; import ApolloClient from "apollo-client" ; export enum RENDER_CONTEXT { DAPP , EMBED } export interface ICivilContext { civil ? : Civil ; uniswap : UniswapService ; features : FeatureFlagService ; auth : AuthService ; web3Provider : any ; network : number ; currentUser : any ; renderContext : RENDER_CONTEXT ; /** See `packages/dapp/src/helpers/config.ts` */ config : any ; waitForTx ( txHash ) : Promise < any > ; fireAnalyticsEvent ( category , event , label , value ) : void ; setAnalyticsEvent ( fire ) : void ; } export interface CivilContextOptions { web3 : any ; featureFlags : string [ ] ; config : any ; renderContext ? : RENDER_CONTEXT ; apolloClient ? : ApolloClient < any > ; onAuthChange ? ( nextUser ) : void ; } export function buildCivilContext ( opts ) { const { web3 , apolloClient , config , featureFlags , onAuthChange } = opts ; if ( ! web3 ) { throw new Error ( "expecting web3 to be initialized" ) ; } console . log ( "building new context" ) ; const civil = new Civil ( { web3Provider : opts . web3 . currentProvider } ) ; const web3Provider = web3 . currentProvider ; const network = web3 . currentProvider . networkVersion || config . DEFAULT_ETHEREUM_NETWORK ; const auth = new AuthService ( { apolloClient : apolloClient ! , onAuthChange : onAuthChange ! } ) ; const features = new FeatureFlagService ( featureFlags ) ; const { provider , signer } = makeEthersProvider ( web3Provider , network ) ; const uniswap = new UniswapService ( provider , signer , network ) ; const ctx = { civil , auth , features , uniswap , web3Provider : web3 . currentProvider , currentUser : auth . currentUser , network , renderContext : opts . renderContext || RENDER_CONTEXT . DAPP , config , fireAnalyticsEvent : ( category , event , label , value ) => undefined , setAnalyticsEvent : ( fire ) : void => { ctx . fireAnalyticsEvent = fire ; } , waitForTx : async ( txHash ) => provider . waitForTransaction ( txHash ) , } ; return ctx ; } export const CivilContext = React . createContext < ICivilContext > ( null ) ;