Skip to content

Commit 1818545

Browse files
authored
chore: Updated balance component (#11413)
![Screenshot 2025-03-27 at 19 09 53](https://github.com/user-attachments/assets/f821f06f-3142-43dd-9b8f-0104707fdf68) ![Screenshot 2025-03-27 at 19 10 01](https://github.com/user-attachments/assets/0d982ae7-feba-4cfc-bef3-f478d5dfbe8d) <!-- start pr-codex --> --- ## PR-Codex overview This PR updates the `@orbs-network/twap-ui` and `@orbs-network/twap-ui-pancake` packages to version `0.11.34`, introduces new functionalities in the `Twap.tsx` file, and enhances balance handling in the `TWAPPanel` component. ### Detailed summary - Updated `@orbs-network/twap-ui` and `@orbs-network/twap-ui-pancake` to `0.11.34`. - Added imports for `Orders`, `TWAP`, `ToastProps`, `Percent`, and `useCurrencyBalances`. - Introduced a new `Balance` component to manage token balances and user input. - Enhanced `onPercentInput` and `onMax` functions for better balance management. > The following files were skipped due to too many changes: `pnpm-lock.yaml` > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent d4d753a commit 1818545

File tree

3 files changed

+247
-280
lines changed

3 files changed

+247
-280
lines changed

Diff for: apps/web/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"@gnosis.pm/safe-apps-wagmi": "^1.2.0",
4242
"@kyberswap/pancake-liquidity-widgets": "1.0.6",
4343
"@next/bundle-analyzer": "13.0.7",
44-
"@orbs-network/twap-ui": "0.11.31",
45-
"@orbs-network/twap-ui-pancake": "0.11.31",
44+
"@orbs-network/twap-ui": "0.11.34",
45+
"@orbs-network/twap-ui-pancake": "0.11.34",
4646
"@pancakeswap/achievements": "workspace:*",
4747
"@pancakeswap/blog": "workspace:*",
4848
"@pancakeswap/canonical-bridge": "workspace:*",

Diff for: apps/web/src/views/Swap/Twap/Twap.tsx

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
import { Orders, ToastProps, TWAP as PancakeTWAP } from '@orbs-network/twap-ui-pancake'
1+
import { Orders, TWAP as PancakeTWAP, ToastProps } from '@orbs-network/twap-ui-pancake'
22
import { useTheme } from '@pancakeswap/hooks'
3+
import { Percent } from '@pancakeswap/sdk'
34
import { Currency, CurrencyAmount, TradeType } from '@pancakeswap/swap-sdk-core'
4-
import { AutoColumn, Button, useMatchBreakpoints, useModal, useToast, useTooltip } from '@pancakeswap/uikit'
5+
import {
6+
AutoColumn,
7+
Button,
8+
domAnimation,
9+
LazyAnimatePresence,
10+
useMatchBreakpoints,
11+
useModal,
12+
useToast,
13+
useTooltip,
14+
} from '@pancakeswap/uikit'
15+
516
import replaceBrowserHistoryMultiple from '@pancakeswap/utils/replaceBrowserHistoryMultiple'
617
import { useUserSingleHopOnly } from '@pancakeswap/utils/user'
718
import { CurrencyLogo, NumericalInput, SwapUIV2 } from '@pancakeswap/widgets-internal'
@@ -26,8 +37,10 @@ import {
2637
useUserV2SwapEnable,
2738
useUserV3SwapEnable,
2839
} from 'state/user/smartRouter'
40+
import { useCurrencyBalances } from 'state/wallet/hooks'
2941
import { keyframes, styled } from 'styled-components'
3042
import currencyId from 'utils/currencyId'
43+
import { maxAmountSpend } from 'utils/maxAmountSpend'
3144
import { useAccount } from 'wagmi'
3245
import ArrowDark from '../../../../public/images/swap/arrow_dark.json' assert { type: 'json' }
3346
import ArrowLight from '../../../../public/images/swap/arrow_light.json' assert { type: 'json' }
@@ -202,10 +215,67 @@ export function TWAPPanel({ limit }: { limit?: boolean }) {
202215
FlipButton={FlipButton}
203216
Input={Input}
204217
CurrencyLogo={TokenLogo}
218+
Balance={Balance}
205219
/>
206220
)
207221
}
208222

223+
const Balance = ({
224+
balance,
225+
insufficientBalance,
226+
isInputFocus,
227+
onValueChange,
228+
isSrcToken,
229+
}: {
230+
balance?: string
231+
insufficientBalance: boolean
232+
isInputFocus: boolean
233+
onValueChange: (value: string) => void
234+
isSrcToken?: boolean
235+
}) => {
236+
const { address: account } = useAccount()
237+
const {
238+
[Field.INPUT]: { currencyId: inputCurrencyId },
239+
[Field.OUTPUT]: { currencyId: outputCurrencyId },
240+
} = useSwapState()
241+
const inputCurrency = useCurrency(inputCurrencyId)
242+
const outputCurrency = useCurrency(outputCurrencyId)
243+
const [inputBalance] = useCurrencyBalances(account, [inputCurrency, outputCurrency])
244+
const maxAmountInput = useMemo(() => maxAmountSpend(inputBalance), [inputBalance])
245+
const onPercentInput = useCallback(
246+
(percent: number) => {
247+
if (!isSrcToken) return
248+
if (maxAmountInput) {
249+
onValueChange(maxAmountInput.multiply(new Percent(percent, 100)).toExact())
250+
}
251+
},
252+
[maxAmountInput, onValueChange, isSrcToken],
253+
)
254+
255+
const onMax = useCallback(() => {
256+
if (!isSrcToken) return
257+
if (maxAmountInput) {
258+
onValueChange(maxAmountInput.toExact())
259+
}
260+
}, [maxAmountInput, onValueChange, isSrcToken])
261+
262+
return (
263+
<LazyAnimatePresence mode="wait" features={domAnimation}>
264+
{account ? (
265+
!isInputFocus ? (
266+
<SwapUIV2.WalletAssetDisplay
267+
isUserInsufficientBalance={insufficientBalance}
268+
balance={balance}
269+
onMax={onMax}
270+
/>
271+
) : (
272+
<SwapUIV2.AssetSettingButtonList onPercentInput={onPercentInput} />
273+
)
274+
) : null}
275+
</LazyAnimatePresence>
276+
)
277+
}
278+
209279
const TokenLogo = ({ address, size }: { address?: string; size?: string }) => {
210280
const currency = useCurrency(address)
211281

0 commit comments

Comments
 (0)