@exodus/sui-api 0.1.0

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 (44) hide show
  1. package/README.md +20 -0
  2. package/package.json +53 -0
  3. package/src/api.js +198 -0
  4. package/src/balances.js +22 -0
  5. package/src/basic-account-state.js +11 -0
  6. package/src/create-unsigned-tx.js +60 -0
  7. package/src/get-fee-async.js +62 -0
  8. package/src/get-fee.js +10 -0
  9. package/src/index.js +9 -0
  10. package/src/sui-no-history-monitor.js +277 -0
  11. package/src/transform-tx.js +100 -0
  12. package/src/tx-send.js +249 -0
  13. package/src/vendors/client/client.js +891 -0
  14. package/src/vendors/client/errors.js +38 -0
  15. package/src/vendors/client/http-transport.js +108 -0
  16. package/src/vendors/client/index.js +1 -0
  17. package/src/vendors/client/rpc-websocket-client.js +178 -0
  18. package/src/vendors/experimental/cache.js +89 -0
  19. package/src/vendors/experimental/client.js +27 -0
  20. package/src/vendors/experimental/core.js +105 -0
  21. package/src/vendors/experimental/errors.js +32 -0
  22. package/src/vendors/experimental/index.js +5 -0
  23. package/src/vendors/experimental/mvr.js +443 -0
  24. package/src/vendors/experimental/transports/json-rpc-resolver.js +359 -0
  25. package/src/vendors/experimental/transports/jsonRPC.js +504 -0
  26. package/src/vendors/experimental/transports/utils.js +90 -0
  27. package/src/vendors/transactions/Arguments.js +22 -0
  28. package/src/vendors/transactions/Commands.js +108 -0
  29. package/src/vendors/transactions/Inputs.js +59 -0
  30. package/src/vendors/transactions/ObjectCache.js +272 -0
  31. package/src/vendors/transactions/Transaction.js +843 -0
  32. package/src/vendors/transactions/TransactionData.js +293 -0
  33. package/src/vendors/transactions/data/internal.js +218 -0
  34. package/src/vendors/transactions/data/v1.js +538 -0
  35. package/src/vendors/transactions/data/v2.js +119 -0
  36. package/src/vendors/transactions/hash.js +12 -0
  37. package/src/vendors/transactions/index.js +1 -0
  38. package/src/vendors/transactions/object.js +22 -0
  39. package/src/vendors/transactions/plugins/NamedPackagesPlugin.js +49 -0
  40. package/src/vendors/transactions/pure.js +40 -0
  41. package/src/vendors/transactions/resolve.js +95 -0
  42. package/src/vendors/transactions/serializer.js +203 -0
  43. package/src/vendors/transactions/utils.js +67 -0
  44. package/src/vendors/valibot.js +7920 -0
