@owney/sdk 0.2.13 → 0.2.14

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
@@ -125,7 +125,7 @@ await sdk.activateAgent(42161);
125
125
 
126
126
  ### `deposit(options)`
127
127
 
128
- 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 agent minimum deposit amounts are taken into account. When splitting across agents, `depositCallback` is invoked once per final valid agent with that agent's split amount and smart wallet address — expect multiple wallet prompts.
128
+ 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. When splitting across agents, `depositCallback` is invoked once per final valid agent with that agent's split amount and smart wallet address — expect multiple wallet prompts.
129
129
 
130
130
  | Param | Type | Description |
131
131
  | ------------------------- | -------------------- | --------------------------------------------------------------- |
@@ -167,13 +167,18 @@ console.log(results.agentResults.sail.amount); // "50000000"
167
167
 
168
168
  Withdraw funds from a specific agent, or all agents that support the active chain+asset if `agentId` is omitted. Validates that the asset is supported.
169
169
 
170
- When `agentId` is omitted with a partial `amount`, the SDK splits the requested amount proportionally across eligible agents based on each agent's per-asset balance. If an agent's withdraw fails, its share is redistributed to remaining not-yet-attempted agents (capped by their headroom). Throws:
170
+ When `agentId` is omitted with a partial `amount`, the SDK splits the requested amount proportionally across eligible agents based on each agent's per-asset balance. If an agent's withdraw fails, its share is redistributed to remaining not-yet-attempted agents (capped by their headroom).
171
+
172
+ Full withdrawals (no `amount`) iterate every eligible agent and continue on per-agent failures — successful agents still return their results. Per-agent failures (e.g. backend returns `success: false`) surface as `WITHDRAW_FAILED` from the agent and are caught at the aggregation layer.
173
+
174
+ Throws:
171
175
 
172
176
  - `WITHDRAW_INSUFFICIENT_BALANCE` — total available across agents is less than `amount`.
173
- - `WITHDRAW_ALL_FAILED` — every agent's withdraw call failed.
174
- - `WITHDRAW_PARTIAL_FAILURE` — some agents succeeded but total withdrawn is less than `amount`. Successful txs are surfaced via `error.details.partialResults`; per-agent failure messages via `error.details.agentErrors`.
177
+ - `WITHDRAW_ALL_FAILED` — every agent's withdraw call failed (full or partial). Carries per-agent failure messages via `error.details.agentErrors` (a `Record<agentId, string>`).
178
+ - `WITHDRAW_PARTIAL_FAILURE` — partial withdrawals only: some agents succeeded but total withdrawn is less than `amount`. Successful txs surfaced via `error.details.partialResults`; per-agent failures via `error.details.agentErrors`.
179
+ - `WITHDRAW_FAILED` — a single agent's underlying withdraw call returned an unsuccessful response. Includes `error.agentId` and the backend message in `error.details.response`.
175
180
 
176
- `WITHDRAW_ALL_FAILED` also carries per-agent failure messages via `error.details.agentErrors` (a `Record<agentId, string>`). Any error originating from a specific agent also has `error.agentId` set and a `[agent:<id>]` prefix on `error.message`.
181
+ Any error originating from a specific agent also has `error.agentId` set and a `[<code>][agent:<id>]` prefix on `error.message`.
177
182
 
178
183
  | Param | Type | Description |
179
184
  | ----------------- | -------------------- | --------------------------------------------- |
@@ -441,10 +446,16 @@ type OwneySupportedChainId = 8453 | 42161;
441
446
  type OwneySupportedChains = "BASE" | "ARBITRUM";
442
447
  type OwneySupportedTokens = "USDC" | "USDT";
443
448
 
449
+ type AgentSupportedAsset = {
450
+ readonly symbol: string;
451
+ /** Minimum deposit in the asset's smallest unit (e.g. wei, "0" for none). */
452
+ readonly minDepositAmount: string;
453
+ };
454
+
444
455
  type AgentSupportedAssets = {
445
456
  readonly chainId: number;
446
457
  readonly chain?: string;
447
- readonly assets: readonly string[];
458
+ readonly assets: readonly AgentSupportedAsset[];
448
459
  };
