forked from OnChainMee/jupitor-swap-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
43 lines (38 loc) · 1 KB
/
validation.js
File metadata and controls
43 lines (38 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { PublicKey } = require("@solana/web3.js");
function validateMint(mint) {
if (typeof mint !== "string" || !/^[A-HJ-NP-Za-km-z1-9]{32,44}$/.test(mint)) {
throw new Error(`Invalid mint address: ${mint}`);
}
}
function validateAmount(amount) {
if (typeof amount !== "number" || isNaN(amount) || amount <= 0) {
throw new Error(
`Invalid amount: ${amount}. Amount must be a positive number.`
);
}
}
function validateSlippage(slippageBps) {
if (
typeof slippageBps !== "number" ||
isNaN(slippageBps) ||
slippageBps < 0 ||
slippageBps > 10000
) {
throw new Error(
`Invalid slippage: ${slippageBps}. Slippage must be a number between 0 and 10000.`
);
}
}
function validateRetries(maxRetries) {
if (!Number.isInteger(maxRetries) || maxRetries < 1) {
throw new Error(
`Invalid max retries: ${maxRetries}. Max retries must be a positive integer.`
);
}
}
module.exports = {
validateMint,
validateAmount,
validateSlippage,
validateRetries,
};