Created
November 1, 2022 09:12
-
-
Save ac12644/f7dc79a88d7fd9cd020d0fac45a65434 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
import Web3 from 'web3'; | |
import Web3Modal from 'web3modal'; | |
import FundraiserFactoryContract from 'contracts/FundraiserFactory.json'; | |
useEffect(() => { | |
init(); | |
}, []); | |
const init = async () => { | |
try { | |
const web3Modal = new Web3Modal({ | |
network: 'mainnet', | |
cacheProvider: true, | |
}); | |
const connection = await web3Modal.connect(); | |
const web3 = new Web3(connection); | |
const networkId = await web3.eth.net.getId(); | |
const deployedNetwork = FundraiserFactoryContract.networks[networkId]; | |
const accounts = await web3.eth.getAccounts(); | |
const instance = new web3.eth.Contract( | |
FundraiserFactoryContract.abi, | |
deployedNetwork && deployedNetwork.address, | |
); | |
} catch (error) { | |
alert( | |
`Failed to load web3, accounts, or contract. Check console for details.`, | |
); | |
console.error(error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment