Skip to content

Commit

Permalink
Merge pull request #36 from InvArch:yaki-input-formatting-fix
Browse files Browse the repository at this point in the history
Fix balance calculation and input handling
  • Loading branch information
shibatales authored Jan 9, 2024
2 parents 14a0a72 + 0279a71 commit 0423d17
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions src/routes/xtransfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,19 @@ const Transfer = () => {
};

const loadBalances = useCallback(async ({ address }: InjectedAccountWithMeta) => {
if (!apiBasilisk) {
return;
}
try {
if (!apiBasilisk) {
return;
}

setLoading(true);
setLoading(true);

try {
toast.loading("Loading balances...");

const balance = await api.query.system.account<SystemAccount>(address);

const total = new BigNumber(balance.data.free.toString());
const frozen = new BigNumber(balance.data.frozen.toString());
const reserved = new BigNumber(balance.data.reserved.toString());

const transferable = total.minus(frozen).minus(reserved);

setBalanceInTinkernet(transferable);
Expand Down Expand Up @@ -185,62 +183,60 @@ const Transfer = () => {
}, [apiBasilisk, api]);

const handleChangedAmount = (e: string, availableBalance: BigNumber) => {
console;
// Remove all non-numeric characters except for the decimal point
const sanitizedInput = e.replace(/[^\d.]/g, '');

// Convert the sanitized string to a number
const number = parseFloat(sanitizedInput);

// Format the available balance with 12 decimals
const formattedBalance = parseFloat(formatBalance(availableBalance.toString(), { decimals: 12, forceUnit: '-', withUnit: false }));

// Check if the number is a valid finite number and within the valid range
if (Number.isFinite(number) && number >= 0 && number <= formattedBalance) {
// Limit the number to 12 decimal places and return the value
setAmount(number.toString());
} else if (number < 0) {
// If the input is less than 0, return the minimum value
setAmount("0");
} else if (number > formattedBalance) {
// If the input is more than the available balance minus 0.01, return the maximum value
setAmount((formattedBalance).toString());
const formattedBalance = new BigNumber(availableBalance.toString()).dividedBy(new BigNumber(10).pow(12));

if (sanitizedInput === '') {
setAmount('0');
} else if (Number(sanitizedInput) >= 0 && Number(sanitizedInput) <= formattedBalance.toNumber()) {
setAmount(sanitizedInput);
} else if (Number(sanitizedInput) > formattedBalance.toNumber()) {
setAmount(formattedBalance.toString());
}
};

const balanceTNKR25 = () => {
setAmount(formatBalance(balanceInTinkernet.multipliedBy(0.25).integerValue().toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
const balance = balanceInTinkernet.multipliedBy(0.25).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceTNKR50 = () => {
setAmount(formatBalance(balanceInTinkernet.multipliedBy(0.5).integerValue().toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
const balance = balanceInTinkernet.multipliedBy(0.5).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceTNKR75 = () => {
setAmount(formatBalance(balanceInTinkernet.multipliedBy(0.75).integerValue().toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
const balance = balanceInTinkernet.multipliedBy(0.75).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceTNKR100 = () => {
const balance = balanceInTinkernet.multipliedBy(1).integerValue();
const formattedBalance = parseFloat(formatBalance(balance.toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
setAmount((formattedBalance).toFixed(4).toString());
const balance = balanceInTinkernet.multipliedBy(1).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceBSX25 = () => {
setAmount(formatBalance(balanceInBasilisk.multipliedBy(0.25).integerValue().toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
const balance = balanceInBasilisk.multipliedBy(0.25).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceBSX50 = () => {
setAmount(formatBalance(balanceInBasilisk.multipliedBy(0.5).integerValue().toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
const balance = balanceInBasilisk.multipliedBy(0.5).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceBSX75 = () => {
setAmount(formatBalance(balanceInBasilisk.multipliedBy(0.75).integerValue().toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
const balance = balanceInBasilisk.multipliedBy(0.75).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const balanceBSX100 = () => {
const balance = balanceInBasilisk.multipliedBy(1).integerValue();
const formattedBalance = parseFloat(formatBalance(balance.toString(), { decimals: 12, forceUnit: '-', withUnit: false }));
setAmount((formattedBalance).toFixed(4).toString());
const balance = balanceInBasilisk.multipliedBy(1).dividedBy(new BigNumber(10).pow(12));
setAmount(balance.toString());
};

const handleChangedDestination = (e: string) => {
Expand Down Expand Up @@ -610,7 +606,7 @@ const Transfer = () => {
TNKR Amount
</label>
<div>
<Input type="number"
<Input type="text"
value={amount}
name="amount"
id="amount"
Expand Down Expand Up @@ -683,7 +679,7 @@ const Transfer = () => {
TNKR Amount
</label>
<div>
<Input type="number"
<Input type="text"
value={amount}
name="amount"
id="amount"
Expand Down

1 comment on commit 0423d17

@vercel
Copy link

@vercel vercel bot commented on 0423d17 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.