@effectstream/midnight-contracts 0.100.12 → 0.100.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effectstream/midnight-contracts",
3
- "version": "0.100.12",
3
+ "version": "0.100.13",
4
4
  "license": "MIT",
5
5
  "homepage": "https://effectstream.github.io/docs/",
6
6
  "repository": {
@@ -20,7 +20,7 @@
20
20
  "./types": "./src/types.ts"
21
21
  },
22
22
  "dependencies": {
23
- "@effectstream/utils": "0.100.12",
23
+ "@effectstream/utils": "0.100.13",
24
24
  "@scure/bip39": "^2.0.1",
25
25
  "dotenv": "^16.4.7",
26
26
  "@midnight-ntwrk/wallet": "5.0.0",
@@ -250,7 +250,10 @@ export async function syncAndWaitForFunds(
250
250
 
251
251
  let dustBalance = 0n;
252
252
  try {
253
- dustBalance = state.dust?.balance?.(new Date()) ?? 0n;
253
+ // Current SDK exposes walletBalance(time); older versions used balance(time).
254
+ // Try both so this stays portable across SDK upgrades.
255
+ const now = new Date();
256
+ dustBalance = state.dust?.walletBalance?.(now) ?? state.dust?.balance?.(now) ?? 0n;
254
257
  } catch (_err) {
255
258
  log.warn("Could not read dust balance from synced state; continuing with dustBalance=0");
256
259
  }
@@ -732,11 +735,26 @@ function getInitialUnshieldedState(
732
735
  return Promise.resolve(null);
733
736
  }
734
737
 
738
+ export interface RegisterNightForDustOptions {
739
+ /**
740
+ * Override for the wait-for-sync precheck timeout. The default
741
+ * (`resolveWalletSyncTimeoutMs()`, 10 min) is correct when the wallet has
742
+ * an active unshielded subscription. Callers that built the wallet with
743
+ * `syncMode: 'dust-only'` should pass a small value (e.g. 30s) — the
744
+ * unshielded sync is stopped, so this wait can never succeed and the
745
+ * full timeout is wasted.
746
+ */
747
+ precheckSyncTimeoutMs?: number;
748
+ }
749
+
735
750
  /**
736
751
  * Register unshielded Night UTXOs for dust generation
737
752
  * This is required before the wallet can pay transaction fees
738
753
  */
739
- export async function registerNightForDust(walletResult: WalletResult): Promise<boolean> {
754
+ export async function registerNightForDust(
755
+ walletResult: WalletResult,
756
+ options?: RegisterNightForDustOptions,
757
+ ): Promise<boolean> {
740
758
  log.info("Checking for unshielded Night UTXOs to register for dust generation...");
741
759
 
742
760
  const state = await Rx.firstValueFrom(
@@ -749,7 +767,7 @@ export async function registerNightForDust(walletResult: WalletResult): Promise<
749
767
  return dustSynced && unshieldedSynced;
750
768
  }),
751
769
  Rx.timeout({
752
- each: resolveWalletSyncTimeoutMs(),
770
+ each: options?.precheckSyncTimeoutMs ?? resolveWalletSyncTimeoutMs(),
753
771
  with: () => Rx.throwError(() => new Error("Timeout waiting for unshielded+dust sync for dust registration")),
754
772
  })
755
773
  )
@@ -785,14 +803,19 @@ export async function registerNightForDust(walletResult: WalletResult): Promise<
785
803
  log.info(`Dust registration submitted with tx id: ${txId}`);
786
804
 
787
805
  log.info("Waiting for dust to be generated...");
806
+ // Current SDK exposes walletBalance(time); older versions used balance(time).
807
+ // Try both so this stays portable across SDK upgrades.
808
+ const readDustBalance = (s: any): bigint => {
809
+ const now = new Date();
810
+ return s.dust?.walletBalance?.(now) ?? s.dust?.balance?.(now) ?? 0n;
811
+ };
788
812
  await Rx.firstValueFrom(
789
813
  walletResult.wallet.state().pipe(
790
814
  Rx.throttleTime(CONSTANTS.WALLET_SYNC_THROTTLE_MS),
791
815
  Rx.tap((s: any) => {
792
- const dustBalance = s.dust?.balance?.(new Date()) ?? 0n;
793
- log.info(`Current dust balance: ${dustBalance}`);
816
+ log.info(`Current dust balance: ${readDustBalance(s)}`);
794
817
  }),
795
- Rx.filter((s: any) => (s.dust?.balance?.(new Date()) ?? 0n) > 0n),
818
+ Rx.filter((s: any) => readDustBalance(s) > 0n),
796
819
  Rx.timeout({
797
820
  each: resolveWalletSyncTimeoutMs(),
798
821
  with: () => Rx.throwError(() => new Error("Timeout waiting for dust generation"))
@@ -950,7 +973,9 @@ async function main() {
950
973
 
951
974
  try {
952
975
  const state = await Rx.firstValueFrom(walletResult.wallet.state());
953
- dustBalance = (state as any).dust?.balance?.(new Date()) ?? 0n;
976
+ // Current SDK exposes walletBalance(time); older versions used balance(time).
977
+ const now = new Date();
978
+ dustBalance = (state as any).dust?.walletBalance?.(now) ?? (state as any).dust?.balance?.(now) ?? 0n;
954
979
 
955
980
  if (dustBalance === 0n) {
956
981
  log.info("Dust balance is 0. Attempting to sync dust wallet...");