@mysten/walrus 0.0.3 → 0.0.5

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 (42) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +6 -8
  3. package/dist/cjs/client.d.ts +5 -89
  4. package/dist/cjs/client.js +194 -137
  5. package/dist/cjs/client.js.map +3 -3
  6. package/dist/cjs/constants.d.ts +13 -2
  7. package/dist/cjs/constants.js +6 -3
  8. package/dist/cjs/constants.js.map +2 -2
  9. package/dist/cjs/contracts/subsidies.d.ts +83 -0
  10. package/dist/cjs/contracts/subsidies.js +253 -0
  11. package/dist/cjs/contracts/subsidies.js.map +7 -0
  12. package/dist/cjs/types.d.ts +5 -6
  13. package/dist/cjs/types.js.map +1 -1
  14. package/dist/cjs/utils/index.d.ts +2 -0
  15. package/dist/cjs/utils/index.js +46 -1
  16. package/dist/cjs/utils/index.js.map +2 -2
  17. package/dist/cjs/utils/memo.d.ts +5 -0
  18. package/dist/cjs/utils/memo.js +59 -0
  19. package/dist/cjs/utils/memo.js.map +7 -0
  20. package/dist/cjs/utils/object-loader.js +2 -2
  21. package/dist/cjs/utils/object-loader.js.map +2 -2
  22. package/dist/esm/client.d.ts +5 -89
  23. package/dist/esm/client.js +182 -120
  24. package/dist/esm/client.js.map +3 -3
  25. package/dist/esm/constants.d.ts +13 -2
  26. package/dist/esm/constants.js +6 -3
  27. package/dist/esm/constants.js.map +2 -2
  28. package/dist/esm/contracts/subsidies.d.ts +83 -0
  29. package/dist/esm/contracts/subsidies.js +223 -0
  30. package/dist/esm/contracts/subsidies.js.map +7 -0
  31. package/dist/esm/types.d.ts +5 -6
  32. package/dist/esm/utils/index.d.ts +2 -0
  33. package/dist/esm/utils/index.js +46 -1
  34. package/dist/esm/utils/index.js.map +2 -2
  35. package/dist/esm/utils/memo.d.ts +5 -0
  36. package/dist/esm/utils/memo.js +39 -0
  37. package/dist/esm/utils/memo.js.map +7 -0
  38. package/dist/esm/utils/object-loader.js +2 -2
  39. package/dist/esm/utils/object-loader.js.map +2 -2
  40. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  41. package/dist/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +3 -3
@@ -32,6 +32,7 @@ module.exports = __toCommonJS(client_exports);
32
32
  var import_bcs = require("@mysten/bcs");
33
33
  var import_client = require("@mysten/sui/client");
34
34
  var import_transactions = require("@mysten/sui/transactions");
35
+ var import_utils = require("@mysten/sui/utils");
35
36
  var import_walrus_wasm = require("@mysten/walrus-wasm");
36
37
  var import_constants = require("./constants.js");
37
38
  var import_blob = require("./contracts/blob.js");
@@ -40,39 +41,95 @@ var import_staking_inner = require("./contracts/staking_inner.js");
40
41
  var import_staking_pool = require("./contracts/staking_pool.js");
41
42
  var import_staking = require("./contracts/staking.js");
42
43
  var import_storage_resource = require("./contracts/storage_resource.js");
44
+ var import_subsidies = require("./contracts/subsidies.js");
43
45
  var import_system_state_inner = require("./contracts/system_state_inner.js");
44
46
  var import_system = require("./contracts/system.js");
45
47
  var import_error = require("./error.js");
46
48
  var import_client2 = require("./storage-node/client.js");
47
49
  var import_error2 = require("./storage-node/error.js");
48
50
  var import_bcs2 = require("./utils/bcs.js");
49
- var import_utils = require("./utils/index.js");
51
+ var import_utils2 = require("./utils/index.js");
52
+ var import_memo = require("./utils/memo.js");
50
53
  var import_object_loader = require("./utils/object-loader.js");
51
54
  var import_randomness = require("./utils/randomness.js");
52
55
  var import_wasm = require("./wasm.js");
