Skip to content

Commit

Permalink
univ3 - form - calc deposti amount (need optimize code)
Browse files Browse the repository at this point in the history
  • Loading branch information
shendel committed Sep 25, 2024
1 parent 2ffe7bf commit c6e1cd3
Showing 1 changed file with 79 additions and 22 deletions.
101 changes: 79 additions & 22 deletions src/front/shared/pages/Exchange/QuickSwap/univ3/MintPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,50 +263,102 @@ function MintPosition(props) {

const isBaseFetching = (isFetchTokensInfo || isPoolsByFeeFetching)


/* @to-do - need optimize code size */
const calcAmount = (amount, token) => {
if (viewSide == VIEW_SIDE.A_TO_B) {
if (token == TOKEN._0) {
const _amount1 = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: amount,
price: startPrice,
priceHigh: token0HighPrice,
priceLow: token0LowerPrice,
}).toNumber()
setAmount0(amount)
setAmount1(_amount1)
}
if (token == TOKEN._1) {
const perTokenPrice = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: 1,
price: startPrice,
priceHigh: token0HighPrice,
priceLow: token0LowerPrice,
}).toNumber()
const _amount0 = new BigNumber(amount).dividedBy(perTokenPrice).toNumber()
setAmount0(_amount0)
setAmount1(amount)
}
} else {
if (token == TOKEN._1) {
const _amount0 = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: amount,
price: startPrice,
priceHigh: token1HighPrice,
priceLow: token1LowerPrice,
}).toNumber()
setAmount1(amount)
setAmount0(_amount0)
}
if (token == TOKEN._0) {
const perTokenPrice = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: 1,
price: startPrice,
priceHigh: token1HighPrice,
priceLow: token1LowerPrice,
}).toNumber()
const _amount1 = new BigNumber(amount).dividedBy(perTokenPrice).toNumber()
setAmount1(_amount1)
setAmount0(amount)
}
}
}

const posInRange = (
viewSide == VIEW_SIDE.A_TO_B
) ? (
new BigNumber(token0LowerPrice).isLessThanOrEqualTo(startPrice)
&& new BigNumber(token0HighPrice).isGreaterThanOrEqualTo(startPrice)
) : (
new BigNumber(token1LowerPrice).isLessThanOrEqualTo(startPrice)
&& new BigNumber(token1HighPrice).isGreaterThanOrEqualTo(startPrice)
)



const startPriceIsLower = (viewSide == VIEW_SIDE.A_TO_B) ? new BigNumber(startPrice).isLessThan(token0LowerPrice) : new BigNumber(startPrice).isLessThan(token1LowerPrice)
const startPriceIsHigh = (viewSide == VIEW_SIDE.A_TO_B) ? new BigNumber(startPrice).isGreaterThan(token0HighPrice) : new BigNumber(startPrice).isGreaterThan(token1HighPrice)

console.log('>>>>> IN RANGE, LOWER, HIGH', posInRange, startPriceIsLower, startPriceIsHigh)


const renderDepositToken0 = () => {
return (
<AmountInput
amount={amount0}
disabled={false}
onChange={(v) => { setAmount0(v) }}
onChange={(v) => { calcAmount(v, TOKEN._0) }}
symbol={getTokenSymbol(TOKEN._0)}
balance={formatAmount(fromWei(TOKEN._0, token0BalanceWei))}
isBalanceUpdate={isFetchingBalanceAllowance}
onBalanceUpdate={() => { setDoFetchBalanceAllowance(true) }}
/>
)
}

const renderDepositToken1 = () => {
return (
<AmountInput
amount={amount0}
amount={amount1}
disabled={false}
onChange={(v) => { setAmount1(v) }}
onChange={(v) => { calcAmount(v, TOKEN._1) }}
symbol={getTokenSymbol(TOKEN._1)}
balance={formatAmount(fromWei(TOKEN._1, token0BalanceWei))}
balance={formatAmount(fromWei(TOKEN._1, token1BalanceWei))}
isBalanceUpdate={isFetchingBalanceAllowance}
onBalanceUpdate={() => { setDoFetchBalanceAllowance(true) }}
/>
)
}

const posInRange = (
viewSide == VIEW_SIDE.A_TO_B
) ? (
new BigNumber(token0LowerPrice).isLessThanOrEqualTo(startPrice)
&& new BigNumber(token0HighPrice).isGreaterThanOrEqualTo(startPrice)
) : (
new BigNumber(token1LowerPrice).isLessThanOrEqualTo(startPrice)
&& new BigNumber(token1HighPrice).isGreaterThanOrEqualTo(startPrice)
)



const startPriceIsLower = (viewSide == VIEW_SIDE.A_TO_B) ? new BigNumber(startPrice).isLessThan(token0LowerPrice) : new BigNumber(startPrice).isLessThan(token1LowerPrice)
const startPriceIsHigh = (viewSide == VIEW_SIDE.A_TO_B) ? new BigNumber(startPrice).isGreaterThan(token0HighPrice) : new BigNumber(startPrice).isGreaterThan(token1HighPrice)

console.log('>>>>> IN RANGE, LOWER, HIGH', posInRange, startPriceIsLower, startPriceIsHigh)

return (
<div>
<BackButton onClick={() => { setCurrentAction(PositionAction.LIST) }}>
Expand Down Expand Up @@ -438,7 +490,12 @@ function MintPosition(props) {
</div>
)}
<div>
<h4>Deposit amounts</h4>
<h4>
<FormattedMessage
id="uni_mint_deposit_amounts"
defaultMessage="Deposit amounts"
/>
</h4>
{(viewSide == VIEW_SIDE.A_TO_B) ? (
<>
{!startPriceIsLower && renderDepositToken0()}
Expand Down

0 comments on commit c6e1cd3

Please sign in to comment.