@0xobelisk/client 0.4.7 → 1.2.0-pre.100

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 (64) hide show
  1. package/README.md +550 -2
  2. package/dist/browser/obelisk-client.js +102 -0
  3. package/dist/browser/obelisk-client.js.LICENSE.txt +16 -0
  4. package/dist/browser/obelisk-client.js.map +1 -0
  5. package/dist/browser/obelisk-client.min.js +101 -0
  6. package/dist/browser/obelisk-client.min.js.map +1 -0
  7. package/dist/index.d.ts +27 -9
  8. package/dist/sui/client.d.ts +59 -0
  9. package/dist/sui/index.d.mts +156 -0
  10. package/dist/sui/index.d.ts +19 -0
  11. package/dist/sui/index.js +93 -0
  12. package/dist/sui/index.js.map +1 -0
  13. package/dist/sui/index.mjs +88 -0
  14. package/dist/sui/index.mjs.map +1 -0
  15. package/dist/sui/types.d.ts +90 -0
  16. package/package.json +60 -121
  17. package/src/index.ts +31 -9
  18. package/src/sui/client.test.ts +277 -0
  19. package/src/sui/client.ts +157 -0
  20. package/src/sui/index.ts +30 -0
  21. package/src/sui/types.ts +94 -0
  22. package/LICENSE +0 -92
  23. package/dist/index.js +0 -1287
  24. package/dist/index.js.map +0 -1
  25. package/dist/index.mjs +0 -1268
  26. package/dist/index.mjs.map +0 -1
  27. package/dist/libs/suiAccountManager/crypto.d.ts +0 -1
  28. package/dist/libs/suiAccountManager/index.d.ts +0 -35
  29. package/dist/libs/suiAccountManager/keypair.d.ts +0 -21
  30. package/dist/libs/suiAccountManager/util.d.ts +0 -29
  31. package/dist/libs/suiContractFactory/index.d.ts +0 -20
  32. package/dist/libs/suiContractFactory/types.d.ts +0 -49
  33. package/dist/libs/suiInteractor/defaultConfig.d.ts +0 -10
  34. package/dist/libs/suiInteractor/index.d.ts +0 -2
  35. package/dist/libs/suiInteractor/suiInteractor.d.ts +0 -205
  36. package/dist/libs/suiInteractor/util.d.ts +0 -1
  37. package/dist/libs/suiModel/index.d.ts +0 -2
  38. package/dist/libs/suiModel/suiOwnedObject.d.ts +0 -24
  39. package/dist/libs/suiModel/suiSharedObject.d.ts +0 -12
  40. package/dist/libs/suiTxBuilder/index.d.ts +0 -544
  41. package/dist/libs/suiTxBuilder/util.d.ts +0 -76
  42. package/dist/metadata/index.d.ts +0 -34
  43. package/dist/obelisk.d.ts +0 -2566
  44. package/dist/types/index.d.ts +0 -141
  45. package/dist/utils/index.d.ts +0 -3
  46. package/src/libs/suiAccountManager/crypto.ts +0 -7
  47. package/src/libs/suiAccountManager/index.ts +0 -72
  48. package/src/libs/suiAccountManager/keypair.ts +0 -38
  49. package/src/libs/suiAccountManager/util.ts +0 -70
  50. package/src/libs/suiContractFactory/index.ts +0 -120
  51. package/src/libs/suiContractFactory/types.ts +0 -54
  52. package/src/libs/suiInteractor/defaultConfig.ts +0 -32
  53. package/src/libs/suiInteractor/index.ts +0 -2
  54. package/src/libs/suiInteractor/suiInteractor.ts +0 -319
  55. package/src/libs/suiInteractor/util.ts +0 -2
  56. package/src/libs/suiModel/index.ts +0 -2
  57. package/src/libs/suiModel/suiOwnedObject.ts +0 -62
  58. package/src/libs/suiModel/suiSharedObject.ts +0 -33
  59. package/src/libs/suiTxBuilder/index.ts +0 -245
  60. package/src/libs/suiTxBuilder/util.ts +0 -84
  61. package/src/metadata/index.ts +0 -22
  62. package/src/obelisk.ts +0 -600
  63. package/src/types/index.ts +0 -205
  64. package/src/utils/index.ts +0 -23