53
- var _storageNodeClient, _suiClient, _objectLoader, _blobMetadataConcurrencyLimit, _activeCommittee, _readCommittee, _WalrusClient_instances, blobContract_get, metadataContract_get, internalReadBlob_fn, getCertificationEpoch_fn, getReadCommittee_fn, forceGetReadCommittee_fn, writeBlobAttributesForRef_fn, executeTransaction_fn, getCommittee_fn, getActiveCommittee_fn, stakingPool_fn, getNodeByShardIndex_fn, retryOnPossibleEpochChange_fn;
56
+ var _storageNodeClient, _packageConfig, _suiClient, _objectLoader, _blobMetadataConcurrencyLimit, _readCommittee, _memo, _walType, _getPackageId, _getBlobType, _getSystemContract, _getSubsidiesContract, _getBlobContract, _getMetadataContract, _WalrusClient_instances, internalReadBlob_fn, getCertificationEpoch_fn, getReadCommittee_fn, forceGetReadCommittee_fn, withWal_fn, writeBlobAttributesForRef_fn, executeTransaction_fn, getCommittee_fn, _getActiveCommittee, stakingPool_fn, getNodeByShardIndex_fn, retryOnPossibleEpochChange_fn;
54
57
  class WalrusClient {
55
58
  constructor(config) {
56
59
  __privateAdd(this, _WalrusClient_instances);
57
60
  __privateAdd(this, _storageNodeClient);
61
+ __privateAdd(this, _packageConfig);
58
62
  __privateAdd(this, _suiClient);
59
63
  __privateAdd(this, _objectLoader);
60
64
  __privateAdd(this, _blobMetadataConcurrencyLimit, 10);
61
- __privateAdd(this, _activeCommittee);
62
65
  __privateAdd(this, _readCommittee);
66
+ __privateAdd(this, _memo, new import_memo.MemoCache());
67
+ /** The Move type for a WAL coin */
68
+ __privateAdd(this, _walType, __privateGet(this, _memo).create("walType", async () => {
69
+ const stakedWal = await __privateGet(this, _suiClient).getNormalizedMoveStruct({
70
+ package: await __privateGet(this, _getPackageId).call(this),
71
+ module: "staked_wal",
72
+ struct: "StakedWal"
73
+ });
74
+ const balanceType = stakedWal.fields.find((field) => field.name === "principal")?.type;
75
+ if (!balanceType) {
76
+ throw new import_error.WalrusClientError("WAL type not found");
77
+ }
78
+ const parsed = (0, import_utils.parseStructTag)((0, import_utils2.toTypeString)(balanceType));
79
+ const coinType = parsed.typeParams[0];
80
+ if (!coinType) {
81
+ throw new import_error.WalrusClientError("WAL type not found");
82
+ }
83
+ return (0, import_utils.normalizeStructTag)(coinType);
84
+ }));
85
+ __privateAdd(this, _getPackageId, __privateGet(this, _memo).create("getPackageId", async () => {
86
+ const system = await __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).systemObjectId);
87
+ return (0, import_utils.parseStructTag)(system.type).address;
88
+ }));
89
+ /** The Move type for a Blob object */
90
+ __privateAdd(this, _getBlobType, __privateGet(this, _memo).create("getBlobType", async () => {
91
+ return `${await __privateGet(this, _getPackageId).call(this)}::blob::Blob`;
92
+ }));
93
+ __privateAdd(this, _getSystemContract, __privateGet(this, _memo).create("getSystemContract", async () => {
94
+ const { package_id } = await this.systemObject();
95
+ return (0, import_system.init)(package_id);
96
+ }));
97
+ __privateAdd(this, _getSubsidiesContract, __privateGet(this, _memo).create("getSubsidiesContract", async () => {
98
+ if (!__privateGet(this, _packageConfig).subsidiesObjectId) {
99
+ throw new import_error.WalrusClientError("Subsidies object ID not defined in package config");
100
+ }
101
+ const subsidiesObject = await __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).subsidiesObjectId);
102
+ const packageId = (0, import_utils.parseStructTag)(subsidiesObject.type).address;
103
+ return (0, import_subsidies.init)(packageId);
104
+ }));
105
+ __privateAdd(this, _getBlobContract, __privateGet(this, _memo).create("getBlobContract", async () => {
106
+ const { package_id } = await this.systemObject();
107
+ return (0, import_blob.init)(package_id);
108
+ }));
109
+ __privateAdd(this, _getMetadataContract, __privateGet(this, _memo).create("getMetadataContract", async () => {
110
+ const { package_id } = await this.systemObject();
111
+ return (0, import_metadata.init)(package_id);
112
+ }));
63
113
  /** Read a blob from the storage nodes */
64
114
  this.readBlob = __privateMethod(this, _WalrusClient_instances, retryOnPossibleEpochChange_fn).call(this, __privateMethod(this, _WalrusClient_instances, internalReadBlob_fn));
115
+ __privateAdd(this, _getActiveCommittee, __privateGet(this, _memo).create("getActiveCommittee", async () => {
116
+ const stakingState = await this.stakingState();
117
+ return __privateMethod(this, _WalrusClient_instances, getCommittee_fn).call(this, stakingState.committee);
118
+ }));
65
119
  if (config.network && !config.packageConfig) {
66
120
  const network = config.network;
67
121
  switch (network) {
68
122
  case "testnet":
69
- this.packageConfig = import_constants.TESTNET_WALRUS_PACKAGE_CONFIG;
123
+ __privateSet(this, _packageConfig, import_constants.TESTNET_WALRUS_PACKAGE_CONFIG);
124
+ break;
125
+ case "mainnet":
126
+ __privateSet(this, _packageConfig, import_constants.MAINNET_WALRUS_PACKAGE_CONFIG);
70
127
  break;
71
128
  default:
72
129
  throw new import_error.WalrusClientError(`Unsupported network: ${network}`);
73
130
  }
74
131
  } else {
75
- this.packageConfig = config.packageConfig;
132
+ __privateSet(this, _packageConfig, config.packageConfig);
76
133
  }
