Skip to content

Commit

Permalink
Additional checks when transferring (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo authored Sep 12, 2023
1 parent 996c120 commit 67af4bd
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ const TransferPage = () => {
return;
}

if (amount === 0) {
toastError("Transfer amount must be greater than 0");
return;
}

if (Number.isNaN(amount)) {
toastError("Amount must be specified")
return;
}

if (countDecimalDigits(amount) > selectedAsset.decimals) {
toastError(`The asset can have only ${selectedAsset.decimals} decimals`);
return;
}

if (sourceChainId === destChainId) {
// Just do a simple token transfer.
const api = await getApi(chains[sourceChainId].rpc);
Expand Down Expand Up @@ -317,6 +332,21 @@ const TransferPage = () => {
setTransferring(false);
};

const countDecimalDigits = (n: number): number => {
const numberStr = n.toString();

// Check for scientific notation
if (numberStr.includes('e')) {
const parts = numberStr.split('e');
const decimalPart = (parts[0].split('.')[1] || '').length;
const exponentPart = parseInt(parts[1], 10);
return decimalPart - exponentPart;
} else {
const decimalPart = (numberStr.split('.')[1] || '').length;
return decimalPart;
}
}

const getParaIdFromXcmInterior = (xcmInterior: any): number => {
if (xcmInterior.length > 1 && Object.hasOwn(xcmInterior[1], 'parachain')) {
return xcmInterior[1].parachain;
Expand Down

0 comments on commit 67af4bd

Please sign in to comment.