-
Notifications
You must be signed in to change notification settings - Fork 15
/
schema.js
181 lines (169 loc) · 4.74 KB
/
schema.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const Ajv = require('ajv');
const ajv = new Ajv();
// supported chains
const CHAINS = [
{
enum: 'Ethereum',
chainId: 1,
nativeToken: { name: 'Ether', symbol: 'ETH', decimals: 18 },
},
{
enum: 'Ropsten',
chainId: 3,
testnet: true,
nativeToken: { name: 'Ropsten Ether', symbol: 'ETH', decimals: 18 },
},
{
enum: 'BSC',
chainId: 56,
nativeToken: { name: 'Binance Token', symbol: 'BNB', decimals: 18 },
},
{
enum: 'BSCTest',
chainId: 97,
testnet: true,
nativeToken: { name: 'Test Binance Token', symbol: 'BNB', decimals: 18 },
},
{
enum: 'Meter',
chainId: 82,
nativeToken: { name: 'Meter Stable', symbol: 'MTR', decimals: 18 },
},
{
enum: 'MeterTest',
chainId: 83,
testnet: true,
nativeToken: { name: 'Test Meter Stable', symbol: 'MTR', decimals: 18 },
},
{
enum: 'Moonriver',
chainId: 1285,
nativeToken: { name: 'Moonriver', symbol: 'MOVR', decimals: 18 },
},
{
enum: 'Moonbase',
chainId: 1287,
testnet: true,
nativeToken: { name: 'DEV Token', symbol: 'DEV', decimals: 18 },
},
{
enum: 'ThetaTest',
chainId: 365,
testnet: true,
nativeToken: { name: 'Theta Fuel', symbol: 'TFUEL', decimals: 18 },
},
{
enum: 'TelosTest',
chainId: 41,
testnet: true,
nativeToken: { name: 'Telos', symbol: 'TLOS', decimals: 18 },
},
{
enum: 'Theta',
chainId: 361,
nativeToken: { name: 'Theta Fuel', symbol: 'TFUEL', decimals: 18 },
},
{
enum: 'Avalanche',
chainId: 43114,
testnet: false,
nativeToken: { name: 'AVAX Token', symbol: 'AVAX', decimals: 18 },
},
{
enum: 'VoltaTest',
chainId: 73799,
testnet: true,
nativeToken: { name: 'VT Token', symbol: 'VT', decimals: 18 },
},
{
enum: 'EnergyWeb',
chainId: 246,
nativeToken: { name: 'EWT Token', symbol: 'EWT', decimals: 18 },
},
{
enum: 'Polis',
chainId: 333999,
nativeToken: { name: 'Polis Token', symbol: 'POLIS', decimals: 18 },
},
{
enum: 'Moonbeam',
chainId: 1284,
nativeToken: { name: 'GLMR token', symbol: 'GLMR', decimals: 18 },
},
{
enum: 'Polygon',
chainId: 137,
nativeToken: { name: 'MATIC Token', symbol: 'MATIC', decimals: 18 },
},
{
enum: 'Base',
chainId: 8453,
nativeToken: { name: 'Ether', symbol: 'ETH', decimals: 18 },
},
{
enum: 'Arbitrum',
chainId: 42161,
nativeToken: { name: 'Ether', symbol: 'ETH', decimals: 18 },
},
];
const tokenSchema = {
type: 'object',
properties: {
network: { enum: CHAINS.map((c) => c.enum) }, // enum for supported network
address: { type: 'string', pattern: '^0x[0-9a-zA-Z]{40}$' }, // string of 0x + 40 digit/letter
// chain-specific configs, optional
name: { type: 'string', pattern: '^[0-9a-zA-Z._ ]{1,100}$' }, // string of 1-100 digit/letter
symbol: { type: 'string', pattern: '^[0-9a-zA-Z.]{1,9}$' }, // string of 1-9 digit/upper_letter
decimals: { type: 'number', maximum: 20, minimum: 1 }, // number between 1-20
native: { type: 'boolean' }, // true - native | false - ERC20
tokenProxy: { type: 'string' }, // optional
},
required: ['network', 'address'],
};
const schema = {
type: 'object',
properties: {
resourceID: { type: 'string', pattern: '^0x[0-9a-z]{64}$' }, // string of 0x + 64 digit/lower_letter
testResourceID: { type: 'string', pattern: '^0x[0-9a-zA-Z]{64}$' }, // string of 0x + 64 digit/letter
name: { type: 'string', pattern: '^[0-9a-zA-Z._ ]{1,100}$' }, // string of 1-100 digit/letter
symbol: { type: 'string', pattern: '^[0-9a-zA-Z.]{1,9}$' }, // string of 1-9 digit/upper_letter
decimals: { type: 'number', maximum: 20, minimum: 1 }, // number between 1-20
enable: { type: 'boolean' }, // true - enable | false - disable
tokens: { type: 'array', items: tokenSchema, minItems: 1 },
rank: { type: 'number' }
},
required: ['resourceID', 'name', 'symbol', 'decimals', 'tokens'],
};
const validate = ajv.compile(schema);
const validateSchema = (jsonObj) => {
const valid = validate(jsonObj);
return { errors: validate.errors, valid };
};
const getChainId = (network) => {
const c = getChainConfig(network);
return c ? c.chainId : -1;
};
const isTestnet = (network) => {
const c = getChainConfig(network);
return c ? !!c.testnet : true;
};
const getChainConfig = (network) => {
for (const c of CHAINS) {
if (c.enum === network.toString()) {
return c;
}
}
return undefined;
};
const getChainConfigs = () => {
return CHAINS;
};
const getChainConfigById = (id) => {
for (const chain of CHAINS) {
if (chain.chainId === id) {
return chain;
}
}
return undefined;
};
module.exports = { validateSchema, getChainId, isTestnet, getChainConfig, getChainConfigs, getChainConfigById };