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