@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,108 @@
1
+ /* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
2
+ import { vendorLib } from '@exodus/sui-lib'
3
+
4
+ import { parse } from '../valibot.js'
5
+ import { Argument } from './data/internal.js'
6
+
7
+ const { toBase64, normalizeSuiObjectId } = vendorLib.utils
8
+
9
+ var UpgradePolicy = /* @__PURE__ */ ((UpgradePolicy2) => {
10
+ UpgradePolicy2[(UpgradePolicy2['COMPATIBLE'] = 0)] = 'COMPATIBLE'
11
+ UpgradePolicy2[(UpgradePolicy2['ADDITIVE'] = 128)] = 'ADDITIVE'
12
+ UpgradePolicy2[(UpgradePolicy2['DEP_ONLY'] = 192)] = 'DEP_ONLY'
13
+ return UpgradePolicy2
14
+ })(Object.create(null))
15
+ const Commands = {
16
+ MoveCall(input) {
17
+ const [pkg, mod = '', fn = ''] =
18
+ 'target' in input ? input.target.split('::') : [input.package, input.module, input.function]
19
+ return {
20
+ $kind: 'MoveCall',
21
+ MoveCall: {
22
+ package: pkg,
23
+ module: mod,
24
+ function: fn,
25
+ typeArguments: input.typeArguments ?? [],
26
+ arguments: input.arguments ?? [],
27
+ },
28
+ }
29
+ },
30
+ TransferObjects(objects, address) {
31
+ return {
32
+ $kind: 'TransferObjects',
33
+ TransferObjects: {
34
+ objects: objects.map((o) => parse(Argument, o)),
35
+ address: parse(Argument, address),
36
+ },
37
+ }
38
+ },
39
+ SplitCoins(coin, amounts) {
40
+ return {
41
+ $kind: 'SplitCoins',
42
+ SplitCoins: {
43
+ coin: parse(Argument, coin),
44
+ amounts: amounts.map((o) => parse(Argument, o)),
45
+ },
46
+ }
47
+ },
48
+ MergeCoins(destination, sources) {
49
+ return {
50
+ $kind: 'MergeCoins',
51
+ MergeCoins: {
52
+ destination: parse(Argument, destination),
53
+ sources: sources.map((o) => parse(Argument, o)),
54
+ },
55
+ }
56
+ },
57
+ Publish({ modules, dependencies }) {
58
+ return {
59
+ $kind: 'Publish',
60
+ Publish: {
61
+ modules: modules.map((module) =>
62
+ typeof module === 'string' ? module : toBase64(new Uint8Array(module))
63
+ ),
64
+ dependencies: dependencies.map((dep) => normalizeSuiObjectId(dep)),
65
+ },
66
+ }
67
+ },
68
+ Upgrade({ modules, dependencies, package: packageId, ticket }) {
69
+ return {
70
+ $kind: 'Upgrade',
71
+ Upgrade: {
72
+ modules: modules.map((module) =>
73
+ typeof module === 'string' ? module : toBase64(new Uint8Array(module))
74
+ ),
75
+ dependencies: dependencies.map((dep) => normalizeSuiObjectId(dep)),
76
+ package: packageId,
77
+ ticket: parse(Argument, ticket),
78
+ },
79
+ }
80
+ },
81
+ MakeMoveVec({ type, elements }) {
82
+ return {
83
+ $kind: 'MakeMoveVec',
84
+ MakeMoveVec: {
85
+ type: type ?? null,
86
+ elements: elements.map((o) => parse(Argument, o)),
87
+ },
88
+ }
89
+ },
90
+ Intent({ name, inputs = Object.create(null), data = Object.create(null) }) {
91
+ return {
92
+ $kind: '$Intent',
93
+ $Intent: {
94
+ name,
95
+ inputs: Object.fromEntries(
96
+ Object.entries(inputs).map(([key, value]) => [
97
+ key,
98
+ Array.isArray(value) ? value.map((o) => parse(Argument, o)) : parse(Argument, value),
99
+ ])
100
+ ),
101
+ data,
102
+ },
103
+ }
104
+ },
105
+ }
106
+
107
+ export { Commands, UpgradePolicy }
108
+ // # sourceMappingURL=Commands.js.map
@@ -0,0 +1,59 @@
1
+ import { vendorLib } from '@exodus/sui-lib'
2
+
3
+ const { toBase64 } = vendorLib.utils
4
+ const { normalizeSuiAddress } = vendorLib.utils
5
+
6
+ function Pure(data) {
7
+ return {
8
+ $kind: 'Pure',
9
+ Pure: {
10
+ bytes: data instanceof Uint8Array ? toBase64(data) : data.toBase64(),
11
+ },
12
+ }
13
+ }
14
+
15
+ const Inputs = {
16
+ Pure,
17
+ ObjectRef({ objectId, digest, version }) {
18
+ return {
19
+ $kind: 'Object',
20
+ Object: {
21
+ $kind: 'ImmOrOwnedObject',
22
+ ImmOrOwnedObject: {
23
+ digest,
24
+ version,
25
+ objectId: normalizeSuiAddress(objectId),
26
+ },
27
+ },
28
+ }
29
+ },
30
+ SharedObjectRef({ objectId, mutable, initialSharedVersion }) {
31
+ return {
32
+ $kind: 'Object',
33
+ Object: {
34
+ $kind: 'SharedObject',
35
+ SharedObject: {
36
+ mutable,
37
+ initialSharedVersion,
38
+ objectId: normalizeSuiAddress(objectId),
39
+ },
40
+ },
41
+ }
42
+ },
43
+ ReceivingRef({ objectId, digest, version }) {
44
+ return {
45
+ $kind: 'Object',
46
+ Object: {
47
+ $kind: 'Receiving',
48
+ Receiving: {
49
+ digest,
50
+ version,
51
+ objectId: normalizeSuiAddress(objectId),
52
+ },
53
+ },
54
+ }
55
+ },
56
+ }
57
+
58
+ export { Inputs }
59
+ // # sourceMappingURL=Inputs.js.map
@@ -0,0 +1,272 @@
1
+ /* eslint-disable no-sequences */
2
+ import { vendorLib } from '@exodus/sui-lib'
3
+
4
+ const { normalizeSuiAddress } = vendorLib.utils
5
+
6
+ var __typeError = (msg) => {
7
+ throw new TypeError(msg)
8
+ }
9
+
10
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError('Cannot ' + msg)
11
+ var __privateGet = (obj, member, getter) => (
12
+ __accessCheck(obj, member, 'read from private field'), getter ? getter.call(obj) : member.get(obj)
13
+ )
14
+ var __privateAdd = (obj, member, value) =>
15
+ member.has(obj)
16
+ ? __typeError('Cannot add the same private member more than once')
17
+ : member instanceof WeakSet
18
+ ? member.add(obj)
19
+ : member.set(obj, value)
20
+ var __privateSet = (obj, member, value, setter) => (
21
+ __accessCheck(obj, member, 'write to private field'),
22
+ setter ? setter.call(obj, value) : member.set(obj, value),
23
+ value
24
+ )
25
+ var _caches, _cache, _onEffects
26
+
27
+ class AsyncCache {
28
+ async getObject(id) {
29
+ const [owned, shared] = await Promise.all([
30
+ this.get('OwnedObject', id),
31
+ this.get('SharedOrImmutableObject', id),
32
+ ])
33
+ return owned ?? shared ?? null
34
+ }
35
+
36
+ async getObjects(ids) {
37
+ return Promise.all(ids.map((id) => this.getObject(id)))
38
+ }
39
+
40
+ async addObject(object) {
41
+ if (object.owner) {
42
+ await this.set('OwnedObject', object.objectId, object)
43
+ } else {
44
+ await this.set('SharedOrImmutableObject', object.objectId, object)
45
+ }
46
+
47
+ return object
48
+ }
49
+
50
+ async addObjects(objects) {
51
+ await Promise.all(objects.map(async (object) => this.addObject(object)))
52
+ }
53
+
54
+ async deleteObject(id) {
55
+ await Promise.all([this.delete('OwnedObject', id), this.delete('SharedOrImmutableObject', id)])
56
+ }
57
+
58
+ async deleteObjects(ids) {
59
+ await Promise.all(ids.map((id) => this.deleteObject(id)))
60
+ }
61
+
62
+ async getMoveFunctionDefinition(ref) {
63
+ const functionName = `${normalizeSuiAddress(ref.package)}::${ref.module}::${ref.function}`
64
+ return this.get('MoveFunction', functionName)
65
+ }
66
+
67
+ async addMoveFunctionDefinition(functionEntry) {
68
+ const pkg = normalizeSuiAddress(functionEntry.package)
69
+ const functionName = `${pkg}::${functionEntry.module}::${functionEntry.function}`
70
+ const entry = {
71
+ ...functionEntry,
72
+ package: pkg,
73
+ }
74
+ await this.set('MoveFunction', functionName, entry)
75
+ return entry
76
+ }
77
+
78
+ async deleteMoveFunctionDefinition(ref) {
79
+ const functionName = `${normalizeSuiAddress(ref.package)}::${ref.module}::${ref.function}`
80
+ await this.delete('MoveFunction', functionName)
81
+ }
82
+
83
+ async getCustom(key) {
84
+ return this.get('Custom', key)
85
+ }
86
+
87
+ async setCustom(key, value) {
88
+ return this.set('Custom', key, value)
89
+ }
90
+
91
+ async deleteCustom(key) {
92
+ return this.delete('Custom', key)
93
+ }
94
+ }
95
+
96
+ class InMemoryCache extends AsyncCache {
97
+ constructor() {
98
+ super(...arguments)
99
+ __privateAdd(this, _caches, {
100
+ OwnedObject: /* @__PURE__ */ new Map(),
101
+ SharedOrImmutableObject: /* @__PURE__ */ new Map(),
102
+ MoveFunction: /* @__PURE__ */ new Map(),
103
+ Custom: /* @__PURE__ */ new Map(),
104
+ })
105
+ }
106
+
107
+ async get(type, key) {
108
+ return __privateGet(this, _caches)[type].get(key) ?? null
109
+ }
110
+
111
+ async set(type, key, value) {
112
+ __privateGet(this, _caches)[type].set(key, value)
113
+ }
114
+
115
+ async delete(type, key) {
116
+ __privateGet(this, _caches)[type].delete(key)
117
+ }
118
+
119
+ async clear(type) {
120
+ if (type) {
121
+ __privateGet(this, _caches)[type].clear()
122
+ } else {
123
+ for (const cache of Object.values(__privateGet(this, _caches))) {
124
+ cache.clear()
125
+ }
126
+ }
127
+ }
128
+ }
129
+ _caches = new WeakMap()
130
+
131
+ class ObjectCache {
132
+ constructor({ cache = new InMemoryCache(), onEffects }) {
133
+ __privateAdd(this, _cache)
134
+ __privateAdd(this, _onEffects)
135
+ __privateSet(this, _cache, cache)
136
+ __privateSet(this, _onEffects, onEffects)
137
+ }
138
+
139
+ asPlugin() {
140
+ return async (transactionData, _options, next) => {
141
+ const unresolvedObjects = transactionData.inputs
142
+ .filter((input) => input.UnresolvedObject)
143
+ .map((input) => input.UnresolvedObject.objectId)
144
+ // eslint-disable-next-line unicorn/no-await-expression-member
145
+ const cached = (await __privateGet(this, _cache).getObjects(unresolvedObjects)).filter(
146
+ (obj) => obj !== null
147
+ )
148
+ const byId = new Map(cached.map((obj) => [obj.objectId, obj]))
149
+ for (const input of transactionData.inputs) {
150
+ if (!input.UnresolvedObject) {
151
+ continue
152
+ }
153
+
154
+ const cached2 = byId.get(input.UnresolvedObject.objectId)
155
+ if (!cached2) {
156
+ continue
157
+ }
158
+
159
+ if (cached2.initialSharedVersion && !input.UnresolvedObject.initialSharedVersion) {
160
+ input.UnresolvedObject.initialSharedVersion = cached2.initialSharedVersion
161
+ } else {
162
+ if (cached2.version && !input.UnresolvedObject.version) {
163
+ input.UnresolvedObject.version = cached2.version
164
+ }
165
+
166
+ if (cached2.digest && !input.UnresolvedObject.digest) {
167
+ input.UnresolvedObject.digest = cached2.digest
168
+ }
169
+ }
170
+ }
171
+
172
+ await Promise.all(
173
+ transactionData.commands.map(async (commands) => {
174
+ if (commands.MoveCall) {
175
+ const def = await this.getMoveFunctionDefinition({
176
+ package: commands.MoveCall.package,
177
+ module: commands.MoveCall.module,
178
+ function: commands.MoveCall.function,
179
+ })
180
+ if (def) {
181
+ // eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
182
+ commands.MoveCall._argumentTypes = def.parameters
183
+ }
184
+ }
185
+ })
186
+ )
187
+ await next()
188
+ await Promise.all(
189
+ transactionData.commands.map(async (commands) => {
190
+ if (commands.MoveCall?._argumentTypes) {
191
+ await __privateGet(this, _cache).addMoveFunctionDefinition({
192
+ package: commands.MoveCall.package,
193
+ module: commands.MoveCall.module,
194
+ function: commands.MoveCall.function,
195
+ parameters: commands.MoveCall._argumentTypes,
196
+ })
197
+ }
198
+ })
199
+ )
200
+ }
201
+ }
202
+
203
+ async clear() {
204
+ await __privateGet(this, _cache).clear()
205
+ }
206
+
207
+ async getMoveFunctionDefinition(ref) {
208
+ return __privateGet(this, _cache).getMoveFunctionDefinition(ref)
209
+ }
210
+
211
+ async getObjects(ids) {
212
+ return __privateGet(this, _cache).getObjects(ids)
213
+ }
214
+
215
+ async deleteObjects(ids) {
216
+ return __privateGet(this, _cache).deleteObjects(ids)
217
+ }
218
+
219
+ async clearOwnedObjects() {
220
+ await __privateGet(this, _cache).clear('OwnedObject')
221
+ }
222
+
223
+ async clearCustom() {
224
+ await __privateGet(this, _cache).clear('Custom')
225
+ }
226
+
227
+ async getCustom(key) {
228
+ return __privateGet(this, _cache).getCustom(key)
229
+ }
230
+
231
+ async setCustom(key, value) {
232
+ return __privateGet(this, _cache).setCustom(key, value)
233
+ }
234
+
235
+ async deleteCustom(key) {
236
+ return __privateGet(this, _cache).deleteCustom(key)
237
+ }
238
+
239
+ async applyEffects(effects) {
240
+ var _a
241
+ if (!effects.V2) {
242
+ throw new Error(`Unsupported transaction effects version ${effects.$kind}`)
243
+ }
244
+
245
+ const { lamportVersion, changedObjects } = effects.V2
246
+ const deletedIds = []
247
+ const addedObjects = []
248
+ changedObjects.forEach(([id, change]) => {
249
+ if (change.outputState.NotExist) {
250
+ deletedIds.push(id)
251
+ } else if (change.outputState.ObjectWrite) {
252
+ const [digest, owner] = change.outputState.ObjectWrite
253
+ addedObjects.push({
254
+ objectId: id,
255
+ digest,
256
+ version: lamportVersion,
257
+ owner: owner.AddressOwner ?? owner.ObjectOwner ?? null,
258
+ initialSharedVersion: owner.Shared?.initialSharedVersion ?? null,
259
+ })
260
+ }
261
+ })
262
+ await Promise.all([
263
+ __privateGet(this, _cache).addObjects(addedObjects),
264
+ __privateGet(this, _cache).deleteObjects(deletedIds),
265
+ (_a = __privateGet(this, _onEffects)) == null ? void 0 : _a.call(this, effects),
266
+ ])
267
+ }
268
+ }
269
+ _cache = new WeakMap()
270
+ _onEffects = new WeakMap()
271
+ export { AsyncCache, InMemoryCache, ObjectCache }
272
+ // # sourceMappingURL=ObjectCache.js.map