Skip to content

Commit

Permalink
Merge pull request #224 from xave-finance/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
schystz authored Aug 30, 2022
2 parents 758f0ac + 6ae04d9 commit 3591959
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 45 deletions.
20 changes: 14 additions & 6 deletions src/components/Tailwind/InputFields/CurrencyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import SelectButton from '../Buttons/SelectButton'
import NumericalInput from 'components/NumericalInput'
import TokenSelectModal from 'components/Tailwind/Modals/TokenSelectModal'
import { formatNumber, NumberFormat } from 'utils/formatNumber'
import { useCurrencyBalance } from 'state/wallet/hooks'
import { useActiveWeb3React } from 'hooks'
import { MouseoverTooltip } from '../../Tooltip'
import useTokenBalance from 'sushi-hooks/queries/useTokenBalance'
import { formatUnits } from 'ethers/lib/utils'
import { bigNumberToNumber } from 'utils/bigNumberHelper'

interface TokenInputProps {
currency: Currency
Expand All @@ -35,12 +37,16 @@ const TokenInput = ({
}: TokenInputProps) => {
const [showModal, setShowModal] = useState(false)
const { account } = useActiveWeb3React()
const currencyBalance = useCurrencyBalance(account ?? undefined, currency)

const currencyBalance = useTokenBalance(
currency instanceof Token ? (currency as Token).address : undefined,
account ?? undefined
)

const onMax = () => {
balance = balance ? balance : currencyBalance?.toExact()
if (balance && Number(balance) > 0) {
didChangeValue(balance)
const _balance = balance ? balance : formatUnits(currencyBalance.value, currencyBalance.decimals)
if (_balance && Number(_balance) > 0) {
didChangeValue(_balance)
}
}

Expand Down Expand Up @@ -79,7 +85,9 @@ const TokenInput = ({
</MouseoverTooltip>
{balance
? formatNumber(Number(balance), NumberFormat.long)
: currencyBalance?.toFixed(2, { groupSeparator: ',' }) || '-'}
: currencyBalance
? formatNumber(bigNumberToNumber(currencyBalance.value, currencyBalance.decimals), NumberFormat.long)
: '-'}
</div>
)}
<NumericalInput
Expand Down
65 changes: 31 additions & 34 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Demo from './Test/Demo'
import TailwindDemo from './Test/TailwindDemo'
import Pool from './Tailwind/Pool'
import Swap from './Tailwind/Swap'
import GeoBlocked from 'components/Tailwind/Panels/GeoBlocked'

const AppWrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -55,38 +54,36 @@ const Marginer = styled.div`

export default function App() {
return (
<GeoBlocked>
<Suspense fallback={null}>
<Route component={GoogleAnalyticsReporter} />
<AppWrapper>
<URLWarning />
<DisclaimerAlert />
<HeaderWrapper>
<Header />
</HeaderWrapper>
<BodyWrapper>
<Popups />
<Polling />
<Web3ReactManager>
<Switch>
<Route exact strict path="/pool" component={Pool} />
<Route exact strict path="/farm" component={Farm} />
<Route path="/farm/:address" component={Farm} />
<Route exact strict path="/vesting" component={HaloHalo} />
<Route exact strict path="/swap" component={Swap} />
{(process.env.NODE_ENV === 'development' || process.env.REACT_APP_SHOW_DEMO === 'true') && (
<Route exact strict path="/demo" component={Demo} />
)}
{(process.env.NODE_ENV === 'development' || process.env.REACT_APP_SHOW_DEMO === 'true') && (
<Route exact strict path="/tw-demo" component={TailwindDemo} />
)}
<Route component={RedirectPathToFarmOnly} />
</Switch>
</Web3ReactManager>
<Marginer />
</BodyWrapper>
</AppWrapper>
</Suspense>
</GeoBlocked>
<Suspense fallback={null}>
<Route component={GoogleAnalyticsReporter} />
<AppWrapper>
<URLWarning />
<DisclaimerAlert />
<HeaderWrapper>
<Header />
</HeaderWrapper>
<BodyWrapper>
<Popups />
<Polling />
<Web3ReactManager>
<Switch>
<Route exact strict path="/pool" component={Pool} />
<Route exact strict path="/farm" component={Farm} />
<Route path="/farm/:address" component={Farm} />
<Route exact strict path="/vesting" component={HaloHalo} />
<Route exact strict path="/swap" component={Swap} />
{(process.env.NODE_ENV === 'development' || process.env.REACT_APP_SHOW_DEMO === 'true') && (
<Route exact strict path="/demo" component={Demo} />
)}
{(process.env.NODE_ENV === 'development' || process.env.REACT_APP_SHOW_DEMO === 'true') && (
<Route exact strict path="/tw-demo" component={TailwindDemo} />
)}
<Route component={RedirectPathToFarmOnly} />
</Switch>
</Web3ReactManager>
<Marginer />
</BodyWrapper>
</AppWrapper>
</Suspense>
)
}
11 changes: 8 additions & 3 deletions src/pages/Tailwind/Pool/liquidity/AddLiquidity.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useState } from 'react'
import { PoolData } from '../models/PoolData'
import { useTokenBalances } from 'state/wallet/hooks'
import { useActiveWeb3React } from 'hooks'
import SegmentControl from 'components/Tailwind/SegmentControl/SegmentControl'
import MultiSidedLiquidity from './MultiSidedLiquidity'
import SingleSidedLiquidity from './SingleSidedLiquidity'
import AddLiquityModal from './AddLiquityModal'
import ErrorModal from 'components/Tailwind/Modals/ErrorModal'
import useTokenBalance from 'sushi-hooks/queries/useTokenBalance'
import { TokenAmount } from '@halodao/sdk'

interface AddLiquidityProps {
pool: PoolData
Expand All @@ -24,8 +25,12 @@ const AddLiquidity = ({ pool, isEnabled }: AddLiquidityProps) => {
const [error, setError] = useState<any>(undefined)

const { account } = useActiveWeb3React()
const tokenBalances = useTokenBalances(account ?? undefined, [pool.token0, pool.token1])
const balances = [tokenBalances[pool.token0.address], tokenBalances[pool.token1.address]]
const token0Balance = useTokenBalance(pool.token0.address, account ?? undefined)
const token1Balance = useTokenBalance(pool.token1.address, account ?? undefined)
const balances = [
new TokenAmount(pool.token0, BigInt(token0Balance.value.toString())),
new TokenAmount(pool.token1, BigInt(token1Balance.value.toString()))
]

const disabledSegments = pool.pooled.total > 0 ? undefined : [1]

Expand Down
3 changes: 1 addition & 2 deletions src/sushi-hooks/queries/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const useTokenBalance = (tokenAddress?: string, accountAddress?: string) => {
const getBalance = async (contract: Contract | null, owner: string | null | undefined): Promise<BalanceProps> => {
try {
//console.log('token_contract:', contract)
const balance = await contract?.balanceOf(owner)
const decimals = await contract?.decimals()
const [balance, decimals] = await Promise.all([contract?.balanceOf(owner), contract?.decimals()])
return { value: BigNumber.from(balance), decimals: decimals }
//todo: return as BigNumber as opposed toString since information will
//return Fraction.from(BigNumber.from(balance), BigNumber.from(10).pow(decimals)).toString()
Expand Down
24 changes: 24 additions & 0 deletions src/utils/bigNumberHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BigNumber } from 'ethers'
import { formatUnits, parseUnits } from 'ethers/lib/utils'

export const bigNumberToNumber = (bn: BigNumber, decimals = 18) => {
const strValue = formatUnits(bn.toString(), decimals)
const parts = strValue.split('.')
const decimalDigits = parts[1] ?? ''
if (decimalDigits.length > 15) {
return parseFloat(parts[0] + '.' + decimalDigits.slice(0, 15))
} else {
return parseFloat(strValue)
}
}

export const numberToBigNumber = (n: number, decimals = 18) => {
return parseUnits(`${n.toFixed(decimals)}`, decimals)
}

export const scale = (input: BigNumber | string, decimalPlaces: number) => {
const unscaled = typeof input === 'string' ? BigNumber.from(input) : input
const scalePow = BigNumber.from(decimalPlaces.toString())
const scaleMul = BigNumber.from(10).pow(scalePow)
return unscaled.mul(scaleMul)
}

0 comments on commit 3591959

Please sign in to comment.