From c0689585139f604d1dd1d599df713b27b199cbb7 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 13 Oct 2020 14:02:06 +0200 Subject: [PATCH] use slider prototype --- .../AssetActions/Pool/Remove.module.css | 13 +- .../organisms/AssetActions/Pool/Remove.tsx | 128 ++++++------------ .../organisms/AssetActions/Pool/index.tsx | 1 + 3 files changed, 54 insertions(+), 88 deletions(-) diff --git a/src/components/organisms/AssetActions/Pool/Remove.module.css b/src/components/organisms/AssetActions/Pool/Remove.module.css index f565b05a7..3c7e54727 100644 --- a/src/components/organisms/AssetActions/Pool/Remove.module.css +++ b/src/components/organisms/AssetActions/Pool/Remove.module.css @@ -12,11 +12,14 @@ composes: userLiquidity from './Add.module.css'; } -.removeRow { - position: relative; - margin-bottom: var(--spacer); +.range { + text-align: center; } -.removeRow:last-child { - margin-bottom: 0; +.range h3 { + margin-bottom: calc(var(--spacer) / 4); +} + +.range input { + width: 100%; } diff --git a/src/components/organisms/AssetActions/Pool/Remove.tsx b/src/components/organisms/AssetActions/Pool/Remove.tsx index 7eae14de3..d1b017d7e 100644 --- a/src/components/organisms/AssetActions/Pool/Remove.tsx +++ b/src/components/organisms/AssetActions/Pool/Remove.tsx @@ -1,47 +1,44 @@ -import React, { ReactElement, useState, ChangeEvent } from 'react' +import React, { ReactElement, useState, ChangeEvent, useEffect } from 'react' import styles from './Remove.module.css' import { useOcean } from '@oceanprotocol/react' import Header from './Header' import { toast } from 'react-toastify' -import InputElement from '../../../atoms/Input/InputElement' import Actions from './Actions' import { Logger } from '@oceanprotocol/lib' -import Button from '../../../atoms/Button' -import PriceUnit from '../../../atoms/Price/PriceUnit' import { Balance } from '.' +import Token from './Token' export default function Remove({ setShowRemove, poolAddress, + poolTokens, totalPoolTokens, userLiquidity, dtSymbol }: { setShowRemove: (show: boolean) => void poolAddress: string + poolTokens: string totalPoolTokens: string userLiquidity: Balance dtSymbol: string }): ReactElement { const { ocean, accountId } = useOcean() - const [amountOcean, setAmountOcean] = useState('') - const [amountDatatoken, setAmountDatatoken] = useState('') + const [amountPercent, setAmountPercent] = useState('0') + const [amountPoolShares, setAmountPoolShares] = useState('0') + const [amountOcean, setAmountOcean] = useState('0') + const [amountDatatoken, setAmountDatatoken] = useState('0') const [isLoading, setIsLoading] = useState() const [txId, setTxId] = useState('') async function handleRemoveLiquidity() { setIsLoading(true) - // TODO: can remove OCEAN & DT in one transaction? - // exitswapExternAmountOut? exitswapPoolAmountIn? - // TODO: when user hits 'use max', use pool.exitPool() - try { - const result = await ocean.pool.removeOceanLiquidity( + const result = await ocean.pool.removePoolLiquidity( accountId, poolAddress, - amountOcean, - totalPoolTokens + amountPoolShares ) setTxId(result.transactionHash) } catch (error) { @@ -52,26 +49,32 @@ export default function Remove({ } } - // TODO: enforce correct weight rules - function handleOceanAmountChange(e: ChangeEvent) { - setAmountOcean(e.target.value) - setAmountDatatoken(`${Number(e.target.value) * 9}`) + function handleAmountPercentChange(e: ChangeEvent) { + setAmountPercent(e.target.value) } - function handleMaxOcean() { - setAmountOcean(`${userLiquidity.ocean}`) - setAmountDatatoken(`${userLiquidity.ocean * 9}`) - } + useEffect(() => { + if (!ocean) return - function handleMaxDatatoken() { - setAmountDatatoken(`${userLiquidity.datatoken}`) - setAmountOcean(`${userLiquidity.ocean / 9}`) - } + async function getValues() { + const amountPoolShares = + (Number(poolTokens) / Number(amountPercent)) * 100 + setAmountPoolShares(`${amountPoolShares}`) - function handleDatatokenAmountChange(e: ChangeEvent) { - setAmountDatatoken(e.target.value) - setAmountOcean(`${Number(e.target.value) / 9}`) - } + const amountOcean = await ocean.pool.getPoolSharesForRemoveOcean( + poolAddress, + `${amountPoolShares}` + ) + setAmountOcean(amountOcean) + + const amountDatatoken = await ocean.pool.getPoolSharesForRemoveDT( + poolAddress, + `${amountPoolShares}` + ) + setAmountDatatoken(amountDatatoken) + } + getValues() + }, [amountPercent, ocean, poolTokens, poolAddress]) return (
@@ -81,65 +84,24 @@ export default function Remove({ />
-
-
- Your pool liquidity: - -
- +

{amountPercent}%

+ - - {userLiquidity.ocean > Number(amountOcean) && ( - - )} -
- -
-
- Your pool liquidity: - -
- - - {userLiquidity.datatoken > Number(amountDatatoken) && ( - - )} -
+

You will receive

+ + +