Skip to content

Commit

Permalink
fix: replace insufficient with undercapitalized where appropropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 25, 2024
1 parent 0ffc174 commit 244624d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
37 changes: 17 additions & 20 deletions app/orders/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip"

import { useCheckInsufficientBalancesForOrder } from "@/hooks/use-check-insufficient-balances-for-order"
import { useIsOrderUndercapitalized } from "@/hooks/use-is-order-undercapitalized"
import { ExtendedOrderMetadata } from "@/hooks/use-order-table-data"
import { useSavingsAcrossFillsQuery } from "@/hooks/use-savings-across-fills-query"
import { Side } from "@/lib/constants/protocol"
import { INSUFFICIENT_BALANCE_TOOLTIP } from "@/lib/constants/tooltips"
import { UNDERCAPITALIZED_ORDER_TOOLTIP } from "@/lib/constants/tooltips"
import {
formatCurrency,
formatNumber,
Expand Down Expand Up @@ -63,7 +63,7 @@ export const columns: ColumnDef<ExtendedOrderMetadata>[] = [
const remainingAmount =
row.original.data.amount -
row.original.fills.reduce((acc, fill) => acc + fill.amount, BigInt(0))
const { isInsufficient } = useCheckInsufficientBalancesForOrder({
const { isUndercapitalized } = useIsOrderUndercapitalized({
amount: remainingAmount,
baseMint: row.original.data.base_mint,
quoteMint: row.original.data.quote_mint,
Expand All @@ -81,7 +81,7 @@ export const columns: ColumnDef<ExtendedOrderMetadata>[] = [
OrderState.Matching,
OrderState.SettlingMatch,
].includes(row.original.state) &&
isInsufficient
isUndercapitalized
) {
return (
<div className="flex items-center justify-center">
Expand All @@ -91,7 +91,7 @@ export const columns: ColumnDef<ExtendedOrderMetadata>[] = [
</TooltipTrigger>
<TooltipContent>
<p className="font-sans">
{INSUFFICIENT_BALANCE_TOOLTIP({ ticker: token.ticker })}
{UNDERCAPITALIZED_ORDER_TOOLTIP({ ticker: token.ticker })}
</p>
</TooltipContent>
</Tooltip>
Expand All @@ -106,21 +106,17 @@ export const columns: ColumnDef<ExtendedOrderMetadata>[] = [
accessorKey: "state",
header: () => <div>Status</div>,
cell: function Cell({ row }) {
const { data: isUndercapitalized } = useBackOfQueueWallet({
query: {
select: (data) =>
data.balances.some(
(balance) =>
balance.mint ===
(row.original.data.side === "Buy"
? row.original.data.quote_mint
: row.original.data.base_mint) &&
balance.amount > row.original.data.amount,
),
},
const remainingAmount =
row.original.data.amount -
row.original.fills.reduce((acc, fill) => acc + fill.amount, BigInt(0))
const { isUndercapitalized } = useIsOrderUndercapitalized({
amount: remainingAmount,
baseMint: row.original.data.base_mint,
quoteMint: row.original.data.quote_mint,
side: row.original.data.side === "Buy" ? Side.BUY : Side.SELL,
})
let status: string = formatOrderState[row.getValue<OrderState>("status")]
if (!isUndercapitalized && status === "Open") {
if (isUndercapitalized && status === "Open") {
status = "Undercapitalized"
}
return <div className="whitespace-nowrap">{status}</div>
Expand Down Expand Up @@ -285,7 +281,8 @@ export const columns: ColumnDef<ExtendedOrderMetadata>[] = [
Number(filledAmount),
Number(totalAmount),
)
const { data: isCapitalized } = useBackOfQueueWallet({
// TODO: Check if amount > MIN_FILL_SIZE
const { data: isMatchable } = useBackOfQueueWallet({
query: {
select: (data) =>
data.balances.some(
Expand Down Expand Up @@ -314,7 +311,7 @@ export const columns: ColumnDef<ExtendedOrderMetadata>[] = [
</div>
</div>
)
} else if (isCapitalized) {
} else if (isMatchable) {
return (
<div className="whitespace-nowrap">
Finding counterparties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
ResponsiveTooltipTrigger,
} from "@/components/ui/responsive-tooltip"

import { useCheckInsufficientBalancesForOrder } from "@/hooks/use-check-insufficient-balances-for-order"
import { useIsOrderUndercapitalized } from "@/hooks/use-is-order-undercapitalized"
import { Side } from "@/lib/constants/protocol"
import { INSUFFICIENT_BALANCE_TOOLTIP } from "@/lib/constants/tooltips"
import { UNDERCAPITALIZED_ORDER_TOOLTIP } from "@/lib/constants/tooltips"
import { cn } from "@/lib/utils"

export function InsufficientWarning({
Expand All @@ -30,14 +30,14 @@ export function InsufficientWarning({
side: Side
withDialog?: boolean
}) {
const { isInsufficient, token } = useCheckInsufficientBalancesForOrder({
const { isUndercapitalized, token } = useIsOrderUndercapitalized({
amount,
baseMint,
quoteMint,
side,
})

if (!isInsufficient) return null
if (!isUndercapitalized) return null

const warningContent = (
<div className={cn("flex items-center gap-2", className)}>
Expand Down Expand Up @@ -66,7 +66,7 @@ export function InsufficientWarning({
</ResponsiveTooltipTrigger>
<ResponsiveTooltipContent>
<p>
{INSUFFICIENT_BALANCE_TOOLTIP({
{UNDERCAPITALIZED_ORDER_TOOLTIP({
ticker: token.ticker,
})}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import { formatUnits } from "viem/utils"
import { useUSDPrice } from "@/hooks/use-usd-price"
import { Side } from "@/lib/constants/protocol"

interface CheckInsufficientBalancesProps {
amount: bigint
baseMint: `0x${string}`
quoteMint: `0x${string}`
side: Side
}

export function useCheckInsufficientBalancesForOrder({
export function useIsOrderUndercapitalized({
amount,
baseMint,
quoteMint,
side,
}: CheckInsufficientBalancesProps) {
}: {
amount: bigint
baseMint: `0x${string}`
quoteMint: `0x${string}`
side: Side
}) {
const baseToken = Token.findByAddress(baseMint)
const quoteToken = Token.findByAddress(quoteMint)
const token = side === Side.BUY ? quoteToken : baseToken
Expand All @@ -30,7 +28,7 @@ export function useCheckInsufficientBalancesForOrder({

const usdPrice = useUSDPrice(Token.findByAddress(baseMint), amount)

const isInsufficient = (() => {
const isUndercapitalized = (() => {
if (side === Side.BUY) {
const formattedUsdPrice = formatUnits(
usdPrice,
Expand All @@ -46,7 +44,7 @@ export function useCheckInsufficientBalancesForOrder({
})()

return {
isInsufficient,
isUndercapitalized,
token,
}
}
6 changes: 5 additions & 1 deletion lib/constants/tooltips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export const FEES_SECTION_BINANCE_FEES =
"The estimated fees you would pay if you were to execute this order on Binance."
export const FEES_SECTION_TOTAL_SAVINGS =
"The amount you save by executing this order on Renegade."
export const INSUFFICIENT_BALANCE_TOOLTIP = ({ ticker }: { ticker: string }) =>
export const UNDERCAPITALIZED_ORDER_TOOLTIP = ({
ticker,
}: {
ticker: string
}) =>
`You do not have enough ${ticker} in your wallet to fully execute this order. Only part of the order will be filled.`
export const MAX_BALANCES_TOOLTIP = `Renegade wallets can hold a maximum of ${MAX_BALANCES} assets at a time.`
export const MAX_ORDERS_TOOLTIP = `Renegade wallets can hold a maximum of ${MAX_ORDERS} orders at a time.`
Expand Down

0 comments on commit 244624d

Please sign in to comment.