import { Contract } from '@ethersproject/contracts' ; import { JsonRpcProvider , JsonRpcSigner } from '@ethersproject/providers' ; import { NetworkContext } from 'contexts/networkContext' ; import { ContractName } from 'models/contract-name' ; import { Contracts } from 'models/contracts' ; import { NetworkName } from 'models/network-name' ; import { useContext , useEffect , useState } from 'react' ; import { readProvider } from 'constants/readProvider' ; import { readNetwork } from 'constants/networks' ; export function useContractLoader ( ) { const [ contracts , setContracts ] = useState < Contracts > ( ) ; const { signingProvider } = useContext ( NetworkContext ) ; useEffect ( ( ) => { async function loadContracts ( ) { try { const network = readNetwork . name ; const signerOrProvider = signingProvider ?. getSigner ( ) ?? readProvider ; const newContracts = Object . values ( ContractName ) . reduce ( ( accumulator , contractName ) => ( { ... accumulator , [ contractName ] : loadContract ( contractName , network , signerOrProvider ) , } ) , { } as Contracts ) ; setContracts ( newContracts ) ; } catch ( e ) { console . log ( 'CONTRACT LOADER ERROR:' , e ) ; } } loadContracts ( ) ; } , [ signingProvider , setContracts ] ) ; return contracts ; } const loadContract = ( contractName , network , signerOrProvider ) : Contract => { let contract = require ( `@jbx-protocol/contracts-v1/deployments/${ network }/${ contractName }.json` ) ; return new Contract ( contract . address , contract . abi , signerOrProvider ) ; } ;