mirror of
https://github.com/status-im/liquid-funding.git
synced 2025-02-17 13:57:10 +00:00
use CurrencySelect in CreateFunding
This commit is contained in:
parent
5711fb4831
commit
1351f31fc1
@ -1,18 +1,14 @@
|
|||||||
/*global web3*/
|
/*global web3*/
|
||||||
import React, { useContext, useState, useEffect } from 'react';
|
import React, { useContext } from 'react';
|
||||||
import { Formik } from 'formik';
|
import { Formik } from 'formik';
|
||||||
import LiquidPledging from '../embarkArtifacts/contracts/LiquidPledging';
|
import LiquidPledging from '../embarkArtifacts/contracts/LiquidPledging';
|
||||||
import Button from '@material-ui/core/Button';
|
import Button from '@material-ui/core/Button';
|
||||||
import TextField from '@material-ui/core/TextField';
|
import TextField from '@material-ui/core/TextField';
|
||||||
import Snackbar from '@material-ui/core/Snackbar';
|
import Snackbar from '@material-ui/core/Snackbar';
|
||||||
import MenuItem from '@material-ui/core/MenuItem';
|
|
||||||
import FormControlLabel from '@material-ui/core/FormControlLabel'
|
|
||||||
import Switch from '@material-ui/core/Switch'
|
|
||||||
import { MySnackbarContentWrapper } from './base/SnackBars'
|
import { MySnackbarContentWrapper } from './base/SnackBars'
|
||||||
import { currencies, TOKEN_ICON_API, getTokenLabel } from '../utils/currencies'
|
import { getTokenLabel } from '../utils/currencies'
|
||||||
import { toEther } from '../utils/conversions'
|
|
||||||
import { getLpAllowance, standardTokenApproval } from '../utils/initialize'
|
|
||||||
import { FundingContext } from '../context'
|
import { FundingContext } from '../context'
|
||||||
|
import CurrencySelect from './base/CurrencySelect'
|
||||||
|
|
||||||
const { donate } = LiquidPledging.methods
|
const { donate } = LiquidPledging.methods
|
||||||
const _hoursToSeconds = hours => hours * 60 * 60
|
const _hoursToSeconds = hours => hours * 60 * 60
|
||||||
@ -24,42 +20,6 @@ const _addFunderSucessMsg = response => {
|
|||||||
const CreateFunding = ({ refreshTable }) => {
|
const CreateFunding = ({ refreshTable }) => {
|
||||||
const context = useContext(FundingContext)
|
const context = useContext(FundingContext)
|
||||||
const { account } = context
|
const { account } = context
|
||||||
const [balances, setBalances] = useState({})
|
|
||||||
const [allowances, setAllowances] = useState({})
|
|
||||||
|
|
||||||
const updateBalancesAllowances = () => {
|
|
||||||
const latestBalances = {}
|
|
||||||
const latestAllowances = {}
|
|
||||||
currencies.forEach(async c => {
|
|
||||||
if (c.contract) {
|
|
||||||
const amount = await c.contract.methods.balanceOf(account).call()
|
|
||||||
const allowance = await getLpAllowance(c.contract)
|
|
||||||
latestBalances[c.value] = toEther(amount)
|
|
||||||
latestAllowances[c.value] = toEther(allowance)
|
|
||||||
} else {
|
|
||||||
latestBalances[c.value] = '0'
|
|
||||||
latestAllowances[c.value] = '0'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
setBalances(latestBalances)
|
|
||||||
setAllowances(latestAllowances)
|
|
||||||
}
|
|
||||||
|
|
||||||
const toggleAllowance = e => {
|
|
||||||
const token = currencies[e.target.value]
|
|
||||||
const allowance = allowances[token.value]
|
|
||||||
standardTokenApproval(
|
|
||||||
token.contract,
|
|
||||||
Number(allowance) ? '0' : undefined
|
|
||||||
).then(res => {
|
|
||||||
const { events: { Approval: { returnValues: { value } } } } = res
|
|
||||||
setAllowances(state => ({ ...state, [token.value]: toEther(value) }))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (account) updateBalancesAllowances()
|
|
||||||
}, [account])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
@ -123,44 +83,16 @@ const CreateFunding = ({ refreshTable }) => {
|
|||||||
onBlur={handleBlur}
|
onBlur={handleBlur}
|
||||||
value={values.receiverId || ''}
|
value={values.receiverId || ''}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<CurrencySelect
|
||||||
id="tokenAddress"
|
id="tokenAddress"
|
||||||
name="tokenAddress"
|
name="tokenAddress"
|
||||||
select
|
|
||||||
label="Select token for funding"
|
label="Select token for funding"
|
||||||
placeholder="Select token for funding"
|
|
||||||
margin="normal"
|
|
||||||
variant="outlined"
|
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
onBlur={handleBlur}
|
onBlur={handleBlur}
|
||||||
value={values.tokenAddress || ''}
|
value={values.tokenAddress}
|
||||||
>
|
showBalances
|
||||||
{currencies.map((option, idx) => (
|
enableToggles
|
||||||
<MenuItem style={{display: 'flex', alignItems: 'center'}} key={option.value} value={option.value}>
|
/>
|
||||||
<div style={{display: 'flex', alignItems: 'center'}}>
|
|
||||||
{option.icon || <img
|
|
||||||
src={option.img || `${TOKEN_ICON_API}/${option.value}.png`}
|
|
||||||
style={{width: option.width, marginRight: '3%'}}
|
|
||||||
/>}
|
|
||||||
{option.label}
|
|
||||||
<span style={{ marginLeft: '10%' }}>Your Balance: <strong>{balances[option.value]}</strong></span>
|
|
||||||
<FormControlLabel
|
|
||||||
style={{ marginLeft: '10%' }}
|
|
||||||
onClick={e => e.stopPropagation()}
|
|
||||||
control={
|
|
||||||
<Switch
|
|
||||||
checked={!!Number(allowances[option.value])}
|
|
||||||
onChange={toggleAllowance}
|
|
||||||
value={idx}
|
|
||||||
color="primary"
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
label="Enabled"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</MenuItem>
|
|
||||||
))}
|
|
||||||
</TextField>
|
|
||||||
<TextField
|
<TextField
|
||||||
id="amount"
|
id="amount"
|
||||||
name="amount"
|
name="amount"
|
||||||
|
@ -12,8 +12,8 @@ import { FundingContext } from '../../context'
|
|||||||
CurrencySelect.propTypes = {
|
CurrencySelect.propTypes = {
|
||||||
id: PropTypes.string.isRequired,
|
id: PropTypes.string.isRequired,
|
||||||
label: PropTypes.string.isRequired,
|
label: PropTypes.string.isRequired,
|
||||||
handleChange: PropTypes.func.isRequired,
|
onChange: PropTypes.func.isRequired,
|
||||||
handleBlur: PropTypes.func.isRequired,
|
onBlur: PropTypes.func.isRequired,
|
||||||
value: PropTypes.string,
|
value: PropTypes.string,
|
||||||
showBalances: PropTypes.bool,
|
showBalances: PropTypes.bool,
|
||||||
enableToggles: PropTypes.bool
|
enableToggles: PropTypes.bool
|
||||||
@ -22,8 +22,8 @@ CurrencySelect.propTypes = {
|
|||||||
function CurrencySelect({
|
function CurrencySelect({
|
||||||
id,
|
id,
|
||||||
label,
|
label,
|
||||||
handleChange,
|
onChange,
|
||||||
handleBlur,
|
onBlur,
|
||||||
value,
|
value,
|
||||||
showBalances,
|
showBalances,
|
||||||
enableToggles
|
enableToggles
|
||||||
@ -67,7 +67,6 @@ function CurrencySelect({
|
|||||||
if (account && showBalances) updateBalancesAllowances()
|
if (account && showBalances) updateBalancesAllowances()
|
||||||
}, [account])
|
}, [account])
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TextField
|
<TextField
|
||||||
id={id}
|
id={id}
|
||||||
@ -77,8 +76,8 @@ function CurrencySelect({
|
|||||||
placeholder={label}
|
placeholder={label}
|
||||||
margin="normal"
|
margin="normal"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
onChange={handleChange}
|
onChange={onChange}
|
||||||
onBlur={handleBlur}
|
onBlur={onBlur}
|
||||||
value={value || ''}
|
value={value || ''}
|
||||||
>
|
>
|
||||||
{currencies.map((option, idx) => (
|
{currencies.map((option, idx) => (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user