@mysten/walrus 0.0.3 → 0.0.4

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 +7 -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
@@ -6,18 +6,24 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
6
6
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
7
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
8
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
- 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;
9
+ 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;
10
10
  import { bcs, fromBase64 } from "@mysten/bcs";
11
11
  import { SuiClient } from "@mysten/sui/client";
12
12
  import { coinWithBalance, Transaction } from "@mysten/sui/transactions";
13
+ import { normalizeStructTag, parseStructTag } from "@mysten/sui/utils";
13
14
  import { bls12381_min_pk_verify } from "@mysten/walrus-wasm";
14
- import { statusLifecycleRank, TESTNET_WALRUS_PACKAGE_CONFIG } from "./constants.js";
15
+ import {
16
+ MAINNET_WALRUS_PACKAGE_CONFIG,
17
+ statusLifecycleRank,
18
+ TESTNET_WALRUS_PACKAGE_CONFIG
19
+ } from "./constants.js";
15
20
  import { Blob, init as initBlobContract } from "./contracts/blob.js";
16
21
  import { init as initMetadataContract } from "./contracts/metadata.js";
17
22
  import { StakingInnerV1 } from "./contracts/staking_inner.js";
18
23
  import { StakingPool } from "./contracts/staking_pool.js";
19
24
  import { Staking } from "./contracts/staking.js";
20
25
  import { Storage } from "./contracts/storage_resource.js";
26
+ import { init as initSubsidiesContract } from "./contracts/subsidies.js";
21
27
  import { SystemStateInnerV1 } from "./contracts/system_state_inner.js";
22
28
  import { init as initSystemContract, System } from "./contracts/system.js";
23
29
  import {
@@ -52,8 +58,10 @@ import {
52
58
  signersToBitmap,
53
59
  storageUnitsFromSize,
54
60
  toPairIndex,
55
- toShardIndex
61
+ toShardIndex,
62
+ toTypeString
56
63
  } from "./utils/index.js";
64
+ import { MemoCache } from "./utils/memo.js";
57
65
  import { SuiObjectDataLoader } from "./utils/object-loader.js";
58
66
  import { shuffle, weightedShuffle } from "./utils/randomness.js";
59
67
  import { combineSignatures, computeMetadata, decodePrimarySlivers, encodeBlob } from "./wasm.js";
@@ -61,24 +69,78 @@ class WalrusClient {
61
69
  constructor(config) {
62
70
  __privateAdd(this, _WalrusClient_instances);
63
71
  __privateAdd(this, _storageNodeClient);
72
+ __privateAdd(this, _packageConfig);
64
73
  __privateAdd(this, _suiClient);
65
74
  __privateAdd(this, _objectLoader);
66
75
  __privateAdd(this, _blobMetadataConcurrencyLimit, 10);
67
- __privateAdd(this, _activeCommittee);
68
76
  __privateAdd(this, _readCommittee);
77
+ __privateAdd(this, _memo, new MemoCache());
78
+ /** The Move type for a WAL coin */
79
+ __privateAdd(this, _walType, __privateGet(this, _memo).create("walType", async () => {
80
+ const stakedWal = await __privateGet(this, _suiClient).getNormalizedMoveStruct({
81
+ package: await __privateGet(this, _getPackageId).call(this),
82
+ module: "staked_wal",
83
+ struct: "StakedWal"
84
+ });
85
+ const balanceType = stakedWal.fields.find((field) => field.name === "principal")?.type;
86
+ if (!balanceType) {
87
+ throw new WalrusClientError("WAL type not found");
88
+ }
89
+ const parsed = parseStructTag(toTypeString(balanceType));
90
+ const coinType = parsed.typeParams[0];
91
+ if (!coinType) {
92
+ throw new WalrusClientError("WAL type not found");
93
+ }
94
+ return normalizeStructTag(coinType);
95
+ }));
96
+ __privateAdd(this, _getPackageId, __privateGet(this, _memo).create("getPackageId", async () => {
97
+ const system = await __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).systemObjectId);
98
+ return parseStructTag(system.type).address;
99
+ }));
100
+ /** The Move type for a Blob object */
101
+ __privateAdd(this, _getBlobType, __privateGet(this, _memo).create("getBlobType", async () => {
102
+ return `${await __privateGet(this, _getPackageId).call(this)}::blob::Blob`;
103
+ }));
104
+ __privateAdd(this, _getSystemContract, __privateGet(this, _memo).create("getSystemContract", async () => {
105
+ const { package_id } = await this.systemObject();
106
+ return initSystemContract(package_id);
107
+ }));
108
+ __privateAdd(this, _getSubsidiesContract, __privateGet(this, _memo).create("getSubsidiesContract", async () => {
109
+ if (!__privateGet(this, _packageConfig).subsidiesObjectId) {
110
+ throw new WalrusClientError("Subsidies object ID not defined in package config");
111
+ }
112
+ const subsidiesObject = await __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).subsidiesObjectId);
113
+ const packageId = parseStructTag(subsidiesObject.type).address;
114
+ return initSubsidiesContract(packageId);
115
+ }));
116
+ __privateAdd(this, _getBlobContract, __privateGet(this, _memo).create("getBlobContract", async () => {
117
+ const { package_id } = await this.systemObject();
118
+ return initBlobContract(package_id);
119
+ }));
120
+ __privateAdd(this, _getMetadataContract, __privateGet(this, _memo).create("getMetadataContract", async () => {
121
+ const { package_id } = await this.systemObject();
122
+ return initMetadataContract(package_id);
123
+ }));
69
124
  /** Read a blob from the storage nodes */