449
460
 
450
461
  interface OwneySDKConfig {
@@ -646,7 +657,7 @@ type DepositCallback = (
646
657
 
647
658
  ### Error Handling
648
659
 
649
- All errors thrown by the SDK are instances of `OwneyError` (which extends `Error`), each carrying a stable `code` string, optional `details` object, and optional `agentId` identifying the originating agent. When `agentId` is set, `error.message` is also prefixed with `[agent:<id>]` for logs.
660
+ All errors thrown by the SDK are instances of `OwneyError` (which extends `Error`), each carrying a stable `code` string, optional `details` object, and optional `agentId` identifying the originating agent. `error.message` is always prefixed with `[<code>]` (and `[<code>][agent:<id>]` when agent-scoped) so the code is visible in browser console logs without inspecting the error object.
650
661
 
651
662
  ```typescript
652
663
  import { OwneyError } from "@owney/sdk";
@@ -690,7 +701,7 @@ type OwneyErrorCode =
690
701
  | "ASSET_UNSUPPORTED" // Agent doesn't support the asset on the active chain
691
702
  | "ASSET_NO_COMPATIBLE_AGENTS" // No agents support the asset on the active chain
692
703
  // Deposit
693
- | "DEPOSIT_AMOUNT_BELOW_MINIMUM" // Amount below an agent's required minimum deposit
704
+ | "DEPOSIT_AMOUNT_BELOW_MINIMUM" // Amount below an agent's required minimum deposit for the chain+asset
694
705
  | "DEPOSIT_CALLBACK_REQUIRED" // Sail agent requires a depositCallback
695
706
  | "DEPOSIT_CALLBACK_INVALID" // depositCallback didn't return a tx hash
696
707
  | "DEPOSIT_NO_PERMITTED_TOKENS" // No permitted tokens available for deposit
@@ -699,6 +710,7 @@ type OwneyErrorCode =
699
710
  | "WITHDRAW_INSUFFICIENT_BALANCE" // Requested amount exceeds total available across agents
700
711
  | "WITHDRAW_ALL_FAILED" // Every eligible agent's withdraw call failed
701
712
  | "WITHDRAW_PARTIAL_FAILURE" // Some agents succeeded; total withdrawn < requested
713
+ | "WITHDRAW_FAILED" // Agent backend returned an unsuccessful withdraw response
702
714
  // API
703
715
  | "API_ROUTING_ERROR" // Routing API returned a non-OK HTTP status
704
716
  | "API_ROUTING_FAILED" // Routing API returned { success: false }
package/dist/index.cjs CHANGED
@@ -65,7 +65,8 @@ var OwneyError = class extends Error {
65
65
  details;
66
66
  agentId;
67
67
  constructor(code, message, details, agentId) {
68
- super(agentId ? `[agent:${agentId}] ${message}` : message);
68
+ const prefix = agentId ? `[${code}][agent:${agentId}]` : `[${code}]`;
69
+ super(`${prefix} ${message}`);
69
70
  this.name = "OwneyError";
70
71
  this.code = code;
71
72
  this.details = details;
@@ -316,18 +317,17 @@ var DEFAULT_ZYFAI_RPC_URLS = {
316
317
  };
317
318
  var ZyfaiAgent = class {
318
319
  id = "zyfai";
319
- minDepositAmount = "0";
320
320
  supportedChainIds = [8453, 42161];
321
321
  supportedAssets = [
322
322
  {
323
323
  chainId: 8453,
324
324
  chain: "BASE",
325
- assets: ["USDC"]
325
+ assets: [{ symbol: "USDC", minDepositAmount: "1500000" }]
326
326
  },
327
327
  {
328
328
  chainId: 42161,
329
329
  chain: "ARBITRUM",
330
- assets: ["USDC"]
330
+ assets: [{ symbol: "USDC", minDepositAmount: "1500000" }]
331
331
  }
332
332
  ];
333
333
  sdk;
@@ -447,6 +447,7 @@ var ZyfaiAgent = class {
447
447
  validChainId,
448
448
  amount
449
449
  );
450
+ console.log("zyf deposit raw", raw);
450
451
  return mapDeposit(raw);
451
452
  } catch (error) {
452
453
  await this.disconnect();
@@ -463,12 +464,22 @@ var ZyfaiAgent = class {
463
464
  amount,
464
465
  token
465
466
  );
467
+ console.log("zyf withdraw raw", raw);
468
+ if (!raw.success) {
469
+ throw new OwneyError(
470
+ "WITHDRAW_FAILED",
471
+ raw.message || "Zyfai withdraw failed.",
472
+ { chainId: validChainId, token, amount, response: raw },
473
+ this.id
474
+ );
475
+ }
466
476
  return mapWithdraw(raw);
467
477
  }
468
478
  // --- IAgent: Portfolio reads ---
469
479
  async getBalances(state, chainId) {
470
480
  const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
471
481
  const raw = await this.sdk.getPortfolio(this.getAddress());
482
+ console.log("zyf balance raw", raw);
472
483
  return mapBalances(raw, validChainId, smartWallet);
473
484
  }
474
485
  async getEarnings(state, chainId) {
@@ -499,7 +510,11 @@ var ZyfaiAgent = class {
499
510
  }
500
511
  async getUserProfile(state, chainId) {
501
512
  await this.connectAuth(state, chainId);
513
+ const validChainId = isValidChainId(chainId);
502
514
  const raw = await this.sdk.getUserDetails();
515
+ if (!raw.hasActiveSessionKey) {
516
+ await this.sdk.createSessionKey(this.getAddress(), validChainId);
517
+ }
503
518
  return mapUserProfile(raw, this.connectedAddress);
504
519
  }
505
520
  // --- IAgent: Discovery (no wallet required) ---
@@ -813,12 +828,17 @@ var OwneySDK = class {
813
828
  if (agentId) {
814
829
  const agent = this.getAgent(agentId);
815
830
  this.validateAssetSupport(agent, chainId, asset);
816
- this.validateMinDepositAmount(agent, amount);
831
+ this.validateMinDepositAmount(agent, chainId, asset, amount);
817
832
  return agent.deposit(state, chainId, amount, depositCallback);
818
833
  }
819
834
  const totalAmount = BigInt(amount);
820
835
  const eligibleAgents = this.getEligibleAgents(chainId, asset);
821
- const agentAmounts = this.splitDepositAmount(totalAmount, eligibleAgents);
836
+ const agentAmounts = this.splitDepositAmount(
837
+ totalAmount,
838
+ eligibleAgents,
839
+ chainId,
840
+ asset
841
+ );
822
842
  if (agentAmounts.length === 0) {
823
843
  throw new OwneyError(
824
844
  "DEPOSIT_AMOUNT_BELOW_MINIMUM",
@@ -837,7 +857,14 @@ var OwneySDK = class {
837
857
  }
838
858
  return { agentResults };
839
859
  }
840
- splitDepositAmount(totalAmount, agents) {
860
+ getMinDepositAmount(agent, chainId, asset) {
861
+ const chainAssets = agent.supportedAssets.find(
862
+ (sa) => sa.chainId === chainId
863
+ );
864
+ const assetEntry = chainAssets?.assets.find((a) => a.symbol === asset);
865
+ return BigInt(assetEntry?.minDepositAmount ?? "0");
866
+ }
867
+ splitDepositAmount(totalAmount, agents, chainId, asset) {
841
868
  if (agents.length === 0)
842
869
  return [];
843
870
  const perAgent = totalAmount / BigInt(agents.length);
@@ -847,26 +874,31 @@ var OwneySDK = class {
847
874
  amount: i === agents.length - 1 ? perAgent + remainder : perAgent
848
875
  }));
849
876
  const valid = splits.filter(
850
- (s) => s.amount >= BigInt(s.agent.minDepositAmount)
877
+ (s) => s.amount >= this.getMinDepositAmount(s.agent, chainId, asset)
851
878
  );
852
879
  if (valid.length === agents.length) {
853
880
  return splits.map((s) => ({ agent: s.agent, amount: String(s.amount) }));
854
881
  }
855
882
  return this.splitDepositAmount(
856
883
  totalAmount,
857
- valid.map((s) => s.agent)
884
+ valid.map((s) => s.agent),
885
+ chainId,
886
+ asset
858
887
  );
859
888
  }
860
- validateMinDepositAmount(agent, amount) {
861
- const minDepositAmount = BigInt(agent.minDepositAmount);
889
+ validateMinDepositAmount(agent, chainId, asset, amount) {
890
+ const minDepositAmount = this.getMinDepositAmount(agent, chainId, asset);
862
891
  const parsedAmount = BigInt(amount);
863
892
  if (parsedAmount < minDepositAmount) {
864
893
  throw new OwneyError(
865
894
  "DEPOSIT_AMOUNT_BELOW_MINIMUM",
866
- `Amount "${amount}" is below the required minimum deposit for agent "${agent.id}".`,
895
+ `Amount "${amount}" is below the required minimum deposit "${minDepositAmount.toString()}" for agent "${agent.id}" on chain ${chainId} for asset "${asset}".`,
867
896
  {
868
897
  agentId: agent.id,
869
- amount
898
+ chainId,
899
+ asset,
900
+ amount,
901
+ minDepositAmount: minDepositAmount.toString()
870
902
  }
871
903
  );
872
904
  }
@@ -875,7 +907,7 @@ var OwneySDK = class {
875
907
  const chainAssets = agent.supportedAssets.find(
876
908
  (sa) => sa.chainId === chainId
877
909
  );
878
- if (!chainAssets || !chainAssets.assets.includes(asset)) {
910
+ if (!chainAssets || !chainAssets.assets.some((a) => a.symbol === asset)) {
879
911
  throw new OwneyError(
880
912
  "ASSET_UNSUPPORTED",
881
913
  `Agent "${agent.id}" does not support asset "${asset}" on chain ${chainId}.`,
@@ -889,7 +921,7 @@ var OwneySDK = class {
889
921
  const chainAssets = agent.supportedAssets.find(
890
922
  (sa) => sa.chainId === chainId
891
923
  );
892
- return chainAssets && chainAssets.assets.includes(asset);
924
+ return chainAssets?.assets.some((a) => a.symbol === asset) ?? false;
893
925
  });
894
926
  if (eligible.length === 0) {
895
927
  throw new OwneyError(
@@ -932,8 +964,21 @@ var OwneySDK = class {
932
964
  const eligibleAgents = this.getEligibleAgents(chainId, asset);
933
965
  if (!amount) {
934
966
  const results2 = {};
967
+ const agentErrors2 = {};
935
968
  for (const agent of eligibleAgents) {
936
- results2[agent.id] = await agent.withdraw(state, chainId, token);
969
+ try {
970
+ results2[agent.id] = await agent.withdraw(state, chainId, token);
971
+ } catch (err) {
972
+ console.error(`withdraw failed for agent "${agent.id}":`, err);
973
+ agentErrors2[agent.id] = err instanceof Error ? err.message : String(err);
974
+ }
975
+ }
976
+ if (Object.keys(results2).length === 0) {
977
+ throw new OwneyError(
978
+ "WITHDRAW_ALL_FAILED",
979
+ `Failed to withdraw "${asset}" from all eligible agents.`,
980
+ { asset, agentErrors: agentErrors2 }
981
+ );
937
982
  }
938
983
  return { agentResult: results2 };
939
984
  }
package/dist/index.d.cts CHANGED
@@ -24,7 +24,6 @@ interface IAgent {
24
24
  readonly id: string;
25
25
  readonly supportedChainIds: readonly OwneySupportedChainId[];
26
26
  readonly supportedAssets: readonly AgentSupportedAssets[];
27
- readonly minDepositAmount: string;
28
27
  disconnect(): Promise<void>;
29
28
  activateAgent(state: ConnectionState, chainId: number): Promise<void>;
30
29
  deposit(state: ConnectionState, chainId: number, amount: string, depositCallback?: DepositCallback): Promise<OwneyDepositResult>;
@@ -51,10 +50,14 @@ type AgentApyOptions = {
51
50
 
52
51
  type AgentId = "zyfai";
53
52
  type Asset = string;
53
+ type AgentSupportedAsset = {
54
+ readonly symbol: string;
55
+ readonly minDepositAmount: string;
56
+ };
54
57
  type AgentSupportedAssets = {
55
58
  readonly chainId: number;
56
59
  readonly chain?: string;
57
- readonly assets: readonly string[];
60
+ readonly assets: readonly AgentSupportedAsset[];
58
61
  };
59
62
  type DailyApyDays = "7D" | "14D" | "30D";
60
63
  type HistoryFilters = {
@@ -261,6 +264,7 @@ declare class OwneySDK {
261
264
  * @returns {OwneyDepositResult} for a single agent, or {OwneyMultiDepositResult} with per-agent results
262
265
  */
263
266
  deposit(options: DepositOptions): Promise<OwneyDepositResult | OwneyMultiDepositResult>;
267
+ private getMinDepositAmount;
264
268
  private splitDepositAmount;
265
269
  private validateMinDepositAmount;
266
270
  private validateAssetSupport;
@@ -342,7 +346,7 @@ declare class OwneySDK {
342
346
  getAgentAllocation(): Promise<OwneyAgentAllocation>;
343
347
  }
344
348
 
345
- type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "CHAIN_UNSUPPORTED" | "CHAIN_NO_COMPATIBLE_AGENTS" | "ASSET_UNSUPPORTED" | "ASSET_NO_COMPATIBLE_AGENTS" | "DEPOSIT_AMOUNT_BELOW_MINIMUM" | "DEPOSIT_CALLBACK_REQUIRED" | "DEPOSIT_CALLBACK_INVALID" | "DEPOSIT_NO_PERMITTED_TOKENS" | "WITHDRAW_NO_PERMITTED_TOKENS" | "WITHDRAW_INSUFFICIENT_BALANCE" | "WITHDRAW_ALL_FAILED" | "WITHDRAW_PARTIAL_FAILURE" | "API_ROUTING_ERROR" | "API_ROUTING_FAILED" | "API_SAIL_ERROR" | "API_SAIL_TIMEOUT" | "API_NO_AGENTS" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
349
+ type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "CHAIN_UNSUPPORTED" | "CHAIN_NO_COMPATIBLE_AGENTS" | "ASSET_UNSUPPORTED" | "ASSET_NO_COMPATIBLE_AGENTS" | "DEPOSIT_AMOUNT_BELOW_MINIMUM" | "DEPOSIT_CALLBACK_REQUIRED" | "DEPOSIT_CALLBACK_INVALID" | "DEPOSIT_NO_PERMITTED_TOKENS" | "WITHDRAW_NO_PERMITTED_TOKENS" | "WITHDRAW_INSUFFICIENT_BALANCE" | "WITHDRAW_ALL_FAILED" | "WITHDRAW_PARTIAL_FAILURE" | "WITHDRAW_FAILED" | "API_ROUTING_ERROR" | "API_ROUTING_FAILED" | "API_SAIL_ERROR" | "API_SAIL_TIMEOUT" | "API_NO_AGENTS" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
346
350
  declare class OwneyError extends Error {
347
351
  readonly code: OwneyErrorCode;
348
352
  readonly details?: Record<string, unknown>;
@@ -367,4 +371,4 @@ declare class AgentChainIncompatibleError extends OwneyError {
367
371
  }>, connectedChainId: number);
368
372
  }
369
373
 
370
- export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
374
+ export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
package/dist/index.d.ts CHANGED
@@ -24,7 +24,6 @@ interface IAgent {
24
24
  readonly id: string;
25
25
  readonly supportedChainIds: readonly OwneySupportedChainId[];
26
26
  readonly supportedAssets: readonly AgentSupportedAssets[];
27
- readonly minDepositAmount: string;
28
27
  disconnect(): Promise<void>;
29
28
  activateAgent(state: ConnectionState, chainId: number): Promise<void>;
30
29
  deposit(state: ConnectionState, chainId: number, amount: string, depositCallback?: DepositCallback): Promise<OwneyDepositResult>;
@@ -51,10 +50,14 @@ type AgentApyOptions = {
51
50
 
52
51
  type AgentId = "zyfai";
53
52
  type Asset = string;
53
+ type AgentSupportedAsset = {
54
+ readonly symbol: string;
55
+ readonly minDepositAmount: string;
56
+ };
54
57
  type AgentSupportedAssets = {
55
58
  readonly chainId: number;
56
59
  readonly chain?: string;
57
- readonly assets: readonly string[];
60
+ readonly assets: readonly AgentSupportedAsset[];
58
61
  };
59
62
  type DailyApyDays = "7D" | "14D" | "30D";
60
63
  type HistoryFilters = {
@@ -261,6 +264,7 @@ declare class OwneySDK {
261
264
  * @returns {OwneyDepositResult} for a single agent, or {OwneyMultiDepositResult} with per-agent results
262
265
  */
263
266
  deposit(options: DepositOptions): Promise<OwneyDepositResult | OwneyMultiDepositResult>;
267
+ private getMinDepositAmount;
264
268
  private splitDepositAmount;
265
269
  private validateMinDepositAmount;
266
270
  private validateAssetSupport;
@@ -342,7 +346,7 @@ declare class OwneySDK {
342
346
  getAgentAllocation(): Promise<OwneyAgentAllocation>;
343
347
  }
344
348
 
345
- type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "CHAIN_UNSUPPORTED" | "CHAIN_NO_COMPATIBLE_AGENTS" | "ASSET_UNSUPPORTED" | "ASSET_NO_COMPATIBLE_AGENTS" | "DEPOSIT_AMOUNT_BELOW_MINIMUM" | "DEPOSIT_CALLBACK_REQUIRED" | "DEPOSIT_CALLBACK_INVALID" | "DEPOSIT_NO_PERMITTED_TOKENS" | "WITHDRAW_NO_PERMITTED_TOKENS" | "WITHDRAW_INSUFFICIENT_BALANCE" | "WITHDRAW_ALL_FAILED" | "WITHDRAW_PARTIAL_FAILURE" | "API_ROUTING_ERROR" | "API_ROUTING_FAILED" | "API_SAIL_ERROR" | "API_SAIL_TIMEOUT" | "API_NO_AGENTS" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
349
+ type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "CHAIN_UNSUPPORTED" | "CHAIN_NO_COMPATIBLE_AGENTS" | "ASSET_UNSUPPORTED" | "ASSET_NO_COMPATIBLE_AGENTS" | "DEPOSIT_AMOUNT_BELOW_MINIMUM" | "DEPOSIT_CALLBACK_REQUIRED" | "DEPOSIT_CALLBACK_INVALID" | "DEPOSIT_NO_PERMITTED_TOKENS" | "WITHDRAW_NO_PERMITTED_TOKENS" | "WITHDRAW_INSUFFICIENT_BALANCE" | "WITHDRAW_ALL_FAILED" | "WITHDRAW_PARTIAL_FAILURE" | "WITHDRAW_FAILED" | "API_ROUTING_ERROR" | "API_ROUTING_FAILED" | "API_SAIL_ERROR" | "API_SAIL_TIMEOUT" | "API_NO_AGENTS" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
346
350
  declare class OwneyError extends Error {
347
351
  readonly code: OwneyErrorCode;
348
352
  readonly details?: Record<string, unknown>;
@@ -367,4 +371,4 @@ declare class AgentChainIncompatibleError extends OwneyError {
367
371
  }>, connectedChainId: number);
368
372
  }
369
373
 
370
- export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
374
+ export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
package/dist/index.js CHANGED
@@ -35,7 +35,8 @@ var OwneyError = class extends Error {
35
35
  details;
36
36
  agentId;
37
37
  constructor(code, message, details, agentId) {
38
- super(agentId ? `[agent:${agentId}] ${message}` : message);
38
+ const prefix = agentId ? `[${code}][agent:${agentId}]` : `[${code}]`;
39
+ super(`${prefix} ${message}`);
39
40
  this.name = "OwneyError";
40
41
  this.code = code;
41
42
  this.details = details;
@@ -286,18 +287,17 @@ var DEFAULT_ZYFAI_RPC_URLS = {
286
287
  };
287
288
  var ZyfaiAgent = class {
288
289
  id = "zyfai";
289
- minDepositAmount = "0";
290
290
  supportedChainIds = [8453, 42161];
291
291
  supportedAssets = [
292
292
  {
293
293
  chainId: 8453,
294
294
  chain: "BASE",
295
- assets: ["USDC"]
295
+ assets: [{ symbol: "USDC", minDepositAmount: "1500000" }]
296
296
  },
297
297
  {
298
298
  chainId: 42161,
299
299
  chain: "ARBITRUM",
300
- assets: ["USDC"]
300
+ assets: [{ symbol: "USDC", minDepositAmount: "1500000" }]
301
301
  }
302
302
  ];
303
303
  sdk;
@@ -417,6 +417,7 @@ var ZyfaiAgent = class {
417
417
  validChainId,
418
418
  amount
419
419
  );
420
+ console.log("zyf deposit raw", raw);
420
421
  return mapDeposit(raw);
421
422
  } catch (error) {
422
423
  await this.disconnect();
@@ -433,12 +434,22 @@ var ZyfaiAgent = class {
433
434
  amount,
434
435
  token
435
436
  );
437
+ console.log("zyf withdraw raw", raw);
438
+ if (!raw.success) {
439
+ throw new OwneyError(
440
+ "WITHDRAW_FAILED",
441
+ raw.message || "Zyfai withdraw failed.",
442
+ { chainId: validChainId, token, amount, response: raw },
443
+ this.id
444
+ );
445
+ }
436
446
  return mapWithdraw(raw);
437
447
  }
438
448
  // --- IAgent: Portfolio reads ---
439
449
  async getBalances(state, chainId) {
440
450
  const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
441
451
  const raw = await this.sdk.getPortfolio(this.getAddress());
452
+ console.log("zyf balance raw", raw);
442
453
  return mapBalances(raw, validChainId, smartWallet);
443
454
  }
444
455
  async getEarnings(state, chainId) {
@@ -469,7 +480,11 @@ var ZyfaiAgent = class {
469
480
  }
470
481
  async getUserProfile(state, chainId) {
471
482
  await this.connectAuth(state, chainId);
483
+ const validChainId = isValidChainId(chainId);
472
484
  const raw = await this.sdk.getUserDetails();
485
+ if (!raw.hasActiveSessionKey) {
486
+ await this.sdk.createSessionKey(this.getAddress(), validChainId);
487
+ }
473
488
  return mapUserProfile(raw, this.connectedAddress);
474
489
  }
475
490
  // --- IAgent: Discovery (no wallet required) ---
@@ -783,12 +798,17 @@ var OwneySDK = class {
783
798
  if (agentId) {
784
799
  const agent = this.getAgent(agentId);
785
800
  this.validateAssetSupport(agent, chainId, asset);
786
- this.validateMinDepositAmount(agent, amount);
801
+ this.validateMinDepositAmount(agent, chainId, asset, amount);
787
802
  return agent.deposit(state, chainId, amount, depositCallback);
788
803
  }
789
804
  const totalAmount = BigInt(amount);
790
805
  const eligibleAgents = this.getEligibleAgents(chainId, asset);
791
- const agentAmounts = this.splitDepositAmount(totalAmount, eligibleAgents);
806
+ const agentAmounts = this.splitDepositAmount(
807
+ totalAmount,
808
+ eligibleAgents,
809
+ chainId,
810
+ asset
811
+ );
792
812
  if (agentAmounts.length === 0) {
793
813
  throw new OwneyError(
794
814
  "DEPOSIT_AMOUNT_BELOW_MINIMUM",
@@ -807,7 +827,14 @@ var OwneySDK = class {
807
827
  }
808
828
  return { agentResults };
809
829
  }
810
- splitDepositAmount(totalAmount, agents) {
830
+ getMinDepositAmount(agent, chainId, asset) {
831
+ const chainAssets = agent.supportedAssets.find(
832
+ (sa) => sa.chainId === chainId
833
+ );
834
+ const assetEntry = chainAssets?.assets.find((a) => a.symbol === asset);
835
+ return BigInt(assetEntry?.minDepositAmount ?? "0");
836
+ }
837
+ splitDepositAmount(totalAmount, agents, chainId, asset) {
811
838
  if (agents.length === 0)
812
839
  return [];
813
840
  const perAgent = totalAmount / BigInt(agents.length);
@@ -817,26 +844,31 @@ var OwneySDK = class {
817
844
  amount: i === agents.length - 1 ? perAgent + remainder : perAgent
818
845
  }));
819
846
  const valid = splits.filter(
820
- (s) => s.amount >= BigInt(s.agent.minDepositAmount)
847
+ (s) => s.amount >= this.getMinDepositAmount(s.agent, chainId, asset)
821
848
  );
822
849
  if (valid.length === agents.length) {
823
850
  return splits.map((s) => ({ agent: s.agent, amount: String(s.amount) }));
824
851
  }
825
852
  return this.splitDepositAmount(
826
853
  totalAmount,
827
- valid.map((s) => s.agent)
854
+ valid.map((s) => s.agent),
855
+ chainId,
856
+ asset
828
857
  );
829
858
  }
830
- validateMinDepositAmount(agent, amount) {
831
- const minDepositAmount = BigInt(agent.minDepositAmount);
859
+ validateMinDepositAmount(agent, chainId, asset, amount) {
860
+ const minDepositAmount = this.getMinDepositAmount(agent, chainId, asset);
832
861
  const parsedAmount = BigInt(amount);
833
862
  if (parsedAmount < minDepositAmount) {
834
863
  throw new OwneyError(
835
864
  "DEPOSIT_AMOUNT_BELOW_MINIMUM",
836
- `Amount "${amount}" is below the required minimum deposit for agent "${agent.id}".`,
865
+ `Amount "${amount}" is below the required minimum deposit "${minDepositAmount.toString()}" for agent "${agent.id}" on chain ${chainId} for asset "${asset}".`,
837
866
  {
838
867
  agentId: agent.id,
839
- amount
868
+ chainId,
869
+ asset,
870
+ amount,
871
+ minDepositAmount: minDepositAmount.toString()
840
872
  }
841
873
  );
842
874
  }
@@ -845,7 +877,7 @@ var OwneySDK = class {
845
877
  const chainAssets = agent.supportedAssets.find(
846
878
  (sa) => sa.chainId === chainId
847
879
  );
848
- if (!chainAssets || !chainAssets.assets.includes(asset)) {
880
+ if (!chainAssets || !chainAssets.assets.some((a) => a.symbol === asset)) {
849
881
  throw new OwneyError(
850
882
  "ASSET_UNSUPPORTED",
851
883
  `Agent "${agent.id}" does not support asset "${asset}" on chain ${chainId}.`,
@@ -859,7 +891,7 @@ var OwneySDK = class {
859
891
  const chainAssets = agent.supportedAssets.find(
860
892
  (sa) => sa.chainId === chainId
861
893
  );
862
- return chainAssets && chainAssets.assets.includes(asset);
894
+ return chainAssets?.assets.some((a) => a.symbol === asset) ?? false;
863
895
  });
864
896
  if (eligible.length === 0) {
865
897
  throw new OwneyError(
@@ -902,8 +934,21 @@ var OwneySDK = class {
902
934
  const eligibleAgents = this.getEligibleAgents(chainId, asset);
903
935
  if (!amount) {
904
936
  const results2 = {};
937
+ const agentErrors2 = {};
905
938
  for (const agent of eligibleAgents) {
906
- results2[agent.id] = await agent.withdraw(state, chainId, token);
939
+ try {
940
+ results2[agent.id] = await agent.withdraw(state, chainId, token);
941
+ } catch (err) {
942
+ console.error(`withdraw failed for agent "${agent.id}":`, err);
943
+ agentErrors2[agent.id] = err instanceof Error ? err.message : String(err);
944
+ }
945
+ }
946
+ if (Object.keys(results2).length === 0) {
947
+ throw new OwneyError(
948
+ "WITHDRAW_ALL_FAILED",
949
+ `Failed to withdraw "${asset}" from all eligible agents.`,
950
+ { asset, agentErrors: agentErrors2 }
951
+ );
907
952
  }
908
953
  return { agentResult: results2 };
909
954
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",