77
134
  __privateSet(this, _suiClient, config.suiClient ?? new import_client.SuiClient({
78
135
  url: config.suiRpcUrl
@@ -80,42 +137,27 @@ class WalrusClient {
80
137
  __privateSet(this, _storageNodeClient, new import_client2.StorageNodeClient(config.storageNodeClientOptions));
81
138
  __privateSet(this, _objectLoader, new import_object_loader.SuiObjectDataLoader(__privateGet(this, _suiClient)));
82
139
  }
83
- /** The Move type for a WAL coin */
84
- get walType() {
85
- return `${this.packageConfig.walPackageId}::wal::WAL`;
86
- }
87
- /** The Move type for a Blob object */
88
- get blobType() {
89
- return `${this.packageConfig.packageId}::blob::Blob`;
90
- }
91
- /** The Move type for a Storage object */
92
- get storageType() {
93
- return `${this.packageConfig.packageId}::storage_resource::Storage`;
94
- }
95
- get systemContract() {
96
- return (0, import_system.init)(this.packageConfig.latestPackageId);
97
- }
98
140
  /** The cached system object for the walrus package */
99
141
  systemObject() {
100
- return __privateGet(this, _objectLoader).load(this.packageConfig.systemObjectId, (0, import_system.System)());
142
+ return __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).systemObjectId, (0, import_system.System)());
101
143
  }
102
144
  /** The cached staking pool object for the walrus package */
103
145
  stakingObject() {
104
- return __privateGet(this, _objectLoader).load(this.packageConfig.stakingPoolId, (0, import_staking.Staking)());
146
+ return __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).stakingPoolId, (0, import_staking.Staking)());
105
147
  }
106
148
  /** The system state for the current version of walrus contract */
107
149
  async systemState() {
108
150
  const systemState = await __privateGet(this, _objectLoader).loadFieldObject(
109
- this.packageConfig.systemObjectId,
151
+ __privateGet(this, _packageConfig).systemObjectId,
110
152
  { type: "u64", value: (await this.systemObject()).version },
111
- this.packageConfig.systemObjectId === import_constants.TESTNET_WALRUS_PACKAGE_CONFIG.systemObjectId ? import_bcs2.TestnetSystemStateInnerV1 : (0, import_system_state_inner.SystemStateInnerV1)()
153
+ __privateGet(this, _packageConfig).systemObjectId === import_constants.TESTNET_WALRUS_PACKAGE_CONFIG.systemObjectId ? import_bcs2.TestnetSystemStateInnerV1 : (0, import_system_state_inner.SystemStateInnerV1)()
112
154
  );
113
155
  return systemState;
114
156
  }
115
157
  /** The staking state for the current version of walrus contract */
