Skip to content

Commit

Permalink
trade: refactor order and balance hooks to use Map
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Jun 23, 2024
1 parent 243f43b commit ee8d64b
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 38 deletions.
11 changes: 4 additions & 7 deletions trade.renegade.fi/app/(desktop)/place-order-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ export function PlaceOrderButton({

const hasZeroBalance = useMemo(() => {
if (!baseTokenAmount) return false
const baseBalance =
balances.find(({ mint }) => mint === baseAddress)?.amount || BigInt(0)
const quoteBalance =
balances.find(({ mint }) => mint === quoteAddress)?.amount || BigInt(0)
const baseBalance = balances.get(baseAddress)?.amount || BigInt(0)
const quoteBalance = balances.get(quoteAddress)?.amount || BigInt(0)
if (direction === Direction.SELL) {
return baseBalance === BigInt(0)
}
Expand All @@ -74,9 +72,8 @@ export function PlaceOrderButton({

const isMaxOrders = orders.length === MAX_ORDERS
const isMaxBalances = backOfQueueBalances.length === MAX_BALANCES
const isExistingBalance = balances.find(
({ mint }) =>
mint === (direction === Direction.BUY ? baseAddress : quoteAddress)
const isExistingBalance = balances.get(
direction === Direction.BUY ? baseAddress : quoteAddress
)

const isDisabled =
Expand Down
6 changes: 2 additions & 4 deletions trade.renegade.fi/app/(desktop)/withdraw-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export default function WithdrawButton({
const balances = useBalances()

const hasInsufficientBalance = useMemo(() => {
const renegadeBalance = balances.find(
(b) => b.mint === Token.findByTicker(baseTicker).address
)
const renegadeBalance = balances.get(Token.findByTicker(baseTicker).address)
if (!renegadeBalance) return true
return (
renegadeBalance.amount <
Expand Down Expand Up @@ -74,7 +72,7 @@ export default function WithdrawButton({
toast.message(QUEUED_WITHDRAWAL_MSG(token, amount))
}

if (fees.length > 0) {
if (fees.size > 0) {
await payFees(config)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ export function OrderConfirmationModal({

const hasInsufficientBalance = useMemo(() => {
if (!amount) return false
const baseBalance =
balances.find(({ mint }) => mint === baseToken.address)?.amount ||
BigInt(0)
const quoteBalance =
balances.find(({ mint }) => mint === quoteToken.address)?.amount ||
BigInt(0)
const baseBalance = balances.get(baseToken.address)?.amount || BigInt(0)
const quoteBalance = balances.get(quoteToken.address)?.amount || BigInt(0)
if (direction === Direction.SELL) {
return baseBalance < parseAmount(amount, baseToken)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export function TradingTokenSelectModal({
})
.map(({ address }) => ({
address: address as `0x${string}`,
balance:
balances.find((balance) => balance.mint === address)?.amount ??
BigInt(0),
balance: balances.get(address)?.amount ?? BigInt(0),
}))
.sort((a, b) => Number(b.balance - a.balance)) // Sort in descending order
.map(({ address, balance }) => ({
Expand Down
6 changes: 3 additions & 3 deletions trade.renegade.fi/components/panels/orders-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ function OrdersPanel() {
>
{orderHistory.map((order) => {
const sendBalance =
balances.find(({ mint }) =>
balances.get(
order.data.side === "Buy"
? mint === order.data.quote_mint
: mint === order.data.base_mint
? order.data.quote_mint
: order.data.base_mint
)?.amount || BigInt(0)
return (
<SingleOrder
Expand Down
12 changes: 6 additions & 6 deletions trade.renegade.fi/components/panels/wallets-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ function RenegadeWalletPanel() {
const wethAddress = Token.findByTicker("WETH").address
const usdcAddress = Token.findByTicker("USDC").address

const nonzero: Array<[`0x${string}`, bigint]> = Object.entries(
balances
).map(([_, b]) => [b.mint, b.amount])
const nonzero: Array<[`0x${string}`, bigint]> = Array.from(
balances.values()
).map((b) => [b.mint, b.amount])
const placeholders: Array<[`0x${string}`, bigint]> = DISPLAY_TOKENS()
.filter((t) => !nonzero.some(([a]) => a === t.address))
.map((t) => [t.address as `0x${string}`, BigInt(0)])
Expand All @@ -224,7 +224,7 @@ function RenegadeWalletPanel() {
}, [balances])

const Content = useMemo(() => {
if (isConnected && balances.length) {
if (isConnected && balances.size) {
return (
<>
<SimpleBar
Expand Down Expand Up @@ -292,7 +292,7 @@ function RenegadeWalletPanel() {
}
}, [
address,
balances.length,
balances.size,
formattedBalances,
isConnected,
isSigningIn,
Expand Down Expand Up @@ -325,7 +325,7 @@ function HistorySection() {
)
const balances = useBalances()
if (status !== "in relayer") return null
if (!taskHistory.length && !balances.length) return null
if (!taskHistory.length && !balances.size) return null
return (
<>
<Tooltip placement="right" label={TASK_HISTORY_TOOLTIP}>
Expand Down
4 changes: 1 addition & 3 deletions trade.renegade.fi/hooks/use-max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { formatUnits } from "viem/utils"

export function useMax(base: string) {
const balances = useBalances()
const max = balances.find(
(b) => b.mint === Token.findByTicker(base).address
)?.amount
const max = balances.get(Token.findByTicker(base).address)?.amount
return max ? formatUnits(max, Token.findByTicker(base).decimals) : ""
}
2 changes: 1 addition & 1 deletion trade.renegade.fi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@datadog/browser-rum": "^5.15.0",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@renegade-fi/react": "^0.0.45",
"@renegade-fi/react": "^0.0.47",
"@t3-oss/env-nextjs": "^0.6.0",
"@tanstack/react-query": "^5.24.1",
"@vercel/analytics": "^1.2.2",
Expand Down
10 changes: 5 additions & 5 deletions trade.renegade.fi/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee8d64b

Please sign in to comment.