70
125
  this.readBlob = __privateMethod(this, _WalrusClient_instances, retryOnPossibleEpochChange_fn).call(this, __privateMethod(this, _WalrusClient_instances, internalReadBlob_fn));
126
+ __privateAdd(this, _getActiveCommittee, __privateGet(this, _memo).create("getActiveCommittee", async () => {
127
+ const stakingState = await this.stakingState();
128
+ return __privateMethod(this, _WalrusClient_instances, getCommittee_fn).call(this, stakingState.committee);
129
+ }));
71
130
  if (config.network && !config.packageConfig) {
72
131
  const network = config.network;
73
132
  switch (network) {
74
133
  case "testnet":
75
- this.packageConfig = TESTNET_WALRUS_PACKAGE_CONFIG;
134
+ __privateSet(this, _packageConfig, TESTNET_WALRUS_PACKAGE_CONFIG);
135
+ break;
136
+ case "mainnet":
137
+ __privateSet(this, _packageConfig, MAINNET_WALRUS_PACKAGE_CONFIG);
76
138
  break;
77
139
  default:
78
140
  throw new WalrusClientError(`Unsupported network: ${network}`);
79
141
  }
80
142
  } else {
81
- this.packageConfig = config.packageConfig;
143
+ __privateSet(this, _packageConfig, config.packageConfig);
82
144
  }
