diff --git a/src/components/NumberInput.tsx b/src/components/NumberInput.tsx index 2a3016a..1999dec 100644 --- a/src/components/NumberInput.tsx +++ b/src/components/NumberInput.tsx @@ -3,6 +3,7 @@ import { TextField } from '@mui/material'; import { debounce } from 'lodash'; const pattern = /^(0|[1-9][0-9]*)(\.[0-9]{0,1})?$/; +const integerPattern = /^[1-9][0-9]*$/; export type NumberInputRef = { reset(): void; @@ -15,6 +16,7 @@ type NumberInputProps = { label?: string; max?: number; min?: number; + integerOnly?: boolean; size?: 'small' | 'medium'; onChange(value: number | null): void; }; @@ -23,6 +25,7 @@ export default forwardRef(function NumberInput max = Number.MAX_SAFE_INTEGER, min = Number.MIN_SAFE_INTEGER, onChange, + integerOnly = false, ...restProps }: NumberInputProps, ref @@ -38,7 +41,7 @@ export default forwardRef(function NumberInput } return; } - if (pattern.test(nextValue)) { + if (integerOnly ? integerPattern.test(nextValue) : pattern.test(nextValue)) { const n = +nextValue; if (n <= max && n >= min) { setValue(nextValue); diff --git a/src/service/contract/shares.ts b/src/service/contract/shares.ts index 770598d..7ff5c92 100644 --- a/src/service/contract/shares.ts +++ b/src/service/contract/shares.ts @@ -93,3 +93,11 @@ export async function transfer(address: string, amount: string) { address, }); } + +export async function getSupply(address: string) { + const supply = await http.get('/xfans/api/shares/supply', { + address, + }); + + return +supply / SHARE_UNIT_MODIFIER; +} diff --git a/src/welcome/Wallet/BuyModal.tsx b/src/welcome/Wallet/BuyModal.tsx index 46d4658..727a398 100644 --- a/src/welcome/Wallet/BuyModal.tsx +++ b/src/welcome/Wallet/BuyModal.tsx @@ -15,6 +15,7 @@ import { getBuyPrice, getBuyPriceAfterFee, getFloorPrice, + getSupply, } from '../../service/contract/shares'; import { getBalance } from '../../service/contract/user'; import useProfileModal from '../../store/useProfileModal'; @@ -70,15 +71,27 @@ const BuyModal = ({ onClose }: BuyModalProps) => { const [balance, setBalance] = useState('0'); const [isBuying, setIsBuying] = useState(false); const [floorPrice, setFloorPrice] = useState('0'); + const [supply, setSupply] = useState(0); const numberInputRef = useRef(null); const [loadingFloorPrice, setLoadingFloorPrice] = useState(true); const [loadingPrice, setLoadingPrice] = useState(false); const [loadingPirceAfterFee, setLoadingPirceAfterFee] = useState(false); const [loadingBalance, setLoadingBalance] = useState(true); + const [loadingSupply, setLoadingSupply] = useState(true); const ethPrice = useETHPrice(); + useEffect(() => { + if (currentInfo?.walletAddress != null) { + setLoadingSupply(true); + getSupply(currentInfo?.walletAddress).then((data) => { + setLoadingSupply(false); + setSupply(data); + }); + } + }, [currentInfo]); + useEffect(() => { if (amount === 0) { setGasFee('0'); @@ -181,6 +194,11 @@ const BuyModal = ({ onClose }: BuyModalProps) => { }); } + const unit = useMemo(() => { + if (supply <= 5) return 1; + return 0.1; + }, [supply]); + return (
@@ -200,7 +218,8 @@ const BuyModal = ({ onClose }: BuyModalProps) => { className="!mt-6" fullWidth label="Amount" - disabled={isBuying} + integerOnly={unit === 1} + disabled={isBuying || loadingSupply} onChange={(v) => { setAmount(v ?? 0); }} @@ -208,7 +227,9 @@ const BuyModal = ({ onClose }: BuyModalProps) => {
Minimum unit: - 0.1 + + {loadingSupply ? : unit} +