Commit 39248a0f authored by Chi Kei Chan's avatar Chi Kei Chan Committed by GitHub

Handle first liquidity deposit and add warning message (#88)

parent 43244fec
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1 6H6M11 6L6 6M6 1V6M6 6L6 11" stroke="#2F80ED" stroke-linecap="round"/>
</svg>
...@@ -260,7 +260,7 @@ export const sync = () => async (dispatch, getState) => { ...@@ -260,7 +260,7 @@ export const sync = () => async (dispatch, getState) => {
const decimals = tokenBalance.decimals || await contract.methods.decimals().call(); const decimals = tokenBalance.decimals || await contract.methods.decimals().call();
const symbol = TOKEN_LABEL_FALLBACK[tokenAddress] || tokenBalance.label || await contract.methods.symbol().call(); const symbol = TOKEN_LABEL_FALLBACK[tokenAddress] || tokenBalance.label || await contract.methods.symbol().call();
if (tokenBalance.value.isEqualTo(BN(balance))) { if (tokenBalance.value.isEqualTo(BN(balance)) && tokenBalance.label && tokenBalance.decimals) {
return; return;
} }
......
...@@ -7,8 +7,8 @@ import CurrencyInputPanel from '../../components/CurrencyInputPanel'; ...@@ -7,8 +7,8 @@ import CurrencyInputPanel from '../../components/CurrencyInputPanel';
import OversizedPanel from '../../components/OversizedPanel'; import OversizedPanel from '../../components/OversizedPanel';
import NavigationTabs from '../../components/NavigationTabs'; import NavigationTabs from '../../components/NavigationTabs';
import Modal from '../../components/Modal'; import Modal from '../../components/Modal';
import { selectors, sync } from '../../ducks/web3connect'; import { selectors } from '../../ducks/web3connect';
import ArrowDown from '../../assets/images/arrow-down-blue.svg'; import ArrowDown from '../../assets/images/plus-blue.svg';
import DropdownBlue from "../../assets/images/dropdown-blue.svg"; import DropdownBlue from "../../assets/images/dropdown-blue.svg";
import DropupBlue from "../../assets/images/dropup-blue.svg"; import DropupBlue from "../../assets/images/dropup-blue.svg";
import ModeSelector from './ModeSelector'; import ModeSelector from './ModeSelector';
...@@ -57,6 +57,41 @@ class AddLiquidity extends Component { ...@@ -57,6 +57,41 @@ class AddLiquidity extends Component {
showSummaryModal !== nextState.showSummaryModal; showSummaryModal !== nextState.showSummaryModal;
} }
componentWillReceiveProps() {
this.recalcForm();
}
recalcForm = () => {
const {
outputCurrency,
inputValue,
outputValue,
lastEditedField,
} = this.state;
const exchangeRate = this.getExchangeRate();
const append = {};
if (!outputCurrency || this.isNewExchange()) {
return;
}
if (lastEditedField === INPUT) {
const newOutputValue = exchangeRate.multipliedBy(inputValue).toFixed(7);
if (newOutputValue !== outputValue) {
append.outputValue = newOutputValue;
}
}
if (lastEditedField === OUTPUT) {
const newInputValue = BN(outputValue).dividedBy(exchangeRate).toFixed(7);
if (newInputValue !== inputValue) {
append.inputValue = newInputValue;
}
}
this.setState(append);
}
getBalance(currency) { getBalance(currency) {
const { selectors, account } = this.props; const { selectors, account } = this.props;
...@@ -130,11 +165,16 @@ class AddLiquidity extends Component { ...@@ -130,11 +165,16 @@ class AddLiquidity extends Component {
outputValue = BN(value).dividedBy(exchangeRate).toFixed(7); outputValue = BN(value).dividedBy(exchangeRate).toFixed(7);
} }
this.setState({ const append = {
outputValue,
inputValue: value, inputValue: value,
lastEditedField: INPUT, lastEditedField: INPUT,
}); };
if (!this.isNewExchange()) {
append.outputValue = outputValue;
}
this.setState(append);
}; };
onOutputChange = value => { onOutputChange = value => {
...@@ -150,13 +190,34 @@ class AddLiquidity extends Component { ...@@ -150,13 +190,34 @@ class AddLiquidity extends Component {
inputValue = exchangeRate.multipliedBy(value).toFixed(7); inputValue = exchangeRate.multipliedBy(value).toFixed(7);
} }
this.setState({ const append = {
inputValue,
outputValue: value, outputValue: value,
lastEditedField: OUTPUT, lastEditedField: INPUT,
}); };
if (!this.isNewExchange()) {
append.inputValue = inputValue;
}
this.setState(append);
}; };
isNewExchange() {
const { selectors, exchangeAddresses: { fromToken } } = this.props;
const { inputCurrency, outputCurrency } = this.state;
const eth = [inputCurrency, outputCurrency].filter(currency => currency === 'ETH')[0];
const token = [inputCurrency, outputCurrency].filter(currency => currency !== 'ETH')[0];
if (!eth || !token) {
return false;
}
const { value: tokenValue } = selectors().getBalance(fromToken[token], token);
const { value: ethValue } = selectors().getBalance(fromToken[token], eth);
return tokenValue.isZero() && ethValue.isZero();
}
getExchangeRate() { getExchangeRate() {
const { selectors, exchangeAddresses: { fromToken } } = this.props; const { selectors, exchangeAddresses: { fromToken } } = this.props;
const { inputCurrency, outputCurrency } = this.state; const { inputCurrency, outputCurrency } = this.state;
...@@ -209,33 +270,52 @@ class AddLiquidity extends Component { ...@@ -209,33 +270,52 @@ class AddLiquidity extends Component {
} }
renderInfo() { renderInfo() {
const blank = (
<div className="pool__summary-panel">
<div className="pool__exchange-rate-wrapper">
<span className="pool__exchange-rate">Exchange Rate</span>
<span> - </span>
</div>
<div className="pool__exchange-rate-wrapper">
<span className="swap__exchange-rate">Current Pool Size</span>
<span> - </span>
</div>
</div>
);
const { selectors, exchangeAddresses: { fromToken } } = this.props; const { selectors, exchangeAddresses: { fromToken } } = this.props;
const { inputCurrency, outputCurrency } = this.state; const { getBalance } = selectors();
const { inputCurrency, outputCurrency, inputValue, outputValue } = this.state;
const eth = [inputCurrency, outputCurrency].filter(currency => currency === 'ETH')[0]; const eth = [inputCurrency, outputCurrency].filter(currency => currency === 'ETH')[0];
const token = [inputCurrency, outputCurrency].filter(currency => currency !== 'ETH')[0]; const token = [inputCurrency, outputCurrency].filter(currency => currency !== 'ETH')[0];
if (!eth || !token) { if (!eth || !token) {
return blank;
}
const { value: tokenValue, decimals, label } = getBalance(fromToken[token], token);
const { value: ethValue } = getBalance(fromToken[token]);
if (this.isNewExchange()) {
const rate = BN(outputValue).dividedBy(inputValue);
const rateText = rate.isNaN() ? '---' : rate.toFixed(4);
return ( return (
<div className="pool__summary-panel"> <div className="pool__summary-panel">
<div className="pool__exchange-rate-wrapper"> <div className="pool__exchange-rate-wrapper">
<span className="pool__exchange-rate">Exchange Rate</span> <span className="pool__exchange-rate">Exchange Rate</span>
<span> - </span> <span>{`1 ETH = ${rateText} ${label}`}</span>
</div> </div>
<div className="pool__exchange-rate-wrapper"> <div className="pool__exchange-rate-wrapper">
<span className="swap__exchange-rate">Current Pool Size</span> <span className="swap__exchange-rate">Current Pool Size</span>
<span> - </span> <span>{` ${ethValue.dividedBy(10 ** 18).toFixed(2)} ${eth} + ${tokenValue.dividedBy(10 ** decimals).toFixed(2)} ${label}`}</span>
</div> </div>
</div> </div>
) )
} }
const { if (tokenValue.dividedBy(ethValue).isNaN()) {
value: tokenValue, return blank;
decimals, }
label
} = selectors().getTokenBalance(token, fromToken[token]);
const { value: ethValue } = selectors().getBalance(fromToken[token]);
return ( return (
<div className="pool__summary-panel"> <div className="pool__summary-panel">
...@@ -302,7 +382,7 @@ class AddLiquidity extends Component { ...@@ -302,7 +382,7 @@ class AddLiquidity extends Component {
) )
} }
const { value, decimals, label } = selectors().getTokenBalance(outputCurrency, fromToken[outputCurrency]); const { label } = selectors().getTokenBalance(outputCurrency, fromToken[outputCurrency]);
if (!inputValue || !outputValue) { if (!inputValue || !outputValue) {
return ( return (
...@@ -330,7 +410,6 @@ class AddLiquidity extends Component { ...@@ -330,7 +410,6 @@ class AddLiquidity extends Component {
const { const {
inputValue, inputValue,
outputValue, outputValue,
inputCurrency,
outputCurrency, outputCurrency,
showSummaryModal, showSummaryModal,
} = this.state; } = this.state;
...@@ -381,6 +460,8 @@ class AddLiquidity extends Component { ...@@ -381,6 +460,8 @@ class AddLiquidity extends Component {
render() { render() {
const { const {
isConnected, isConnected,
exchangeAddresses: { fromToken },
selectors,
} = this.props; } = this.props;
const { const {
...@@ -388,10 +469,10 @@ class AddLiquidity extends Component { ...@@ -388,10 +469,10 @@ class AddLiquidity extends Component {
outputValue, outputValue,
inputCurrency, inputCurrency,
outputCurrency, outputCurrency,
lastEditedField,
} = this.state; } = this.state;
const { inputError, outputError, isValid } = this.validate(); const { inputError, outputError, isValid } = this.validate();
const { label } = selectors().getTokenBalance(outputCurrency, fromToken[outputCurrency]);
return [ return [
<div <div
...@@ -405,6 +486,20 @@ class AddLiquidity extends Component { ...@@ -405,6 +486,20 @@ class AddLiquidity extends Component {
'header--inactive': !isConnected, 'header--inactive': !isConnected,
})} })}
/> />
{
this.isNewExchange()
? (
<div className="pool__new-exchange-warning">
<div className="pool__new-exchange-warning-text">
You are the first person to add liquidity🚰!
</div>
<div className="pool__new-exchange-warning-text">
{`A new exchange rate will be set based on your deposits. Please make sure that your ETH and ${label} deposits have the same fiat value.`}
</div>
</div>
)
: null
}
<ModeSelector /> <ModeSelector />
<CurrencyInputPanel <CurrencyInputPanel
title="Deposit" title="Deposit"
...@@ -426,7 +521,9 @@ class AddLiquidity extends Component { ...@@ -426,7 +521,9 @@ class AddLiquidity extends Component {
extraText={this.getBalance(outputCurrency)} extraText={this.getBalance(outputCurrency)}
selectedTokenAddress={outputCurrency} selectedTokenAddress={outputCurrency}
onCurrencySelected={currency => { onCurrencySelected={currency => {
this.setState({ outputCurrency: currency }); this.setState({
outputCurrency: currency,
}, this.recalcForm);
}} }}
onValueChange={this.onOutputChange} onValueChange={this.onOutputChange}
value={outputValue} value={outputValue}
...@@ -465,7 +562,6 @@ export default drizzleConnect( ...@@ -465,7 +562,6 @@ export default drizzleConnect(
}), }),
dispatch => ({ dispatch => ({
selectors: () => dispatch(selectors()), selectors: () => dispatch(selectors()),
sync: () => dispatch(sync()),
}) })
) )
......
...@@ -58,6 +58,25 @@ ...@@ -58,6 +58,25 @@
background: $mercury-gray; background: $mercury-gray;
} }
} }
&__new-exchange-warning {
padding: 1rem;
margin-bottom: 2rem;
border: 1px solid rgba($pizazz-orange, .4);
background-color: rgba($pizazz-orange, .1);
border-radius: 1rem;
}
&__new-exchange-warning-text {
font-size: .75rem;
line-height: 1rem;
text-align: center;
&:first-child {
padding-bottom: .3rem;
font-weight: 500;
}
}
} }
.pool-modal { .pool-modal {
......
...@@ -18,7 +18,10 @@ $royal-blue: #2F80ED; ...@@ -18,7 +18,10 @@ $royal-blue: #2F80ED;
$wisteria-purple: #AE60B9; $wisteria-purple: #AE60B9;
// Red // Red
$salmon-red: #FF8368; $salmon-red: #FF6871;
// Orange
$pizazz-orange: #FF8F05;
%col-nowrap { %col-nowrap {
display: flex; display: flex;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment