Skip to content

Commit

Permalink
fix(orders-table): display allowance warning in Safe (#5207)
Browse files Browse the repository at this point in the history
* fix(orders-table): display allowance warning in Safe

* refactor: check order allowance sufficiency taking permit into account

* chore: fix lint

* fix: check permit amount only if order was never filled

* fix: parse permit with try/catch

---------

Co-authored-by: Anxo Rodriguez <[email protected]>
  • Loading branch information
shoom3301 and anxolin authored Dec 17, 2024
1 parent 25d906c commit 21e8c57
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 241 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Order } from 'legacy/state/orders/actions'
import { useInjectedWidgetParams } from 'modules/injectedWidget'
import { pendingOrdersPricesAtom } from 'modules/orders/state/pendingOrdersPricesAtom'
import { useGetSpotPrice } from 'modules/orders/state/spotPricesAtom'
import { PendingPermitUpdater, useGetOrdersPermitStatus } from 'modules/permit'
import { BalancesAndAllowances } from 'modules/tokens'

import { useCancelOrder } from 'common/hooks/useCancelOrder'
import { useCategorizeRecentActivity } from 'common/hooks/useCategorizeRecentActivity'
Expand All @@ -21,12 +21,10 @@ import { useNavigate } from 'common/hooks/useNavigate'
import { CancellableOrder } from 'common/utils/isOrderCancellable'
import { ParsedOrder } from 'utils/orderUtils/parseOrder'

import { useGetOrdersToCheckPendingPermit } from './hooks/useGetOrdersToCheckPendingPermit'
import { OrdersTableList, useOrdersTableList } from './hooks/useOrdersTableList'
import { useOrdersTableTokenApprove } from './hooks/useOrdersTableTokenApprove'
import { useValidatePageUrlParams } from './hooks/useValidatePageUrlParams'

