-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidators.ts
More file actions
100 lines (73 loc) · 2.29 KB
/
validators.ts
File metadata and controls
100 lines (73 loc) · 2.29 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { type } from "arktype";
import { InvalidArgumentError } from "commander";
import { Decimal } from "decimal.js";
import { validate } from "./commands/address.js";
import { Networks } from "./endpoints.js";
const parseEmail = (value: string) => {
const result = type("string.email")(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`email ${result.summary}`);
return result;
};
const Amount = type("string.numeric")
.pipe((v) => Decimal(v))
.narrow((amount, ctx) => amount.gt(0) || ctx.mustBe("positive"));
const parseAmount = (value: string) => {
const result = Amount(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`amount ${result.summary}`);
return result;
};
const Email = type("string.email").pipe((v) => v.toLowerCase());
const Address = type(/^[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38}$/i)
.narrow((address, ctx) => validate(address) || ctx.mustBe("valid address"))
.pipe((v) => v.toLowerCase());
const parseDestination = (value: string) => {
if (value.includes("@")) {
const result = Email(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`destination ${result.summary}`);
return result;
}
const result = Address(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`destination ${result.summary}`);
return result;
};
const parseNetwork = (value: string) => {
const result = type(["===", ...Networks])(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`network ${result.summary}`);
return result;
};
const Stablecoin = type(["===", "USDA", "USDC", "USDM", "USDT"]);
const parseStablecoin = (value: string) => {
const result = Stablecoin(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`stablecoin ${result.summary}`);
return result;
};
const Blockchain = type([
"===",
"base",
"cardano",
"hyperliquid",
"solana",
"stable",
"sui",
"tron",
]);
const parseBlockchain = (value: string) => {
const result = Blockchain(value);
if (result instanceof type.errors)
throw new InvalidArgumentError(`blockchain ${result.summary}`);
return result;
};
export {
parseAmount,
parseBlockchain,
parseDestination,
parseEmail,
parseNetwork,
parseStablecoin,
};