@toon-protocol/client 0.14.5 → 0.14.6

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/dist/index.d.ts CHANGED
@@ -976,6 +976,8 @@ declare class ToonClient {
976
976
  */
977
977
  private minaPrivateKey?;
978
978
  private channelManager?;
979
+ /** Concrete on-chain client, kept so deposit/withdraw can reach chain methods. */
980
+ private onChainChannelClient?;
979
981
  private readonly peerNegotiations;
980
982
  /**
981
983
  * Creates a new ToonClient instance.
@@ -1226,6 +1228,18 @@ declare class ToonClient {
1226
1228
  * The available (spendable) balance is this minus the cumulative spent amount.
1227
1229
  */
1228
1230
  getChannelDepositTotal(channelId: string): bigint;
1231
+ /**
1232
+ * Deposit additional collateral into an open channel. `amount` is the delta to
1233
+ * add (base units, decimal string or bigint). The daemon signs its own tx; no
1234
+ * key material leaves the client. Reads the current tracked deposit, performs
1235
+ * the on-chain deposit, updates the tracked total, and returns the new total.
1236
+ * EVM is live; Solana/Mina deposit lands in a follow-up.
1237
+ */
1238
+ depositToChannel(channelId: string, amount: string | bigint): Promise<{
1239
+ channelId: string;
1240
+ txHash?: string;
1241
+ depositTotal: string;
1242
+ }>;
1229
1243
  /**
1230
1244
  * Read the on-chain settlement-token balance of this client's OWN wallet on
1231
1245
  * each configured chain (EVM token, Solana SPL, native MINA). A free read — no
@@ -2135,6 +2149,29 @@ declare class OnChainChannelClient implements ConnectorChannelClient {
2135
2149
  * 4. Deposit initial funds if specified
2136
2150
  */
2137
2151
  openChannel(params: OpenChannelParams): Promise<OpenChannelResult>;
2152
+ /**
2153
+ * Deposit additional collateral into an already-open channel. `amount` is the
2154
+ * DELTA to add (base units). Dispatches by the channel's cached chain context;
2155
+ * `currentDeposit` is the channel's current locked total (tracked off-chain by
2156
+ * the caller) — required for EVM, whose `setTotalDeposit` takes the new
2157
+ * cumulative total, not a delta. Returns the new on-chain deposit total.
2158
+ *
2159
+ * Non-custodial: the client deposits its OWN funds and signs its OWN tx.
2160
+ * EVM is live; Solana/Mina deposit extraction is a follow-up (PR B.1).
2161
+ */
2162
+ depositToChannel(channelId: string, amount: bigint, opts: {
2163
+ currentDeposit: bigint;
2164
+ }): Promise<{
2165
+ txHash?: string;
2166
+ depositTotal: bigint;
2167
+ }>;
2168
+ /**
2169
+ * EVM deposit: approve the token-network for the delta if the allowance is
2170
+ * short, then `setTotalDeposit(channelId, participant, current + delta)` —
2171
+ * the contract takes the new cumulative total, so we add the delta to the
2172
+ * caller-supplied current locked amount.
2173
+ */
2174
+ private depositEvm;
2138
2175
  /**
2139
2176
  * Opens a REAL on-chain Solana payment channel.
2140
2177
  *
@@ -2305,6 +2342,12 @@ declare class ChannelManager {
2305
2342
  * (spendable) balance is `depositTotal - cumulativeAmount`.
2306
2343
  */
2307
2344
  getDepositTotal(channelId: string): bigint;
2345
+ /**
2346
+ * Update the tracked on-chain deposit total after a successful deposit, so the
2347
+ * available balance (`depositTotal - cumulativeAmount`) reflects the new
2348
+ * collateral on the next read.
2349
+ */
2350
+ setDepositTotal(channelId: string, total: bigint): void;
2308
2351
  /**
2309
2352
  * Gets all tracked channel IDs.
2310
2353
  */
package/dist/index.js CHANGED
@@ -2410,6 +2410,70 @@ var OnChainChannelClient = class {
2410
2410
  if (chainPrefix === "mina") return this.openMinaChannel(params);
2411
2411
  return this.openEvmChannel(params);
2412
2412
  }
2413
+ /**
2414
+ * Deposit additional collateral into an already-open channel. `amount` is the
2415
+ * DELTA to add (base units). Dispatches by the channel's cached chain context;
2416
+ * `currentDeposit` is the channel's current locked total (tracked off-chain by
2417
+ * the caller) — required for EVM, whose `setTotalDeposit` takes the new
2418
+ * cumulative total, not a delta. Returns the new on-chain deposit total.
2419
+ *
2420
+ * Non-custodial: the client deposits its OWN funds and signs its OWN tx.
2421
+ * EVM is live; Solana/Mina deposit extraction is a follow-up (PR B.1).
2422
+ */
2423
+ async depositToChannel(channelId, amount, opts) {
2424
+ if (amount <= 0n) throw new Error("Deposit amount must be positive.");
2425
+ const ctx = this.channelContext.get(channelId);
2426
+ if (!ctx) {
2427
+ throw new Error(
2428
+ `No on-chain context for channel "${channelId}" \u2014 it must be opened by this client first.`
2429
+ );
2430
+ }
2431
+ const chainPrefix = ctx.chain.split(":")[0];
2432
+ if (chainPrefix === "solana" || chainPrefix === "mina") {
2433
+ throw new Error(
2434
+ `Deposit on ${chainPrefix} is not yet supported (EVM only; follow-up PR adds it).`
2435
+ );
2436
+ }
2437
+ return this.depositEvm(channelId, amount, opts.currentDeposit, ctx);
2438
+ }
2439
+ /**
2440
+ * EVM deposit: approve the token-network for the delta if the allowance is
2441
+ * short, then `setTotalDeposit(channelId, participant, current + delta)` —
2442
+ * the contract takes the new cumulative total, so we add the delta to the
2443
+ * caller-supplied current locked amount.
2444
+ */
2445
+ async depositEvm(channelId, amount, currentDeposit, ctx) {
2446
+ const { publicClient, walletClient } = this.createClients(ctx.chain);
2447
+ const tokenNetworkAddr = ctx.tokenNetworkAddress;
2448
+ const myAddress = this.evmSigner.address;
2449
+ const newTotal = currentDeposit + amount;
2450
+ if (ctx.tokenAddress) {
2451
+ const tokenAddr = ctx.tokenAddress;
2452
+ const allowance = await publicClient.readContract({
2453
+ address: tokenAddr,
2454
+ abi: ERC20_ABI,
2455
+ functionName: "allowance",
2456
+ args: [myAddress, tokenNetworkAddr]
2457
+ });
2458
+ if (allowance < amount) {
2459
+ const approveHash = await walletClient.writeContract({
2460
+ address: tokenAddr,
2461
+ abi: ERC20_ABI,
2462
+ functionName: "approve",
2463
+ args: [tokenNetworkAddr, maxUint256]
2464
+ });
2465
+ await publicClient.waitForTransactionReceipt({ hash: approveHash });
2466
+ }
2467
+ }
2468
+ const depositHash = await walletClient.writeContract({
2469
+ address: tokenNetworkAddr,
2470
+ abi: TOKEN_NETWORK_ABI,
2471
+ functionName: "setTotalDeposit",
2472
+ args: [channelId, myAddress, newTotal]
2473
+ });
2474
+ await publicClient.waitForTransactionReceipt({ hash: depositHash });
2475
+ return { txHash: depositHash, depositTotal: newTotal };
2476
+ }
2413
2477
  /**
2414
2478
  * Opens a REAL on-chain Solana payment channel.
2415
2479
  *
@@ -2611,7 +2675,8 @@ var OnChainChannelClient = class {
2611
2675
  }
2612
2676
  this.channelContext.set(channelId, {
2613
2677
  chain,
2614
- tokenNetworkAddress: tokenNetwork
2678
+ tokenNetworkAddress: tokenNetwork,
2679
+ ...params.token ? { tokenAddress: params.token } : {}
2615
2680
  });
2616
2681
  if (deposit > 0n) {
2617
2682
  const depositHash = await walletClient.writeContract({
@@ -3501,6 +3566,18 @@ var ChannelManager = class {
3501
3566
  }
3502
3567
  return tracking.depositTotal ?? 0n;
3503
3568
  }
3569
+ /**
3570
+ * Update the tracked on-chain deposit total after a successful deposit, so the
3571
+ * available balance (`depositTotal - cumulativeAmount`) reflects the new
3572
+ * collateral on the next read.
3573
+ */
3574
+ setDepositTotal(channelId, total) {
3575
+ const tracking = this.channels.get(channelId);
3576
+ if (!tracking) {
3577
+ throw new Error(`Channel "${channelId}" is not being tracked.`);
3578
+ }
3579
+ tracking.depositTotal = total;
3580
+ }
3504
3581
  /**
3505
3582
  * Gets all tracked channel IDs.
3506
3583
  */
@@ -4029,6 +4106,8 @@ var ToonClient = class {
4029
4106
  */
4030
4107
  minaPrivateKey;
4031
4108
  channelManager;
4109
+ /** Concrete on-chain client, kept so deposit/withdraw can reach chain methods. */
4110
+ onChainChannelClient;
4032
4111
  peerNegotiations = /* @__PURE__ */ new Map();
4033
4112
  /**
4034
4113
  * Creates a new ToonClient instance.
@@ -4222,6 +4301,7 @@ var ToonClient = class {
4222
4301
  }
4223
4302
  }
4224
4303
  if (this.channelManager && initialization.onChainChannelClient) {
4304
+ this.onChainChannelClient = initialization.onChainChannelClient;
4225
4305
  this.channelManager.setChannelClient(
4226
4306
  initialization.onChainChannelClient
4227
4307
  );
@@ -4638,6 +4718,31 @@ var ToonClient = class {
4638
4718
  if (!this.channelManager) throw new Error("ChannelManager not initialized");
4639
4719
  return this.channelManager.getDepositTotal(channelId);
4640
4720
  }
4721
+ /**
4722
+ * Deposit additional collateral into an open channel. `amount` is the delta to
4723
+ * add (base units, decimal string or bigint). The daemon signs its own tx; no
4724
+ * key material leaves the client. Reads the current tracked deposit, performs
4725
+ * the on-chain deposit, updates the tracked total, and returns the new total.
4726
+ * EVM is live; Solana/Mina deposit lands in a follow-up.
4727
+ */
4728
+ async depositToChannel(channelId, amount) {
4729
+ if (!this.channelManager) throw new Error("ChannelManager not initialized");
4730
+ if (!this.onChainChannelClient) {
4731
+ throw new Error("On-chain channel client not configured (no chainRpcUrls).");
4732
+ }
4733
+ const delta = BigInt(amount);
4734
+ if (delta <= 0n) throw new Error("Deposit amount must be positive.");
4735
+ const currentDeposit = this.channelManager.getDepositTotal(channelId);
4736
+ const result = await this.onChainChannelClient.depositToChannel(channelId, delta, {
4737
+ currentDeposit
4738
+ });
4739
+ this.channelManager.setDepositTotal(channelId, result.depositTotal);
4740
+ return {
4741
+ channelId,
4742
+ ...result.txHash ? { txHash: result.txHash } : {},
4743
+ depositTotal: result.depositTotal.toString()
4744
+ };
4745
+ }
4641
4746
  /**
4642
4747
  * Read the on-chain settlement-token balance of this client's OWN wallet on
4643
4748
  * each configured chain (EVM token, Solana SPL, native MINA). A free read — no