83
145
  __privateSet(this, _suiClient, config.suiClient ?? new SuiClient({
84
146
  url: config.suiRpcUrl
@@ -86,42 +148,27 @@ class WalrusClient {
86
148
  __privateSet(this, _storageNodeClient, new StorageNodeClient(config.storageNodeClientOptions));
87
149
  __privateSet(this, _objectLoader, new SuiObjectDataLoader(__privateGet(this, _suiClient)));
88
150
  }
89
- /** The Move type for a WAL coin */
90
- get walType() {
91
- return `${this.packageConfig.walPackageId}::wal::WAL`;
92
- }
93
- /** The Move type for a Blob object */
94
- get blobType() {
95
- return `${this.packageConfig.packageId}::blob::Blob`;
96
- }
97
- /** The Move type for a Storage object */
98
- get storageType() {
99
- return `${this.packageConfig.packageId}::storage_resource::Storage`;
100
- }
101
- get systemContract() {
102
- return initSystemContract(this.packageConfig.latestPackageId);
103
- }
104
151
  /** The cached system object for the walrus package */
105
152
  systemObject() {
106
- return __privateGet(this, _objectLoader).load(this.packageConfig.systemObjectId, System());
153
+ return __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).systemObjectId, System());
107
154
  }
108
155
  /** The cached staking pool object for the walrus package */
109
156
  stakingObject() {
110
- return __privateGet(this, _objectLoader).load(this.packageConfig.stakingPoolId, Staking());
157
+ return __privateGet(this, _objectLoader).load(__privateGet(this, _packageConfig).stakingPoolId, Staking());
111
158
  }
112
159
  /** The system state for the current version of walrus contract */
113
160
  async systemState() {
114
161
  const systemState = await __privateGet(this, _objectLoader).loadFieldObject(
115
- this.packageConfig.systemObjectId,
162
+ __privateGet(this, _packageConfig).systemObjectId,
116
163
  { type: "u64", value: (await this.systemObject()).version },
117
- this.packageConfig.systemObjectId === TESTNET_WALRUS_PACKAGE_CONFIG.systemObjectId ? TestnetSystemStateInnerV1 : SystemStateInnerV1()
164
+ __privateGet(this, _packageConfig).systemObjectId === TESTNET_WALRUS_PACKAGE_CONFIG.systemObjectId ? TestnetSystemStateInnerV1 : SystemStateInnerV1()
118
165
  );
119
166
  return systemState;
120
167
  }
121
168
  /** The staking state for the current version of walrus contract */
122
169
  async stakingState() {
123
170
  return __privateGet(this, _objectLoader).loadFieldObject(
124
- this.packageConfig.stakingPoolId,
171
+ __privateGet(this, _packageConfig).stakingPoolId,
125
172
  {
126
173
  type: "u64",
127
174
  value: (await this.stakingObject()).version
@@ -275,7 +322,7 @@ class WalrusClient {
275
322
  * Gets the blob status from multiple storage nodes and returns the latest status that can be verified.
276
323
  */
277
324
  async getVerifiedBlobStatus({ blobId, signal }) {
278
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
325
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
279
326
  const stakingState = await this.stakingState();
280
327
  const numShards = stakingState.n_shards;
281
328
  const controller = new AbortController();
@@ -370,30 +417,28 @@ class WalrusClient {
370
417
  * tx.transferObjects([await client.createStorage({ size: 1000, epochs: 3 })], owner);
371
418
  * ```
372
419
  */
373
- async createStorage({ size, epochs, walCoin }) {
420
+ async createStorage({ size, epochs, walCoin, owner }) {
374
421
  const systemObject = await this.systemObject();
375
422
  const systemState = await this.systemState();
376
423
  const encodedSize = encodedBlobLength(size, systemState.committee.n_shards);
377
424
  const { storageCost } = await this.storageCost(size, epochs);
378
- return (tx) => {
379
- const coin = walCoin ? tx.splitCoins(walCoin, [storageCost])[0] : tx.add(
380
- coinWithBalance({
381
- balance: storageCost,
382
- type: this.walType
383
- })
384
- );
385
- const storage = tx.add(
386
- this.systemContract.reserve_space({
425
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
426
+ const subsidiesContract = __privateGet(this, _packageConfig).subsidiesObjectId ? await __privateGet(this, _getSubsidiesContract).call(this) : null;
427
+ return __privateMethod(this, _WalrusClient_instances, withWal_fn).call(this, storageCost, owner, walCoin ?? null, subsidiesContract !== null, (coin, tx) => {
428
+ return tx.add(
429
+ subsidiesContract ? subsidiesContract.reserve_space({
430
+ arguments: [
431
+ __privateGet(this, _packageConfig).subsidiesObjectId,
432
+ systemObject.id.id,
433
+ encodedSize,
434
+ epochs,
435
+ coin
436
+ ]
437
+ }) : systemContract.reserve_space({
387
438
  arguments: [systemObject.id.id, encodedSize, epochs, coin]
388
439
  })
389
440
  );
390
- tx.moveCall({
391
- target: "0x2::coin::destroy_zero",
392
- typeArguments: [this.walType],
393
- arguments: [coin]
394
- });
395
- return storage;
396
- };
441
+ });
397
442
  }
398
443
  /**
399
444
  * Create a transaction that creates a storage object
@@ -409,7 +454,7 @@ class WalrusClient {
409
454
  epochs,
410
455
  owner
411
456
  }) {
412
- transaction.transferObjects([await this.createStorage({ size, epochs })], owner);
457
+ transaction.transferObjects([await this.createStorage({ size, epochs, owner })], owner);
413
458
  return transaction;
414
459
  }
415
460
  /**
@@ -428,6 +473,7 @@ class WalrusClient {
428
473
  ...options,
429
474
  owner: options.transaction?.getData().sender ?? signer.toSuiAddress()
430
475
  });
476
+ const blobType = await __privateGet(this, _getBlobType).call(this);
431
477
  const { digest, effects } = await __privateMethod(this, _WalrusClient_instances, executeTransaction_fn).call(this, transaction, signer, "create storage");
432
478
  const createdObjectIds = effects?.created?.map((effect) => effect.reference.objectId) ?? [];
433
479
  const createdObjects = await __privateGet(this, _suiClient).multiGetObjects({
@@ -437,7 +483,7 @@ class WalrusClient {
437
483
  showBcs: true
438
484
  }
439
485
  });
440
- const suiBlobObject = createdObjects.find((object) => object.data?.type === this.blobType);
486
+ const suiBlobObject = createdObjects.find((object) => object.data?.type === blobType);
441
487
  if (!suiBlobObject || suiBlobObject.data?.bcs?.dataType !== "moveObject") {
442
488
  throw new WalrusClientError("Storage object not found in transaction effects");
443
489
  }
@@ -461,21 +507,21 @@ class WalrusClient {
461
507
  rootHash,
462
508
  deletable,
463
509
  walCoin,
510
+ owner,
464
511
  attributes
465
512
  }) {
466
- const storage = await this.createStorage({ size, epochs, walCoin });
513
+ const storage = await this.createStorage({ size, epochs, walCoin, owner });
467
514
  const { writeCost } = await this.storageCost(size, epochs);
468
- return (tx) => {
469
- const writeCoin = walCoin ? tx.splitCoins(walCoin, [writeCost])[0] : tx.add(
470
- coinWithBalance({
471
- balance: writeCost,
472
- type: this.walType
473
- })
474
- );
515
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
516
+ const writeAttributes = attributes ? await __privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, {
517
+ attributes,
518
+ existingAttributes: null
519
+ }) : null;
520
+ return __privateMethod(this, _WalrusClient_instances, withWal_fn).call(this, writeCost, owner, walCoin ?? null, false, (writeCoin, tx) => {
475
521
  const blob = tx.add(
476
- this.systemContract.register_blob({
522
+ systemContract.register_blob({
477
523
  arguments: [
478
- tx.object(this.packageConfig.systemObjectId),
524
+ tx.object(__privateGet(this, _packageConfig).systemObjectId),
479
525
  storage,
480
526
  blobIdToInt(blobId),
481
527
  BigInt(bcs.u256().parse(rootHash)),
@@ -486,16 +532,11 @@ class WalrusClient {
486
532
  ]
487
533
  })
488
534
  );
489
- tx.moveCall({
490
- target: "0x2::coin::destroy_zero",
491
- typeArguments: [this.walType],
492
- arguments: [writeCoin]
493
- });
494
- if (attributes) {
495
- tx.add(__privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, { blob, attributes, existingAttributes: null }));
535
+ if (writeAttributes) {
536
+ tx.add((tx2) => writeAttributes(tx2, blob));
496
537
  }
497
538
  return blob;
498
- };
539
+ });
499
540
  }
500
541
  /**
501
542
  * Create a transaction that registers a blob
@@ -507,11 +548,10 @@ class WalrusClient {
507
548
  */
508
549
  async registerBlobTransaction({
509
550
  transaction = new Transaction(),
510
- owner,
511
551
  ...options
512
552
  }) {
513
553
  const registration = transaction.add(await this.registerBlob(options));
514
- transaction.transferObjects([registration], owner);
554
+ transaction.transferObjects([registration], options.owner);
515
555
  return transaction;
516
556
  }
517
557
  /**
@@ -530,6 +570,7 @@ class WalrusClient {
530
570
  ...options,
531
571
  owner: options.owner ?? options.transaction?.getData().sender ?? signer.toSuiAddress()
532
572
  });
573
+ const blobType = await __privateGet(this, _getBlobType).call(this);
533
574
  const { digest, effects } = await __privateMethod(this, _WalrusClient_instances, executeTransaction_fn).call(this, transaction, signer, "register blob");
534
575
  const createdObjectIds = effects?.created?.map((effect) => effect.reference.objectId) ?? [];
535
576
  const createdObjects = await __privateGet(this, _suiClient).multiGetObjects({
@@ -539,7 +580,7 @@ class WalrusClient {
539
580
  showBcs: true
540
581
  }
541
582
  });
542
- const suiBlobObject = createdObjects.find((object) => object.data?.type === this.blobType);
583
+ const suiBlobObject = createdObjects.find((object) => object.data?.type === blobType);
543
584
  if (!suiBlobObject || suiBlobObject.data?.bcs?.dataType !== "moveObject") {
544
585
  throw new WalrusClientError("Blob object not found in transaction effects");
545
586
  }
@@ -558,7 +599,7 @@ class WalrusClient {
558
599
  */
559
600
  async certifyBlob({ blobId, blobObjectId, confirmations, deletable }) {
560
601
  const systemState = await this.systemState();
561
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
602
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
562
603
  if (confirmations.length !== systemState.committee.members.length) {
563
604
  throw new WalrusClientError(
564
605
  "Invalid number of confirmations. Confirmations array must contain an entry for each node"
@@ -598,10 +639,11 @@ class WalrusClient {
598
639
  filteredConfirmations,
599
640
  filteredConfirmations.map(({ index }) => index)
600
641
  );
642
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
601
643
  return (tx) => {
602
- this.systemContract.certify_blob({
644
+ systemContract.certify_blob({
603
645
  arguments: [
604
- tx.object(this.packageConfig.systemObjectId),
646
+ tx.object(__privateGet(this, _packageConfig).systemObjectId),
605
647
  tx.object(blobObjectId),
606
648
  tx.pure.vector("u8", fromBase64(combinedSignature.signature)),
607
649
  tx.pure.vector(
@@ -658,10 +700,10 @@ class WalrusClient {
658
700
  */
659
701
  deleteBlob({ blobObjectId }) {
660
702
  return (tx) => tx.moveCall({
661
- package: this.packageConfig.systemObjectId,
703
+ package: __privateGet(this, _packageConfig).systemObjectId,
662
704
  module: "system",
663
705
  function: "delete_blob",
664
- arguments: [tx.object(this.packageConfig.systemObjectId), tx.object(blobObjectId)]
706
+ arguments: [tx.object(__privateGet(this, _packageConfig).systemObjectId), tx.object(blobObjectId)]
665
707
  });
666
708
  }
667
709
  /**
@@ -708,7 +750,7 @@ class WalrusClient {
708
750
  * const tx = await client.extendBlobTransaction({ blobObjectId, epochs });
709
751
  * ```
710
752
  */
711
- async extendBlob({ blobObjectId, epochs, endEpoch, walCoin }) {
753
+ async extendBlob({ blobObjectId, epochs, endEpoch, walCoin, owner }) {
712
754
  const blob = await __privateGet(this, _objectLoader).load(blobObjectId, Blob());
713
755
  const numEpochs = typeof epochs === "number" ? epochs : endEpoch - blob.storage.end_epoch;
714
756
  if (numEpochs <= 0) {
@@ -716,29 +758,23 @@ class WalrusClient {
716
758
  };
717
759
  }
718
760
  const { storageCost } = await this.storageCost(Number(blob.storage.storage_size), numEpochs);
719
- return (tx) => {
720
- const coin = walCoin ? tx.splitCoins(walCoin, [storageCost])[0] : tx.add(
721
- coinWithBalance({
722
- balance: storageCost,
723
- type: this.walType
724
- })
725
- );
761
+ const systemContract = await __privateGet(this, _getSystemContract).call(this);
762
+ const subsidiesContract = __privateGet(this, _packageConfig).subsidiesObjectId ? await __privateGet(this, _getSubsidiesContract).call(this) : null;
763
+ return __privateMethod(this, _WalrusClient_instances, withWal_fn).call(this, storageCost, owner, walCoin ?? null, subsidiesContract !== null, (coin, tx) => {
726
764
  tx.add(
727
- this.systemContract.extend_blob({
765
+ subsidiesContract ? subsidiesContract.extend_blob({
728
766
  arguments: [
729
- tx.object(this.packageConfig.systemObjectId),
730
- tx.object(blobObjectId),
767
+ __privateGet(this, _packageConfig).subsidiesObjectId,
768
+ __privateGet(this, _packageConfig).systemObjectId,
769
+ blobObjectId,
731
770
  numEpochs,
732
771
  coin
733
772
  ]
773
+ }) : systemContract.extend_blob({
774
+ arguments: [__privateGet(this, _packageConfig).systemObjectId, blobObjectId, numEpochs, coin]
734
775
  })
735
776
  );
736
- tx.moveCall({
737
- target: "0x2::coin::destroy_zero",
738
- typeArguments: [this.walType],
739
- arguments: [coin]
740
- });
741
- };
777
+ });
742
778
  }
743
779
  /**
744
780
  * Create a transaction that extends a blob
@@ -804,9 +840,13 @@ class WalrusClient {
804
840
  */
805
841
  async writeBlobAttributes({ blobObject, blobObjectId, attributes }) {
806
842
  const existingAttributes = blobObjectId ? await this.readBlobAttributes({ blobObjectId }) : null;
843
+ const writeAttributes = await __privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, {
844
+ attributes,
845
+ existingAttributes
846
+ });
807
847
  return (tx) => {
808
848
  const blob = blobObject ?? tx.object(blobObjectId);
809
- tx.add(__privateMethod(this, _WalrusClient_instances, writeBlobAttributesForRef_fn).call(this, { blob, attributes, existingAttributes }));
849
+ tx.add((tx2) => writeAttributes(tx2, blob));
810
850
  };
811
851
  }
812
852
  /**
@@ -855,7 +895,7 @@ class WalrusClient {
855
895
  */
856
896
  async writeSliver({ blobId, sliverPairIndex, sliverType, sliver, signal }) {
857
897
  const systemState = await this.systemState();
858
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
898
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
859
899
  const shardIndex = toShardIndex(sliverPairIndex, blobId, systemState.committee.n_shards);
860
900
  const node = await __privateMethod(this, _WalrusClient_instances, getNodeByShardIndex_fn).call(this, committee, shardIndex);
861
901
  return await __privateGet(this, _storageNodeClient).storeSliver(
@@ -872,7 +912,7 @@ class WalrusClient {
872
912
  * ```
873
913
  */
874
914
  async writeMetadataToNode({ nodeIndex, blobId, metadata, signal }) {
875
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
915
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
876
916
  const node = committee.nodes[nodeIndex];
877
917
  return await __privateGet(this, _storageNodeClient).storeBlobMetadata(
878
918
  { blobId, metadata },
@@ -894,7 +934,7 @@ class WalrusClient {
894
934
  objectId,
895
935
  signal
896
936
  }) {
897
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
937
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
898
938
  const node = committee.nodes[nodeIndex];
899
939
  const result = deletable ? await __privateGet(this, _storageNodeClient).getDeletableBlobConfirmation(
900
940
  { blobId, objectId },
@@ -915,7 +955,7 @@ class WalrusClient {
915
955
  */
916
956
  async encodeBlob(blob) {
917
957
  const systemState = await this.systemState();
918
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
958
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
919
959
  const numShards = systemState.committee.n_shards;
920
960
  const { blobId, metadata, sliverPairs, rootHash } = encodeBlob(numShards, blob);
921
961
  const sliversByNodeMap = /* @__PURE__ */ new Map();
@@ -996,7 +1036,7 @@ class WalrusClient {
996
1036
  ...options
997
1037
  }) {
998
1038
  const systemState = await this.systemState();
999
- const committee = await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
1039
+ const committee = await __privateGet(this, _getActiveCommittee).call(this);
1000
1040
  const controller = new AbortController();
1001
1041
  let failures = 0;
1002
1042
  const confirmations = await Promise.all(
@@ -1076,7 +1116,7 @@ class WalrusClient {
1076
1116
  blobId,
1077
1117
  rootHash,
1078
1118
  deletable,
1079
- owner,
1119
+ owner: owner ?? signer.toSuiAddress(),
1080
1120
  attributes
1081
1121
  });
1082
1122
  const blobObjectId = suiBlobObject.blob.id.id;
@@ -1110,23 +1150,25 @@ class WalrusClient {
1110
1150
  */
1111
1151
  reset() {
1112
1152
  __privateGet(this, _objectLoader).clearAll();
1113
- __privateSet(this, _activeCommittee, null);
1114
1153
  __privateSet(this, _readCommittee, null);
1154
+ __privateGet(this, _memo).reset();
1115
1155
  }
1116
1156
  }
1117
1157
  _storageNodeClient = new WeakMap();
1158
+ _packageConfig = new WeakMap();
1118
1159
  _suiClient = new WeakMap();
1119
1160
  _objectLoader = new WeakMap();
1120
1161
  _blobMetadataConcurrencyLimit = new WeakMap();
1121
- _activeCommittee = new WeakMap();
1122
1162
  _readCommittee = new WeakMap();
1163
+ _memo = new WeakMap();
1164
+ _walType = new WeakMap();
1165
+ _getPackageId = new WeakMap();
1166
+ _getBlobType = new WeakMap();
1167
+ _getSystemContract = new WeakMap();
1168
+ _getSubsidiesContract = new WeakMap();
1169
+ _getBlobContract = new WeakMap();
1170
+ _getMetadataContract = new WeakMap();
1123
1171
  _WalrusClient_instances = new WeakSet();
1124
- blobContract_get = function() {
1125
- return initBlobContract(this.packageConfig.packageId);
1126
- };
1127
- metadataContract_get = function() {
1128
- return initMetadataContract(this.packageConfig.packageId);
1129
- };
1130
1172
  internalReadBlob_fn = async function({ blobId, signal }) {
1131
1173
  const systemState = await this.systemState();
1132
1174
  const numShards = systemState.committee.n_shards;
@@ -1177,20 +1219,47 @@ forceGetReadCommittee_fn = async function({ blobId, signal }) {
1177
1219
  if (isTransitioning && certificationEpoch < stakingState.epoch) {
1178
1220
  return await __privateMethod(this, _WalrusClient_instances, getCommittee_fn).call(this, stakingState.previous_committee);
1179
1221
  }
1180
- return await __privateMethod(this, _WalrusClient_instances, getActiveCommittee_fn).call(this);
1222
+ return await __privateGet(this, _getActiveCommittee).call(this);
1181
1223
  };
1182
- writeBlobAttributesForRef_fn = function({
1183
- blob,
1224
+ withWal_fn = async function(amount, owner, source, withSubsidies, fn) {
1225
+ const walType = await __privateGet(this, _walType).call(this);
1226
+ return (tx) => {
1227
+ const coin = source ? tx.splitCoins(source, [amount])[0] : tx.add(
1228
+ coinWithBalance({
1229
+ balance: amount,
1230
+ type: walType
1231
+ })
1232
+ );
1233
+ const result = fn(coin, tx);
1234
+ if (withSubsidies) {
1235
+ if (source) {
1236
+ tx.mergeCoins(source, [coin]);
1237
+ } else {
1238
+ tx.transferObjects([coin], owner);
1239
+ }
1240
+ } else {
1241
+ tx.moveCall({
1242
+ target: "0x2::coin::destroy_zero",
1243
+ typeArguments: [walType],
1244
+ arguments: [coin]
1245
+ });
1246
+ }
1247
+ return result;
1248
+ };
1249
+ };
1250
+ writeBlobAttributesForRef_fn = async function({
1184
1251
  attributes,
1185
1252
  existingAttributes
1186
1253
  }) {
1187
- return (tx) => {
1254
+ const blobContract = await __privateGet(this, _getBlobContract).call(this);
1255
+ const metadataContract = await __privateGet(this, _getMetadataContract).call(this);
1256
+ return (tx, blob) => {
1188
1257
  if (!existingAttributes) {
1189
1258
  tx.add(
1190
- __privateGet(this, _WalrusClient_instances, blobContract_get).add_metadata({
1259
+ blobContract.add_metadata({
1191
1260
  arguments: [
1192
1261
  blob,
1193
- __privateGet(this, _WalrusClient_instances, metadataContract_get)._new({
1262
+ metadataContract._new({
1194
1263
  arguments: []
1195
1264
  })
1196
1265
  ]
@@ -1202,14 +1271,14 @@ writeBlobAttributesForRef_fn = function({
1202
1271
  if (value === null) {
1203
1272
  if (existingAttributes && key in existingAttributes) {
1204
1273
  tx.add(
1205
- __privateGet(this, _WalrusClient_instances, blobContract_get).remove_metadata_pair({
1274
+ blobContract.remove_metadata_pair({
1206
1275
  arguments: [blob, key]
1207
1276
  })
1208
1277
  );
1209
1278
  }
1210
1279
  } else {
1211
1280
  tx.add(
1212
- __privateGet(this, _WalrusClient_instances, blobContract_get).insert_or_update_metadata_pair({
1281
+ blobContract.insert_or_update_metadata_pair({
1213
1282
  arguments: [blob, key, value]
1214
1283
  })
1215
1284
  );
@@ -1256,14 +1325,7 @@ getCommittee_fn = async function(committee) {
1256
1325
  nodes
1257
1326
  };
1258
1327
  };
1259
- getActiveCommittee_fn = async function() {
1260
- if (!__privateGet(this, _activeCommittee)) {
1261
- const stakingState = await this.stakingState();
1262
- __privateSet(this, _activeCommittee, __privateMethod(this, _WalrusClient_instances, getCommittee_fn).call(this, stakingState.committee));
1263
- __privateSet(this, _activeCommittee, await __privateGet(this, _activeCommittee));
1264
- }
1265
- return __privateGet(this, _activeCommittee);
1266
- };
1328
+ _getActiveCommittee = new WeakMap();
1267
1329
  stakingPool_fn = async function(committee) {
1268
1330
  const nodeIds = committee.pos0.contents.map((node) => node.key);
1269
1331
  return __privateGet(this, _objectLoader).loadManyOrThrow(nodeIds, StakingPool());