Skip to content

Commit

Permalink
Additional checks when transferring
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Sep 12, 2023
1 parent 001eee3 commit 8ac16fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type RELAY_CHAIN_OPTION = 'polkadot' | 'kusama';
const RELAY_CHAIN_ENDPOINTS = {
polkadot: "wss://polkadot.api.onfinality.io/public-ws",
kusama: "wss://kusama.api.onfinality.io/public-ws"
polkadot: "wss://rpc.polkadot.io",
kusama: "wss://kusama-rpc.polkadot.io"
};
export const RELAY_CHAIN = (process.env.RELAY_CHAIN || 'polkadot') as RELAY_CHAIN_OPTION;
export const RELAY_CHAIN_ENDPOINT = RELAY_CHAIN_ENDPOINTS[RELAY_CHAIN];
Expand Down
31 changes: 31 additions & 0 deletions src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,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 @@ -316,6 +331,22 @@ 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 8ac16fe

Please sign in to comment.