@scallop-io/sui-scallop-sdk 0.37.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +276 -0
  3. package/dist/constants/common.d.ts +7 -0
  4. package/dist/constants/index.d.ts +1 -0
  5. package/dist/index.d.ts +3 -0
  6. package/dist/index.js +1487 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/index.mjs +1444 -0
  9. package/dist/index.mjs.map +1 -0
  10. package/dist/models/index.d.ts +4 -0
  11. package/dist/models/scallop.d.ts +46 -0
  12. package/dist/models/scallopAddress.d.ts +107 -0
  13. package/dist/models/scallopClient.d.ts +151 -0
  14. package/dist/models/scallopUtils.d.ts +56 -0
  15. package/dist/queries/index.d.ts +2 -0
  16. package/dist/queries/market.d.ts +4 -0
  17. package/dist/queries/obligation.d.ts +8 -0
  18. package/dist/txBuilders/coin.d.ts +67 -0
  19. package/dist/txBuilders/index.d.ts +1 -0
  20. package/dist/txBuilders/normalMethods.d.ts +3 -0
  21. package/dist/txBuilders/oracle.d.ts +7 -0
  22. package/dist/txBuilders/quickMethods.d.ts +7 -0
  23. package/dist/types/data.d.ts +127 -0
  24. package/dist/types/index.d.ts +3 -0
  25. package/dist/types/model.d.ts +9 -0
  26. package/dist/types/txBuilder.d.ts +66 -0
  27. package/package.json +147 -0
  28. package/src/constants/common.ts +36 -0
  29. package/src/constants/index.ts +1 -0
  30. package/src/index.ts +3 -0
  31. package/src/models/index.ts +4 -0
  32. package/src/models/scallop.ts +76 -0
  33. package/src/models/scallopAddress.ts +460 -0
  34. package/src/models/scallopClient.ts +461 -0
  35. package/src/models/scallopUtils.ts +133 -0
  36. package/src/queries/index.ts +2 -0
  37. package/src/queries/market.ts +16 -0
  38. package/src/queries/obligation.ts +44 -0
  39. package/src/txBuilders/coin.ts +38 -0
  40. package/src/txBuilders/index.ts +1 -0
  41. package/src/txBuilders/normalMethods.ts +216 -0
  42. package/src/txBuilders/oracle.ts +376 -0
  43. package/src/txBuilders/quickMethods.ts +231 -0
  44. package/src/types/data.ts +170 -0
  45. package/src/types/index.ts +3 -0
  46. package/src/types/model.ts +15 -0
  47. package/src/types/txBuilder.ts +136 -0
