Skip to content

Commit

Permalink
fix: passing address
Browse files Browse the repository at this point in the history
  • Loading branch information
thechefpenguin committed Feb 13, 2025
1 parent a9b74ea commit db2c328
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
2 changes: 2 additions & 0 deletions apps/ton/src/atoms/liquidity/addLiquidityStateAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { currencyFamily } from 'atoms/currencyAtoms'
import { atom } from 'jotai'
import { CurrencyField } from 'types/currency'

export const liquidityIndependentFieldAtom = atom(CurrencyField.ADD_LIQUIDITY_CURRENCY0)

export const currency0Atom = atom((get) => get(currencyFamily(CurrencyField.ADD_LIQUIDITY_CURRENCY0)))
export const currency1Atom = atom((get) => get(currencyFamily(CurrencyField.ADD_LIQUIDITY_CURRENCY1)))

Expand Down
2 changes: 1 addition & 1 deletion apps/ton/src/components/Modals/AddLiquidityModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const AddLiquidityModal = ({

<Flex justifyContent="space-between" alignItems="center">
<Text color="textSubtle">{t('Slippage Tolerance')}</Text>
<Text>{slippage}%</Text>
<Text>{slippage / 100}%</Text>
</Flex>

<Button mt="8px" onClick={onConfirm}>
Expand Down
7 changes: 7 additions & 0 deletions apps/ton/src/ton/atom/chainIdAtom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TonChainId, TonNetworks } from '@pancakeswap/ton-v2-sdk'
import { atom } from 'jotai'
import { tonStateAtom } from './tonStateAtom'

export const chainIdAtom = atom((get) =>
get(tonStateAtom).network === TonNetworks.Mainnet ? TonChainId.Mainnet : TonChainId.Testnet,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTranslation } from '@pancakeswap/localization'
import { Currency } from '@pancakeswap/ton-v2-sdk'
import { AddIcon, Box, BoxProps, Button, Flex, FlexGap, Loading, Text, useToast } from '@pancakeswap/uikit'
import { setCurrencyAtom } from 'atoms/currencyAtoms'
import { currency0Value, currency1Value } from 'atoms/liquidity/addLiquidityStateAtom'
import { currency0Value, currency1Value, liquidityIndependentFieldAtom } from 'atoms/liquidity/addLiquidityStateAtom'
import { setAddLiquidityModalAtom } from 'atoms/modals/addLiquidityModalAtom'
import { BigNumber as BN } from 'bignumber.js'
import { ConnectWalletButton } from 'components/Buttons/ConnectWalletButton'
Expand Down Expand Up @@ -52,6 +52,7 @@ export const CardContent = (props: CardContentProps) => {
const [currency1] = useCurrency(CurrencyField.ADD_LIQUIDITY_CURRENCY1, token1Address)
const [token0Value, setToken0Value] = useAtom(currency0Value)
const [token1Value, setToken1Value] = useAtom(currency1Value)
const [independentField, setIndependentField] = useAtom(liquidityIndependentFieldAtom)

const setCurrency = useSetAtom(setCurrencyAtom)
const setAddLiquidityModal = useSetAtom(setAddLiquidityModalAtom)
Expand Down Expand Up @@ -107,18 +108,49 @@ export const CardContent = (props: CardContentProps) => {
if (currency0 || currency1) updateQueryParams()
}, [currency0, currency1, updateQueryParams])

// const calculateOutputAmount = useCallback(() => {
// if (rates.currency0 && rates.currency1 && currency0 && currency1) {
// const amount0 = BN(token0Value)
// const amount1 = BN(token1Value)

// if (independentField === CurrencyField.ADD_LIQUIDITY_CURRENCY0) {
// if (amount0.isZero()) setToken1Value('0')
// setToken1Value(amount0.times(rates.currency0).toString())
// } else if (independentField === CurrencyField.ADD_LIQUIDITY_CURRENCY1) {
// if (amount1.isZero()) setToken0Value('0')
// setToken0Value(amount1.times(rates.currency1).toString())
// }
// }
// }, [
// rates.currency0,
// rates.currency1,
// currency0,
// currency1,
// token0Value,
// token1Value,
// independentField,
// setToken0Value,
// setToken1Value,
// ])

const handleToken0Input = useCallback(
(value: string) => {
setToken0Value(value)
setIndependentField(CurrencyField.ADD_LIQUIDITY_CURRENCY0)

// calculateOutputAmount()
},
[setToken0Value],
[setToken0Value, setIndependentField],
)

const handleToken1Input = useCallback(
(value: string) => {
setToken1Value(value)
setIndependentField(CurrencyField.ADD_LIQUIDITY_CURRENCY1)

// calculateOutputAmount()
},
[setToken1Value],
[setToken1Value, setIndependentField],
)

const onCurrencySelection = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAtomValue } from 'jotai'
import { useRouter } from 'next/router'
import { useCallback, useMemo, useState } from 'react'
import styled from 'styled-components'
import { addressAtom } from 'ton/atom/addressAtom'
import { lpBalanceQueryAtom } from 'ton/atom/liquidity/lpBalanceQueryAtom'
import { poolDataQueryAtom } from 'ton/atom/liquidity/poolDataQueryAtom'
import { networkAtom } from 'ton/atom/networkAtom'
Expand Down Expand Up @@ -54,7 +55,9 @@ const QUICK_INPUTS = [10, 20, 75, 100]
interface CardContentProps extends BoxProps {}
export const CardContent = (props: CardContentProps) => {
const { t } = useTranslation()
const isWalletConnected = true

const userAddress = useAtomValue(addressAtom)
const isWalletConnected = !!userAddress

// Query params
const router = useRouter()
Expand All @@ -66,10 +69,16 @@ export const CardContent = (props: CardContentProps) => {
const { data: currency1 } = useAtomValue(tokenByAddressQueryAtom(address1))

const { data: lpBalance } = useAtomValue(
lpBalanceQueryAtom({ token0Address: currency0?.address, token1Address: currency1?.address }),
lpBalanceQueryAtom({
token0Address: currency0?.isNative ? userAddress : currency0?.address,
token1Address: currency1?.isNative ? userAddress : currency1?.address,
}),
)
const { data: poolData, isLoading: isPoolDataLoading } = useAtomValue(
poolDataQueryAtom({ token0Address: currency0?.address, token1Address: currency1?.address }),
poolDataQueryAtom({
token0Address: currency0?.isNative ? userAddress : currency0?.address,
token1Address: currency1?.isNative ? userAddress : currency1?.address,
}),
)

const [sliderValue, setSliderValue] = useState(10)
Expand Down
37 changes: 17 additions & 20 deletions apps/ton/src/views/TONSwap/SwapForm.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { useTranslation } from '@pancakeswap/localization'
import { Rounding } from '@pancakeswap/swap-sdk-core'
import { Native, TonNetworks } from '@pancakeswap/ton-v2-sdk'
import { useCallback, useEffect, useMemo } from 'react'
import noop from 'lodash/noop'
import { Column, Text } from '@pancakeswap/uikit'
import { ButtonAndDetailsPanel } from 'components/TonSwap/ButtonAndDetailsPanel'
import CurrencyInputPanelSimplify from 'components/TonSwap/CurrencyInputPanelSimplify'
import { FlipButton } from 'components/TonSwap/FlipButton'
import { formatFraction } from '@pancakeswap/utils/formatFractions'
import { useUserSlippage } from '@pancakeswap/utils/user'
import { useTranslation } from '@pancakeswap/localization'
import { toNano } from '@ton/core'
import { fetchListAtom } from 'atoms/lists/fetchListAtom'
import { setApprovalModalAtom } from 'atoms/modals/approvalModalAtom'
import { setTransactionModalAtom } from 'atoms/modals/transactionModalAtom'
import { independentFieldAtom, inputCurrencyAtom, outputCurrencyAtom, typedValueAtom } from 'atoms/swap/swapStateAtom'
import { TransactionActionType } from 'components/Modals/ActionModal'
import { ButtonAndDetailsPanel } from 'components/TonSwap/ButtonAndDetailsPanel'
import CurrencyInputPanelSimplify from 'components/TonSwap/CurrencyInputPanelSimplify'
import { FlipButton } from 'components/TonSwap/FlipButton'
import { SwapCommitButton } from 'components/TonSwap/SwapCommitButton'
import { SwapUIV2 } from 'components/widgets/swap-v2'
import { PRESET_TOKENS } from 'config/constants/tokens'
import { useSwapActionHandlers } from 'hooks/swap/useSwapActionHandlers'
import { useTradeExactIn } from 'hooks/swap/useTradeExactIn'
import { useTradeExactOut } from 'hooks/swap/useTradeExactOut'
import { useAtomValue, useSetAtom } from 'jotai'
import noop from 'lodash/noop'
import { useCallback, useEffect, useMemo } from 'react'
import { chainIdAtom } from 'ton/atom/chainIdAtom'
import { balanceAtom } from 'ton/logic/balanceAtom'
import { useSwap } from 'ton/logic/swap/useSwap'
import { Field } from 'types'
import { Rounding, _10000 } from '@pancakeswap/swap-sdk-core'
import { formatFraction } from '@pancakeswap/utils/formatFractions'
import { useTradeExactIn } from 'hooks/swap/useTradeExactIn'
import { useTradeExactOut } from 'hooks/swap/useTradeExactOut'
import { tryParseAmount } from 'utils/tryParseAmount'
import { useSwap } from 'ton/logic/swap/useSwap'

export const SwapForm = () => {
const { t } = useTranslation()
const chainId = useAtomValue(chainIdAtom)

const inputCurrency = useAtomValue(inputCurrencyAtom)
const outputCurrency = useAtomValue(outputCurrencyAtom)
Expand All @@ -48,8 +50,6 @@ export const SwapForm = () => {

const { onUserInput, onCurrencySelection } = useSwapActionHandlers()

const { data: activeList, isFetched } = useAtomValue(fetchListAtom)

const setApprovalModal = useSetAtom(setApprovalModalAtom)
const setTransactionModal = useSetAtom(setTransactionModalAtom)
const [userAllowedSlippage] = useUserSlippage()
Expand Down Expand Up @@ -105,14 +105,11 @@ export const SwapForm = () => {

// Set default currencies on load
useEffect(() => {
if (isFetched && !inputCurrency && !outputCurrency && activeList && activeList.length > 1) {
if (!inputCurrency && !outputCurrency) {
onCurrencySelection(Field.INPUT, Native.onNetwork(TonNetworks.Testnet))
onCurrencySelection(
Field.OUTPUT,
activeList.find((item) => item.symbol === 'CAKE'),
)
onCurrencySelection(Field.OUTPUT, PRESET_TOKENS.CAKE[chainId])
}
}, [activeList, inputCurrency, outputCurrency, isFetched, onCurrencySelection])
}, [inputCurrency, outputCurrency, chainId, onCurrencySelection])

return (
<SwapUIV2.SwapFormWrapper>
Expand Down

0 comments on commit db2c328

Please sign in to comment.