From 1ff0878f53098a7e96303031aadaf4e707598849 Mon Sep 17 00:00:00 2001 From: Alexangelj Date: Wed, 3 Feb 2021 12:38:12 -0800 Subject: [PATCH 01/22] fix(pool-size): Updates pool size to show entire pool. Adds Your Liquidity column. --- src/components/Liquidity/LiquidityTable.tsx | 38 +------------ .../LiquidityTableHeader.tsx | 6 +- .../LiquidityTableRow/LiquidityTableRow.tsx | 57 ++++++++++++++++++- 3 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/components/Liquidity/LiquidityTable.tsx b/src/components/Liquidity/LiquidityTable.tsx index a1a1cbc1..af934302 100644 --- a/src/components/Liquidity/LiquidityTable.tsx +++ b/src/components/Liquidity/LiquidityTable.tsx @@ -1,6 +1,5 @@ import React, { useCallback } from 'react' import styled from 'styled-components' -import Spacer from '@/components/Spacer' // table import Table from '@/components/Table' @@ -12,12 +11,11 @@ import LiquidityTableRow from './LiquidityTable/LiquidityTableRow' import LoadingTable from '@/components/Market/OptionsTable/LoadingTable' import { usePositions } from '@/state/positions/hooks' -import { useItem, useUpdateItem, useRemoveItem } from '@/state/order/hooks' -import { useOptions, useUpdateOptions } from '@/state/options/hooks' -import { formatEther, parseEther } from 'ethers/lib/utils' +import { useUpdateItem } from '@/state/order/hooks' +import { useOptions } from '@/state/options/hooks' +import { formatEther } from 'ethers/lib/utils' import { BigNumber } from 'ethers' import { Operation } from '@primitivefi/sdk' -import { Fraction, TokenAmount } from '@uniswap/sdk' export interface OptionsTableProps { callActive: boolean @@ -29,30 +27,12 @@ const LiquidityTable: React.FC = (props) => { const updateItem = useUpdateItem() const options = useOptions() const type = callActive ? 'calls' : 'puts' - /* const tableColumns: TableColumns = { - key: 'key', - asset: 'tableAssset', - strike: '0.00', - share: '0.00', - asset1: '0.00', - asset2: '0.00', - fees: '0.00', - liquidity: ['0.00', '0.00'], - expiry: 0, - isCall: true, - } */ const formatTableColumns = useCallback( (option: any): TableColumns => { const tableKey: string = option.entity.address /* const tableAssset: string = asset.toUpperCase() */ const tableStrike: string = option.entity.strikePrice.toString() - const tableBid: string = formatEther( - option.market.spotClosePremium.raw.toString() - ).toString() - const tableAsk: string = formatEther( - option.market.spotOpenPremium.raw.toString() - ).toString() const tableReserve0: string = formatEther( option.market.reserveOf(option.entity.underlying).raw.toString() @@ -65,18 +45,6 @@ const LiquidityTable: React.FC = (props) => { const tableReserves: string[] = [tableReserve0, tableReserve1] const tableExpiry: number = option.entity.expiryValue - /* const supply = BigNumber.from(parseEther(lpTotalSupply).toString()) - const tSupply = new TokenAmount( - option.market.liquidityToken, - parseEther(lpTotalSupply).toString() - ) - - supply.gt(0) && lpBalance.gt(0) - ? lpBalance.divide(tSupply.add(lpBalance)) - : new Fraction('0') - - poolShare = poolShare.multiply('100').toSignificant(6) */ - const tableColumns: TableColumns = { key: tableKey, asset: option.entity.underlying.symbol, diff --git a/src/components/Liquidity/LiquidityTable/LiquidityTableHeader/LiquidityTableHeader.tsx b/src/components/Liquidity/LiquidityTable/LiquidityTableHeader/LiquidityTableHeader.tsx index f930d785..633b12fc 100644 --- a/src/components/Liquidity/LiquidityTable/LiquidityTableHeader/LiquidityTableHeader.tsx +++ b/src/components/Liquidity/LiquidityTable/LiquidityTableHeader/LiquidityTableHeader.tsx @@ -17,12 +17,16 @@ export const headers = [ }, { name: 'Pool Size', - tip: 'The amount of underlying tokens in the pool.', + tip: 'The total amount of tokens in the pool.', }, { name: 'Your Share', tip: 'The proportion of ownership of the option pair.', }, + { + name: 'Your Liquidity', + tip: 'Your quantity of tokens in the pool.', + }, { name: 'Market', tip: 'The option market the pool serves.', diff --git a/src/components/Liquidity/LiquidityTable/LiquidityTableRow/LiquidityTableRow.tsx b/src/components/Liquidity/LiquidityTable/LiquidityTableRow/LiquidityTableRow.tsx index b94656f9..aec25be9 100644 --- a/src/components/Liquidity/LiquidityTable/LiquidityTableRow/LiquidityTableRow.tsx +++ b/src/components/Liquidity/LiquidityTable/LiquidityTableRow/LiquidityTableRow.tsx @@ -196,6 +196,41 @@ const LiquidityTableRow: React.FC = ({ return { shortPerLp, underlyingPerLp, totalUnderlyingPerLp } }, [market, lp, lpTotalSupply]) + const calculateTotalLiquidity = useCallback(() => { + if ( + typeof market === 'undefined' || + market === null || + BigNumber.from(parseEther(lpTotalSupply)).isZero() + ) + return { + shortPerLp: '0', + underlyingPerLp: '0', + totalUnderlyingPerLp: '0', + } + + const [ + shortValue, + underlyingValue, + totalUnderlyingValue, + ] = market.getLiquidityValuePerShare( + new TokenAmount( + market.liquidityToken, + parseEther(lpTotalSupply).toString() + ) + ) + const shortPerLp = parseEther(lpTotalSupply) + .mul(shortValue.raw.toString()) + .div(parseEther('1')) + const underlyingPerLp = parseEther(lpTotalSupply) + .mul(underlyingValue.raw.toString()) + .div(parseEther('1')) + const totalUnderlyingPerLp = parseEther(lpTotalSupply) + .mul(totalUnderlyingValue.raw.toString()) + .div(parseEther('1')) + + return { shortPerLp, underlyingPerLp, totalUnderlyingPerLp } + }, [market, lpTotalSupply, lpTotalSupply]) + const handleOnClick = useCallback(() => { //setProvide(true) setToggle(!toggle) @@ -278,10 +313,13 @@ const LiquidityTableRow: React.FC = ({ )} - {parseFloat(liquidity[0]) > 0 ? ( + {!isZero(parseEther(lpTotalSupply)) ? ( - {numeral(liquidity[0]).format('0.00a')} {units} + {numeral( + formatEther(calculateTotalLiquidity().totalUnderlyingPerLp) + ).format('0.00a')}{' '} + {units} ) : ( @@ -297,6 +335,21 @@ const LiquidityTableRow: React.FC = ({ )} + {parseFloat(liquidity[0]) > 0 ? ( + + + {numeral( + formatEther( + calculateLiquidityValuePerShare().totalUnderlyingPerLp + ) + ).format('0.00a')}{' '} + {units} + + + ) : ( + - + )} + {isCall ? asset : entity.strike.symbol} {expiry ? ( From 7325bd9b83595223871f7f402b6d87122e783d80 Mon Sep 17 00:00:00 2001 From: Zachary Thielemann Date: Wed, 3 Feb 2021 12:58:31 -0800 Subject: [PATCH 02/22] fix(market): formatted pricing, added notifs to lp --- src/components/LineItem/LineItem.tsx | 6 +- .../components/AddLiquidity/AddLiquidity.tsx | 1 - .../Market/OrderCard/components/Swap/Swap.tsx | 133 +++++++++++------- src/components/TopBar/TopBar.tsx | 1 - src/pages/liquidity/index.tsx | 12 +- src/pages/markets/[...id].tsx | 8 +- 6 files changed, 99 insertions(+), 62 deletions(-) diff --git a/src/components/LineItem/LineItem.tsx b/src/components/LineItem/LineItem.tsx index 37faba20..9380d034 100644 --- a/src/components/LineItem/LineItem.tsx +++ b/src/components/LineItem/LineItem.tsx @@ -82,7 +82,11 @@ interface ColorProps { const Color = styled.span` color: ${(props) => - props.color == 'red' ? props.theme.color.red[500] : 'inherit'}; + props.color == 'red' + ? props.theme.color.red[500] + : props.color == 'green' + ? props.theme.color.green[500] + : 'inherit'}; ` const StyledSym = styled.a` diff --git a/src/components/Market/OrderCard/components/AddLiquidity/AddLiquidity.tsx b/src/components/Market/OrderCard/components/AddLiquidity/AddLiquidity.tsx index f7a7fdef..71edb36c 100644 --- a/src/components/Market/OrderCard/components/AddLiquidity/AddLiquidity.tsx +++ b/src/components/Market/OrderCard/components/AddLiquidity/AddLiquidity.tsx @@ -528,7 +528,6 @@ const AddLiquidity: React.FC = () => { ) } - const LiquidityContainer = styled.div` width: 34em; ` diff --git a/src/components/Market/OrderCard/components/Swap/Swap.tsx b/src/components/Market/OrderCard/components/Swap/Swap.tsx index aa542bcf..603bf4fa 100644 --- a/src/components/Market/OrderCard/components/Swap/Swap.tsx +++ b/src/components/Market/OrderCard/components/Swap/Swap.tsx @@ -54,6 +54,12 @@ import { Token, TokenAmount } from '@uniswap/sdk' import formatEtherBalance from '@/utils/formatEtherBalance' import numeral from 'numeral' import { tryParseAmount } from '@/utils/tryParseAmount' +const formatParsedAmount = (amount: BigNumberish) => { + const bigAmt = BigNumber.from(amount) + return numeral(formatEther(bigAmt)).format( + bigAmt.lt(parseEther('0.01')) ? '0.0000' : '0.00' + ) +} const Swap: React.FC = () => { //state @@ -73,6 +79,7 @@ const Swap: React.FC = () => { ? formatEther(item.market.spotUnderlyingToShort.raw.toString()) : formatEther(item.market.spotUnderlyingToShort.raw.toString()) ) + const [impact, setImpact] = useState('0.00') const [error, setError] = useState(false) // set null lp @@ -302,9 +309,7 @@ const Swap: React.FC = () => { }, [item, parsedAmount]) const isBelowSlippage = useCallback(() => { - return impact !== 'NaN' - ? Math.abs(parseFloat(impact)) < parseFloat(slippage) * 100 - : true + return impact !== 'NaN' ? Math.abs(parseFloat(impact)) < 30 : true }, [impact, slippage]) const handleApproval = useCallback(() => { @@ -356,7 +361,7 @@ const Swap: React.FC = () => { )} - + { orderType === Operation.LONG ? underlyingAmount : tokenAmount } /> + + + Order Summary - {parsedAmount.eq(0) && !error ? : <>} - - {parsedAmount.gt(0) && !error ? ( + {inputLoading && hasLiquidity ? ( <> + - Description + + ) : ( + <> + {parsedAmount.gt(0) && !error ? ( + <> + + + + ) : ( + <> + {' '} + + + )} + + )} + {parsedAmount.gt(0) && !error ? ( + <> + + @@ -405,47 +473,11 @@ const Swap: React.FC = () => { - - ) : null} - - - Order Summary - {!inputLoading ? ( - <> - - - ) : ( - <>{hasLiquidity ? : null} - )} - - {inputLoading && hasLiquidity ? ( - <> - - - - ) : ( - - )} - - - {error ? ( - Order quantity too large! @@ -496,17 +528,12 @@ const Swap: React.FC = () => { )} {approved[0] && approved[1] ? (