@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,49 @@
1
+ import { ClientCache } from '../../experimental/cache.js'
2
+ import { findNamesInTransaction, MvrClient, replaceNames } from '../../experimental/mvr.js'
3
+
4
+ const cacheMap = /* @__PURE__ */ new WeakMap()
5
+ const namedPackagesPlugin = (options) => {
6
+ let mvrClient
7
+ if (options) {
8
+ const overrides = options.overrides ?? {
9
+ packages: Object.create(null),
10
+ types: Object.create(null),
11
+ }
12
+ if (!cacheMap.has(overrides)) {
13
+ cacheMap.set(overrides, new ClientCache())
14
+ }
15
+
16
+ mvrClient = new MvrClient({
17
+ cache: cacheMap.get(overrides),
18
+ url: options.url,
19
+ pageSize: options.pageSize,
20
+ overrides,
21
+ })
22
+ }
23
+
24
+ return async (transactionData, buildOptions, next) => {
25
+ const names = findNamesInTransaction(transactionData)
26
+ if (names.types.length === 0 && names.packages.length === 0) {
27
+ return next()
28
+ }
29
+
30
+ const resolved = await (mvrClient || getClient(buildOptions).core.mvr).resolve({
31
+ types: names.types,
32
+ packages: names.packages,
33
+ })
34
+ replaceNames(transactionData, resolved)
35
+ await next()
36
+ }
37
+ }
38
+
39
+ function getClient(options) {
40
+ if (!options.client) {
41
+ throw new Error(
42
+ `No sui client passed to Transaction#build, but transaction data was not sufficient to build offline.`
43
+ )
44
+ }
45
+
46
+ return options.client
47
+ }
48
+
49
+ export { getClient, namedPackagesPlugin }
@@ -0,0 +1,40 @@
1
+ import { vendorLib } from '@exodus/sui-lib'
2
+
3
+ const { bcs } = vendorLib
4
+ const { pureBcsSchemaFromTypeName } = vendorLib.bcs
5
+
6
+ function createPure(makePure) {
7
+ function pure(typeOrSerializedValue, value) {
8
+ if (typeof typeOrSerializedValue === 'string') {
9
+ return makePure(pureBcsSchemaFromTypeName(typeOrSerializedValue).serialize(value))
10
+ }
11
+
12
+ if (typeOrSerializedValue instanceof Uint8Array || bcs.isSerializedBcs(typeOrSerializedValue)) {
13
+ return makePure(typeOrSerializedValue)
14
+ }
15
+
16
+ throw new Error('tx.pure must be called either a bcs type name, or a serialized bcs value')
17
+ }
18
+
19
+ pure.u8 = (value) => makePure(bcs.U8.serialize(value))
20
+ pure.u16 = (value) => makePure(bcs.U16.serialize(value))
21
+ pure.u32 = (value) => makePure(bcs.U32.serialize(value))
22
+ pure.u64 = (value) => makePure(bcs.U64.serialize(value))
23
+ pure.u128 = (value) => makePure(bcs.U128.serialize(value))
24
+ pure.u256 = (value) => makePure(bcs.U256.serialize(value))
25
+ pure.bool = (value) => makePure(bcs.Bool.serialize(value))
26
+ pure.string = (value) => makePure(bcs.String.serialize(value))
27
+ pure.address = (value) => makePure(bcs.Address.serialize(value))
28
+ pure.id = pure.address
29
+ pure.vector = (type, value) => {
30
+ return makePure(bcs.vector(pureBcsSchemaFromTypeName(type)).serialize(value))
31
+ }
32
+
33
+ pure.option = (type, value) => {
34
+ return makePure(bcs.option(pureBcsSchemaFromTypeName(type)).serialize(value))
35
+ }
36
+
37
+ return pure
38
+ }
39
+
40
+ export { createPure }
@@ -0,0 +1,95 @@
1
+ import { vendorLib } from '@exodus/sui-lib'
2
+
3
+ import { suiClientResolveTransactionPlugin } from '../experimental/transports/json-rpc-resolver.js'
4
+ import { Inputs } from './Inputs.js'
5
+
6
+ const { bcs } = vendorLib
7
+
8
+ function needsTransactionResolution(data, options) {
9
+ if (
10
+ data.inputs.some((input) => {
11
+ return input.UnresolvedObject || input.UnresolvedPure
12
+ })
13
+ ) {
14
+ return true
15
+ }
16
+
17
+ // eslint-disable-next-line sonarjs/prefer-single-boolean-return
18
+ if (
19
+ !options.onlyTransactionKind &&
20
+ (!data.gasConfig.price || !data.gasConfig.budget || !data.gasConfig.payment)
21
+ ) {
22
+ return true
23
+ }
24
+
25
+ return false
26
+ }
27
+
28
+ async function resolveTransactionPlugin(transactionData, options, next) {
29
+ normalizeRawArguments(transactionData)
30
+ if (!needsTransactionResolution(transactionData, options)) {
31
+ await validate(transactionData)
32
+ return next()
33
+ }
34
+
35
+ const client = getClient(options)
36
+ const plugin =
37
+ client.core?.resolveTransactionPlugin() ?? suiClientResolveTransactionPlugin(client)
38
+ return plugin(transactionData, options, async () => {
39
+ await validate(transactionData)
40
+ await next()
41
+ })
42
+ }
43
+
44
+ function validate(transactionData) {
45
+ transactionData.inputs.forEach((input, index) => {
46
+ if (input.$kind !== 'Object' && input.$kind !== 'Pure') {
47
+ throw new Error(
48
+ `Input at index ${index} has not been resolved. Expected a Pure or Object input, but found ${JSON.stringify(
49
+ input
50
+ )}`
51
+ )
52
+ }
53
+ })
54
+ }
55
+
56
+ function getClient(options) {
57
+ if (!options.client) {
58
+ throw new Error(
59
+ `No sui client passed to Transaction#build, but transaction data was not sufficient to build offline.`
60
+ )
61
+ }
62
+
63
+ return options.client
64
+ }
65
+
66
+ function normalizeRawArguments(transactionData) {
67
+ for (const command of transactionData.commands) {
68
+ switch (command.$kind) {
69
+ case 'SplitCoins':
70
+ command.SplitCoins.amounts.forEach((amount) => {
71
+ normalizeRawArgument(amount, bcs.U64, transactionData)
72
+ })
73
+ break
74
+ case 'TransferObjects':
75
+ normalizeRawArgument(command.TransferObjects.address, bcs.Address, transactionData)
76
+ break
77
+ }
78
+ }
79
+ }
80
+
81
+ function normalizeRawArgument(arg, schema, transactionData) {
82
+ if (arg.$kind !== 'Input') {
83
+ return
84
+ }
85
+
86
+ const input = transactionData.inputs[arg.Input]
87
+ if (input.$kind !== 'UnresolvedPure') {
88
+ return
89
+ }
90
+
91
+ // eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
92
+ transactionData.inputs[arg.Input] = Inputs.Pure(schema.serialize(input.UnresolvedPure.value))
93
+ }
94
+
95
+ export { getClient, needsTransactionResolution, resolveTransactionPlugin }
@@ -0,0 +1,203 @@
1
+ import { vendorLib } from '@exodus/sui-lib'
2
+
3
+ const { bcs } = vendorLib
4
+ const { MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS, normalizeSuiAddress } = vendorLib.utils
5
+
6
+ const OBJECT_MODULE_NAME = 'object'
7
+ const ID_STRUCT_NAME = 'ID'
8
+ const STD_ASCII_MODULE_NAME = 'ascii'
9
+ const STD_ASCII_STRUCT_NAME = 'String'
10
+ const STD_UTF8_MODULE_NAME = 'string'
11
+ const STD_UTF8_STRUCT_NAME = 'String'
12
+ const STD_OPTION_MODULE_NAME = 'option'
13
+ const STD_OPTION_STRUCT_NAME = 'Option'
14
+
15
+ function isTxContext(param) {
16
+ const struct =
17
+ typeof param.body === 'object' && 'datatype' in param.body ? param.body.datatype : null
18
+ return (
19
+ !!struct &&
20
+ normalizeSuiAddress(struct.package) === normalizeSuiAddress('0x2') &&
21
+ struct.module === 'tx_context' &&
22
+ struct.type === 'TxContext'
23
+ )
24
+ }
25
+
26
+ function getPureBcsSchema(typeSignature) {
27
+ if (typeof typeSignature === 'string') {
28
+ switch (typeSignature) {
29
+ case 'address':
30
+ return bcs.Address
31
+ case 'bool':
32
+ return bcs.Bool
33
+ case 'u8':
34
+ return bcs.U8
35
+ case 'u16':
36
+ return bcs.U16
37
+ case 'u32':
38
+ return bcs.U32
39
+ case 'u64':
40
+ return bcs.U64
41
+ case 'u128':
42
+ return bcs.U128
43
+ case 'u256':
44
+ return bcs.U256
45
+ default:
46
+ throw new Error(`Unknown type signature ${typeSignature}`)
47
+ }
48
+ }
49
+
50
+ if ('vector' in typeSignature) {
51
+ if (typeSignature.vector === 'u8') {
52
+ return bcs.vector(bcs.U8).transform({
53
+ input: (val) => (typeof val === 'string' ? new TextEncoder().encode(val) : val),
54
+ output: (val) => val,
55
+ })
56
+ }
57
+
58
+ const type = getPureBcsSchema(typeSignature.vector)
59
+ return type ? bcs.vector(type) : null
60
+ }
61
+
62
+ if ('datatype' in typeSignature) {
63
+ const pkg = normalizeSuiAddress(typeSignature.datatype.package)
64
+ if (pkg === normalizeSuiAddress(MOVE_STDLIB_ADDRESS)) {
65
+ if (
66
+ typeSignature.datatype.module === STD_ASCII_MODULE_NAME &&
67
+ typeSignature.datatype.type === STD_ASCII_STRUCT_NAME
68
+ ) {
69
+ return bcs.String
70
+ }
71
+
72
+ if (
73
+ typeSignature.datatype.module === STD_UTF8_MODULE_NAME &&
74
+ typeSignature.datatype.type === STD_UTF8_STRUCT_NAME
75
+ ) {
76
+ return bcs.String
77
+ }
78
+
79
+ if (
80
+ typeSignature.datatype.module === STD_OPTION_MODULE_NAME &&
81
+ typeSignature.datatype.type === STD_OPTION_STRUCT_NAME
82
+ ) {
83
+ const type = getPureBcsSchema(typeSignature.datatype.typeParameters[0])
84
+ return type ? bcs.vector(type) : null
85
+ }
86
+ }
87
+
88
+ if (
89
+ pkg === normalizeSuiAddress(SUI_FRAMEWORK_ADDRESS) &&
90
+ typeSignature.datatype.module === OBJECT_MODULE_NAME &&
91
+ typeSignature.datatype.type === ID_STRUCT_NAME
92
+ ) {
93
+ return bcs.Address
94
+ }
95
+ }
96
+
97
+ return null
98
+ }
99
+
100
+ function normalizedTypeToMoveTypeSignature(type) {
101
+ if (typeof type === 'object' && 'Reference' in type) {
102
+ return {
103
+ ref: '&',
104
+ body: normalizedTypeToMoveTypeSignatureBody(type.Reference),
105
+ }
106
+ }
107
+
108
+ if (typeof type === 'object' && 'MutableReference' in type) {
109
+ return {
110
+ ref: '&mut',
111
+ body: normalizedTypeToMoveTypeSignatureBody(type.MutableReference),
112
+ }
113
+ }
114
+
115
+ return {
116
+ ref: null,
117
+ body: normalizedTypeToMoveTypeSignatureBody(type),
118
+ }
119
+ }
120
+
121
+ function normalizedTypeToMoveTypeSignatureBody(type) {
122
+ if (typeof type === 'string') {
123
+ switch (type) {
124
+ case 'Address':
125
+ return 'address'
126
+ case 'Bool':
127
+ return 'bool'
128
+ case 'U8':
129
+ return 'u8'
130
+ case 'U16':
131
+ return 'u16'
132
+ case 'U32':
133
+ return 'u32'
134
+ case 'U64':
135
+ return 'u64'
136
+ case 'U128':
137
+ return 'u128'
138
+ case 'U256':
139
+ return 'u256'
140
+ default:
141
+ throw new Error(`Unexpected type ${type}`)
142
+ }
143
+ }
144
+
145
+ if ('Vector' in type) {
146
+ return { vector: normalizedTypeToMoveTypeSignatureBody(type.Vector) }
147
+ }
148
+
149
+ if ('Struct' in type) {
150
+ return {
151
+ datatype: {
152
+ package: type.Struct.address,
153
+ module: type.Struct.module,
154
+ type: type.Struct.name,
155
+ typeParameters: type.Struct.typeArguments.map(normalizedTypeToMoveTypeSignatureBody),
156
+ },
157
+ }
158
+ }
159
+
160
+ if ('TypeParameter' in type) {
161
+ return { typeParameter: type.TypeParameter }
162
+ }
163
+
164
+ throw new Error(`Unexpected type ${JSON.stringify(type)}`)
165
+ }
166
+
167
+ function pureBcsSchemaFromOpenMoveTypeSignatureBody(typeSignature) {
168
+ if (typeof typeSignature === 'string') {
169
+ switch (typeSignature) {
170
+ case 'address':
171
+ return bcs.Address
172
+ case 'bool':
173
+ return bcs.Bool
174
+ case 'u8':
175
+ return bcs.U8
176
+ case 'u16':
177
+ return bcs.U16
178
+ case 'u32':
179
+ return bcs.U32
180
+ case 'u64':
181
+ return bcs.U64
182
+ case 'u128':
183
+ return bcs.U128
184
+ case 'u256':
185
+ return bcs.U256
186
+ default:
187
+ throw new Error(`Unknown type signature ${typeSignature}`)
188
+ }
189
+ }
190
+
191
+ if ('vector' in typeSignature) {
192
+ return bcs.vector(pureBcsSchemaFromOpenMoveTypeSignatureBody(typeSignature.vector))
193
+ }
194
+
195
+ throw new Error(`Expected pure typeSignature, but got ${JSON.stringify(typeSignature)}`)
196
+ }
197
+
198
+ export {
199
+ getPureBcsSchema,
200
+ isTxContext,
201
+ normalizedTypeToMoveTypeSignature,
202
+ pureBcsSchemaFromOpenMoveTypeSignatureBody,
203
+ }
@@ -0,0 +1,67 @@
1
+ import { vendorLib } from '@exodus/sui-lib'
2
+
3
+ import { is } from '../valibot.js'
4
+ import { Argument } from './data/internal.js'
5
+
6
+ const { normalizeSuiAddress } = vendorLib.utils
7
+
8
+ function extractMutableReference(normalizedType) {
9
+ return typeof normalizedType === 'object' && 'MutableReference' in normalizedType
10
+ ? normalizedType.MutableReference
11
+ : void 0
12
+ }
13
+
14
+ function extractReference(normalizedType) {
15
+ return typeof normalizedType === 'object' && 'Reference' in normalizedType
16
+ ? normalizedType.Reference
17
+ : void 0
18
+ }
19
+
20
+ function extractStructTag(normalizedType) {
21
+ if (typeof normalizedType === 'object' && 'Struct' in normalizedType) {
22
+ return normalizedType
23
+ }
24
+
25
+ const ref = extractReference(normalizedType)
26
+ const mutRef = extractMutableReference(normalizedType)
27
+ if (typeof ref === 'object' && 'Struct' in ref) {
28
+ return ref
29
+ }
30
+
31
+ if (typeof mutRef === 'object' && 'Struct' in mutRef) {
32
+ return mutRef
33
+ }
34
+
35
+ return void 0
36
+ }
37
+
38
+ function getIdFromCallArg(arg) {
39
+ if (typeof arg === 'string') {
40
+ return normalizeSuiAddress(arg)
41
+ }
42
+
43
+ if (arg.Object) {
44
+ if (arg.Object.ImmOrOwnedObject) {
45
+ return normalizeSuiAddress(arg.Object.ImmOrOwnedObject.objectId)
46
+ }
47
+
48
+ if (arg.Object.Receiving) {
49
+ return normalizeSuiAddress(arg.Object.Receiving.objectId)
50
+ }
51
+
52
+ return normalizeSuiAddress(arg.Object.SharedObject.objectId)
53
+ }
54
+
55
+ if (arg.UnresolvedObject) {
56
+ return normalizeSuiAddress(arg.UnresolvedObject.objectId)
57
+ }
58
+
59
+ return void 0
60
+ }
61
+
62
+ function isArgument(value) {
63
+ return is(Argument, value)
64
+ }
65
+
66
+ export { extractMutableReference, extractReference, extractStructTag, getIdFromCallArg, isArgument }
67
+ // # sourceMappingURL=utils.js.map