@0xobelisk/client 0.2.8 → 0.2.9

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.
@@ -1,152 +0,0 @@
1
-
2
- import { FieldsWithTypes, Type, parseTypeName } from './util'
3
-
4
- export type PrimitiveValue = string | number | boolean | bigint
5
-
6
- export class StructClassLoader {
7
- private map: Map<
8
- string,
9
- {
10
- numTypeParams: number
11
- fromFields: (fields: Record<string, any>, typeArgs?: Type[]) => any
12
- fromFieldsWithTypes: (item: FieldsWithTypes) => any
13
- }
14
- > = new Map()
15
-
16
- register(...classes: any) {
17
- for (const cls of classes) {
18
- if (
19
- '$typeName' in cls === false ||
20
- '$numTypeParams' in cls === false ||
21
- 'fromFields' in cls === false ||
22
- 'fromFieldsWithTypes' in cls === false
23
- ) {
24
- throw new Error(`class ${cls.name} is not a valid struct class`)
25
- }
26
-
27
- const typeName = cls.$typeName
28
- const numTypeParams = cls.$numTypeParams
29
-
30
- this.map.set(typeName, {
31
- numTypeParams,
32
- fromFields: (fields: Record<string, any>, typeArgs: Type[] = []) => {
33
- if (typeArgs.length !== numTypeParams) {
34
- throw new Error(`expected ${numTypeParams} type args, got ${typeArgs.length}`)
35
- }
36
- if (numTypeParams === 0) {
37
- return cls.fromFields(fields)
38
- } else if (numTypeParams === 1) {
39
- return cls.fromFields(typeArgs[0], fields)
40
- } else {
41
- return cls.fromFields(typeArgs, fields)
42
- }
43
- },
44
- fromFieldsWithTypes: (item: FieldsWithTypes) => {
45
- return cls.fromFieldsWithTypes(item)
46
- },
47
- })
48
- }
49
- }
50
-
51
- fromFields(type: Type, value: Record<string, any> | PrimitiveValue) {
52
- const ret = this.#handlePrimitiveValue(type, value, this.fromFields.bind(this))
53
- if (ret !== undefined) {
54
- return ret
55
- }
56
- value = value as FieldsWithTypes // type hint
57
-
58
- const { typeName, typeArgs } = parseTypeName(type)
59
-
60
- const loader = this.map.get(typeName)
61
- if (!loader) {
62
- throw new Error(`no loader registered for type ${typeName}, include relevant package in gen.toml`)
63
- }
64
-
65
- if (loader.numTypeParams !== typeArgs.length) {
66
- throw new Error(`expected ${loader.numTypeParams} type args, got ${typeArgs.length}`)
67
- }
68
-
69
- return loader.fromFields(value, typeArgs)
70
- }
71
-
72
- fromFieldsWithTypes(type: Type, value: FieldsWithTypes | PrimitiveValue) {
73
- const { typeName, typeArgs } = parseTypeName(type)
74
-
75
- // some types are special-cased in the RPC so we need to handle this manually
76
- // https://github.com/MystenLabs/sui/blob/416a980749ba8208917bae37a1ec1d43e50037b7/crates/sui-json-rpc-types/src/sui_move.rs#L482-L533
77
- switch (typeName) {
78
- case '0x1::string::String':
79
- case '0x1::ascii::String':
80
- case '0x2::url::Url':
81
- return value
82
- case '0x2::object::ID':
83
- return value
84
- case '0x2::object::UID':
85
- return (value as unknown as { id: string }).id
86
- case '0x2::balance::Balance': {
87
- const loader = this.map.get('0x2::balance::Balance')
88
- if (!loader) {
89
- throw new Error('no loader registered for type 0x2::balance::Balance')
90
- }
91
- return loader.fromFields({ value }, typeArgs)
92
- }
93
- case '0x1::option::Option': {
94
- if (value === null) {
95
- return null
96
- }
97
-
98
- const loader = this.map.get('0x1::option::Option')
99
- if (!loader) {
100
- throw new Error('no loader registered for type 0x1::option::Option')
101
- }
102
- return loader.fromFieldsWithTypes({
103
- type: type,
104
- fields: { vec: [value] },
105
- }).vec[0]
106
- }
107
- }
108
-
109
- const ret = this.#handlePrimitiveValue(type, value, this.fromFieldsWithTypes.bind(this))
110
- if (ret !== undefined) {
111
- return ret
112
- }
113
- value = value as FieldsWithTypes // type hint
114
-
115
- const loader = this.map.get(typeName)
116
- if (!loader) {
117
- throw new Error(`no loader registered for type ${typeName}, include relevant package in gen.toml`)
118
- }
119
-
120
- return loader.fromFieldsWithTypes(value)
121
- }
122
-
123
- #handlePrimitiveValue(type: Type, value: any, vecCb: (type: Type, value: any) => any) {
124
- const { typeName, typeArgs } = parseTypeName(type)
125
-
126
- switch (typeName) {
127
- case 'bool':
128
- return value as boolean
129
- case 'u8':
130
- case 'u16':
131
- case 'u32':
132
- return value as number
133
- case 'u64':
134
- case 'u128':
135
- case 'u256':
136
- return BigInt(value)
137
- case 'address':
138
- return value as string
139
- case 'signer':
140
- return value as string
141
- case 'vector':
142
- value = value as any[]
143
- return value.map((item: any) => vecCb(typeArgs[0], item))
144
- default:
145
- return undefined
146
- }
147
- }
148
- }
149
-
150
- export const structClassLoaderSource = new StructClassLoader()
151
- export const structClassLoaderOnchain = new StructClassLoader()
152
-
@@ -1,219 +0,0 @@
1
-
2
- import {
3
- is,
4
- ObjectCallArg,
5
- ObjectId,
6
- TransactionArgument,
7
- TransactionBlock,
8
- normalizeSuiAddress,
9
- bcs,
10
- } from '@mysten/sui.js'
11
- import { BCS } from '@mysten/bcs'
12
-
13
- /** A Move type, e.g., `address`, `bool`, `u64`, `vector<u64>`, `0x2::sui::SUI`... */
14
- export type Type = string
15
-
16
- export interface FieldsWithTypes {
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
18
- fields: Record<string, any>
19
- type: string
20
- }
21
-
22
- export type ObjectArg = ObjectId | ObjectCallArg | TransactionArgument
23
- export type PureArg =
24
- | bigint
25
- | string
26
- | number
27
- | boolean
28
- | null
29
- | TransactionArgument
30
- | Array<PureArg>
31
- export type GenericArg = ObjectArg | PureArg | Array<ObjectArg> | Array<PureArg> | Array<GenericArg>
32
-
33
- export function parseTypeName(name: Type): { typeName: string; typeArgs: Type[] } {
34
- const parsed = bcs.parseTypeName(name)
35
- return { typeName: parsed.name, typeArgs: parsed.params as string[] }
36
- }
37
-
38
- export function obj(txb: TransactionBlock, arg: ObjectArg) {
39
- return is(arg, TransactionArgument) ? arg : txb.object(arg)
40
- }
41
-
42
- export function pure(txb: TransactionBlock, arg: PureArg, type: Type) {
43
- if (is(arg, TransactionArgument)) {
44
- return obj(txb, arg)
45
- }
46
-
47
- function convertType(type: Type): string {
48
- const { typeName, typeArgs } = parseTypeName(type)
49
- switch (typeName) {
50
- case '0x1::string::String':
51
- case '0x1::ascii::String':
52
- return BCS.STRING
53
- case '0x2::object::ID':
54
- return BCS.ADDRESS
55
- case '0x1::option::Option':
56
- return `vector<${convertType(typeArgs[0])}>`
57
- case 'vector':
58
- return `vector<${convertType(typeArgs[0])}>`
59
- default:
60
- return type
61
- }
62
- }
63
-
64
- function isOrHasNestedTransactionArgument(arg: PureArg): boolean {
65
- if (Array.isArray(arg)) {
66
- return arg.some(item => isOrHasNestedTransactionArgument(item))
67
- }
68
- return is(arg, TransactionArgument)
69
- }
70
-
71
- function convertArg(arg: PureArg, type: Type): PureArg {
72
- const { typeName, typeArgs } = parseTypeName(type)
73
- if (typeName === '0x1::option::Option') {
74
- if (arg === null) {
75
- return []
76
- } else {
77
- return [convertArg(arg, typeArgs[0])]
78
- }
79
- } else if (typeName === 'vector' && Array.isArray(arg)) {
80
- return arg.map(item => convertArg(item, typeArgs[0]))
81
- } else if (typeName === '0x2::object::ID' || typeName === 'address') {
82
- return normalizeSuiAddress(arg as string)
83
- } else {
84
- return arg
85
- }
86
- }
87
-
88
- // handle some cases when TransactionArgument is nested within a vector or option
89
- const { typeName, typeArgs } = parseTypeName(type)
90
- switch (typeName) {
91
- case '0x1::option::Option':
92
- if (arg === null) {
93
- return txb.pure([], `vector<${convertType(typeArgs[0])}>`)
94
- }
95
- if (isOrHasNestedTransactionArgument(arg)) {
96
- throw new Error('nesting TransactionArgument is not currently supported')
97
- }
98
- break
99
- case 'vector':
100
- if (!Array.isArray(arg)) {
101
- throw new Error('expected an array for vector type')
102
- }
103
- if (arg.length === 0) {
104
- return txb.pure([], `vector<${convertType(typeArgs[0])}>`)
105
- }
106
- if (arg.some(arg => Array.isArray(arg) && isOrHasNestedTransactionArgument(arg))) {
107
- throw new Error('nesting TransactionArgument is not currently supported')
108
- }
109
- if (
110
- is(arg[0], TransactionArgument) &&
111
- arg.filter(arg => !is(arg, TransactionArgument)).length > 0
112
- ) {
113
- throw new Error('mixing TransactionArgument with other types is not currently supported')
114
- }
115
- if (is(arg[0], TransactionArgument)) {
116
- return txb.makeMoveVec({
117
- objects: arg as Array<TransactionArgument>,
118
- type: typeArgs[0],
119
- })
120
- }
121
- }
122
-
123
- return txb.pure(convertArg(arg, type), convertType(type))
124
- }
125
-
126
- export function option(txb: TransactionBlock, type: Type, arg: GenericArg | null) {
127
- if (arg === null) {
128
- return pure(txb, arg, `0x1::option::Option<${type}>`)
129
- }
130
-
131
- if (typeArgIsPure(type)) {
132
- return pure(txb, arg as PureArg | TransactionArgument, `0x1::option::Option<${type}>`)
133
- } else if (is(arg, TransactionArgument)) {
134
- return arg
135
- } else {
136
- if (arg === null) {
137
- return pure(txb, arg, `vector<${type}>`)
138
- }
139
-
140
- // wrap it with some
141
- const val = generic(txb, type, arg)
142
- return txb.moveCall({
143
- target: `0x1::option::some`,
144
- typeArguments: [type],
145
- arguments: [val],
146
- })
147
- }
148
- }
149
-
150
- export function generic(txb: TransactionBlock, type: Type, arg: GenericArg) {
151
- if (typeArgIsPure(type)) {
152
- return pure(txb, arg as PureArg | TransactionArgument, type)
153
- } else {
154
- const { typeName, typeArgs } = parseTypeName(type)
155
- if (typeName === 'vector' && Array.isArray(arg)) {
156
- const itemType = typeArgs[0]
157
-
158
- return txb.makeMoveVec({
159
- objects: arg.map(item => obj(txb, item as ObjectArg)),
160
- type: itemType,
161
- })
162
- } else {
163
- return obj(txb, arg as ObjectArg)
164
- }
165
- }
166
- }
167
-
168
- export function vector(
169
- txb: TransactionBlock,
170
- itemType: Type,
171
- items: Array<GenericArg> | TransactionArgument
172
- ) {
173
- if (typeArgIsPure(itemType)) {
174
- return pure(txb, items as PureArg, `vector<${itemType}>`)
175
- } else if (is(items, TransactionArgument)) {
176
- return items
177
- } else {
178
- const { typeName: itemTypeName, typeArgs: itemTypeArgs } = parseTypeName(itemType)
179
- if (itemTypeName === '0x1::option::Option') {
180
- const objects = items.map(item => option(txb, itemTypeArgs[0], item))
181
- return txb.makeMoveVec({
182
- objects,
183
- type: itemType,
184
- })
185
- }
186
-
187
- return txb.makeMoveVec({
188
- objects: items as Array<TransactionArgument>,
189
- type: itemType,
190
- })
191
- }
192
- }
193
-
194
- export function typeArgIsPure(type: Type): boolean {
195
- const { typeName, typeArgs } = parseTypeName(type)
196
- switch (typeName) {
197
- case 'bool':
198
- case 'u8':
199
- case 'u16':
200
- case 'u32':
201
- case 'u64':
202
- case 'u128':
203
- case 'u256':
204
- case 'address':
205
- case 'signer':
206
- return true
207
- case 'vector':
208
- return typeArgIsPure(typeArgs[0])
209
- case '0x1::string::String':
210
- case '0x1::ascii::String':
211
- case '0x2::object::ID':
212
- return true
213
- case '0x1::option::Option':
214
- return typeArgIsPure(typeArgs[0])
215
- default:
216
- return false
217
- }
218
- }
219
-