Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nig-773 #140

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,7 @@ type NumberInputProps = {
label?: string;
max?: number;
min?: number;
integerOnly?: boolean;
size?: 'small' | 'medium';
onChange(value: number | null): void;
};
Expand All @@ -23,6 +25,7 @@ export default forwardRef<NumberInputRef, NumberInputProps>(function NumberInput
max = Number.MAX_SAFE_INTEGER,
min = Number.MIN_SAFE_INTEGER,
onChange,
integerOnly = false,
...restProps
}: NumberInputProps,
ref
Expand All @@ -38,7 +41,7 @@ export default forwardRef<NumberInputRef, NumberInputProps>(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);
Expand Down
8 changes: 8 additions & 0 deletions src/service/contract/shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ export async function transfer(address: string, amount: string) {
address,
});
}

export async function getSupply(address: string) {
const supply = await http.get<string>('/xfans/api/shares/supply', {
address,
});

return +supply / SHARE_UNIT_MODIFIER;
}
25 changes: 23 additions & 2 deletions src/welcome/Wallet/BuyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getBuyPrice,
getBuyPriceAfterFee,
getFloorPrice,
getSupply,
} from '../../service/contract/shares';
import { getBalance } from '../../service/contract/user';
import useProfileModal from '../../store/useProfileModal';
Expand Down Expand Up @@ -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<NumberInputRef>(null);

const [loadingFloorPrice, setLoadingFloorPrice] = useState<boolean>(true);
const [loadingPrice, setLoadingPrice] = useState<boolean>(false);
const [loadingPirceAfterFee, setLoadingPirceAfterFee] = useState<boolean>(false);
const [loadingBalance, setLoadingBalance] = useState<boolean>(true);
const [loadingSupply, setLoadingSupply] = useState<boolean>(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');
Expand Down Expand Up @@ -181,6 +194,11 @@ const BuyModal = ({ onClose }: BuyModalProps) => {
});
}

const unit = useMemo(() => {
if (supply <= 5) return 1;
return 0.1;
}, [supply]);

return (
<Modal open onClose={onClose} width={553} closebuttonstyle={{ marginTop: '5px' }}>
<div className="relative flex flex-col items-center">
Expand All @@ -200,15 +218,18 @@ const BuyModal = ({ onClose }: BuyModalProps) => {
className="!mt-6"
fullWidth
label="Amount"
disabled={isBuying}
integerOnly={unit === 1}
disabled={isBuying || loadingSupply}
onChange={(v) => {
setAmount(v ?? 0);
}}
/>

<div className="mt-4 flex items-center space-x-1 self-end text-black">
<span className="text-sm">Minimum unit: </span>
<span className="text-sm font-medium">0.1 </span>
<span className="text-sm font-medium">
{loadingSupply ? <CircularProgress size={8} /> : unit}
</span>
</div>

<Divider
Expand Down
Loading