116
158
  async stakingState() {
117
159
  return __privateGet(this, _objectLoader).loadFieldObject(
118
- this.packageConfig.stakingPoolId,
160
+ __privateGet(this, _packageConfig).stakingPoolId,
119
161
  {
120
162
  type: "u64",
121
163
  value: (await this.stakingObject()).version
@@ -156,7 +198,7 @@ class WalrusClient {
156
198
  return await attemptGetMetadata();
157
199
  } catch (error) {
158
200
  const chunkSize = Math.floor(metadataExecutors.length / __privateGet(this, _blobMetadataConcurrencyLimit));
159
- const chunkedExecutors = (0, import_utils.chunk)(metadataExecutors, chunkSize);
201
+ const chunkedExecutors = (0, import_utils2.chunk)(metadataExecutors, chunkSize);
160
202
  return await new Promise((resolve, reject) => {
161
203
  chunkedExecutors.forEach(async (executors) => {
162
204
  for (const executor of executors) {
@@ -168,7 +210,7 @@ class WalrusClient {
168
210
  if (error2 instanceof import_error2.UserAbortError) {
169
211
  reject(error2);
170
212
  return;
171
- } else if ((0, import_utils.isQuorum)(numBlockedWeight + numNotFoundWeight, numShards)) {
213
+ } else if ((0, import_utils2.isQuorum)(numBlockedWeight + numNotFoundWeight, numShards)) {
172
214
  const abortError = numNotFoundWeight > numBlockedWeight ? new import_error.BlobNotCertifiedError(`The specified blob ${blobId} is not certified.`) : new import_error.BlobBlockedError(`The specified blob ${blobId} is blocked.`);
173
215
  controller.abort(abortError);
174
216
  reject(abortError);
@@ -197,15 +239,15 @@ class WalrusClient {
197
239
  );
198
240
  const stakingState = await this.stakingState();
199
241
  const numShards = stakingState.n_shards;
200
- const { primarySymbols: minSymbols } = (0, import_utils.getSourceSymbols)(numShards);
242
+ const { primarySymbols: minSymbols } = (0, import_utils2.getSourceSymbols)(numShards);
201
243
  const sliverPairIndices = randomizedNodes.flatMap(
202
244
  (node) => node.shardIndices.map((shardIndex) => ({
203
245
  url: node.networkUrl,
204
- sliverPairIndex: (0, import_utils.toPairIndex)(shardIndex, blobId, numShards)
246
+ sliverPairIndex: (0, import_utils2.toPairIndex)(shardIndex, blobId, numShards)
205
247
  }))
206
248
  );
207
249
  const controller = new AbortController();
208
- const chunkedSliverPairIndices = (0, import_utils.chunk)(sliverPairIndices, minSymbols);
250
+ const chunkedSliverPairIndices = (0, import_utils2.chunk)(sliverPairIndices, minSymbols);
209
251
  const slivers = [];
210
252
  const failedNodes = /* @__PURE__ */ new Set();
211
253
  let numNotFoundWeight = 0;
@@ -243,7 +285,7 @@ class WalrusClient {
243
285
  reject(error);
244
286
  return;
245
287
  }
246
- if ((0, import_utils.isQuorum)(numBlockedWeight + numNotFoundWeight, numShards)) {
288
+ if ((0, import_utils2.isQuorum)(numBlockedWeight + numNotFoundWeight, numShards)) {
247
289
  const abortError = numNotFoundWeight > numBlockedWeight ? new import_error.BlobNotCertifiedError(`The specified blob ${blobId} is not certified.`) : new import_error.BlobBlockedError(`The specified blob ${blobId} is blocked.`);
248
290
  controller.abort(abortError);
249
291
  reject(abortError);
@@ -269,7 +311,7 @@ class WalrusClient {
269
311
  * Gets the blob status from multiple storage nodes and returns the latest status that can be verified.
270
312
  */
271
313
  async getVerifiedBlobStatus({ blobId, signal }) {
272
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
314
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
273
315
  const stakingState = await this.stakingState();
274
316
  const numShards = stakingState.n_shards;
275
317
  const controller = new AbortController();
@@ -289,7 +331,7 @@ class WalrusClient {
289
331
  signal: signal ? AbortSignal.any([controller.signal, signal]) : controller.signal
290
332
  }
291
333
  );
292
- if ((0, import_utils.isQuorum)(successWeight, numShards)) {
334
+ if ((0, import_utils2.isQuorum)(successWeight, numShards)) {
293
335
  controller.abort("Quorum of blob statuses retrieved successfully.");
294
336
  resolve(results);
295
337
  } else {
@@ -302,7 +344,7 @@ class WalrusClient {
302
344
  } else if (error instanceof import_error2.UserAbortError) {
303
345
  reject(error);
304
346
  }
305
- if ((0, import_utils.isQuorum)(numNotFoundWeight, numShards)) {
347
+ if ((0, import_utils2.isQuorum)(numNotFoundWeight, numShards)) {
306
348
  const abortError = new import_error.BlobNotCertifiedError("The blob does not exist.");
307
349
  controller.abort(abortError);
308
350
  reject(abortError);
@@ -336,7 +378,7 @@ class WalrusClient {
336
378
  (a, b) => import_constants.statusLifecycleRank[b.status.type] - import_constants.statusLifecycleRank[a.status.type]
337
379
  );
338
380
  for (const value of sortedStatuses) {
339
- if ((0, import_utils.isAboveValidity)(value.totalWeight, numShards)) {
381
+ if ((0, import_utils2.isAboveValidity)(value.totalWeight, numShards)) {
340
382
  return value.status;
341
383
  }
342
384
  }
@@ -349,8 +391,8 @@ class WalrusClient {
349
391
  */
350
392
  async storageCost(size, epochs) {
351
393
  const systemState = await this.systemState();
352
- const encodedSize = (0, import_utils.encodedBlobLength)(size, systemState.committee.n_shards);
353
- const storageUnits = (0, import_utils.storageUnitsFromSize)(encodedSize);
394
+ const encodedSize = (0, import_utils2.encodedBlobLength)(size, systemState.committee.n_shards);
395
+ const storageUnits = (0, import_utils2.storageUnitsFromSize)(encodedSize);
354
396
  const storageCost = BigInt(storageUnits) * BigInt(systemState.storage_price_per_unit_size) * BigInt(epochs);
355
397
  BigInt(epochs);
356
398
  const writeCost = BigInt(storageUnits) * BigInt(systemState.write_price_per_unit_size);
@@ -364,30 +406,28 @@ class WalrusClient {
364
406
  * tx.transferObjects([await client.createStorage({ size: 1000, epochs: 3 })], owner);
365
407
  * ```
366
408
  */
367
- async createStorage({ size, epochs, walCoin }) {
409
+ async createStorage({ size, epochs, walCoin, owner }) {
368
410
  const systemObject = await this.systemObject();
369
411
  const systemState = await this.systemState();
370
- const encodedSize = (0, import_utils.encodedBlobLength)(size, systemState.committee.n_shards);
412
+ const encodedSize = (0, import_utils2.encodedBlobLength)(size, systemState.committee.n_shards);
371
413
  const { storageCost } = await this.storageCost(size, epochs);
372
- return (tx) => {
373
- const coin = walCoin ? tx.splitCoins(walCoin, [storageCost])[0] : tx.add(
374
- (0, import_transactions.coinWithBalance)({
375
- balance: storageCost,
376
- type: this.walType
377
- })
378
- );
379
- const storage = tx.add(
380
- this.systemContract.reserve_space({
414
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
415
+ const subsidiesContract = __privateGet(this, _packageConfig).subsidiesObjectId ? await __privateGet(this, _getSubsidiesContract).call(this) : null;
416
+ return __privateMethod(this, _WalrusClient_instances, withWal_fn).call(this, storageCost, owner, walCoin ?? null, subsidiesContract !== null, (coin, tx) => {
417
+ return tx.add(
418
+ subsidiesContract ? subsidiesContract.reserve_space({
419
+ arguments: [
420
+ __privateGet(this, _packageConfig).subsidiesObjectId,
421
+ systemObject.id.id,
422
+ encodedSize,
423
+ epochs,
424
+ coin
425
+ ]
426
+ }) : systemContract.reserve_space({
381
427
  arguments: [systemObject.id.id, encodedSize, epochs, coin]
382
428
  })
383
429
  );
384
- tx.moveCall({
385
- target: "0x2::coin::destroy_zero",
386
- typeArguments: [this.walType],
387
- arguments: [coin]
388
- });
389
- return storage;
390
- };
430
+ });
391
431
  }
392
432
  /**
393
433
  * Create a transaction that creates a storage object
@@ -403,7 +443,7 @@ class WalrusClient {
403
443
  epochs,
404
444
  owner
405
445
  }) {
406
- transaction.transferObjects([await this.createStorage({ size, epochs })], owner);
446
+ transaction.transferObjects([await this.createStorage({ size, epochs, owner })], owner);
407
447
  return transaction;
408
448
  }
409
449
  /**
@@ -422,6 +462,7 @@ class WalrusClient {
422
462
  ...options,
423
463
  owner: options.transaction?.getData().sender ?? signer.toSuiAddress()
424
464
  });
465
+ const blobType = await __privateGet(this, _getBlobType).call(this);
425
466
  const { digest, effects } = await __privateMethod(this, _WalrusClient_instances, executeTransaction_fn).call(this, transaction, signer, "create storage");
426
467
  const createdObjectIds = effects?.created?.map((effect) => effect.reference.objectId) ?? [];
427
468
  const createdObjects = await __privateGet(this, _suiClient).multiGetObjects({
@@ -431,7 +472,7 @@ class WalrusClient {
431
472
  showBcs: true
432
473
  }
433
474
  });
434
- const suiBlobObject = createdObjects.find((object) => object.data?.type === this.blobType);
475
+ const suiBlobObject = createdObjects.find((object) => object.data?.type === blobType);
435
476
  if (!suiBlobObject || suiBlobObject.data?.bcs?.dataType !== "moveObject") {
436
477
  throw new import_error.WalrusClientError("Storage object not found in transaction effects");
437
478
  }
@@ -455,21 +496,21 @@ class WalrusClient {
455
496
  rootHash,
456
497
  deletable,
457
498
  walCoin,
499
+ owner,
458
500
  attributes
459
501
  }) {
460
- const storage = await this.createStorage({ size, epochs, walCoin });
502
+ const storage = await this.createStorage({ size, epochs, walCoin, owner });
461
503
  const { writeCost } = await this.storageCost(size, epochs);
462
- return (tx) => {
463
- const writeCoin = walCoin ? tx.splitCoins(walCoin, [writeCost])[0] : tx.add(
464
- (0, import_transactions.coinWithBalance)({
465
- balance: writeCost,
466
- type: this.walType
467
- })
468
- );
504
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
505
+ const writeAttributes = attributes ? await __privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, {
506
+ attributes,
507
+ existingAttributes: null
508
+ }) : null;
509
+ return __privateMethod(this, _WalrusClient_instances, withWal_fn).call(this, writeCost, owner, walCoin ?? null, false, (writeCoin, tx) => {
469
510
  const blob = tx.add(
470
- this.systemContract.register_blob({
511
+ systemContract.register_blob({
471
512
  arguments: [
472
- tx.object(this.packageConfig.systemObjectId),
513
+ tx.object(__privateGet(this, _packageConfig).systemObjectId),
473
514
  storage,
474
515
  (0, import_bcs2.blobIdToInt)(blobId),
475
516
  BigInt(import_bcs.bcs.u256().parse(rootHash)),
@@ -480,16 +521,11 @@ class WalrusClient {
480
521
  ]
481
522
  })
482
523
  );
483
- tx.moveCall({
484
- target: "0x2::coin::destroy_zero",
485
- typeArguments: [this.walType],
486
- arguments: [writeCoin]
487
- });
488
- if (attributes) {
489
- tx.add(__privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, { blob, attributes, existingAttributes: null }));
524
+ if (writeAttributes) {
525
+ tx.add((tx2) => writeAttributes(tx2, blob));
490
526
  }
491
527
  return blob;
492
- };
528
+ });
493
529
  }
494
530
  /**
495
531
  * Create a transaction that registers a blob
@@ -501,11 +537,10 @@ class WalrusClient {
501
537
  */
502
538
  async registerBlobTransaction({
503
539
  transaction = new import_transactions.Transaction(),
504
- owner,
505
540
  ...options
506
541
  }) {
507
542
  const registration = transaction.add(await this.registerBlob(options));
508
- transaction.transferObjects([registration], owner);
543
+ transaction.transferObjects([registration], options.owner);
509
544
  return transaction;
510
545
  }
511
546
  /**
@@ -524,6 +559,7 @@ class WalrusClient {
524
559
  ...options,
525
560
  owner: options.owner ?? options.transaction?.getData().sender ?? signer.toSuiAddress()
526
561
  });
562
+ const blobType = await __privateGet(this, _getBlobType).call(this);
527
563
  const { digest, effects } = await __privateMethod(this, _WalrusClient_instances, executeTransaction_fn).call(this, transaction, signer, "register blob");
528
564
  const createdObjectIds = effects?.created?.map((effect) => effect.reference.objectId) ?? [];
529
565
  const createdObjects = await __privateGet(this, _suiClient).multiGetObjects({
@@ -533,7 +569,7 @@ class WalrusClient {
533
569
  showBcs: true
534
570
  }
535
571
  });
536
- const suiBlobObject = createdObjects.find((object) => object.data?.type === this.blobType);
572
+ const suiBlobObject = createdObjects.find((object) => object.data?.type === blobType);
537
573
  if (!suiBlobObject || suiBlobObject.data?.bcs?.dataType !== "moveObject") {
538
574
  throw new import_error.WalrusClientError("Blob object not found in transaction effects");
539
575
  }
@@ -552,7 +588,7 @@ class WalrusClient {
552
588
  */
553
589
  async certifyBlob({ blobId, blobObjectId, confirmations, deletable }) {
554
590
  const systemState = await this.systemState();
555
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
591
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
556
592
  if (confirmations.length !== systemState.committee.members.length) {
557
593
  throw new import_error.WalrusClientError(
558
594
  "Invalid number of confirmations. Confirmations array must contain an entry for each node"
@@ -583,7 +619,7 @@ class WalrusClient {
583
619
  ...confirmation
584
620
  } : null;
585
621
  }).filter((confirmation) => confirmation !== null);
586
- if (!(0, import_utils.isQuorum)(filteredConfirmations.length, systemState.committee.members.length)) {
622
+ if (!(0, import_utils2.isQuorum)(filteredConfirmations.length, systemState.committee.members.length)) {
587
623
  throw new import_error.NotEnoughBlobConfirmationsError(
588
624
  `Too many invalid confirmations received for blob (${filteredConfirmations.length} of ${systemState.committee.members.length})`
589
625
  );
@@ -592,15 +628,16 @@ class WalrusClient {
592
628
  filteredConfirmations,
593
629
  filteredConfirmations.map(({ index }) => index)
594
630
  );
631
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
595
632
  return (tx) => {
596
- this.systemContract.certify_blob({
633
+ systemContract.certify_blob({
597
634
  arguments: [
598
- tx.object(this.packageConfig.systemObjectId),
635
+ tx.object(__privateGet(this, _packageConfig).systemObjectId),
599
636
  tx.object(blobObjectId),
600
637
  tx.pure.vector("u8", (0, import_bcs.fromBase64)(combinedSignature.signature)),
601
638
  tx.pure.vector(
602
639
  "u8",
603
- (0, import_utils.signersToBitmap)(combinedSignature.signers, systemState.committee.members.length)
640
+ (0, import_utils2.signersToBitmap)(combinedSignature.signers, systemState.committee.members.length)
604
641
  ),
605
642
  tx.pure.vector("u8", combinedSignature.serializedMessage)
606
643
  ]
@@ -652,10 +689,10 @@ class WalrusClient {
652
689
  */
653
690
  deleteBlob({ blobObjectId }) {
654
691
  return (tx) => tx.moveCall({
655
- package: this.packageConfig.systemObjectId,
692
+ package: __privateGet(this, _packageConfig).systemObjectId,
656
693
  module: "system",
657
694
  function: "delete_blob",
658
- arguments: [tx.object(this.packageConfig.systemObjectId), tx.object(blobObjectId)]
695
+ arguments: [tx.object(__privateGet(this, _packageConfig).systemObjectId), tx.object(blobObjectId)]
659
696
  });
660
697
  }
661
698
  /**
@@ -702,7 +739,7 @@ class WalrusClient {
702
739
  * const tx = await client.extendBlobTransaction({ blobObjectId, epochs });
703
740
  * ```
704
741
  */
705
- async extendBlob({ blobObjectId, epochs, endEpoch, walCoin }) {
742
+ async extendBlob({ blobObjectId, epochs, endEpoch, walCoin, owner }) {
706
743
  const blob = await __privateGet(this, _objectLoader).load(blobObjectId, (0, import_blob.Blob)());
707
744
  const numEpochs = typeof epochs === "number" ? epochs : endEpoch - blob.storage.end_epoch;
708
745
  if (numEpochs <= 0) {
@@ -710,29 +747,23 @@ class WalrusClient {
710
747
  };
711
748
  }
712
749
  const { storageCost } = await this.storageCost(Number(blob.storage.storage_size), numEpochs);
713
- return (tx) => {
714
- const coin = walCoin ? tx.splitCoins(walCoin, [storageCost])[0] : tx.add(
715
- (0, import_transactions.coinWithBalance)({
716
- balance: storageCost,
717
- type: this.walType
718
- })
719
- );
750
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
751
+ const subsidiesContract = __privateGet(this, _packageConfig).subsidiesObjectId ? await __privateGet(this, _getSubsidiesContract).call(this) : null;
752
+ return __privateMethod(this, _WalrusClient_instances, withWal_fn).call(this, storageCost, owner, walCoin ?? null, subsidiesContract !== null, (coin, tx) => {
720
753
  tx.add(
721
- this.systemContract.extend_blob({
754
+ subsidiesContract ? subsidiesContract.extend_blob({
722
755
  arguments: [
723
- tx.object(this.packageConfig.systemObjectId),
724
- tx.object(blobObjectId),
756
+ __privateGet(this, _packageConfig).subsidiesObjectId,
757
+ __privateGet(this, _packageConfig).systemObjectId,
758
+ blobObjectId,
725
759
  numEpochs,
726
760
  coin
727
761
  ]
762
+ }) : systemContract.extend_blob({
763
+ arguments: [__privateGet(this, _packageConfig).systemObjectId, blobObjectId, numEpochs, coin]
728
764
  })
729
765
  );
730
- tx.moveCall({
731
- target: "0x2::coin::destroy_zero",
732
- typeArguments: [this.walType],
733
- arguments: [coin]
734
- });
735
- };
766
+ });
736
767
  }
737
768
  /**
738
769
  * Create a transaction that extends a blob
@@ -798,9 +829,13 @@ class WalrusClient {
798
829
  */
799
830
  async writeBlobAttributes({ blobObject, blobObjectId, attributes }) {
800
831
  const existingAttributes = blobObjectId ? await this.readBlobAttributes({ blobObjectId }) : null;
832
+ const writeAttributes = await __privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, {
833
+ attributes,
834
+ existingAttributes
835
+ });
801
836
  return (tx) => {
802
837
  const blob = blobObject ?? tx.object(blobObjectId);
803
- tx.add(__privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, { blob, attributes, existingAttributes }));
838
+ tx.add((tx2) => writeAttributes(tx2, blob));
804
839
  };
805
840
  }
806
841
  /**
@@ -849,8 +884,8 @@ class WalrusClient {
849
884
  */
850
885
  async writeSliver({ blobId, sliverPairIndex, sliverType, sliver, signal }) {
851
886
  const systemState = await this.systemState();
852
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
853
- const shardIndex = (0, import_utils.toShardIndex)(sliverPairIndex, blobId, systemState.committee.n_shards);
887
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
888
+ const shardIndex = (0, import_utils2.toShardIndex)(sliverPairIndex, blobId, systemState.committee.n_shards);
854
889
  const node = await __privateMethod(this, _WalrusClient_instances, getNodeByShardIndex_fn).call(this, committee, shardIndex);
855
890
  return await __privateGet(this, _storageNodeClient).storeSliver(
856
891
  { blobId, sliverPairIndex, sliverType, sliver },
@@ -866,7 +901,7 @@ class WalrusClient {
866
901
  * ```
867
902
  */
868
903
  async writeMetadataToNode({ nodeIndex, blobId, metadata, signal }) {
869
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
904
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
870
905
  const node = committee.nodes[nodeIndex];
871
906
  return await __privateGet(this, _storageNodeClient).storeBlobMetadata(
872
907
  { blobId, metadata },
@@ -888,7 +923,7 @@ class WalrusClient {
888
923
  objectId,
889
924
  signal
890
925
  }) {
891
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
926
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
892
927
  const node = committee.nodes[nodeIndex];
893
928
  const result = deletable ? await __privateGet(this, _storageNodeClient).getDeletableBlobConfirmation(
894
929
  { blobId, objectId },
@@ -909,14 +944,14 @@ class WalrusClient {
909
944
  */
910
945
  async encodeBlob(blob) {
911
946
  const systemState = await this.systemState();
912
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
947
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
913
948
  const numShards = systemState.committee.n_shards;
914
949
  const { blobId, metadata, sliverPairs, rootHash } = (0, import_wasm.encodeBlob)(numShards, blob);
915
950
  const sliversByNodeMap = /* @__PURE__ */ new Map();
916
951
  while (sliverPairs.length > 0) {
917
952
  const { primary, secondary } = sliverPairs.pop();
918
953
  const sliverPairIndex = primary.index;
919
- const shardIndex = (0, import_utils.toShardIndex)(sliverPairIndex, blobId, numShards);
954
+ const shardIndex = (0, import_utils2.toShardIndex)(sliverPairIndex, blobId, numShards);
920
955
  const node = await __privateMethod(this, _WalrusClient_instances, getNodeByShardIndex_fn).call(this, committee, shardIndex);
921
956
  if (!sliversByNodeMap.has(node.nodeIndex)) {
922
957
  sliversByNodeMap.set(node.nodeIndex, { primary: [], secondary: [] });
@@ -990,7 +1025,7 @@ class WalrusClient {
990
1025
  ...options
991
1026
  }) {
992
1027
  const systemState = await this.systemState();
993
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
1028
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
994
1029
  const controller = new AbortController();
995
1030
  let failures = 0;
996
1031
  const confirmations = await Promise.all(
@@ -1004,7 +1039,7 @@ class WalrusClient {
1004
1039
  ...options
1005
1040
  }).catch(() => {
1006
1041
  failures += committee.nodes[nodeIndex].shardIndices.length;
1007
- if ((0, import_utils.isAboveValidity)(failures, systemState.committee.n_shards)) {
1042
+ if ((0, import_utils2.isAboveValidity)(failures, systemState.committee.n_shards)) {
1008
1043
  controller.abort();
1009
1044
  throw new import_error.NotEnoughBlobConfirmationsError(
1010
1045
  `Too many failures while writing blob ${blobId} to nodes`
@@ -1070,7 +1105,7 @@ class WalrusClient {
1070
1105
  blobId,
1071
1106
  rootHash,
1072
1107
  deletable,
1073
- owner,
1108
+ owner: owner ?? signer.toSuiAddress(),
1074
1109
  attributes
1075
1110
  });
1076
1111
  const blobObjectId = suiBlobObject.blob.id.id;
@@ -1104,23 +1139,25 @@ class WalrusClient {
1104
1139
  */
1105
1140
  reset() {
1106
1141
  __privateGet(this, _objectLoader).clearAll();
1107
- __privateSet(this, _activeCommittee, null);
1108
1142
  __privateSet(this, _readCommittee, null);
1143
+ __privateGet(this, _memo).reset();
1109
1144
  }
1110
1145
  }
1111
1146
  _storageNodeClient = new WeakMap();
1147
+ _packageConfig = new WeakMap();
1112
1148
  _suiClient = new WeakMap();
1113
1149
  _objectLoader = new WeakMap();
1114
1150
  _blobMetadataConcurrencyLimit = new WeakMap();
1115
- _activeCommittee = new WeakMap();
1116
1151
  _readCommittee = new WeakMap();
1152
+ _memo = new WeakMap();
1153
+ _walType = new WeakMap();
1154
+ _getPackageId = new WeakMap();
1155
+ _getBlobType = new WeakMap();
1156
+ _getSystemContract = new WeakMap();
1157
+ _getSubsidiesContract = new WeakMap();
1158
+ _getBlobContract = new WeakMap();
1159
+ _getMetadataContract = new WeakMap();
1117
1160
  _WalrusClient_instances = new WeakSet();
1118
- blobContract_get = function() {
1119
- return (0, import_blob.init)(this.packageConfig.packageId);
1120
- };
1121
- metadataContract_get = function() {
1122
- return (0, import_metadata.init)(this.packageConfig.packageId);
1123
- };
1124
1161
  internalReadBlob_fn = async function({ blobId, signal }) {
1125
1162
  const systemState = await this.systemState();
1126
1163
  const numShards = systemState.committee.n_shards;
@@ -1171,20 +1208,47 @@ forceGetReadCommittee_fn = async function({ blobId, signal }) {
1171
1208
  if (isTransitioning && certificationEpoch < stakingState.epoch) {
1172
1209
  return await __privateMethod(this, _WalrusClient_instances, getCommittee_fn).call(this, stakingState.previous_committee);
1173
1210
  }
1174
- return await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
1211
+ return await __privateGet(this, _getActiveCommittee).call(this);
1175
1212
  };
1176
- writeBlobAttributesForRef_fn = function({
1177
- blob,
1213
+ withWal_fn = async function(amount, owner, source, withSubsidies, fn) {
1214
+ const walType = await __privateGet(this, _walType).call(this);
1215
+ return (tx) => {
1216
+ const coin = source ? tx.splitCoins(source, [amount])[0] : tx.add(
1217
+ (0, import_transactions.coinWithBalance)({
1218
+ balance: amount,
1219
+ type: walType
1220
+ })
1221
+ );
1222
+ const result = fn(coin, tx);
1223
+ if (withSubsidies) {
1224
+ if (source) {
1225
+ tx.mergeCoins(source, [coin]);
1226
+ } else {
1227
+ tx.transferObjects([coin], owner);
1228
+ }
1229
+ } else {
1230
+ tx.moveCall({
1231
+ target: "0x2::coin::destroy_zero",
1232
+ typeArguments: [walType],
1233
+ arguments: [coin]
1234
+ });
1235
+ }
1236
+ return result;
1237
+ };
1238
+ };
1239
+ writeBlobAttributesForRef_fn = async function({
1178
1240
  attributes,
1179
1241
  existingAttributes
1180
1242
  }) {
1181
- return (tx) => {
1243
+ const blobContract = await __privateGet(this, _getBlobContract).call(this);
1244
+ const metadataContract = await __privateGet(this, _getMetadataContract).call(this);
1245
+ return (tx, blob) => {
1182
1246
  if (!existingAttributes) {
1183
1247
  tx.add(
1184
- __privateGet(this, _WalrusClient_instances, blobContract_get).add_metadata({
1248
+ blobContract.add_metadata({
1185
1249
  arguments: [
1186
1250
  blob,
1187
- __privateGet(this, _WalrusClient_instances, metadataContract_get)._new({
1251
+ metadataContract._new({
1188
1252
  arguments: []
1189
1253
  })
1190
1254
  ]
@@ -1196,14 +1260,14 @@ writeBlobAttributesForRef_fn = function({
1196
1260
  if (value === null) {
1197
1261
  if (existingAttributes && key in existingAttributes) {
1198
1262
  tx.add(
1199
- __privateGet(this, _WalrusClient_instances, blobContract_get).remove_metadata_pair({
1263
+ blobContract.remove_metadata_pair({
1200
1264
  arguments: [blob, key]
1201
1265
  })
1202
1266
  );
1203
1267
  }
1204
1268
  } else {
1205
1269
  tx.add(
1206
- __privateGet(this, _WalrusClient_instances, blobContract_get).insert_or_update_metadata_pair({
1270
+ blobContract.insert_or_update_metadata_pair({
1207
1271
  arguments: [blob, key, value]
1208
1272
  })
1209
1273
  );
@@ -1229,7 +1293,7 @@ executeTransaction_fn = async function(transaction, signer, action) {
1229
1293
  };
1230
1294
  getCommittee_fn = async function(committee) {
1231
1295
  const stakingPool = await __privateMethod(this, _WalrusClient_instances, stakingPool_fn).call(this, committee);
1232
- const shardIndicesByNodeId = (0, import_utils.getShardIndicesByNodeId)(committee);
1296
+ const shardIndicesByNodeId = (0, import_utils2.getShardIndicesByNodeId)(committee);
1233
1297
  const byShardIndex = /* @__PURE__ */ new Map();
1234
1298
  const nodes = stakingPool.map(({ node_info }, nodeIndex) => {
1235
1299
  const shardIndices = shardIndicesByNodeId.get(node_info.node_id) ?? [];
@@ -1250,14 +1314,7 @@ getCommittee_fn = async function(committee) {
1250
1314
  nodes
1251
1315
  };
1252
1316
  };
1253
- getActiveCommittee_fn = async function() {
1254
- if (!__privateGet(this, _activeCommittee)) {
1255
- const stakingState = await this.stakingState();
1256
- __privateSet(this, _activeCommittee, __privateMethod(this, _WalrusClient_instances, getCommittee_fn).call(this, stakingState.committee));
1257
- __privateSet(this, _activeCommittee, await __privateGet(this, _activeCommittee));
1258
- }
1259
- return __privateGet(this, _activeCommittee);
1260
- };
1317
+ _getActiveCommittee = new WeakMap();
1261
1318
  stakingPool_fn = async function(committee) {
1262
1319
  const nodeIds = committee.pos0.contents.map((node) => node.key);
1263
1320
  return __privateGet(this, _objectLoader).loadManyOrThrow(nodeIds, (0, import_staking_pool.StakingPool)());