@owney/sdk 0.7.32-beta.1 → 0.7.32-beta.11

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
@@ -152,7 +152,7 @@ if (chainId) {
152
152
  }
153
153
  ```
154
154
 
155
- ### `activateAgent(chainId, agentId?)`
155
+ ### `activateAgent(chainId, agentId?, options?)`
156
156
 
157
157
  Activate the user's smart wallet for the specified agents, or all chain-compatible agents if omitted. Activation performs each provider's required setup before deposits are allowed. For Yieldseeker, this creates the user and agent when needed and calls the agent `/deploy` endpoint before any funding transfer. Saves the chainId for use by all subsequent SDK calls.
158
158
 
@@ -160,6 +160,7 @@ Activate the user's smart wallet for the specified agents, or all chain-compatib
160
160
  | --------- | ---------------------- | ----------------------------------------------------------------------------- |
161
161
  | `chainId` | `number` | Target chain ID (e.g. `8453` for Base, `42161` for Arbitrum) |
162
162
  | `agentId` | `AgentId[]` (optional) | Agents to activate. Omit to activate all agents that support the given chain. |
163
+ | `options` | `ActivateAgentOptions` (optional) | `asset` limits setup to that asset. `onAgentsSelected` fires once reads can start. |
163
164
 
164
165
  ```typescript
165
166
  // Activate specific agents on Base
@@ -169,6 +170,24 @@ await sdk.activateAgent(8453, ["zyfai"]);
169
170
  await sdk.activateAgent(42161);
