suidouble 0.0.41 → 0.0.42

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.
package/README.md CHANGED
@@ -276,6 +276,19 @@ contract.modules.modulename.pushObject('0x10cded4f9df05e37b44e3be2ffa9004dec7778
276
276
  await contract.modules.modulename.fetchObjects(); // fetch objects fields etc
277
277
  const object = contract.modules.modulename.objectStorage.byAddress('0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2');
278
278
  ```
279
+
280
+ Another option (if you don't know the object id) is to query current wallet owned module's objects from blockchain:
281
+
282
+ ```javascript
283
+ const module = await contract.getModule('suidouble_chat');
284
+ const paginatedResponse = await module.getOwnedObjects(); // all module objects owned by you
285
+ const paginatedResponse2 = await module.getOwnedObjects({ typeName: 'ChatResponse' }); // specific type objects owned by you
286
+
287
+ await paginatedResponse.forEach(async(suiObject)=>{
288
+ console.log(suiObject.id, suiObject.typeName, suiObject.fields);
289
+ }, maxLimit); // optional maxLimit, if (!maxLimit) - it will fetch and call callback for all available objects
290
+ ```
291
+
279
292
  @todo: move pushing/fetching to SuiMemoryObjectStorage directly, as there's nothing package or module related?
280
293
  @todo: invalidation? No need to re-fetch all objects each time
281
294
 
package/lib/SuiPackage.js CHANGED
@@ -157,19 +157,13 @@ class SuiPackage extends SuiObject {
157
157
  method: 'getOwnedObjects',
158
158
  });
159
159
 
160
- do {
161
- await paginatedResponse.nextPage();
162
-
163
- if (paginatedResponse.data && paginatedResponse.data.length) {
164
- for (const object of paginatedResponse.data) {
165
- if (object?.data?.content?.fields?.package) {
166
- if (packagesOnChainIds.indexOf(object?.data?.content?.fields?.package) === -1) {
167
- packagesOnChainIds.push(object?.data?.content?.fields?.package);
168
- }
169
- }
170
- }
160
+ await paginatedResponse.forEach((suiObject)=>{
161
+ const packageId = suiObject.fields.package;
162
+ if (packageId && packagesOnChainIds.indexOf(packageId) === -1) {
163
+ packagesOnChainIds.push(packageId);
171
164
  }
172
- } while(paginatedResponse.hasNextPage);
165
+ }); // go through all available UpgradeCap
166
+ // paginatedResponse.forEach also accepts async callbacks
173
167
 
174
168
  // queriing packages out of the loop, as not sure if pagination cursor works ok with mixed calls, @todo: check
175
169
  // @todo: what is the max count of ids here?
@@ -223,6 +223,41 @@ class SuiPackageModule extends SuiCommonMethods {
223
223
  status: status,
224
224
  };
225
225
  }
226
+
227
+ async getOwnedObjects(params = {}) {
228
+ ///
229
+ /// the pagination is not accurate, because previous page may have
230
+ /// been updated when the next page is fetched. Please use suix_queryObjects if this is a concern.
231
+ ///
232
+ const normalizedPackageAddress = await this.getNormalizedPackageAddress();
233
+ const queryParams = {
234
+ owner: this._suiMaster.address,
235
+ filter: { MoveModule: { package: normalizedPackageAddress, module: this._moduleName } },
236
+ limit: 50, // max limit is 50
237
+ options: {
238
+ showType: true,
239
+ showContent: true,
240
+ showOwner: true,
241
+ showDisplay: true,
242
+ },
243
+ };
244
+
245
+ if (params.typeName) {
246
+ queryParams.filter = { StructType: `${normalizedPackageAddress}::${this._moduleName}::${params.typeName}`};
247
+ }
248
+
249
+ const paginatedResponse = new SuiPaginatedResponse({
250
+ debug: this._debug,
251
+ suiMaster: this._suiMaster,
252
+ params: queryParams,
253
+ method: 'getOwnedObjects',
254
+ order: params.order,
255
+ });
256
+
257
+ await paginatedResponse.fetch();
258
+
259
+ return paginatedResponse;
260
+ }
226
261
 
227
262
  async fetchEvents(params = {}) {
228
263
  const moduleFilter = {};
@@ -85,8 +85,11 @@ class SuiPaginatedResponse extends SuiCommonMethods {
85
85
  // convert data to SuiEvent instances
86
86
  this._data = response.data.map((raw)=>(new SuiEvent({data: raw, suiMaster: this._suiMaster, debug: this._debug})));
87
87
  } else if (this._method === 'queryTransactionBlocks') {
88
- // convert data to SuiEvent instances
88
+ // convert data to SuiTransaction instances
89
89
  this._data = response.data.map((raw)=>(new SuiTransaction({data: raw, suiMaster: this._suiMaster, debug: this._debug})));
90
+ } else if (this._method === 'getOwnedObjects') {
91
+ // convert data to SuiObject instances
92
+ this._data = response.data.map((raw)=>(new (this._suiMaster.SuiObject)({suiMaster: this._suiMaster, debug: this._debug, objectChange: raw})));
90
93
  } else {
91
94
  this._data = response.data;
92
95
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "suidouble",
3
- "version": "0.0.41",
3
+ "version": "0.0.42",
4
4
  "description": "Set of provider, package and object classes for javascript representation of Sui Move smart contracts. Use same code for publishing, upgrading, integration testing, interaction with smart contracts and integration in browser web3 dapps",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -280,6 +280,40 @@ test('testing paginatedResponse', async t => {
280
280
  });
281
281
 
282
282
 
283
+ test('find owned module objects with query', async t => {
284
+ const module = await contract.getModule('suidouble_chat');
285
+ const paginatedResponse = await module.getOwnedObjects();
286
+
287
+ let foundCount = 0;
288
+ let foundChatOwnerCap = false;
289
+ // loop through all module objects owned by current wall
290
+ await paginatedResponse.forEach((suiObject)=>{
291
+ if (suiObject.typeName == 'ChatOwnerCap') {
292
+ foundChatOwnerCap = true;
293
+ }
294
+
295
+ foundCount++; // total count
296
+ });
297
+ t.ok(foundCount >= 60); // it's 60 in move code, but let's keep chat flexible
298
+ t.ok(foundChatOwnerCap); // it's 60 in move code, but let's keep chat flexible
299
+
300
+ /// also lets try querying specific typeName
301
+ const paginatedResponse2 = await module.getOwnedObjects({ typeName: 'ChatOwnerCap' });
302
+
303
+ let foundCount2 = 0;
304
+ let foundChatOwnerCap2 = false;
305
+
306
+ await paginatedResponse2.forEach(async(suiObject)=>{ // paginatedResponse forEach also accepts async callbacks
307
+ if (suiObject.typeName == 'ChatOwnerCap') {
308
+ foundChatOwnerCap2 = true;
309
+ }
310
+ foundCount2++;
311
+ });
312
+
313
+ t.ok(foundChatOwnerCap2);
314
+ t.ok(foundCount2 == 1); // ChatOwnerCap only
315
+ });
316
+
283
317
  test('testing move call with coins', async t => {
284
318
  const balanceWas = await suiMaster.getBalance();
285
319