Skip to content

Instantly share code, notes, and snippets.

@ac12644
Created November 1, 2022 09:12
Show Gist options
  • Save ac12644/f7dc79a88d7fd9cd020d0fac45a65434 to your computer and use it in GitHub Desktop.
Save ac12644/f7dc79a88d7fd9cd020d0fac45a65434 to your computer and use it in GitHub Desktop.
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