Skip to content

Commit

Permalink
Add Warning Messages and various fixes (Uniswap#100)
Browse files Browse the repository at this point in the history
* Fix summary panel in Remove Liquidity

* Add warning message

* Add wrong network warning and env vars
  • Loading branch information
chikeichan authored Oct 28, 2018
1 parent c798045 commit bd28ef8
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 44 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
},
"scripts": {
"start": "react-scripts start",
"start:rinkeby": "REACT_APP_NETWORK_ID=4 REACT_APP_NETWORK='Rinkeby Test Network' react-scripts start",
"build": "react-scripts build && cp build/index.html build/404.html",
"build:rinkeby": "REACT_APP_NETWORK_ID=4 REACT_APP_NETWORK='Rinkeby Test Network' react-scripts build && cp build/index.html build/404.html",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"deploy": "npm run build&&gh-pages -d build"
Expand Down
74 changes: 60 additions & 14 deletions src/components/Header/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import classnames from 'classnames';
Expand Down Expand Up @@ -74,23 +74,38 @@ function isMobile() {
return ua.getDevice().type === 'mobile';
}

function Header (props) {
return (
<div className="header">
<div
className={classnames('header__dialog', {
'header__dialog--disconnected': !props.isConnected && props.initialized,
})}
>
<div>No Ethereum wallet found</div>
class BlockingWarning extends Component {
state = {
networkId: 0,
};

componentWillReceiveProps(nextProps) {
const { web3 } = nextProps;
const { networkId } = this.state;

if (!networkId) {
web3.eth.net.getId((_, networkId) => this.setState({ networkId }))
}
}

render () {
const {
isConnected,
initialized,
} = this.props;
const { networkId } = this.state;
let content = [];

if (!isConnected && initialized) {
content = [
<div>No Ethereum wallet found</div>,
<div className="header__dialog__description">
{
isMobile()
? 'Please visit us from a web3-enabled mobile browser, such as Trust Wallet and Cipher Browser.'
: 'Please visit us after installing Metamask on Chrome or Brave.'

}
</div>
</div>,
<div className="header__download">
{
isMobile()
Expand All @@ -107,8 +122,38 @@ function Header (props) {
]
)
}
</div>
</div>,
]
}

const correctNetworkId = process.env.REACT_APP_NETWORK_ID || 1;
const correctNetwork = process.env.REACT_APP_NETWORK || 'Main Ethereum Network';
const wrongNetwork = networkId != correctNetworkId;
if (wrongNetwork) {
content = [
<div>You are on the wrong network</div>,
<div className="header__dialog__description">
{`Please switch to ${correctNetwork}`}
</div>,
];
}

return (
<div
className={classnames('header__dialog', {
'header__dialog--disconnected': (!isConnected || wrongNetwork) && initialized,
})}
>
{content}
</div>
);
}
}

function Header (props) {
return (
<div className="header">
<BlockingWarning {...props} />
<div
className={classnames('header__top', {
'header--inactive': !props.isConnected,
Expand All @@ -133,6 +178,7 @@ export default connect(
state => ({
currentAddress: state.web3connect.account,
initialized: state.web3connect.initialized,
isConnected: !!state.web3connect.web3 && !!state.web3connect.account,
isConnected: !!state.web3connect.account && state.web3connect.networkId == process.env.REACT_APP_NETWORK_ID,
web3: state.web3connect.web3,
}),
)(Header);
12 changes: 12 additions & 0 deletions src/components/NavigationTabs/beta-message.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@import "../../variables";

.beta-message {
padding: 1rem;
margin-bottom: 1rem;
border: 1px solid rgba($salmon-red, .4);
background-color: rgba($salmon-red, .1);
border-radius: 1rem;
font-size: .75rem;
line-height: 1rem;
text-align: center;
}
17 changes: 12 additions & 5 deletions src/components/NavigationTabs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { withRouter } from 'react-router-dom';
import PropTypes from 'prop-types';
import {Tab, Tabs} from "../Tab";

import './beta-message.scss';

class NavigationTabs extends Component {
static propTypes = {
history: PropTypes.shape({
Expand Down Expand Up @@ -32,11 +34,16 @@ class NavigationTabs extends Component {

render() {
return (
<Tabs className={this.props.className}>
{ this.renderTab('Swap', '/swap', /swap/) }
{ this.renderTab('Send', '/send', /send/) }
{ this.renderTab('Pool', '/add-liquidity', /add-liquidity|remove-liquidity|create-exchange/) }
</Tabs>
<div>
<Tabs className={this.props.className}>
{ this.renderTab('Swap', '/swap', /swap/) }
{ this.renderTab('Send', '/send', /send/) }
{ this.renderTab('Pool', '/add-liquidity', /add-liquidity|remove-liquidity|create-exchange/) }
</Tabs>
<div className="beta-message">
🦄 Uniswap is an experimental project. Use at your own risk 💀
</div>
</div>
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ducks/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const setAddresses = networkId => {
// Rinkeby
case 4:
case '4':
default:
return {
type: SET_ADDRESSES,
payload: RINKEBY,
Expand Down
12 changes: 12 additions & 0 deletions src/ducks/web3connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export const UPDATE_TOKEN_BALANCE = 'web3connect/updateTokenBalance';
export const WATCH_APPROVALS = 'web3connect/watchApprovals';
export const UPDATE_APPROVALS = 'web3connect/updateApprovals';
export const ADD_CONTRACT = 'web3connect/addContract';
export const UPDATE_NETWORK_ID = 'web3connect/updateNetworkId';

const initialState = {
web3: null,
networkId: 0,
initialized: false,
account: '',
balances: {
Expand Down Expand Up @@ -212,6 +214,7 @@ export const sync = () => async (dispatch, getState) => {
account,
watched,
contracts,
networkId,
} = getState().web3connect;

// Sync Account
Expand All @@ -221,6 +224,13 @@ export const sync = () => async (dispatch, getState) => {
dispatch(watchBalance({ balanceOf: accounts[0] }));
}

if (!networkId) {
dispatch({
type: UPDATE_NETWORK_ID,
payload: await web3.eth.net.getId(),
});
}

// Sync Ethereum Balances
watched.balances.ethereum.forEach(async address => {
const balance = await web3.eth.getBalance(address);
Expand Down Expand Up @@ -433,6 +443,8 @@ export default function web3connectReducer(state = initialState, { type, payload
},
},
};
case UPDATE_NETWORK_ID:
return { ...state, networkId: payload };
default:
return state;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Pool/AddLiquidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class AddLiquidity extends Component {
</div>
<div className="pool__exchange-rate-wrapper">
<span className="swap__exchange-rate">Current Pool Size</span>
<span>{` ${ethValue.dividedBy(10 ** 18).toFixed(2)} ${eth} + ${tokenValue.dividedBy(10 ** decimals).toFixed(2)} ${label}`}</span>
<span>{` ${ethValue.dividedBy(10 ** 18).toFixed(2)} ${eth} / ${tokenValue.dividedBy(10 ** decimals).toFixed(2)} ${label}`}</span>
</div>
</div>
)
Expand Down Expand Up @@ -553,7 +553,7 @@ class AddLiquidity extends Component {

export default connect(
state => ({
isConnected: Boolean(state.web3connect.account),
isConnected: Boolean(state.web3connect.account) && state.web3connect.networkId == process.env.REACT_APP_NETWORK_ID,
account: state.web3connect.account,
balances: state.web3connect.balances,
web3: state.web3connect.web3,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Pool/CreateExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class CreateExchange extends Component {

export default connect(
state => ({
isConnected: Boolean(state.web3connect.account),
isConnected: Boolean(state.web3connect.account) && state.web3connect.networkId == process.env.REACT_APP_NETWORK_ID,
account: state.web3connect.account,
balances: state.web3connect.balances,
web3: state.web3connect.web3,
Expand Down
49 changes: 30 additions & 19 deletions src/pages/Pool/RemoveLiquidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class RemoveLiquidity extends Component {


const exchangeAddress = fromToken[tokenAddress];
if (!exchangeAddress || !web3 || !input) {
if (!exchangeAddress || !web3) {
return [
<CurrencyInputPanel
key="remove-liquidity-input"
Expand Down Expand Up @@ -183,46 +183,57 @@ class RemoveLiquidity extends Component {
</OversizedPanel>
];
}
const { value, decimals } = getBalance(account, exchangeAddress);
const { value: liquidityBalance } = getBalance(account, exchangeAddress);
const { value: ethReserve } = getBalance(exchangeAddress);
const { value: tokenReserve, label } = getBalance(exchangeAddress, tokenAddress);
const { value: tokenReserve, decimals: tokenDecimals, label } = getBalance(exchangeAddress, tokenAddress);

const ownership = value.dividedBy(totalSupply);
const ownership = liquidityBalance.dividedBy(totalSupply);
const ethPer = ethReserve.dividedBy(totalSupply);
const tokenPer = tokenReserve.dividedBy(totalSupply);
const exchangeRate = tokenReserve.div(ethReserve);

const ownedEth = ethPer.multipliedBy(liquidityBalance).dividedBy(10 ** 18);
const ownedToken = tokenPer.multipliedBy(liquidityBalance).dividedBy(10 ** tokenDecimals);

return [
<CurrencyInputPanel
title="Output"
description="(estimated)"
key="remove-liquidity-input"
renderInput={() => (
<div className="remove-liquidity__output">
<div className="remove-liquidity__output-text">
{`${ethPer.multipliedBy(input).toFixed(3)} ETH`}
</div>
<div className="remove-liquidity__output-plus"> + </div>
<div className="remove-liquidity__output-text">
{`${tokenPer.multipliedBy(input).toFixed(3)} ${label}`}
renderInput={() => input
? (
<div className="remove-liquidity__output">
<div className="remove-liquidity__output-text">
{`${ethPer.multipliedBy(input).toFixed(3)} ETH`}
</div>
<div className="remove-liquidity__output-plus"> + </div>
<div className="remove-liquidity__output-text">
{`${tokenPer.multipliedBy(input).toFixed(3)} ${label}`}
</div>
</div>
</div>
)}
)
: <div className="remove-liquidity__output" />
}
disableTokenSelect
disableUnlock
/>,
<OversizedPanel key="remove-liquidity-input-under" hideBottom>
<div className="pool__summary-panel">
<div className="pool__exchange-rate-wrapper">
<span className="pool__exchange-rate">Exchange Rate</span>
<span>{` ${ethReserve.dividedBy(10 ** 18).toFixed(2)} ETH + ${tokenReserve.dividedBy(10 ** decimals).toFixed(2)} ${label}`}</span>
<span>
{`1 ETH = ${exchangeRate.toFixed(4)} ${label}`}
</span>
</div>
<div className="pool__exchange-rate-wrapper">
<span className="swap__exchange-rate">Current Pool Size</span>
<span>{totalSupply.dividedBy(10 ** decimals).toFixed(4)}</span>
<span>{`${ethReserve.dividedBy(10 ** 18).toFixed(2)} ETH / ${tokenReserve.dividedBy(10 ** tokenDecimals).toFixed(2)} ${label}`}</span>
</div>
<div className="pool__exchange-rate-wrapper">
<span className="swap__exchange-rate">Your Pool Share</span>
<span>{ownership.multipliedBy(100).toFixed(2)}%</span>
<span className="swap__exchange-rate">
Your Pool Share ({ownership.multipliedBy(100).toFixed(2)}%)
</span>
<span>{`${ownedEth.toFixed(2)} ETH / ${ownedToken.toFixed(2)} ${label}`}</span>
</div>
</div>
</OversizedPanel>
Expand Down Expand Up @@ -282,7 +293,7 @@ class RemoveLiquidity extends Component {

export default connect(
state => ({
isConnected: Boolean(state.web3connect.account),
isConnected: Boolean(state.web3connect.account) && state.web3connect.networkId == process.env.REACT_APP_NETWORK_ID,
web3: state.web3connect.web3,
balances: state.web3connect.balances,
account: state.web3connect.account,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Send/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ class Send extends Component {
</OversizedPanel>
);
}
console.log(outputLabel)

return (
<OversizedPanel hideBottom>
<div className="swap__exchange-rate-wrapper">
Expand Down Expand Up @@ -776,7 +776,7 @@ class Send extends Component {
export default connect(
state => ({
balances: state.web3connect.balances,
isConnected: !!state.web3connect.account,
isConnected: !!state.web3connect.account && state.web3connect.networkId == process.env.REACT_APP_NETWORK_ID,
account: state.web3connect.account,
web3: state.web3connect.web3,
exchangeAddresses: state.addresses.exchangeAddresses,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Swap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ class Swap extends Component {
export default connect(
state => ({
balances: state.web3connect.balances,
isConnected: !!state.web3connect.account,
isConnected: !!state.web3connect.account && state.web3connect.networkId == process.env.REACT_APP_NETWORK_ID,
account: state.web3connect.account,
web3: state.web3connect.web3,
exchangeAddresses: state.addresses.exchangeAddresses,
Expand Down

0 comments on commit bd28ef8

Please sign in to comment.