Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './App.css';

import SetProtocol from 'setprotocol.js';
import BigNumber from 'bignumber.js';
import Web3 from 'web3';

// Kovan configuration
const config = {
Expand All @@ -15,27 +16,44 @@ const config = {
class App extends Component {
constructor() {
super();
const injectedWeb3 = window.web3 || undefined;
let setProtocol;
try {
// Use MetaMask/Mist provider
const provider = injectedWeb3.currentProvider;
setProtocol = new SetProtocol(provider, config);
} catch (err) {
// Throws when user doesn't have MetaMask/Mist running
throw new Error(`No injected web3 found when initializing setProtocol: ${err}`);
}

this.state = {
setProtocol,
web3: injectedWeb3,
setProtocol: null,
web3: null,
// Etherscan Links
createdSetLink: '',
};
this.createSet = this.createSet.bind(this);
this.getAccount = this.getAccount.bind(this);
}

componentDidMount() {
if (!this.state.web3) {
this.requestWeb3Access();
}
}

async requestWeb3Access() {
let setProtocol, injectedWeb3;
if (window.ethereum) {
try {
injectedWeb3 = new Web3(window.ethereum);
await window.ethereum.enable();
} catch (err) {
console.error(`Unable to access web3: ${err.message}`);
}
}
else if (window.web3) {
injectedWeb3 = window.web3;
}
if (injectedWeb3) {
setProtocol = new SetProtocol(injectedWeb3.currentProvider, config);
this.setState((prevState) => ({ web3: injectedWeb3, setProtocol }));
}
else {
console.error('Web3 provider not found.');
}
}

async createSet() {
const { setProtocol } = this.state;

Expand Down Expand Up @@ -99,8 +117,8 @@ class App extends Component {

getAccount() {
const { web3 } = this.state;
if (web3.eth.accounts[0]) return web3.eth.accounts[0];
throw new Error('Your MetaMask is locked. Unlock it to continue.');
if (web3 && web3.eth.accounts[0]) return web3.eth.accounts[0];
throw new Error('Unlock MetaMask or allow account access when prompted to continue.');
}

renderEtherScanLink(link, content) {
Expand Down