170
171
  ```
171
172
 
173
+ Agents sign in one at a time, and activation stops at the first agent that fails, so no later agent's wallet prompt opens. A connect needs every agent: the app should end the session on any activation error. When an earlier agent activated, it throws `AGENT_ACTIVATION_PARTIAL_FAILURE` with `details: { activatedAgentIds, failedAgentIds, failures }`, plus `details.walletRejected: true` when the user rejected the signature. When the agent can never be readied (`details.permanent`), it throws `AGENT_ACTIVATION_BLOCKED`. Otherwise the agent's own error is thrown. Until it signs in, an agent whose signature the user rejected is read from a saved sign-in only (a read that would need a wallet prompt fails for that agent instead of opening one).
174
+
175
+ ### `getUnavailableAgentIds(chainId, asset?)`
176
+
177
+ The agents that take no new deposits on this chain and asset this session, so the app can show them as unavailable: each agent whose provider has no agent for that slot (`IAgent.isSlotUnavailable`). They stay withdrawable.
178
+
179
+ ```typescript
180
+ const unavailable = sdk.getUnavailableAgentIds(8453, "USDC"); // e.g. ["yieldseeker"]
181
+ ```
182
+
183
+ ### `getAvailableAgents({ chainId?, asset?, includeDisabled?, includeUnavailable? })`
184
+
185
+ Lists the configured agents. Disabled agents and agents unavailable on `chainId` (and `asset`) this session are omitted by default, because neither takes deposits. Pass `includeUnavailable: true` to list the unavailable ones too; `includeDisabled: true` lists both, since a disabled or unavailable agent may still hold funds to withdraw.
186
+
187
+ An adapter may implement the optional `IAgent.isSlotUnavailable(state, chainId, asset)`: true when its provider reported this session that the user cannot have an agent for that chain and asset. That is not a failure: `activateAgent` returns normally for it, deposits leave the slot out, and withdrawals without an explicit agent skip it.
188
+
189
+ Yieldseeker closes (deactivates) agents that stayed empty. The SDK reopens the user's verified agent when it finds it closed, at sign-in, on reads and before a deposit (at most one reopen request per slot per 120 s, and none after a permanent refusal). An agent that cannot be reopened fails with `YIELDSEEKER_AGENT_CLOSED`; at Yieldseeker's active-agent limit the slot reports `YIELDSEEKER_SLOT_UNAVAILABLE` and is unavailable for the session. A deposit is never sent to a closed agent.
190
+
172
191
  ### `deposit(options)`
173
192
 
174
193
  Deposit funds into a specific agent, or split equally across all agents if `agentId` is omitted. Validates that the asset is supported by the target agent(s) on the active chain, and per-agent minimum deposit amounts (defined per chain+asset on `agent.supportedAssets[].assets[].minDepositAmount`) are taken into account. Custom `depositCallback` implementations run once per final valid agent. The default Permit2 flow signs one recipient-bound batch authorization for every agent share. On the first Base USDC deposit, the SDK first requests an ERC-2612 signature; the relayer then submits the Permit2 approval and deposit as two sponsored transactions. Later deposits need only the batch signature.
@@ -769,6 +788,9 @@ type OwneyErrorCode =
769
788
  | "AGENT_NOT_FOUND" // Unknown agent ID passed
770
789
  | "AGENT_CHAIN_INCOMPATIBLE" // Agent(s) don't support the given chain
771
790
  | "AGENT_EMPTY_LIST" // Empty agentId array passed to activateAgent()
791
+ | "AGENT_ACTIVATION_PARTIAL_FAILURE" // An earlier agent activated, then one failed and activation stopped; details.walletRejected when the user rejected a signature
792
+ | "YIELDSEEKER_AGENT_CLOSED" // Yieldseeker closed the slot's agent and it cannot be reopened; before a deposit details carry notSubmitted: true, safeToFallback: false (nothing was sent)
793
+ | "YIELDSEEKER_SLOT_UNAVAILABLE" // No Yieldseeker agent for this slot this session (the user's active-agent limit); details.providerCode names the limit
772
794
  // Chain
773
795
  | "CHAIN_UNSUPPORTED" // Chain ID not in supported list (8453, 42161)
774
796
  | "CHAIN_NO_COMPATIBLE_AGENTS" // No agents support the given chain
@@ -175,6 +175,7 @@ function batchArgs(b) {
175
175
 
176
176
  // src/lib/token-batch-family.ts
177
177
  var memory = /* @__PURE__ */ new Map();
178
+ var unfinished = /* @__PURE__ */ new Map();
178
179
  var settled = /* @__PURE__ */ new Map();
179
180
  var hashPattern = /^0x[\da-f]{64}$/i;
180
181
  var uint = (value) => typeof value === "string" && /^\d{1,78}$/.test(value) && BigInt(value) < 1n << 256n;
@@ -395,6 +396,9 @@ function merge(base, incoming) {
395
396
  async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
396
397
  try {
397
398
  const key = tokenBatchKey(scope), records = /* @__PURE__ */ new Map();
399
+ for (const sidecar of [...unfinished.keys()])
400
+ if (sidecar.startsWith(`${key}:family:`) && !tokenBatchRead(sidecar))
401
+ unfinished.delete(sidecar);
398
402
  for (const k of keys().filter((k2) => k2.startsWith(`${key}:family:`))) {
399
403
  const raw = tokenBatchRead(k);
400
404
  if (!raw) continue;
@@ -422,6 +426,19 @@ async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
422
426
  );
423
427
  }
424
428
  }
429
+ const marked = /* @__PURE__ */ new Set();
430
+ for (const [id, family] of [...records]) {
431
+ const sidecar = `${key}:family:${id}`;
432
+ const terminal = unfinished.get(sidecar);
433
+ if (!terminal) continue;
434
+ if (family.terminal) {
435
+ unfinished.delete(sidecar);
436
+ continue;
437
+ }
438
+ finishTokenBatchFamily(scope, family, terminal);
439
+ marked.add(id);
440
+ records.set(id, { ...family, terminal });
441
+ }
425
442
  if (expectedHash) {
426
443
  let found = [...records.values()].filter(
427
444
  (f) => f.attempts.some((a) => same(a.hash, expectedHash))
@@ -440,7 +457,7 @@ async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
440
457
  return found[0];
441
458
  }
442
459
  const active = [...records.values()].filter(
443
- (f) => !f.terminal || f.authorizationId === projectedId
460
+ (f) => !f.terminal || f.authorizationId === projectedId && !marked.has(f.authorizationId)
444
461
  );
445
462
  if (active.length > 1) throw pending();
446
463
  return active[0];
@@ -449,6 +466,59 @@ async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
449
466
  throw pending();
450
467
  }
451
468
  }
469
+ async function dropExpiredHashlessTokenBatch(scope, pub) {
470
+ try {
471
+ const key = tokenBatchKey(scope);
472
+ const family = await loadTokenBatchFamily(scope);
473
+ const raw = family ? void 0 : tokenBatchRead(key);
474
+ const body = family ? family.terminal ? void 0 : family.body : raw ? JSON.parse(raw) : void 0;
475
+ if (!body || body.serializedTransaction || family?.attempts.some((a) => a.phase === "deposit") || !uint(body.deadline))
476
+ return false;
477
+ const block = await pub.getBlock();
478
+ if (typeof block?.timestamp !== "bigint" || block.timestamp <= BigInt(body.deadline))
479
+ return false;
480
+ if (family)
481
+ finishTokenBatchFamily(scope, family, {
482
+ kind: "expired",
483
+ blockNumber: typeof block.number === "bigint" ? block.number.toString() : "0"
484
+ });
485
+ else tokenBatchClear(key, raw);
486
+ return true;
487
+ } catch {
488
+ return false;
489
+ }
490
+ }
491
+ async function discardHashlessTokenBatch(scope, deadline, matches) {
492
+ try {
493
+ const key = tokenBatchKey(scope);
494
+ const family = await loadTokenBatchFamily(scope);
495
+ const raw = family ? void 0 : tokenBatchRead(key);
496
+ const body = family ? family.terminal ? void 0 : family.body : raw ? JSON.parse(raw) : void 0;
497
+ if (!body || body.serializedTransaction || family?.attempts.some((a) => a.phase === "deposit") || body.deadline !== deadline || body.chainId !== scope.chainId || !same(body.from, scope.owner) || !same(body.token, scope.token) || !matches(body))
498
+ return false;
499
+ if (family)
500
+ finishTokenBatchFamily(scope, family, { kind: "failed", blockNumber: "0" });
501
+ else tokenBatchClear(key, raw);
502
+ return true;
503
+ } catch {
504
+ return false;
505
+ }
506
+ }
507
+ async function savedTokenBatchBody(scope, pub) {
508
+ try {
509
+ if (pub) await dropExpiredHashlessTokenBatch(scope, pub);
510
+ const family = await loadTokenBatchFamily(scope);
511
+ if (family) return family.terminal ? void 0 : family.body;
512
+ const raw = tokenBatchRead(tokenBatchKey(scope));
513
+ if (!raw) return void 0;
514
+ const body = JSON.parse(raw);
515
+ if (body?.chainId !== scope.chainId || typeof body.from !== "string" || typeof body.token !== "string" || !same(body.from, scope.owner) || !same(body.token, scope.token) || !uint(body.deadline) || !Array.isArray(body.transfers) || !body.transfers.length)
516
+ return void 0;
517
+ return body;
518
+ } catch {
519
+ return void 0;
520
+ }
521
+ }
452
522
  async function saveTokenBatchAttempt(scope, body, attempt, assertCurrent = () => {
453
523
  }) {
454
524
  try {
@@ -507,10 +577,15 @@ function finishTokenBatchFamily(scope, family, terminal) {
507
577
  return;
508
578
  const evidence = terminalOf(terminal, current.attempts);
509
579
  if (!evidence) return;
510
- tokenBatchWrite(
511
- sidecar,
512
- JSON.stringify({ ...current, terminal: evidence })
513
- );
580
+ try {
581
+ tokenBatchWrite(
582
+ sidecar,
583
+ JSON.stringify({ ...current, terminal: evidence })
584
+ );
585
+ unfinished.delete(sidecar);
586
+ } catch {
587
+ unfinished.set(sidecar, evidence);
588
+ }
514
589
  const projected = tokenBatchRead(key);
515
590
  if (projected && tokenBatchAuthorizationId(JSON.parse(projected), family.relayer) === family.authorizationId)
516
591
  tokenBatchClear(key, projected);
@@ -539,6 +614,9 @@ export {
539
614
  tokenBatchCall,
540
615
  validateTokenBatchAttempt,
541
616
  loadTokenBatchFamily,
617
+ dropExpiredHashlessTokenBatch,
618
+ discardHashlessTokenBatch,
619
+ savedTokenBatchBody,
542
620
  saveTokenBatchAttempt,
543
621
  finishTokenBatchFamily
544
622
  };