|
| 1 | +import React from 'react' |
| 2 | +import styled from 'styled-components' |
| 3 | +import { withRouter } from 'react-router-dom' |
| 4 | +import { Percent, Exchange } from '@uniswap/sdk' |
| 5 | + |
| 6 | +import { useWeb3React } from '@web3-react/core' |
| 7 | +import { useAllBalances } from '../../contexts/Balances' |
| 8 | +import { useTotalSupply } from '../../contexts/Exchanges' |
| 9 | + |
| 10 | +import Card from '../Card' |
| 11 | +import TokenLogo from '../TokenLogo' |
| 12 | +import DoubleLogo from '../DoubleLogo' |
| 13 | +import { Text } from 'rebass' |
| 14 | +import { GreyCard } from '../../components/Card' |
| 15 | +import { AutoColumn } from '../Column' |
| 16 | +import { ButtonSecondary } from '../Button' |
| 17 | +import { RowBetween, RowFixed } from '../Row' |
| 18 | + |
| 19 | +const FixedHeightRow = styled(RowBetween)` |
| 20 | + height: 24px; |
| 21 | +` |
| 22 | + |
| 23 | +function PositionCard({ exchangeAddress, token0, token1, history, minimal = false, ...rest }) { |
| 24 | + const { account } = useWeb3React() |
| 25 | + const allBalances = useAllBalances() |
| 26 | + |
| 27 | + const tokenAmount0 = allBalances?.[exchangeAddress]?.[token0.address] |
| 28 | + const tokenAmount1 = allBalances?.[exchangeAddress]?.[token1.address] |
| 29 | + |
| 30 | + const exchange = tokenAmount0 && tokenAmount1 && new Exchange(tokenAmount0, tokenAmount1) |
| 31 | + |
| 32 | + const userPoolBalance = allBalances?.[account]?.[exchangeAddress] |
| 33 | + const totalPoolTokens = useTotalSupply(exchange) |
| 34 | + |
| 35 | + const poolTokenPercentage = |
| 36 | + !!userPoolBalance && !!totalPoolTokens ? new Percent(userPoolBalance.raw, totalPoolTokens.raw) : undefined |
| 37 | + |
| 38 | + const token0Deposited = poolTokenPercentage?.multiply(allBalances[exchangeAddress][token0.address]) |
| 39 | + const token1Deposited = poolTokenPercentage?.multiply(allBalances[exchangeAddress][token1.address]) |
| 40 | + |
| 41 | + function DynamicCard({ children, ...rest }) { |
| 42 | + if (!minimal) { |
| 43 | + return ( |
| 44 | + <Card border="1px solid #EDEEF2" {...rest}> |
| 45 | + {children} |
| 46 | + </Card> |
| 47 | + ) |
| 48 | + } else { |
| 49 | + return <GreyCard {...rest}>{children}</GreyCard> |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <DynamicCard {...rest}> |
| 55 | + <AutoColumn gap="20px"> |
| 56 | + <FixedHeightRow> |
| 57 | + <Text fontWeight={500} fontSize={16}> |
| 58 | + Current Position |
| 59 | + </Text> |
| 60 | + </FixedHeightRow> |
| 61 | + <FixedHeightRow> |
| 62 | + <RowFixed> |
| 63 | + <DoubleLogo a0={token0?.address || ''} a1={token1?.address || ''} margin={true} size={24} /> |
| 64 | + <Text fontWeight={500} fontSize={20}> |
| 65 | + {token0?.symbol}:{token1?.symbol} |
| 66 | + </Text> |
| 67 | + </RowFixed> |
| 68 | + <Text fontWeight={500} fontSize={20}> |
| 69 | + {userPoolBalance ? userPoolBalance.toFixed(6) : '-'} |
| 70 | + </Text> |
| 71 | + </FixedHeightRow> |
| 72 | + <AutoColumn gap="12px"> |
| 73 | + <FixedHeightRow> |
| 74 | + <Text color="#888D9B" fontSize={16} fontWeight={500}> |
| 75 | + {token0?.symbol} Deposited: |
| 76 | + </Text> |
| 77 | + {token0Deposited ? ( |
| 78 | + <RowFixed> |
| 79 | + <TokenLogo address={token0?.address || ''} /> |
| 80 | + <Text color="#888D9B" fontSize={16} fontWeight={500} marginLeft={'6px'}> |
| 81 | + {token0Deposited?.toFixed(8)} |
| 82 | + </Text> |
| 83 | + </RowFixed> |
| 84 | + ) : ( |
| 85 | + '-' |
| 86 | + )} |
| 87 | + </FixedHeightRow> |
| 88 | + <FixedHeightRow> |
| 89 | + <Text color="#888D9B" fontSize={16} fontWeight={500}> |
| 90 | + {token1?.symbol} Deposited: |
| 91 | + </Text> |
| 92 | + {token1Deposited ? ( |
| 93 | + <RowFixed> |
| 94 | + <TokenLogo address={token1.address || ''} /> |
| 95 | + <Text color="#888D9B" fontSize={16} fontWeight={500} marginLeft={'6px'}> |
| 96 | + {token1Deposited?.toFixed(8)} |
| 97 | + </Text> |
| 98 | + </RowFixed> |
| 99 | + ) : ( |
| 100 | + '-' |
| 101 | + )} |
| 102 | + </FixedHeightRow> |
| 103 | + {!minimal && ( |
| 104 | + <FixedHeightRow> |
| 105 | + <Text color="#888D9B" fontSize={16} fontWeight={500}> |
| 106 | + Your pool share: |
| 107 | + </Text> |
| 108 | + <Text color="#888D9B" fontSize={16} fontWeight={500}> |
| 109 | + {poolTokenPercentage ? poolTokenPercentage.toFixed(2) + '%' : '-'} |
| 110 | + </Text> |
| 111 | + </FixedHeightRow> |
| 112 | + )} |
| 113 | + </AutoColumn> |
| 114 | + {!minimal && ( |
| 115 | + <RowBetween> |
| 116 | + <ButtonSecondary |
| 117 | + width="48%" |
| 118 | + onClick={() => { |
| 119 | + history.push('/add/' + token0?.address + '-' + token1?.address) |
| 120 | + }} |
| 121 | + > |
| 122 | + Add |
| 123 | + </ButtonSecondary> |
| 124 | + <ButtonSecondary |
| 125 | + width="48%" |
| 126 | + onClick={() => { |
| 127 | + history.push('/remove/' + token0?.address + '-' + token1?.address) |
| 128 | + }} |
| 129 | + > |
| 130 | + Remove |
| 131 | + </ButtonSecondary> |
| 132 | + </RowBetween> |
| 133 | + )} |
| 134 | + </AutoColumn> |
| 135 | + </DynamicCard> |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +export default withRouter(PositionCard) |
0 commit comments