@@ -0,0 +1,460 @@
1
+ import axios, { AxiosInstance } from 'axios';
2
+ import { API_BASE_URL } from '../constants';
3
+ import type { NetworkType } from '@scallop-io/sui-kit';
4
+ import type {
5
+ ScallopAddressParams,
6
+ AddressesInterface,
7
+ AddressStringPath,
8
+ } from '../types';
9
+
10
+ /**
11
+ * it provides methods for managing addresses.
12
+ */
13
+ export class ScallopAddress {
14
+ private _apiClient: AxiosInstance;
15
+ private _id?: string;
16
+ private readonly _auth?: string;
17
+ private readonly _network: NetworkType;
18
+ private _addresses?: AddressesInterface;
19
+ private _addressesMap: Map<NetworkType, AddressesInterface>;
20
+
21
+ public constructor(params: ScallopAddressParams) {
22
+ const { id, auth, network } = params;
23
+
24
+ if (auth) this._auth = auth;
25
+ this._id = id;
26
+ this._network = network || 'mainnet';
27
+ this._addressesMap = new Map();
28
+ this._apiClient = axios.create({
29
+ baseURL: API_BASE_URL,
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ Accept: 'application/json',
33
+ },
34
+ timeout: 30000,
35
+ });
36
+ }
37
+
38
+ /**
39
+ * Get addresses API id.
40
+ *
41
+ * @returns The addresses API id.
42
+ */
43
+ public getId() {
44
+ return this._id;
45
+ }
46
+
47
+ /**
48
+ * Get the address at the provided path.
49
+ *
50
+ * @param path - The path of the address to get.
51
+ * @returns The address at the provided path.
52
+ */
53
+ public get(path: AddressStringPath) {
54
+ if (this._addresses) {
55
+ const value = path
56
+ .split('.')
57
+ .reduce(
58
+ (nestedAddressObj: any, key: string) =>
59
+ typeof nestedAddressObj === 'object'
60
+ ? nestedAddressObj[key]
61
+ : nestedAddressObj,
62
+ this._addresses
63
+ );
64
+ return value || undefined;
65
+ } else {
66
+ return undefined;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Set the address at the provided path.
72
+ *
73
+ * @param path - The path of the address to set.
74
+ * @param address - The address be setted to the tartget path.
75
+ * @returns The addresses.
76
+ */
77
+ public set(path: AddressStringPath, address: string) {
78
+ if (this._addresses) {
79
+ const keys = path.split('.');
80
+ keys.reduce((nestedAddressObj: any, key: string, index) => {
81
+ if (index === keys.length - 1) {
82
+ nestedAddressObj[key] = address;
83
+ } else {
84
+ return nestedAddressObj[key];
85
+ }
86
+ }, this._addresses);
87
+ }
88
+ return this._addresses;
89
+ }
90
+
91
+ /**
92
+ * Get the addresses.
93
+ *
94
+ * @param network - Specifies which network's addresses you want to get.
95
+ * @returns The addresses.
96
+ */
97
+ public getAddresses(network?: NetworkType) {
98
+ if (network) {
99
+ return this._addressesMap.get(network);
100
+ } else {
101
+ return this._addresses;
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Set the addresses into addresses map.
107
+ *
108
+ * @param network - Specifies which network's addresses you want to set.
109
+ * @param addresses - The addresses be setted to the tartget network.
110
+ * @returns The addresses.
111
+ */
112
+ public setAddresses(network?: NetworkType, addresses?: AddressesInterface) {
113
+ const targetNetwork = network || this._network;
114
+ const targetAddresses = addresses || this._addresses || undefined;
115
+ if (targetAddresses) {
116
+ this._addressesMap.set(targetNetwork, targetAddresses);
117
+ } else {
118
+ // TODO: change to new format version
119
+ this._addressesMap.set(targetNetwork, {
120
+ core: {
121
+ version: '',
122
+ versionCap: '',
123
+ market: '',
124
+ adminCap: '',
125
+ coinDecimalsRegistry: '',
126
+ coins: {
127
+ btc: {
128
+ id: '',
129
+ metaData: '',
130
+ treasury: '',
131
+ oracle: {
132
+ supra: '',
133
+ switchboard: '',
134
+ pyth: {
135
+ feed: '',
136
+ feedObject: '',
137
+ },
138
+ },
139
+ },
140
+ eth: {
141
+ id: '',
142
+ metaData: '',
143
+ treasury: '',
144
+ oracle: {
145
+ supra: '',
146
+ switchboard: '',
147
+ pyth: {
148
+ feed: '',
149
+ feedObject: '',
150
+ },
151
+ },
152
+ },
153
+ usdc: {
154
+ id: '',
155
+ metaData: '',
156
+ treasury: '',
157
+ oracle: {
158
+ supra: '',
159
+ switchboard: '',
160
+ pyth: {
161
+ feed: '',
162
+ feedObject: '',
163
+ },
164
+ },
165
+ },
166
+ usdt: {
167
+ id: '',
168
+ metaData: '',
169
+ treasury: '',
170
+ oracle: {
171
+ supra: '',
172
+ switchboard: '',
173
+ pyth: {
174
+ feed: '',
175
+ feedObject: '',
176
+ },
177
+ },
178
+ },
179
+ sui: {
180
+ id: '',
181
+ metaData: '',
182
+ treasury: '',
183
+ oracle: {
184
+ supra: '',
185
+ switchboard: '',
186
+ pyth: {
187
+ feed: '',
188
+ feedObject: '',
189
+ },
190
+ },
191
+ },
192
+ },
193
+ oracles: {
194
+ xOracle: '',
195
+ xOracleCap: '',
196
+ supra: {
197
+ registry: '',
198
+ registryCap: '',
199
+ holder: '',
200
+ },
201
+ switchboard: {
202
+ registry: '',
203
+ registryCap: '',
204
+ },
205
+ pyth: {
206
+ registry: '',
207
+ registryCap: '',
208
+ state: '',
209
+ wormhole: '',
210
+ wormholeState: '',
211
+ },
212
+ },
213
+ packages: {
214
+ coinDecimalsRegistry: {
215
+ id: '',
216
+ upgradeCap: '',
217
+ },
218
+ math: {
219
+ id: '',
220
+ upgradeCap: '',
221
+ },
222
+ whitelist: {
223
+ id: '',
224
+ upgradeCap: '',
225
+ },
226
+ x: {
227
+ id: '',
228
+ upgradeCap: '',
229
+ },
230
+ protocol: {
231
+ id: '',
232
+ upgradeCap: '',
233
+ },
234
+ query: {
235
+ id: '',
236
+ upgradeCap: '',
237
+ },
238
+ // Deploy by pyth on testnet
239
+ pyth: {
240
+ id: '',
241
+ upgradeCap: '',
242
+ },
243
+ // Deploy by ourself on testnet
244
+ switchboard: {
245
+ id: '',
246
+ upgradeCap: '',
247
+ },
248
+ xOracle: {
249
+ id: '',
250
+ upgradeCap: '',
251
+ },
252
+ // Deploy for faucet on testnet
253
+ testCoin: {
254
+ id: '',
255
+ upgradeCap: '',
256
+ },
257
+ },
258
+ },
259
+ });
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Get all addresses.
265
+ *
266
+ * @returns All addresses.
267
+ */
268
+ public getAllAddresses() {
269
+ return Object.fromEntries(this._addressesMap);
270
+ }
271
+
272
+ /**
273
+ * Create a new address through the API and synchronize it back to the
274
+ * instance. If the `network` is not specified, the mainnet is used by default.
275
+ * If no `addresses` is provided, an addresses with all empty strings is created
276
+ * by default.
277
+ *
278
+ * This function only allows for one addresses to be input into a specific network
279
+ * at a time, and does not provide an addresses map for setting addresses
280
+ * across all networks at once.
281
+ *
282
+ * @param network - Specifies which network's addresses you want to set.
283
+ * @param addresses - The addresses be setted to the tartget network.
284
+ * @param auth - The authentication API key.
285
+ * @returns The addresses.
286
+ */
287
+ public async create(
288
+ network?: NetworkType,
289
+ addresses?: AddressesInterface,
290
+ auth?: string
291
+ ) {
292
+ const apiKey = auth || this._auth || undefined;
293
+ const targetNetwork = network || this._network;
294
+ const targetAddresses = addresses || this._addresses || undefined;
295
+ if (apiKey !== undefined) {
296
+ this._addressesMap.clear();
297
+ this.setAddresses(targetNetwork, targetAddresses);
298
+ const response = await this._apiClient.post(
299
+ `${API_BASE_URL}/addresses`,
300
+ JSON.stringify(Object.fromEntries(this._addressesMap)),
301
+ {
302
+ headers: {
303
+ 'Content-Type': 'application/json',
304
+ 'api-key': auth || this._auth,
305
+ },
306
+ }
307
+ );
308
+
309
+ if (response.status === 201) {
310
+ for (const [network, addresses] of Object.entries<AddressesInterface>(
311
+ response.data
312
+ )) {
313
+ if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
314
+ if (network === this._network) this._addresses = addresses;
315
+ this._addressesMap.set(network as NetworkType, addresses);
316
+ }
317
+ }
318
+ this._id = response.data.id;
319
+ return this._addresses;
320
+ } else {
321
+ throw Error('Failed to create addresses.');
322
+ }
323
+ } else {
324
+ throw Error("You don't have permission to access this request.");
325
+ }
326
+ }
327
+
328
+ /**
329
+ * It doesn't read the data stored in the address instance, but reads and
330
+ * synchronizes the data from the API into instance.
331
+ *
332
+ * @param id - The id of the addresses to get.
333
+ * @returns The addresses.
334
+ */
335
+ public async read(id?: string) {
336
+ const addressesId = id || this._id || undefined;
337
+
338
+ if (addressesId !== undefined) {
339
+ const response = await this._apiClient.get(
340
+ `${API_BASE_URL}/addresses/${addressesId}`,
341
+ {
342
+ headers: {
343
+ 'Content-Type': 'application/json',
344
+ },
345
+ }
346
+ );
347
+
348
+ if (response.status === 200) {
349
+ for (const [network, addresses] of Object.entries<AddressesInterface>(
350
+ response.data
351
+ )) {
352
+ if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
353
+ if (network === this._network) this._addresses = addresses;
354
+ this._addressesMap.set(network as NetworkType, addresses);
355
+ }
356
+ }
357
+ this._id = response.data.id;
358
+ return this._addresses;
359
+ } else {
360
+ throw Error('Failed to create addresses.');
361
+ }
362
+ }
363
+ }
364
+
365
+ /**
366
+ * Update the address through the API and synchronize it back to the
367
+ * instance. If the `network` is not specified, the mainnet is used by default.
368
+ * If no `addresses` is provided, an addresses with all empty strings is created
369
+ * by default.
370
+ *
371
+ * This function only allows for one addresses to be input into a specific network
372
+ * at a time, and does not provide an addresses map for setting addresses
373
+ * across all networks at once.
374
+ *
375
+ * @param id - The id of the addresses to update.
376
+ * @param network - Specifies which network's addresses you want to set.
377
+ * @param addresses - The addresses be setted to the tartget network.
378
+ * @param auth - The authentication api key.
379
+ * @returns The addresses.
380
+ */
381
+ public async update(
382
+ id?: string,
383
+ network?: NetworkType,
384
+ addresses?: AddressesInterface,
385
+ auth?: string
386
+ ) {
387
+ const apiKey = auth || this._auth || undefined;
388
+ const targetId = id || this._id || undefined;
389
+ const targetNetwork = network || this._network;
390
+ const targetAddresses = addresses || this._addresses || undefined;
391
+ if (targetId === undefined) throw Error('Require addresses id.');
392
+ if (apiKey !== undefined) {
393
+ if (id !== this._id) {
394
+ this._addressesMap.clear();
395
+ }
396
+ this.setAddresses(targetNetwork, targetAddresses);
397
+ const response = await this._apiClient.put(
398
+ `${API_BASE_URL}/addresses/${targetId}`,
399
+ JSON.stringify(Object.fromEntries(this._addressesMap)),
400
+ {
401
+ headers: {
402
+ 'Content-Type': 'application/json',
403
+ 'api-key': auth || this._auth,
404
+ },
405
+ }
406
+ );
407
+
408
+ if (response.status === 200) {
409
+ for (const [network, addresses] of Object.entries<AddressesInterface>(
410
+ response.data
411
+ )) {
412
+ if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
413
+ if (network === this._network) this._addresses = addresses;
414
+ this._addressesMap.set(network as NetworkType, addresses);
415
+ }
416
+ }
417
+ this._id = response.data.id;
418
+ return this._addresses;
419
+ } else {
420
+ throw Error('Failed to update addresses.');
421
+ }
422
+ } else {
423
+ throw Error("You don't have permission to access this request.");
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Deletes all addresses of a specified id through the API and synchronizes
429
+ * them back to the instance.
430
+ *
431
+ * @param id - The id of the addresses to delete.
432
+ * @param auth - The authentication API key.
433
+ */
434
+ public async delete(id?: string, auth?: string) {
435
+ const apiKey = auth || this._auth || undefined;
436
+ const targetId = id || this._id || undefined;
437
+ if (targetId === undefined) throw Error('Require addresses id.');
438
+ if (apiKey !== undefined) {
439
+ const response = await this._apiClient.delete(
440
+ `${API_BASE_URL}/addresses/${targetId}`,
441
+ {
442
+ headers: {
443
+ 'Content-Type': 'application/json',
444
+ 'api-key': auth || this._auth,
445
+ },
446
+ }
447
+ );
448
+
449
+ if (response.status === 200) {
450
+ this._id = undefined;
451
+ this._addresses = undefined;
452
+ this._addressesMap.clear();
453
+ } else {
454
+ throw Error('Failed to delete addresses.');
455
+ }
456
+ } else {
457
+ throw Error("You don't have permission to access this request.");
458
+ }
459
+ }
460
+ }