@@ -1,84 +0,0 @@
1
- import {
2
- normalizeSuiObjectId,
3
- TransactionArgument,
4
- TransactionBlock,
5
- } from '@mysten/sui.js';
6
- import { SuiTxArg, SuiInputTypes } from '../../types';
7
-
8
- export const getDefaultSuiInputType = (value: any): SuiInputTypes => {
9
- if (typeof value === 'string' && value.startsWith('0x')) {
10
- return 'object';
11
- } else if (typeof value === 'number' || typeof value === 'bigint') {
12
- return 'u64';
13
- } else if (typeof value === 'boolean') {
14
- return 'bool';
15
- } else {
16
- return 'object';
17
- }
18
- };
19
-
20
- /**
21
- * Since we know the elements in the array are the same type
22
- * If type is not provided, we will try to infer the type from the first element
23
- * By default,
24
- *
25
- * string starting with `0x` =====> object id
26
- * number, bigint ====> u64
27
- * boolean =====> bool
28
- *
29
- *
30
- * If type is provided, we will use the type to convert the array
31
- * @param args
32
- * @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'object'
33
- */
34
- export function makeVecParam(
35
- txBlock: TransactionBlock,
36
- args: SuiTxArg[],
37
- type?: SuiInputTypes
38
- ) {
39
- if (args.length === 0)
40
- throw new Error('Transaction builder error: Empty array is not allowed');
41
- const defaultSuiType = getDefaultSuiInputType(args[0]);
42
- if (type === 'object' || (!type && defaultSuiType === 'object')) {
43
- const objects = args.map((arg) =>
44
- typeof arg === 'string'
45
- ? txBlock.object(normalizeSuiObjectId(arg))
46
- : (arg as any)
47
- );
48
- return txBlock.makeMoveVec({ objects });
49
- } else {
50
- const vecType = type || defaultSuiType;
51
- return txBlock.pure(args, `vector<${vecType}>`);
52
- }
53
- }
54
-
55
- export function isMoveVecArg(arg: any) {
56
- const isFullMoveVecArg =
57
- arg && arg.value && Array.isArray(arg.value) && arg.vecType;
58
- const isSimpleMoveVecArg = Array.isArray(arg);
59
- return isFullMoveVecArg || isSimpleMoveVecArg;
60
- }
61
-
62
- export function convertArgs(
63
- txBlock: TransactionBlock,
64
- args: any[]
65
- ): TransactionArgument[] {
66
- return args.map((arg) => {
67
- if (typeof arg === 'string' && arg.startsWith('0x')) {
68
- // We always treat string starting with `0x` as object id
69
- return txBlock.object(normalizeSuiObjectId(arg));
70
- } else if (isMoveVecArg(arg)) {
71
- // if it's an array arg, we will convert it to move vec
72
- const vecType = arg.vecType || undefined;
73
- return vecType
74
- ? makeVecParam(txBlock, arg.value, vecType)
75
- : makeVecParam(txBlock, arg);
76
- } else if (typeof arg !== 'object') {
77
- // Other basic types such as string, number, boolean are converted to pure value
78
- return txBlock.pure(arg);
79
- } else {
80
- // We do nothing, because it's most likely already a move value
81
- return arg;
82
- }
83
- });
84
- }
@@ -1,22 +0,0 @@
1
- import { SuiMoveNormalizedModules } from '@mysten/sui.js';
2
- import { SuiInteractor, getDefaultConnection } from '../libs/suiInteractor';
3
-
4
- import { NetworkType } from '../types';
5
-
6
- export async function loadMetadata(
7
- networkType: NetworkType,
8
- packageId: string
9
- ) {
10
- // Init the rpc provider
11
- const fullnodeUrls = [getDefaultConnection(networkType).fullnode];
12
- const suiInteractor = new SuiInteractor(fullnodeUrls);
13
- if (packageId !== undefined) {
14
- const jsonData = await suiInteractor.getNormalizedMoveModulesByPackage(
15
- packageId
16
- );
17
-
18
- return jsonData as SuiMoveNormalizedModules;
19
- } else {
20
- console.error('please set your package id.');
21
- }
22
- }
package/src/obelisk.ts DELETED
@@ -1,600 +0,0 @@
1
- import {
2
- DevInspectResults,
3
- RawSigner,
4
- SuiAddress,
5
- SuiMoveNormalizedModules,
6
- SuiTransactionBlockResponse,
7
- TransactionBlock,
8
- } from '@mysten/sui.js';
9
- import { SuiAccountManager } from './libs/suiAccountManager';
10
- import { SuiTxBlock } from './libs/suiTxBuilder';
11
- import { getDefaultConnection, SuiInteractor } from './libs/suiInteractor';
12
-
13
- import { ObeliskObjectData } from './types';
14
- import { SuiContractFactory } from './libs/suiContractFactory';
15
- import {
16
- SuiMoveMoudleFuncType,
17
- SuiMoveMoudleValueType,
18
- } from './libs/suiContractFactory/types';
19
- import {
20
- ContractQuery,
21
- ContractTx,
22
- DerivePathParams,
23
- FaucetNetworkType,
24
- MapMoudleFuncQuery,
25
- MapMoudleFuncTx,
26
- ObeliskParams,
27
- SuiTxArg,
28
- SuiTxArgument,
29
- SuiVecTxArg,
30
- } from './types';
31
- import { normalizeHexAddress, numberToAddressHex } from './utils';
32
- import keccak256 from 'keccak256';
33
- import { BCS, getSuiMoveConfig } from '@mysten/bcs';
34
-
35
- export function isUndefined(value?: unknown): value is undefined {
36
- return value === undefined;
37
- }
38
-
39
- export function withMeta<T extends { meta: SuiMoveMoudleFuncType }>(
40
- meta: SuiMoveMoudleFuncType,
41
- creator: Omit<T, 'meta'>
42
- ): T {
43
- (creator as T).meta = meta;
44
-
45
- return creator as T;
46
- }
47
-
48
- function createQuery(
49
- meta: SuiMoveMoudleFuncType,
50
- fn: (
51
- tx: TransactionBlock,
52
- params: SuiTxArgument[],
53
- isRaw?: boolean
54
- ) => Promise<DevInspectResults | TransactionBlock>
55
- ): ContractQuery {
56
- return withMeta(
57
- meta,
58
- async (
59
- tx: TransactionBlock,
60
- params: SuiTxArgument[],
61
- isRaw?: boolean
62
- ): Promise<DevInspectResults | TransactionBlock> => {
63
- const result = await fn(tx, params, isRaw);
64
- return result;
65
- }
66
- );
67
- }
68
-
69
- function createTx(
70
- meta: SuiMoveMoudleFuncType,
71
- fn: (
72
- tx: TransactionBlock,
73
- params: SuiTxArgument[],
74
- isRaw?: boolean
75
- ) => Promise<SuiTransactionBlockResponse | TransactionBlock>
76
- ): ContractTx {
77
- return withMeta(
78
- meta,
79
- async (
80
- tx: TransactionBlock,
81
- params: SuiTxArgument[],
82
- isRaw?: boolean
83
- ): Promise<SuiTransactionBlockResponse | TransactionBlock> => {
84
- const result = await fn(tx, params, isRaw);
85
- return result;
86
- }
87
- );
88
- }
89
-
90
- /**
91
- * @class Obelisk
92
- * @description This class is used to aggregate the tools that used to interact with SUI network.
93
- */
94
- export class Obelisk {
95
- public accountManager: SuiAccountManager;
96
- public suiInteractor: SuiInteractor;
97
- public contractFactory: SuiContractFactory;
98
- public packageId: string | undefined;
99
- public metadata: SuiMoveNormalizedModules | undefined;
100
-
101
- readonly #query: MapMoudleFuncQuery = {};
102
- readonly #tx: MapMoudleFuncTx = {};
103
- /**
104
- * Support the following ways to init the ObeliskClient:
105
- * 1. mnemonics
106
- * 2. secretKey (base64 or hex)
107
- * If none of them is provided, will generate a random mnemonics with 24 words.
108
- *
109
- * @param mnemonics, 12 or 24 mnemonics words, separated by space
110
- * @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
111
- * @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'devnet'
112
- * @param fullnodeUrl, the fullnode url, default is the preconfig fullnode url for the given network type
113
- * @param packageId
114
- */
115
- constructor({
116
- mnemonics,
117
- secretKey,
118
- networkType,
119
- fullnodeUrls,
120
- packageId,
121
- metadata,
122
- }: ObeliskParams = {}) {
123
- // Init the account manager
124
- this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
125
- // Init the rpc provider
126
- fullnodeUrls = fullnodeUrls || [getDefaultConnection(networkType).fullnode];
127
- this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
128
-
129
- this.packageId = packageId;
130
- if (metadata !== undefined) {
131
- this.metadata = metadata as SuiMoveNormalizedModules;
132
- Object.values(metadata as SuiMoveNormalizedModules).forEach((value) => {
133
- const data = value as SuiMoveMoudleValueType;
134
- const moduleName = data.name;
135
- Object.entries(data.exposedFunctions).forEach(([funcName, value]) => {
136
- const meta = value as SuiMoveMoudleFuncType;
137
- meta.moudleName = moduleName;
138
- meta.funcName = funcName;
139
-
140
- if (isUndefined(this.#query[moduleName])) {
141
- this.#query[moduleName] = {};
142
- }
143
- if (isUndefined(this.#query[moduleName][funcName])) {
144
- this.#query[moduleName][funcName] = createQuery(
145
- meta,
146
- (tx, p, isRaw) => this.#read(meta, tx, p, isRaw)
147
- );
148
- }
149
-
150
- if (isUndefined(this.#tx[moduleName])) {
151
- this.#tx[moduleName] = {};
152
- }
153
- if (isUndefined(this.#tx[moduleName][funcName])) {
154
- this.#tx[moduleName][funcName] = createTx(meta, (tx, p, isRaw) =>
155
- this.#exec(meta, tx, p, isRaw)
156
- );
157
- }
158
- });
159
- });
160
- }
161
- this.contractFactory = new SuiContractFactory({
162
- packageId,
163
- metadata,
164
- });
165
- }
166
-
167
- public get query(): MapMoudleFuncQuery {
168
- return this.#query;
169
- }
170
-
171
- public get tx(): MapMoudleFuncTx {
172
- return this.#tx;
173
- }
174
-
175
- #exec = async (
176
- meta: SuiMoveMoudleFuncType,
177
- tx: TransactionBlock,
178
- params: SuiTxArgument[],
179
- isRaw?: boolean
180
- ) => {
181
- tx.moveCall({
182
- target: `${this.contractFactory.packageId}::${meta.moudleName}::${meta.funcName}`,
183
- arguments: params,
184
- });
185
-
186
- if (isRaw === true) {
187
- return tx;
188
- }
189
- return await this.signAndSendTxn(tx);
190
- };
191
-
192
- #read = async (
193
- meta: SuiMoveMoudleFuncType,
194
- tx: TransactionBlock,
195
- params: SuiTxArgument[],
196
- isRaw?: boolean
197
- ) => {
198
- tx.moveCall({
199
- target: `${this.contractFactory.packageId}::${meta.moudleName}::${meta.funcName}`,
200
- arguments: params,
201
- });
202
-
203
- if (isRaw === true) {
204
- return tx;
205
- }
206
- return await this.inspectTxn(tx);
207
- };
208
- /**
209
- * if derivePathParams is not provided or mnemonics is empty, it will return the currentSigner.
210
- * else:
211
- * it will generate signer from the mnemonic with the given derivePathParams.
212
- * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
213
- */
214
- getSigner(derivePathParams?: DerivePathParams) {
215
- const keyPair = this.accountManager.getKeyPair(derivePathParams);
216
- return new RawSigner(keyPair, this.suiInteractor.currentProvider);
217
- }
218
-
219
- /**
220
- * @description Switch the current account with the given derivePathParams
221
- * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
222
- */
223
- switchAccount(derivePathParams: DerivePathParams) {
224
- this.accountManager.switchAccount(derivePathParams);
225
- }
226
-
227
- /**
228
- * @description Get the address of the account for the given derivePathParams
229
- * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
230
- */
231
- getAddress(derivePathParams?: DerivePathParams) {
232
- return this.accountManager.getAddress(derivePathParams);
233
- }
234
- currentAddress() {
235
- return this.accountManager.currentAddress;
236
- }
237
-
238
- provider() {
239
- return this.suiInteractor.currentProvider;
240
- }
241
-
242
- getPackageId() {
243
- return this.contractFactory.packageId;
244
- }
245
-
246
- getMetadata() {
247
- return this.contractFactory.metadata;
248
- }
249
- /**
250
- * Request some SUI from faucet
251
- * @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
252
- */
253
- async requestFaucet(address: SuiAddress, network: FaucetNetworkType) {
254
- // const addr = this.accountManager.getAddress(derivePathParams);
255
- return this.suiInteractor.requestFaucet(address, network);
256
- }
257
-
258
- async getBalance(coinType?: string, derivePathParams?: DerivePathParams) {
259
- const owner = this.accountManager.getAddress(derivePathParams);
260
- return this.suiInteractor.currentProvider.getBalance({ owner, coinType });
261
- }
262
-
263
- async getObject(objectId: string) {
264
- return this.suiInteractor.getObject(objectId);
265
- }
266
-
267
- async getObjects(objectIds: string[]) {
268
- return this.suiInteractor.getObjects(objectIds);
269
- }
270
-
271
- async signTxn(
272
- tx: Uint8Array | TransactionBlock | SuiTxBlock,
273
- derivePathParams?: DerivePathParams
274
- ) {
275
- tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
276
- const signer = this.getSigner(derivePathParams);
277
- return signer.signTransactionBlock({ transactionBlock: tx });
278
- }
279
-
280
- async signAndSendTxn(
281
- tx: Uint8Array | TransactionBlock | SuiTxBlock,
282
- derivePathParams?: DerivePathParams
283
- ): Promise<SuiTransactionBlockResponse> {
284
- const { transactionBlockBytes, signature } = await this.signTxn(
285
- tx,
286
- derivePathParams
287
- );
288
- return this.suiInteractor.sendTx(transactionBlockBytes, signature);
289
- }
290
-
291
- /**
292
- * Transfer the given amount of SUI to the recipient
293
- * @param recipient
294
- * @param amount
295
- * @param derivePathParams
296
- */
297
- async transferSui(
298
- recipient: string,
299
- amount: number,
300
- derivePathParams?: DerivePathParams
301
- ) {
302
- const tx = new SuiTxBlock();
303
- tx.transferSui(recipient, amount);
304
- return this.signAndSendTxn(tx, derivePathParams);
305
- }
306
-
307
- /**
308
- * Transfer to mutliple recipients
309
- * @param recipients the recipients addresses
310
- * @param amounts the amounts of SUI to transfer to each recipient, the length of amounts should be the same as the length of recipients
311
- * @param derivePathParams
312
- */
313
- async transferSuiToMany(
314
- recipients: string[],
315
- amounts: number[],
316
- derivePathParams?: DerivePathParams
317
- ) {
318
- const tx = new SuiTxBlock();
319
- tx.transferSuiToMany(recipients, amounts);
320
- return this.signAndSendTxn(tx, derivePathParams);
321
- }
322
-
323
- /**
324
- * Transfer the given amounts of coin to multiple recipients
325
- * @param recipients the list of recipient address
326
- * @param amounts the amounts to transfer for each recipient
327
- * @param coinType any custom coin type but not SUI
328
- * @param derivePathParams the derive path params for the current signer
329
- */
330
- async transferCoinToMany(
331
- recipients: string[],
332
- amounts: number[],
333
- coinType: string,
334
- derivePathParams?: DerivePathParams
335
- ) {
336
- const tx = new SuiTxBlock();
337
- const owner = this.accountManager.getAddress(derivePathParams);
338
- const totalAmount = amounts.reduce((a, b) => a + b, 0);
339
- const coins = await this.suiInteractor.selectCoins(
340
- owner,
341
- totalAmount,
342
- coinType
343
- );
344
- tx.transferCoinToMany(
345
- coins.map((c) => c.objectId),
346
- owner,
347
- recipients,
348
- amounts
349
- );
350
- return this.signAndSendTxn(tx, derivePathParams);
351
- }
352
-
353
- async transferCoin(
354
- recipient: string,
355
- amount: number,
356
- coinType: string,
357
- derivePathParams?: DerivePathParams
358
- ) {
359
- return this.transferCoinToMany(
360
- [recipient],
361
- [amount],
362
- coinType,
363
- derivePathParams
364
- );
365
- }
366
-
367
- async transferObjects(
368
- objects: string[],
369
- recipient: string,
370
- derivePathParams?: DerivePathParams
371
- ) {
372
- const tx = new SuiTxBlock();
373
- tx.transferObjects(objects, recipient);
374
- return this.signAndSendTxn(tx, derivePathParams);
375
- }
376
-
377
- async moveCall(callParams: {
378
- target: string;
379
- arguments?: (SuiTxArg | SuiVecTxArg)[];
380
- typeArguments?: string[];
381
- derivePathParams?: DerivePathParams;
382
- }) {
383
- const {
384
- target,
385
- arguments: args = [],
386
- typeArguments = [],
387
- derivePathParams,
388
- } = callParams;
389
- const tx = new SuiTxBlock();
390
- tx.moveCall(target, args, typeArguments);
391
- return this.signAndSendTxn(tx, derivePathParams);
392
- }
393
-
394
- /**
395
- * Select coins with the given amount and coin type, the total amount is greater than or equal to the given amount
396
- * @param amount
397
- * @param coinType
398
- * @param owner
399
- */
400
- async selectCoinsWithAmount(
401
- amount: number,
402
- coinType: string,
403
- owner?: string
404
- ) {
405
- owner = owner || this.accountManager.currentAddress;
406
- const coins = await this.suiInteractor.selectCoins(owner, amount, coinType);
407
- return coins.map((c) => c.objectId);
408
- }
409
-
410
- /**
411
- * stake the given amount of SUI to the validator
412
- * @param amount the amount of SUI to stake
413
- * @param validatorAddr the validator address
414
- * @param derivePathParams the derive path params for the current signer
415
- */
416
- async stakeSui(
417
- amount: number,
418
- validatorAddr: string,
419
- derivePathParams?: DerivePathParams
420
- ) {
421
- const tx = new SuiTxBlock();
422
- tx.stakeSui(amount, validatorAddr);
423
- return this.signAndSendTxn(tx, derivePathParams);
424
- }
425
-
426
- /**
427
- * Execute the transaction with on-chain data but without really submitting. Useful for querying the effects of a transaction.
428
- * Since the transaction is not submitted, its gas cost is not charged.
429
- * @param tx the transaction to execute
430
- * @param derivePathParams the derive path params
431
- * @returns the effects and events of the transaction, such as object changes, gas cost, event emitted.
432
- */
433
- async inspectTxn(
434
- tx: Uint8Array | TransactionBlock | SuiTxBlock,
435
- derivePathParams?: DerivePathParams
436
- ): Promise<DevInspectResults> {
437
- tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
438
- return this.suiInteractor.currentProvider.devInspectTransactionBlock({
439
- transactionBlock: tx,
440
- sender: this.getAddress(derivePathParams),
441
- });
442
- }
443
-
444
- async getWorld(worldObjectId: string) {
445
- return this.suiInteractor.getObject(worldObjectId);
446
- }
447
-
448
- async listSchemaNames(worldId: string) {
449
- const worldObject = await this.getObject(worldId);
450
- const newObjectContent = worldObject.objectFields;
451
- return newObjectContent['schemaNames'];
452
- }
453
-
454
- async getEntity(
455
- worldId: string,
456
- schemaName: string,
457
- entityId?: string
458
- ): Promise<any[] | undefined> {
459
- const schemaMoudleName = `${schemaName}_schema`;
460
- const tx = new TransactionBlock();
461
- const params = [tx.pure(worldId)] as SuiTxArgument[];
462
- if (entityId !== undefined) {
463
- params.push(tx.pure(entityId));
464
- }
465
-
466
- const getResult = (await this.query[schemaMoudleName].get(
467
- tx,
468
- params
469
- )) as DevInspectResults;
470
- const returnValue = [];
471
-
472
- // "success" | "failure";
473
- if (getResult.effects.status.status === 'success') {
474
- const resultList = getResult.results![0].returnValues!;
475
- for (const res of resultList) {
476
- const bcs = new BCS(getSuiMoveConfig());
477
- const value = Uint8Array.from(res[0]);
478
- const bcsType = res[1].replace(/0x1::ascii::String/g, 'string');
479
- const data = bcs.de(bcsType, value);
480
- returnValue.push(data);
481
- }
482
- return returnValue;
483
- } else {
484
- return undefined;
485
- }
486
- }
487
-
488
- async containEntity(
489
- worldId: string,
490
- schemaName: string,
491
- entityId?: string
492
- ): Promise<boolean | undefined> {
493
- const schemaMoudleName = `${schemaName}_schema`;
494
- const tx = new TransactionBlock();
495
- const params = [tx.pure(worldId)] as SuiTxArgument[];
496
- if (entityId !== undefined) {
497
- params.push(tx.pure(entityId));
498
- }
499
-
500
- const getResult = (await this.query[schemaMoudleName].contains(
501
- tx,
502
- params
503
- )) as DevInspectResults;
504
-
505
- // "success" | "failure";
506
- if (getResult.effects.status.status === 'success') {
507
- const res = getResult.results![0].returnValues![0];
508
- const bcs = new BCS(getSuiMoveConfig());
509
- const value = Uint8Array.from(res[0]);
510
- return bcs.de(res[1], value);
511
- } else {
512
- return undefined;
513
- }
514
- }
515
-
516
- // async getEntities(
517
- // worldId: string,
518
- // schemaName: string,
519
- // cursor?: string,
520
- // limit?: number
521
- // ) {
522
- // let schemaMoudleName = `${schemaName}_schema`;
523
-
524
- // const tx = new TransactionBlock();
525
- // let params = [tx.pure(worldId)] as SuiTxArgument[];
526
-
527
- // const tableResult = (await this.query[schemaonentMoudleName].entities(
528
- // tx,
529
- // params
530
- // )) as DevInspectResults;
531
- // const entities = tableResult.results as SuiReturnValues;
532
- // const bcs = new BCS(getSuiMoveConfig());
533
-
534
- // let value = Uint8Array.from(entities[0].returnValues[0][0]);
535
- // let tableId = '0x' + bcs.de('address', value);
536
- // let dynamicFields = await this.suiInteractor.getDynamicFields(
537
- // tableId,
538
- // cursor,
539
- // limit
540
- // );
541
- // let objectIds = dynamicFields.data.map((field) => field.objectId);
542
- // let objectDatas = await this.suiInteractor.getEntitiesObjects(objectIds);
543
- // return {
544
- // data: objectDatas,
545
- // nextCursor: dynamicFields.nextCursor,
546
- // hasNextPage: dynamicFields.hasNextPage,
547
- // };
548
- // }
549
-
550
- async getOwnedObjects(owner: SuiAddress, cursor?: string, limit?: number) {
551
- const ownedObjects = await this.suiInteractor.getOwnedObjects(
552
- owner,
553
- cursor,
554
- limit
555
- );
556
- const ownedObjectsRes: ObeliskObjectData[] = [];
557
-
558
- for (const object of ownedObjects.data) {
559
- const objectDetail = await this.getObject(object.data!.objectId);
560
-
561
- if (
562
- objectDetail.objectType.split('::')[0] ===
563
- this.contractFactory.packageId
564
- ) {
565
- ownedObjectsRes.push(objectDetail);
566
- }
567
- }
568
-
569
- return ownedObjectsRes;
570
- }
571
-
572
- async entity_key_from_object(objectId: string) {
573
- const checkObjectId = normalizeHexAddress(objectId);
574
- if (checkObjectId !== null) {
575
- objectId = checkObjectId;
576
- return objectId;
577
- } else {
578
- return undefined;
579
- }
580
- }
581
-
582
- async entity_key_from_bytes(bytes: Uint8Array | Buffer | string) {
583
- const hashBytes = keccak256(bytes);
584
- const hashU8Array: number[] = Array.from(hashBytes);
585
- const bcs = new BCS(getSuiMoveConfig());
586
- const value = Uint8Array.from(hashU8Array);
587
- const data = bcs.de('address', value);
588
- return '0x' + data;
589
- }
590
-
591
- async entity_key_from_u256(x: number) {
592
- return numberToAddressHex(x);
593
- }
594
-
595
- async formatData(type: string, value: Buffer | number[] | Uint8Array) {
596
- const bcs = new BCS(getSuiMoveConfig());
597
- const u8Value = Uint8Array.from(value);
598
- return bcs.de(type, u8Value);
599
- }
600
- }