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: init amount string for staking calc fragment #1252

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
29 changes: 18 additions & 11 deletions app/fragments/staking/StakingCalculatorFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ export const StakingCalculatorFragment = fragment(() => {

const nominator = useLiquidStakingMember(account)?.data;

const balance = useMemo(() => {
if (!isLiquid) {
return pool?.member?.balance ?? 0n;
}
const bal = fromNano(nominator?.balance || 0n);
const rate = fromNano(liquidStaking?.rateWithdraw || 0n);
return toNano((parseFloat(bal) * parseFloat(rate)).toFixed(9));
}, [nominator?.balance, liquidStaking?.rateWithdraw, isLiquid]);

const config = useStakingWalletConfig(account!.toString({ testOnly: network.isTestnet }));

const available = useMemo(() => {
Expand All @@ -72,7 +63,24 @@ export const StakingCalculatorFragment = fragment(() => {

const pool = useStakingPool(target, ledgerAddress);

const [amount, setAmount] = useState(balance ? fromNano(balance) : '');
const balance = useMemo(() => {
if (!isLiquid) {
return pool?.member?.balance ?? 0n;
}
const bal = fromNano(nominator?.balance || 0n);
const rate = fromNano(liquidStaking?.rateWithdraw || 0n);
return toNano((parseFloat(bal) * parseFloat(rate)).toFixed(9));
}, [nominator?.balance, liquidStaking?.rateWithdraw, isLiquid]);

let initialAmount = balance ? fromNano(balance) : '';
if (initialAmount) {
const parts = initialAmount.split('.');
if (parts.length > 1) {
initialAmount = `${parts[0]}.${parts[1].slice(0, 2)}`;
}
}

const [amount, setAmount] = useState(initialAmount);
const validAmount = useValidAmount(amount);

const priceText = useMemo(() => {
Expand All @@ -81,7 +89,6 @@ export const StakingCalculatorFragment = fragment(() => {
}

const isNeg = validAmount < 0n;
const abs = isNeg ? -validAmount : validAmount;

return formatCurrency(
(parseFloat(fromNano(validAmount)) * (price ? price?.price.usd * price.price.rates[currency] : 0)).toFixed(2),
Expand Down