@@ -0,0 +1,443 @@
1
+ /* eslint-disable no-sequences */
2
+ /* eslint-disable camelcase */
3
+ import { vendorLib } from '@exodus/sui-lib'
4
+
5
+ const { chunk, DataLoader } = vendorLib.utils
6
+ const {
7
+ isValidNamedPackage,
8
+ isValidNamedType,
9
+ isValidSuiAddress,
10
+ normalizeStructTag,
11
+ normalizeSuiAddress,
12
+ parseStructTag,
13
+ } = vendorLib.utils
14
+
15
+ var __typeError = (msg) => {
16
+ throw new TypeError(msg)
17
+ }
18
+
19
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError('Cannot ' + msg)
20
+ var __privateGet = (obj, member, getter) => (
21
+ __accessCheck(obj, member, 'read from private field'), getter ? getter.call(obj) : member.get(obj)
22
+ )
23
+ var __privateAdd = (obj, member, value) =>
24
+ member.has(obj)
25
+ ? __typeError('Cannot add the same private member more than once')
26
+ : member instanceof WeakSet
27
+ ? member.add(obj)
28
+ : member.set(obj, value)
29
+ var __privateSet = (obj, member, value, setter) => (
30
+ __accessCheck(obj, member, 'write to private field'),
31
+ setter ? setter.call(obj, value) : member.set(obj, value),
32
+ value
33
+ )
34
+ var __privateMethod = (obj, member, method) => (
35
+ __accessCheck(obj, member, 'access private method'), method
36
+ )
37
+ var _cache,
38
+ _url,
39
+ _pageSize,
40
+ _overrides,
41
+ _MvrClient_instances,
42
+ mvrPackageDataLoader_get,
43
+ mvrTypeDataLoader_get,
44
+ resolvePackages_fn,
45
+ resolveTypes_fn,
46
+ fetch_fn
47
+
48
+ export const PACKAGE_VERSION = '1.36.1'
49
+ export const TARGETED_RPC_VERSION = '1.55.0'
50
+
51
+ const NAME_SEPARATOR = '/'
52
+ const MVR_API_HEADER = {
53
+ 'Mvr-Source': `@mysten/sui@${PACKAGE_VERSION}`,
54
+ }
55
+
56
+ class MvrClient {
57
+ constructor({ cache, url, pageSize = 50, overrides }) {
58
+ __privateAdd(this, _MvrClient_instances)
59
+ __privateAdd(this, _cache)
60
+ __privateAdd(this, _url)
61
+ __privateAdd(this, _pageSize)
62
+ __privateAdd(this, _overrides)
63
+ __privateSet(this, _cache, cache)
64
+ __privateSet(this, _url, url)
65
+ __privateSet(this, _pageSize, pageSize)
66
+ __privateSet(this, _overrides, {
67
+ packages: overrides?.packages,
68
+ types: overrides?.types,
69
+ })
70
+ validateOverrides(__privateGet(this, _overrides))
71
+ }
72
+
73
+ async resolvePackage({ package: name }) {
74
+ const resolved = await __privateGet(this, _MvrClient_instances, mvrPackageDataLoader_get).load(
75
+ name
76
+ )
77
+ return {
78
+ package: resolved,
79
+ }
80
+ }
81
+
82
+ async resolveType({ type }) {
83
+ const mvrTypes = [...extractMvrTypes(type)]
84
+ const resolvedTypes = await __privateGet(
85
+ this,
86
+ _MvrClient_instances,
87
+ mvrTypeDataLoader_get
88
+ ).loadMany(mvrTypes)
89
+ const typeMap = Object.create(null)
90
+ for (const [i, mvrType] of mvrTypes.entries()) {
91
+ const resolvedType = resolvedTypes[i]
92
+ if (resolvedType instanceof Error) {
93
+ throw resolvedType
94
+ }
95
+
96
+ typeMap[mvrType] = resolvedType
97
+ }
98
+
99
+ return {
100
+ type: replaceMvrNames(type, typeMap),
101
+ }
102
+ }
103
+
104
+ async resolve({ types = [], packages = [] }) {
105
+ const mvrTypes = /* @__PURE__ */ new Set()
106
+ for (const type of types ?? []) {
107
+ extractMvrTypes(type, mvrTypes)
108
+ }
109
+
110
+ const typesArray = [...mvrTypes]
111
+ const [resolvedTypes, resolvedPackages] = await Promise.all([
112
+ typesArray.length > 0
113
+ ? __privateGet(this, _MvrClient_instances, mvrTypeDataLoader_get).loadMany(typesArray)
114
+ : [],
115
+ packages.length > 0
116
+ ? __privateGet(this, _MvrClient_instances, mvrPackageDataLoader_get).loadMany(packages)
117
+ : [],
118
+ ])
119
+ const typeMap = {
120
+ ...__privateGet(this, _overrides)?.types,
121
+ }
122
+ for (const [i, type] of typesArray.entries()) {
123
+ const resolvedType = resolvedTypes[i]
124
+ if (resolvedType instanceof Error) {
125
+ throw resolvedType
126
+ }
127
+
128
+ typeMap[type] = resolvedType
129
+ }
130
+
131
+ const replacedTypes = Object.create(null)
132
+ for (const type of types ?? []) {
133
+ const resolvedType = replaceMvrNames(type, typeMap)
134
+ replacedTypes[type] = {
135
+ type: resolvedType,
136
+ }
137
+ }
138
+
139
+ const replacedPackages = Object.create(null)
140
+ for (const [i, pkg] of (packages ?? []).entries()) {
141
+ const resolvedPkg = __privateGet(this, _overrides)?.packages?.[pkg] ?? resolvedPackages[i]
142
+ if (resolvedPkg instanceof Error) {
143
+ throw resolvedPkg
144
+ }
145
+
146
+ replacedPackages[pkg] = {
147
+ package: resolvedPkg,
148
+ }
149
+ }
150
+
151
+ return {
152
+ types: replacedTypes,
153
+ packages: replacedPackages,
154
+ }
155
+ }
156
+ }
157
+ _cache = new WeakMap()
158
+ _url = new WeakMap()
159
+ _pageSize = new WeakMap()
160
+ _overrides = new WeakMap()
161
+ _MvrClient_instances = new WeakSet()
162
+ mvrPackageDataLoader_get = function () {
163
+ return __privateGet(this, _cache).readSync(
164
+ ['#mvrPackageDataLoader', __privateGet(this, _url) ?? ''],
165
+ () => {
166
+ const loader = new DataLoader(async (packages) => {
167
+ if (!__privateGet(this, _url)) {
168
+ throw new Error(
169
+ `MVR Api URL is not set for the current client (resolving ${packages.join(', ')})`
170
+ )
171
+ }
172
+
173
+ const resolved = await __privateMethod(this, _MvrClient_instances, resolvePackages_fn).call(
174
+ this,
175
+ packages
176
+ )
177
+ return packages.map(
178
+ (pkg) => resolved[pkg] ?? new Error(`Failed to resolve package: ${pkg}`)
179
+ )
180
+ })
181
+ const overrides = __privateGet(this, _overrides)?.packages
182
+ if (overrides) {
183
+ for (const [pkg, id] of Object.entries(overrides)) {
184
+ loader.prime(pkg, id)
185
+ }
186
+ }
187
+
188
+ return loader
189
+ }
190
+ )
191
+ }
192
+
193
+ mvrTypeDataLoader_get = function () {
194
+ return __privateGet(this, _cache).readSync(
195
+ ['#mvrTypeDataLoader', __privateGet(this, _url) ?? ''],
196
+ () => {
197
+ const loader = new DataLoader(async (types) => {
198
+ if (!__privateGet(this, _url)) {
199
+ throw new Error(
200
+ `MVR Api URL is not set for the current client (resolving ${types.join(', ')})`
201
+ )
202
+ }
203
+
204
+ const resolved = await __privateMethod(this, _MvrClient_instances, resolveTypes_fn).call(
205
+ this,
206
+ types
207
+ )
208
+ return types.map((type) => resolved[type] ?? new Error(`Failed to resolve type: ${type}`))
209
+ })
210
+ const overrides = __privateGet(this, _overrides)?.types
211
+ if (overrides) {
212
+ for (const [type, id] of Object.entries(overrides)) {
213
+ loader.prime(type, id)
214
+ }
215
+ }
216
+
217
+ return loader
218
+ }
219
+ )
220
+ }
221
+
222
+ resolvePackages_fn = async function (packages) {
223
+ if (packages.length === 0) return Object.create(null)
224
+ const batches = chunk(packages, __privateGet(this, _pageSize))
225
+ const results = Object.create(null)
226
+ await Promise.all(
227
+ batches.map(async (batch) => {
228
+ const data = await __privateMethod(this, _MvrClient_instances, fetch_fn).call(
229
+ this,
230
+ '/v1/resolution/bulk',
231
+ {
232
+ names: batch,
233
+ }
234
+ )
235
+ if (!data?.resolution) return
236
+ for (const pkg of Object.keys(data?.resolution)) {
237
+ const pkgData = data.resolution[pkg]?.package_id
238
+ if (!pkgData) continue
239
+ results[pkg] = pkgData
240
+ }
241
+ })
242
+ )
243
+ return results
244
+ }
245
+
246
+ resolveTypes_fn = async function (types) {
247
+ if (types.length === 0) return Object.create(null)
248
+ const batches = chunk(types, __privateGet(this, _pageSize))
249
+ const results = Object.create(null)
250
+ await Promise.all(
251
+ batches.map(async (batch) => {
252
+ const data = await __privateMethod(this, _MvrClient_instances, fetch_fn).call(
253
+ this,
254
+ '/v1/struct-definition/bulk',
255
+ {
256
+ types: batch,
257
+ }
258
+ )
259
+ if (!data?.resolution) return
260
+ for (const type of Object.keys(data?.resolution)) {
261
+ const typeData = data.resolution[type]?.type_tag
262
+ if (!typeData) continue
263
+ results[type] = typeData
264
+ }
265
+ })
266
+ )
267
+ return results
268
+ }
269
+
270
+ fetch_fn = async function (url, body) {
271
+ if (!__privateGet(this, _url)) {
272
+ throw new Error('MVR Api URL is not set for the current client')
273
+ }
274
+
275
+ const response = await fetch(`${__privateGet(this, _url)}${url}`, {
276
+ method: 'POST',
277
+ headers: {
278
+ 'Content-Type': 'application/json',
279
+ ...MVR_API_HEADER,
280
+ },
281
+ body: JSON.stringify(body),
282
+ })
283
+ if (!response.ok) {
284
+ const errorBody = await response.json().catch(() => Object.create(null))
285
+ throw new Error(`Failed to resolve types: ${errorBody?.message}`)
286
+ }
287
+
288
+ return response.json()
289
+ }
290
+
291
+ function validateOverrides(overrides) {
292
+ if (overrides?.packages) {
293
+ for (const [pkg, id] of Object.entries(overrides.packages)) {
294
+ if (!isValidNamedPackage(pkg)) {
295
+ throw new Error(`Invalid package name: ${pkg}`)
296
+ }
297
+
298
+ if (!isValidSuiAddress(normalizeSuiAddress(id))) {
299
+ throw new Error(`Invalid package ID: ${id}`)
300
+ }
301
+ }
302
+ }
303
+
304
+ if (overrides?.types) {
305
+ for (const [type, val] of Object.entries(overrides.types)) {
306
+ if (parseStructTag(type).typeParams.length > 0) {
307
+ throw new Error(
308
+ 'Type overrides must be first-level only. If you want to supply generic types, just pass each type individually.'
309
+ )
310
+ }
311
+
312
+ const parsedValue = parseStructTag(val)
313
+ if (!isValidSuiAddress(parsedValue.address)) {
314
+ throw new Error(`Invalid type: ${val}`)
315
+ }
316
+ }
317
+ }
318
+ }
319
+
320
+ function extractMvrTypes(type, types = /* @__PURE__ */ new Set()) {
321
+ if (typeof type === 'string' && !hasMvrName(type)) return types
322
+ const tag = isStructTag(type) ? type : parseStructTag(type)
323
+ if (hasMvrName(tag.address)) types.add(`${tag.address}::${tag.module}::${tag.name}`)
324
+ for (const param of tag.typeParams) {
325
+ extractMvrTypes(param, types)
326
+ }
327
+
328
+ return types
329
+ }
330
+
331
+ function replaceMvrNames(tag, typeCache) {
332
+ const type = isStructTag(tag) ? tag : parseStructTag(tag)
333
+ const typeTag = `${type.address}::${type.module}::${type.name}`
334
+ const cacheHit = typeCache[typeTag]
335
+ return normalizeStructTag({
336
+ ...type,
337
+ address: cacheHit ? cacheHit.split('::')[0] : type.address,
338
+ typeParams: type.typeParams.map((param) => replaceMvrNames(param, typeCache)),
339
+ })
340
+ }
341
+
342
+ function hasMvrName(nameOrType) {
343
+ return (
344
+ nameOrType.includes(NAME_SEPARATOR) || nameOrType.includes('@') || nameOrType.includes('.sui')
345
+ )
346
+ }
347
+
348
+ function isStructTag(type) {
349
+ return (
350
+ typeof type === 'object' &&
351
+ 'address' in type &&
352
+ 'module' in type &&
353
+ 'name' in type &&
354
+ 'typeParams' in type
355
+ )
356
+ }
357
+
358
+ function findNamesInTransaction(builder) {
359
+ const packages = /* @__PURE__ */ new Set()
360
+ const types = /* @__PURE__ */ new Set()
361
+ for (const command of builder.commands) {
362
+ switch (command.$kind) {
363
+ case 'MakeMoveVec':
364
+ if (command.MakeMoveVec.type) {
365
+ getNamesFromTypeList([command.MakeMoveVec.type]).forEach((type) => {
366
+ types.add(type)
367
+ })
368
+ }
369
+
370
+ break
371
+ case 'MoveCall':
372
+ const moveCall = command.MoveCall
373
+ const pkg = moveCall.package.split('::')[0]
374
+ if (hasMvrName(pkg)) {
375
+ if (!isValidNamedPackage(pkg)) throw new Error(`Invalid package name: ${pkg}`)
376
+ packages.add(pkg)
377
+ }
378
+
379
+ getNamesFromTypeList(moveCall.typeArguments ?? []).forEach((type) => {
380
+ types.add(type)
381
+ })
382
+ break
383
+ default:
384
+ break
385
+ }
386
+ }
387
+
388
+ return {
389
+ packages: [...packages],
390
+ types: [...types],
391
+ }
392
+ }
393
+
394
+ function replaceNames(builder, resolved) {
395
+ for (const command of builder.commands) {
396
+ if (command.MakeMoveVec?.type) {
397
+ if (!hasMvrName(command.MakeMoveVec.type)) continue
398
+ if (!resolved.types[command.MakeMoveVec.type]) {
399
+ throw new Error(`No resolution found for type: ${command.MakeMoveVec.type}`)
400
+ }
401
+
402
+ command.MakeMoveVec.type = resolved.types[command.MakeMoveVec.type].type
403
+ }
404
+
405
+ const tx = command.MoveCall
406
+ if (!tx) continue
407
+ const nameParts = tx.package.split('::')
408
+ const name = nameParts[0]
409
+ if (hasMvrName(name) && !resolved.packages[name]) {
410
+ throw new Error(`No address found for package: ${name}`)
411
+ }
412
+
413
+ if (hasMvrName(name)) {
414
+ nameParts[0] = resolved.packages[name].package
415
+ tx.package = nameParts.join('::')
416
+ }
417
+
418
+ const types = tx.typeArguments
419
+ if (!types) continue
420
+ for (let i = 0; i < types.length; i++) {
421
+ if (!hasMvrName(types[i])) continue
422
+ if (!resolved.types[types[i]]) throw new Error(`No resolution found for type: ${types[i]}`)
423
+ types[i] = resolved.types[types[i]].type
424
+ }
425
+
426
+ tx.typeArguments = types
427
+ }
428
+ }
429
+
430
+ function getNamesFromTypeList(types) {
431
+ const names = /* @__PURE__ */ new Set()
432
+ for (const type of types) {
433
+ if (hasMvrName(type)) {
434
+ if (!isValidNamedType(type)) throw new Error(`Invalid type with names: ${type}`)
435
+ names.add(type)
436
+ }
437
+ }
438
+
439
+ return names
440
+ }
441
+
442
+ export { MvrClient, extractMvrTypes, findNamesInTransaction, hasMvrName, replaceNames }
443
+ // # sourceMappingURL=mvr.js.map