import { BalancesAndAllowances } from '../../../tokens'
import { OPEN_TAB, ORDERS_TABLE_TABS } from '../../const/tabs'
import { OrdersTableContainer } from '../../pure/OrdersTableContainer'
import { OrderActions } from '../../pure/OrdersTableContainer/types'
Expand Down Expand Up @@ -81,7 +79,6 @@ export function OrdersTableWidget({
const getSpotPrice = useGetSpotPrice()
const selectReceiptOrder = useSelectReceiptOrder()
const isSafeViaWc = useIsSafeViaWc()
const ordersPermitStatus = useGetOrdersPermitStatus()
const injectedWidgetParams = useInjectedWidgetParams()

const { currentTabId, currentPageNumber } = useMemo(() => {
Expand Down Expand Up @@ -163,11 +160,8 @@ export function OrdersTableWidget({

useValidatePageUrlParams(orders.length, currentTabId, currentPageNumber)

const ordersToCheckPendingPermit = useGetOrdersToCheckPendingPermit(ordersList, chainId, balancesAndAllowances)

return (
<>
<PendingPermitUpdater orders={ordersToCheckPendingPermit} />
<ContentWrapper>
{children}
<OrdersTableContainer
Expand All @@ -187,7 +181,6 @@ export function OrdersTableWidget({
allowsOffchainSigning={allowsOffchainSigning}
orderType={orderType}
pendingActivities={pendingActivity}
ordersPermitStatus={ordersPermitStatus}
injectedWidgetParams={injectedWidgetParams}
>
{isOpenOrdersTab && orders.length && <MultipleCancellationMenu pendingOrders={tableItemsToOrders(orders)} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ export interface OrderRowProps {
orderParams: OrderParams
onClick: Command
orderActions: OrderActions
hasValidPendingPermit?: boolean | undefined
children?: JSX.Element
}

Expand All @@ -160,7 +159,6 @@ export function OrderRow({
prices,
spotPrice,
children,
hasValidPendingPermit,
}: OrderRowProps) {
const { buyAmount, rateInfoParams, hasEnoughAllowance, hasEnoughBalance, chainId } = orderParams
const { creationTime, expirationTime, status } = order
Expand All @@ -173,10 +171,10 @@ export function OrderRow({
}, [orderActions, order])
const alternativeOrderModalContext = useMemo(
() => orderActions.getAlternativeOrderModalContext(order),
[order, orderActions]
[order, orderActions],
)

const withAllowanceWarning = hasEnoughAllowance === false && hasValidPendingPermit === false
const withAllowanceWarning = hasEnoughAllowance === false
const withWarning =
(hasEnoughBalance === false || withAllowanceWarning) &&
// show the warning only for pending and scheduled orders
Expand Down Expand Up @@ -401,7 +399,7 @@ export function OrderRow({
function usePricesDifference(
prices: OrderRowProps['prices'],
spotPrice: OrderRowProps['spotPrice'],
isInverted: boolean
isInverted: boolean,
): PriceDifference {
const { estimatedExecutionPrice } = prices || {}

Expand All @@ -415,13 +413,13 @@ function usePricesDifference(
*/
function useFeeAmountDifference(
{ inputCurrencyAmount }: OrderRowProps['orderParams']['rateInfoParams'],
prices: OrderRowProps['prices']
prices: OrderRowProps['prices'],
): Percent | undefined {
const { feeAmount } = prices || {}

return useSafeMemo(
() => calculatePercentageInRelationToReference({ value: feeAmount, reference: inputCurrencyAmount }),
[feeAmount, inputCurrencyAmount]
[feeAmount, inputCurrencyAmount],
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import styled from 'styled-components/macro'

import { PendingOrdersPrices } from 'modules/orders/state/pendingOrdersPricesAtom'
import { SpotPricesKeyParams } from 'modules/orders/state/spotPricesAtom'
import { OrdersPermitStatus } from 'modules/permit'
import { BalancesAndAllowances } from 'modules/tokens'

import { ordersTableFeatures } from 'common/constants/featureFlags'
Expand Down Expand Up @@ -200,7 +199,6 @@ export interface OrdersTableProps {
balancesAndAllowances: BalancesAndAllowances
getSpotPrice: (params: SpotPricesKeyParams) => Price<Currency, Currency> | null
orderActions: OrderActions
ordersPermitStatus: OrdersPermitStatus
}

export function OrdersTable({
Expand All @@ -214,7 +212,6 @@ export function OrdersTable({
getSpotPrice,
orderActions,
currentPageNumber,
ordersPermitStatus,
}: OrdersTableProps) {
const buildOrdersTableUrl = useGetBuildOrdersTableUrl()
const [isRateInverted, setIsRateInverted] = useState(false)
Expand All @@ -234,11 +231,14 @@ export function OrdersTable({
const selectedOrdersMap = useMemo(() => {
if (!selectedOrders) return {}

return selectedOrders.reduce((acc, val) => {
acc[val.id] = true
return selectedOrders.reduce(
(acc, val) => {
acc[val.id] = true

return acc
}, {} as { [key: string]: true })
return acc
},
{} as { [key: string]: true },
)
}, [selectedOrders])

// Explainer banner for orders
Expand All @@ -258,7 +258,7 @@ export function OrdersTable({

const cancellableOrders = useMemo(
() => ordersPage.filter((item) => isOrderOffChainCancellable(getParsedOrderFromTableItem(item))),
[ordersPage]
[ordersPage],
)

const allOrdersSelected = useMemo(() => {
Expand Down Expand Up @@ -294,7 +294,7 @@ export function OrdersTable({
type="checkbox"
onChange={(event) =>
orderActions.toggleOrdersForCancellation(
event.target.checked ? tableItemsToOrders(ordersPage) : []
event.target.checked ? tableItemsToOrders(ordersPage) : [],
)
}
/>
Expand Down Expand Up @@ -408,8 +408,6 @@ export function OrdersTable({

const orderParams = getOrderParams(chainId, balancesAndAllowances, order)

const hasValidPendingPermit = ordersPermitStatus[order.id]

return (
<OrderRow
key={order.id}
Expand All @@ -423,7 +421,6 @@ export function OrdersTable({
isRateInverted={isRateInverted}
orderActions={orderActions}
onClick={() => orderActions.selectReceiptOrder(order)}
hasValidPendingPermit={hasValidPendingPermit}
/>
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export default (
getSpotPrice={() => null}
orderActions={orderActions}
orderType={TabOrderTypes.LIMIT}
ordersPermitStatus={{}}
injectedWidgetParams={{}}
/>
)
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ export function OrdersTableContainer({
children,
orderType,
pendingActivities,
ordersPermitStatus,
injectedWidgetParams,
}: OrdersProps) {
const content = () => {
Expand Down Expand Up @@ -275,7 +274,6 @@ export function OrdersTableContainer({
balancesAndAllowances={balancesAndAllowances}
getSpotPrice={getSpotPrice}
orderActions={orderActions}
ordersPermitStatus={ordersPermitStatus}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Currency, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core'
import { BalancesAndAllowances } from 'modules/tokens'

import { RateInfoParams } from 'common/pure/RateInfo'
import { getOrderPermitAmount } from 'utils/orderUtils/getOrderPermitAmount'
import { ParsedOrder } from 'utils/orderUtils/parseOrder'

export interface OrderParams {
Expand All @@ -22,10 +23,12 @@ const PERCENTAGE_FOR_PARTIAL_FILLS = new Percent(5, 10000) // 0.05%
export function getOrderParams(
chainId: SupportedChainId,
balancesAndAllowances: BalancesAndAllowances,
order: ParsedOrder
order: ParsedOrder,
): OrderParams {
const isOrderAtLeastOnceFilled = order.executionData.filledAmount.gt(0)
const sellAmount = CurrencyAmount.fromRawAmount(order.inputToken, order.sellAmount)
const buyAmount = CurrencyAmount.fromRawAmount(order.outputToken, order.buyAmount)
const permitAmount = getOrderPermitAmount(chainId, order) || undefined

const rateInfoParams: RateInfoParams = {
chainId,
Expand All @@ -43,7 +46,8 @@ export function getOrderParams(
partiallyFillable: order.partiallyFillable,
sellAmount,
balance,
allowance,
// If the order has been filled at least once, we should not consider the permit amount
allowance: isOrderAtLeastOnceFilled ? getBiggerAmount(allowance, permitAmount) : allowance,
})

return {
Expand Down Expand Up @@ -73,3 +77,10 @@ function _hasEnoughBalanceAndAllowance(params: {

return { hasEnoughBalance, hasEnoughAllowance }
}

function getBiggerAmount(a: BigNumber | undefined, b: BigNumber | undefined): BigNumber | undefined {
if (!a) return b
if (!b) return a

return a.gt(b) ? a : b
}

This file was deleted.

Loading

0 comments on commit 21e8c57

Please sign in to comment.