1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-15 01:34:57 +01:00

account for user balance for maximum check

This commit is contained in:
Matthias Kretschmann 2020-10-15 15:39:40 +02:00
parent f60163b54d
commit 51493ef548
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 31 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import React, { ReactElement, useState, ChangeEvent, useEffect } from 'react' import React, { ReactElement, useState, useEffect, ChangeEvent } from 'react'
import styles from './Add.module.css' import styles from './Add.module.css'
import { useOcean } from '@oceanprotocol/react' import { useOcean } from '@oceanprotocol/react'
import Header from './Header' import Header from './Header'
@ -12,7 +12,7 @@ import Tooltip from '../../../atoms/Tooltip'
import { ReactComponent as Caret } from '../../../../images/caret.svg' import { ReactComponent as Caret } from '../../../../images/caret.svg'
import { graphql, useStaticQuery } from 'gatsby' import { graphql, useStaticQuery } from 'gatsby'
import * as Yup from 'yup' import * as Yup from 'yup'
import { Field, FieldInputProps, Formik } from 'formik' import { Field, Formik } from 'formik'
import Input from '../../../atoms/Input' import Input from '../../../atoms/Input'
const contentQuery = graphql` const contentQuery = graphql`
@ -100,14 +100,23 @@ export default function Add({
if (!ocean) return if (!ocean) return
async function getMaximum() { async function getMaximum() {
const amountMax = const amountMaxPool =
coin === 'OCEAN' coin === 'OCEAN'
? await ocean.pool.getOceanMaxAddLiquidity(poolAddress) ? await ocean.pool.getOceanMaxAddLiquidity(poolAddress)
: await ocean.pool.getDTMaxAddLiquidity(poolAddress) : await ocean.pool.getDTMaxAddLiquidity(poolAddress)
const amountMax =
coin === 'OCEAN'
? Number(balance.ocean) > Number(amountMaxPool)
? amountMaxPool
: balance.ocean
: Number(dtBalance) > Number(amountMaxPool)
? amountMaxPool
: dtBalance
setAmountMax(amountMax) setAmountMax(amountMax)
} }
getMaximum() getMaximum()
}, [ocean, poolAddress, coin]) }, [ocean, poolAddress, coin, dtBalance, balance.ocean])
// Submit // Submit
async function handleAddLiquidity(amount: number, resetForm: () => void) { async function handleAddLiquidity(amount: number, resetForm: () => void) {
@ -152,7 +161,15 @@ export default function Add({
setSubmitting(false) setSubmitting(false)
}} }}
> >
{({ values, isSubmitting, setFieldValue, submitForm }) => { {({
values,
touched,
setTouched,
isSubmitting,
setFieldValue,
submitForm,
handleChange
}) => {
// TODO: move these 2 const to some useEffect() so it dos not // TODO: move these 2 const to some useEffect() so it dos not
// constantly re-renders all the time // constantly re-renders all the time
const newPoolTokens = const newPoolTokens =
@ -186,6 +203,7 @@ export default function Add({
<Input <Input
type="number" type="number"
max={amountMax} max={amountMax}
value={`${values.amount}`}
prefix={ prefix={
<Tooltip <Tooltip
content={<CoinSelect />} content={<CoinSelect />}
@ -200,6 +218,11 @@ export default function Add({
placeholder="0" placeholder="0"
field={field} field={field}
form={form} form={form}
onChange={(e) => {
// Workaround so validation kicks in on first touch
!touched?.amount && setTouched({ amount: true })
handleChange(e)
}}
/> />
)} )}
</Field> </Field>

View File

@ -1,7 +1,7 @@
import React, { ReactElement, useEffect, FormEvent } from 'react' import React, { ReactElement, useEffect, FormEvent } from 'react'
import styles from './PublishForm.module.css' import styles from './PublishForm.module.css'
import { useOcean } from '@oceanprotocol/react' import { useOcean } from '@oceanprotocol/react'
import { useFormikContext, Form, Field } from 'formik' import { useFormikContext, Field } from 'formik'
import Input from '../../atoms/Input' import Input from '../../atoms/Input'
import Button from '../../atoms/Button' import Button from '../../atoms/Button'
import { FormContent, FormFieldProps } from '../../../@types/Form' import { FormContent, FormFieldProps } from '../../../@types/Form'
@ -37,7 +37,7 @@ export default function PublishForm({
} }
return ( return (
<Form <form
className={styles.form} className={styles.form}
// do we need this? // do we need this?
onChange={() => status === 'empty' && setStatus(null)} onChange={() => status === 'empty' && setStatus(null)}
@ -61,6 +61,6 @@ export default function PublishForm({
</Button> </Button>
)} )}
</footer> </footer>
</Form> </form>
) )
} }