import { useMemo } from "react"; import { CloseValuesType } from "../interface"; import { formatFloor } from "@utils/FormatNumber"; import { getQuantityByCost, getCostByQuantity, getValueByContr, getRateValue, getContrByValue } from "@utils/contractRules"; type OrderPriceType = { buy: number; sell: number; }; /** * 获取最大值 * @param isClose 开仓/平仓 * @param closeValues 平仓的最大:单位数量 * @param orderPrice 价格:买/卖 * @param avbBalance 资产 * @param getQuantityByClose 获取平仓数量 * @param getQuantityByOpen 获取开仓数量方法 * @returns 买/卖最大值 */ export const useMaxInputQuantity = ( isClose: boolean, closeValues: CloseValuesType | undefined, orderPrice: OrderPriceType, avbBalance: number, ctVal: string, ctValScale: number, takerRate: number, leverage: number ) => { return useMemo(() => { let maxBuyValue, maxSellValue; if (isClose && closeValues) { // 根据数量算最大可平量面值; maxBuyValue = getValueByContr({ value: closeValues.buy, multiplier: ctVal, scale: ctValScale }); maxSellValue = getValueByContr({ value: closeValues.sell, multiplier: ctVal, scale: ctValScale }); } else if (avbBalance && !isClose) { // 开仓 maxBuyValue = formatFloor( getQuantityByCost({ balance: avbBalance, price: orderPrice.buy, takerFee: takerRate, leverage }), ctValScale ); maxSellValue = formatFloor( getQuantityByCost({ balance: avbBalance, price: orderPrice.sell, takerFee: takerRate, leverage }), ctValScale ); } return { maxBuyValue: maxBuyValue, maxSellValue: maxSellValue }; }, [orderPrice, avbBalance, isClose, closeValues, ctVal, ctValScale, takerRate, leverage]); };