@owney/sdk 0.7.9 → 0.7.10-beta.0

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.cjs CHANGED
@@ -3,6 +3,14 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __esm = (fn, res, err) => function __init() {
7
+ if (err) throw err[0];
8
+ try {
9
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
+ } catch (e) {
11
+ throw err = [e], e;
12
+ }
13
+ };
6
14
  var __export = (target, all) => {
7
15
  for (var name in all)
8
16
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,6 +25,645 @@ var __copyProps = (to, from, except, desc) => {
17
25
  };
18
26
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
27
 
28
+ // src/errors.ts
29
+ var OwneyError, AgentNotFoundError, NotConnectedError, AgentChainIncompatibleError;
30
+ var init_errors = __esm({
31
+ "src/errors.ts"() {
32
+ "use strict";
33
+ OwneyError = class extends Error {
34
+ code;
35
+ details;
36
+ agentId;
37
+ constructor(code, message, details, agentId) {
38
+ const prefix = agentId ? `[${code}][agent:${agentId}]` : `[${code}]`;
39
+ super(`${prefix} ${message}`);
40
+ this.name = "OwneyError";
41
+ this.code = code;
42
+ this.details = details;
43
+ this.agentId = agentId;
44
+ }
45
+ };
46
+ AgentNotFoundError = class extends OwneyError {
47
+ constructor(agentId, available) {
48
+ super(
49
+ "AGENT_NOT_FOUND",
50
+ `Unknown agent "${agentId}". Available agents: ${available.join(", ")}`,
51
+ { agentId, available },
52
+ agentId
53
+ );
54
+ this.name = "AgentNotFoundError";
55
+ }
56
+ };
57
+ NotConnectedError = class extends OwneyError {
58
+ constructor() {
59
+ super("NOT_CONNECTED", "Not connected. Call sdk.connect(provider) first.");
60
+ this.name = "NotConnectedError";
61
+ }
62
+ };
63
+ AgentChainIncompatibleError = class extends OwneyError {
64
+ incompatibleAgents;
65
+ connectedChainId;
66
+ constructor(incompatibleAgents, connectedChainId) {
67
+ const details = incompatibleAgents.map(
68
+ ({ agentId, supportedChainIds }) => `"${agentId}" supports chains [${supportedChainIds.join(", ")}]`
69
+ ).join("; ");
70
+ super(
71
+ "AGENT_CHAIN_INCOMPATIBLE",
72
+ `Chain ${connectedChainId} is not supported by the following agents: ${details}`,
73
+ { incompatibleAgents, connectedChainId }
74
+ );
75
+ this.name = "AgentChainIncompatibleError";
76
+ this.incompatibleAgents = incompatibleAgents;
77
+ this.connectedChainId = connectedChainId;
78
+ }
79
+ };
80
+ }
81
+ });
82
+
83
+ // src/lib/permit2.ts
84
+ function buildPermitTransferFromTypedData(input) {
85
+ return {
86
+ domain: {
87
+ name: "Permit2",
88
+ chainId: input.chainId,
89
+ verifyingContract: PERMIT2_ADDRESS
90
+ },
91
+ types: {
92
+ PermitTransferFrom: [
93
+ { name: "permitted", type: "TokenPermissions" },
94
+ { name: "spender", type: "address" },
95
+ { name: "nonce", type: "uint256" },
96
+ { name: "deadline", type: "uint256" }
97
+ ],
98
+ TokenPermissions: [
99
+ { name: "token", type: "address" },
100
+ { name: "amount", type: "uint256" }
101
+ ]
102
+ },
103
+ primaryType: "PermitTransferFrom",
104
+ message: input.message
105
+ };
106
+ }
107
+ function randomPermit2Nonce() {
108
+ const bytes = new Uint8Array(32);
109
+ globalThis.crypto.getRandomValues(bytes);
110
+ return BigInt((0, import_viem4.bytesToHex)(bytes));
111
+ }
112
+ async function readPermit2Allowance(publicClient, token, owner) {
113
+ return publicClient.readContract({
114
+ address: token,
115
+ abi: ERC20_ALLOWANCE_ABI,
116
+ functionName: "allowance",
117
+ args: [owner, PERMIT2_ADDRESS]
118
+ });
119
+ }
120
+ async function readErc20Balance(publicClient, token, owner) {
121
+ return publicClient.readContract({
122
+ address: token,
123
+ abi: ERC20_ALLOWANCE_ABI,
124
+ functionName: "balanceOf",
125
+ args: [owner]
126
+ });
127
+ }
128
+ var import_viem4, PERMIT2_ADDRESS, MAX_UINT256, ERC20_ALLOWANCE_ABI;
129
+ var init_permit2 = __esm({
130
+ "src/lib/permit2.ts"() {
131
+ "use strict";
132
+ import_viem4 = require("viem");
133
+ PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
134
+ MAX_UINT256 = 2n ** 256n - 1n;
135
+ ERC20_ALLOWANCE_ABI = [
136
+ {
137
+ type: "function",
138
+ name: "allowance",
139
+ stateMutability: "view",
140
+ inputs: [
141
+ { name: "owner", type: "address" },
142
+ { name: "spender", type: "address" }
143
+ ],
144
+ outputs: [{ name: "", type: "uint256" }]
145
+ },
146
+ {
147
+ type: "function",
148
+ name: "approve",
149
+ stateMutability: "nonpayable",
150
+ inputs: [
151
+ { name: "spender", type: "address" },
152
+ { name: "amount", type: "uint256" }
153
+ ],
154
+ outputs: [{ name: "", type: "bool" }]
155
+ },
156
+ {
157
+ type: "function",
158
+ name: "balanceOf",
159
+ stateMutability: "view",
160
+ inputs: [{ name: "account", type: "address" }],
161
+ outputs: [{ name: "", type: "uint256" }]
162
+ }
163
+ ];
164
+ }
165
+ });
166
+
167
+ // src/agents/surfliquid/surfliquid.constants.ts
168
+ var SURFLIQUID_CHAIN_ID, SURFLIQUID_CHAIN_NAME, SURFLIQUID_USDC_ADDRESS, SURFLIQUID_USDC_DECIMALS, SURFLIQUID_MIN_DEPOSIT, SURFLIQUID_APPROVAL_TARGET, SURFLIQUID_SUPPORTED_ASSETS, SURFLIQUID_ACTION_MAP;
169
+ var init_surfliquid_constants = __esm({
170
+ "src/agents/surfliquid/surfliquid.constants.ts"() {
171
+ "use strict";
172
+ SURFLIQUID_CHAIN_ID = 8453;
173
+ SURFLIQUID_CHAIN_NAME = "BASE";
174
+ SURFLIQUID_USDC_ADDRESS = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
175
+ SURFLIQUID_USDC_DECIMALS = 6;
176
+ SURFLIQUID_MIN_DEPOSIT = "0";
177
+ SURFLIQUID_APPROVAL_TARGET = 100000000000n;
178
+ SURFLIQUID_SUPPORTED_ASSETS = [
179
+ {
180
+ chainId: SURFLIQUID_CHAIN_ID,
181
+ chain: SURFLIQUID_CHAIN_NAME,
182
+ assets: [{ symbol: "USDC", minDepositAmount: SURFLIQUID_MIN_DEPOSIT }]
183
+ }
184
+ ];
185
+ SURFLIQUID_ACTION_MAP = {
186
+ // INITIAL_DEPOSIT is the user's real first deposit, so surface it like any
187
+ // other funding ("Top up"). The mini-app intentionally hides the "Deposit"
188
+ // action (it marks Zyfai's smart-account-creation event), which would
189
+ // otherwise drop a SurfLiquid user's only history row.
190
+ INITIAL_DEPOSIT: "Top up",
191
+ DEPOSIT: "Top up",
192
+ USER_DEPOSIT: "Top up",
193
+ WITHDRAWAL: "Withdraw",
194
+ USER_WITHDRAWAL: "Withdraw",
195
+ REBALANCE: "Rebalance",
196
+ REBALANCE_COMPLETED: "Rebalance",
197
+ CROSS_CHAIN_REBALANCE: "Rebalance",
198
+ MERKL_CLAIM: "Earned"
199
+ };
200
+ }
201
+ });
202
+
203
+ // src/agents/surfliquid/surfliquid.mapper.ts
204
+ function mapMessage(message) {
205
+ const action = SURFLIQUID_ACTION_MAP[message.transactionType];
206
+ if (!action) return null;
207
+ return {
208
+ agent: "surfliquid",
209
+ action,
210
+ date: message.timestamp,
211
+ oldApy: message.apyBefore != null ? String(message.apyBefore) : null,
212
+ newApy: message.apyAfter != null ? String(message.apyAfter) : null,
213
+ transactions: [
214
+ {
215
+ txHashes: [message.txHash],
216
+ chainId: message.chainId,
217
+ tokenSymbol: message.token ?? void 0,
218
+ amount: message.amount != null ? String(message.amount) : void 0
219
+ }
220
+ ],
221
+ rebalanceLog: []
222
+ };
223
+ }
224
+ function mapHistory(result, _chainId) {
225
+ const data = result.messages.map(mapMessage).filter((entry) => entry !== null);
226
+ const hasMore = result.page < result.pages;
227
+ return {
228
+ data,
229
+ hasMore,
230
+ nextCursor: hasMore ? String(result.page + 1) : void 0
231
+ };
232
+ }
233
+ function mapBalances2(vault, chainId, morphoStats) {
234
+ const assets = (vault.assets ?? []).filter(
235
+ (asset) => asset.assetSymbol === "USDC" && asset.chainId === chainId
236
+ );
237
+ const tokens = assets.map((asset) => ({
238
+ chain: SURFLIQUID_CHAIN_NAME,
239
+ chainId: SURFLIQUID_CHAIN_ID,
240
+ asset: "USDC",
241
+ amount: String(asset.currentValueUSD)
242
+ }));
243
+ const positions = assets.map((asset) => {
244
+ const stats = morphoStats?.get(asset.morphoVaultAddress?.toLowerCase() ?? "");
245
+ return {
246
+ chain: SURFLIQUID_CHAIN_NAME,
247
+ protocol: "Morpho",
248
+ // The specific MetaMorpho vault (e.g. "Gauntlet USDC Frontier"), read from
249
+ // Morpho — renders as "Morpho <vault>" alongside zyfai's vault detail.
250
+ pool: stats?.name ?? void 0,
251
+ asset: "USDC",
252
+ amount: String(asset.currentValueUSD),
253
+ apy: asset.currentAPY,
254
+ // TVL + liquidity come straight from Morpho (SurfLiquid's API omits them).
255
+ tvl: stats?.tvlUsd,
256
+ liquidity: stats?.liquidityUsd
257
+ };
258
+ });
259
+ const total = assets.reduce(
260
+ (sum, asset) => sum + asset.currentValueUSD,
261
+ 0
262
+ );
263
+ return {
264
+ smartWallet: vault.userVaultAddress ?? void 0,
265
+ totalBalance: String(total),
266
+ totalBalanceAsset: "usdc",
267
+ tokens,
268
+ positions
269
+ };
270
+ }
271
+ function mapAccountApy(vault, days) {
272
+ const breakdown = vault.apyBreakdown;
273
+ const windowed = breakdown ? breakdown[APY_WINDOW_KEY[days]] : void 0;
274
+ const apy = windowed != null ? windowed : breakdown?.currentAPY ?? 0;
275
+ return {
276
+ walletAddress: vault.walletAddress ?? "",
277
+ weightedApyAfterFee: apy,
278
+ apyByChainAndAsset: { [SURFLIQUID_CHAIN_ID]: { USDC: apy } },
279
+ // Parity with Sail: SurfLiquid's core-sdk exposes no daily net-APY series.
280
+ history: []
281
+ };
282
+ }
283
+ function mapEarnings2(vault, vaultAddress) {
284
+ return {
285
+ smartWallet: vaultAddress,
286
+ lifetimeEarnings: vault.earned?.totalEarningsUSD ?? 0,
287
+ tokens: []
288
+ };
289
+ }
290
+ function mapUserProfile2(vault, address) {
291
+ return {
292
+ address,
293
+ smartWallet: vault.userVaultAddress ?? "",
294
+ chains: [SURFLIQUID_CHAIN_ID],
295
+ // SurfLiquid uses cookie-based session auth — no session-key concept.
296
+ hasActiveSessionKey: false,
297
+ protocols: ["SurfLiquid"]
298
+ };
299
+ }
300
+ var APY_WINDOW_KEY;
301
+ var init_surfliquid_mapper = __esm({
302
+ "src/agents/surfliquid/surfliquid.mapper.ts"() {
303
+ "use strict";
304
+ init_surfliquid_constants();
305
+ APY_WINDOW_KEY = {
306
+ "7D": "apy7d",
307
+ "14D": "apy14d",
308
+ "30D": "apy30d"
309
+ };
310
+ }
311
+ });
312
+
313
+ // src/agents/surfliquid/surfliquid.wallet-adapter.ts
314
+ var import_core_sdk, OwneyWalletAdapter;
315
+ var init_surfliquid_wallet_adapter = __esm({
316
+ "src/agents/surfliquid/surfliquid.wallet-adapter.ts"() {
317
+ "use strict";
318
+ import_core_sdk = require("@surf_liquid/core-sdk");
319
+ OwneyWalletAdapter = class extends import_core_sdk.BaseWalletAdapter {
320
+ constructor(eip1193Provider) {
321
+ super();
322
+ this.eip1193Provider = eip1193Provider;
323
+ }
324
+ eip1193Provider;
325
+ name = "owney";
326
+ get installed() {
327
+ return true;
328
+ }
329
+ resolveProvider() {
330
+ return this.eip1193Provider;
331
+ }
332
+ };
333
+ }
334
+ });
335
+
336
+ // src/agents/surfliquid/surfliquid.morpho.ts
337
+ async function queryMorpho(query, vaultAddress, chainId) {
338
+ try {
339
+ const res = await fetch(MORPHO_API_URL, {
340
+ method: "POST",
341
+ headers: { "Content-Type": "application/json" },
342
+ body: JSON.stringify({
343
+ query,
344
+ variables: { address: vaultAddress, chainId }
345
+ })
346
+ });
347
+ if (!res.ok) return null;
348
+ const json = await res.json();
349
+ if (json.errors) return null;
350
+ return json.data ?? null;
351
+ } catch (error) {
352
+ console.warn("surfliquid: Morpho vault stats fetch failed", {
353
+ vaultAddress,
354
+ chainId,
355
+ error
356
+ });
357
+ return null;
358
+ }
359
+ }
360
+ function toStats(name, tvlUsd, liquidityUsd) {
361
+ if (typeof tvlUsd !== "number" || typeof liquidityUsd !== "number") {
362
+ return null;
363
+ }
364
+ return {
365
+ name: typeof name === "string" ? name : null,
366
+ tvlUsd,
367
+ liquidityUsd
368
+ };
369
+ }
370
+ async function fetchMorphoVaultStats(vaultAddress, chainId) {
371
+ const v1 = await queryMorpho(VAULT_STATS_QUERY, vaultAddress, chainId);
372
+ const vault = v1?.vaultByAddress;
373
+ const v1Stats = toStats(
374
+ vault?.name,
375
+ vault?.state?.totalAssetsUsd,
376
+ vault?.liquidity?.usd
377
+ );
378
+ if (v1Stats) return v1Stats;
379
+ const v2 = await queryMorpho(VAULT_V2_STATS_QUERY, vaultAddress, chainId);
380
+ const vaultV2 = v2?.vaultV2ByAddress;
381
+ return toStats(vaultV2?.name, vaultV2?.totalAssetsUsd, vaultV2?.liquidityUsd);
382
+ }
383
+ async function fetchMorphoStatsByVault(vaultAddresses, chainId) {
384
+ const unique = [...new Set(vaultAddresses.map((a) => a.toLowerCase()))];
385
+ const entries = await Promise.all(
386
+ unique.map(
387
+ async (address) => [address, await fetchMorphoVaultStats(address, chainId)]
388
+ )
389
+ );
390
+ const byVault = /* @__PURE__ */ new Map();
391
+ for (const [address, stats] of entries) {
392
+ if (stats) byVault.set(address, stats);
393
+ }
394
+ return byVault;
395
+ }
396
+ var MORPHO_API_URL, VAULT_STATS_QUERY, VAULT_V2_STATS_QUERY;
397
+ var init_surfliquid_morpho = __esm({
398
+ "src/agents/surfliquid/surfliquid.morpho.ts"() {
399
+ "use strict";
400
+ MORPHO_API_URL = "https://api.morpho.org/graphql";
401
+ VAULT_STATS_QUERY = `
402
+ query VaultStats($address: String!, $chainId: Int!) {
403
+ vaultByAddress(address: $address, chainId: $chainId) {
404
+ name
405
+ state { totalAssetsUsd }
406
+ liquidity { usd }
407
+ }
408
+ }
409
+ `;
410
+ VAULT_V2_STATS_QUERY = `
411
+ query VaultV2Stats($address: String!, $chainId: Int!) {
412
+ vaultV2ByAddress(address: $address, chainId: $chainId) {
413
+ name
414
+ totalAssetsUsd
415
+ liquidityUsd
416
+ }
417
+ }
418
+ `;
419
+ }
420
+ });
421
+
422
+ // src/agents/surfliquid/surfliquid.agent.ts
423
+ var surfliquid_agent_exports = {};
424
+ __export(surfliquid_agent_exports, {
425
+ SurfLiquidAgent: () => SurfLiquidAgent
426
+ });
427
+ var import_core_sdk2, import_viem6, import_chains2, APY_WINDOW_KEY2, SurfLiquidAgent;
428
+ var init_surfliquid_agent = __esm({
429
+ "src/agents/surfliquid/surfliquid.agent.ts"() {
430
+ "use strict";
431
+ import_core_sdk2 = require("@surf_liquid/core-sdk");
432
+ import_viem6 = require("viem");
433
+ import_chains2 = require("viem/chains");
434
+ init_errors();
435
+ init_surfliquid_constants();
436
+ init_permit2();
437
+ init_surfliquid_mapper();
438
+ init_surfliquid_wallet_adapter();
439
+ init_surfliquid_morpho();
440
+ APY_WINDOW_KEY2 = {
441
+ "7D": "apy7d",
442
+ "14D": "apy14d",
443
+ "30D": "apy30d"
444
+ };
445
+ SurfLiquidAgent = class {
446
+ constructor(config) {
447
+ this.config = config;
448
+ }
449
+ config;
450
+ id = "surfliquid";
451
+ supportedChainIds = [SURFLIQUID_CHAIN_ID];
452
+ supportedAssets = SURFLIQUID_SUPPORTED_ASSETS;
453
+ surf = null;
454
+ connectedAddress = null;
455
+ // In-flight connect+auth, shared by concurrent callers to dedupe SIWE.
456
+ connectPromise = null;
457
+ ensureSurf() {
458
+ if (this.surf) return this.surf;
459
+ this.surf = import_core_sdk2.SurfClient.create({
460
+ projectName: this.config.projectName,
461
+ appId: this.config.appId,
462
+ environment: "mainnet",
463
+ chainId: SURFLIQUID_CHAIN_ID,
464
+ apiBaseUrl: this.config.apiBaseUrl,
465
+ rpcUrl: this.config.rpcUrl,
466
+ // Let the SDK auto-approve USDC to the vault before depositing.
467
+ autoApprove: true
468
+ });
469
+ return this.surf;
470
+ }
471
+ /**
472
+ * Connects + authenticates the SurfClient against owney's provider. Skips the
473
+ * SIWE prompt when already authenticated for this wallet (cookie session).
474
+ */
475
+ async connect(state) {
476
+ const surf = this.ensureSurf();
477
+ const sameWallet = this.connectedAddress?.toLowerCase() === state.walletAddress.toLowerCase();
478
+ if (sameWallet && surf.getAuthState().authenticated) {
479
+ return surf;
480
+ }
481
+ if (this.connectPromise) return this.connectPromise;
482
+ this.connectPromise = (async () => {
483
+ surf.registerWalletAdapter(
484
+ "owney",
485
+ new OwneyWalletAdapter(state.provider)
486
+ );
487
+ await surf.connectWallet("owney");
488
+ await surf.authenticate();
489
+ this.connectedAddress = state.walletAddress;
490
+ return surf;
491
+ })();
492
+ try {
493
+ return await this.connectPromise;
494
+ } finally {
495
+ this.connectPromise = null;
496
+ }
497
+ }
498
+ /**
499
+ * Returns the user's vault address, deploying one on first use. SurfLiquid's
500
+ * deposit requires the vault to exist, so this must run before depositing.
501
+ * Existing vault → returned as-is; otherwise `deployVault()` is sent
502
+ * (user-pays) and the freshly deployed address is returned.
503
+ */
504
+ async resolveVaultAddress(surf, owner) {
505
+ const vault = await surf.getVault(owner).catch(() => null);
506
+ if (vault?.exists && vault.userVaultAddress) {
507
+ return vault.userVaultAddress;
508
+ }
509
+ const { vaultAddress } = await surf.deployVault();
510
+ return vaultAddress;
511
+ }
512
+ /**
513
+ * Grants the user's vault a standing USDC allowance so SurfLiquid's
514
+ * `autoApprove` has nothing to do — see {@link SURFLIQUID_APPROVAL_TARGET}
515
+ * for why one prompt per deposit is worth removing.
516
+ *
517
+ * Fails OPEN on a read error: if the allowance can't be checked we simply
518
+ * return and let SurfLiquid's own per-deposit approve run, because a flaky
519
+ * RPC must never block a deposit. A failed or rejected APPROVE does
520
+ * propagate — the user declined a transaction, and letting SurfLiquid
521
+ * immediately ask again for a smaller one is exactly the double prompt this
522
+ * removes.
523
+ */
524
+ async ensureVaultAllowance(state, vaultAddress, amount) {
525
+ const spender = vaultAddress;
526
+ const transport = (0, import_viem6.custom)(state.provider);
527
+ const publicClient = (0, import_viem6.createPublicClient)({ chain: import_chains2.base, transport });
528
+ let allowance;
529
+ try {
530
+ allowance = await publicClient.readContract({
531
+ address: SURFLIQUID_USDC_ADDRESS,
532
+ abi: ERC20_ALLOWANCE_ABI,
533
+ functionName: "allowance",
534
+ args: [state.walletAddress, spender]
535
+ });
536
+ } catch (error) {
537
+ console.warn(
538
+ "[owney-sdk] SurfLiquid allowance pre-check failed; deferring to its per-deposit approve:",
539
+ error instanceof Error ? error.message : String(error)
540
+ );
541
+ return;
542
+ }
543
+ if (allowance >= amount) return;
544
+ const target = amount > SURFLIQUID_APPROVAL_TARGET ? amount : SURFLIQUID_APPROVAL_TARGET;
545
+ const wallet = (0, import_viem6.createWalletClient)({
546
+ account: state.walletAddress,
547
+ chain: import_chains2.base,
548
+ transport
549
+ });
550
+ const hash = await wallet.writeContract({
551
+ address: SURFLIQUID_USDC_ADDRESS,
552
+ abi: ERC20_ALLOWANCE_ABI,
553
+ functionName: "approve",
554
+ args: [spender, target],
555
+ account: state.walletAddress,
556
+ chain: import_chains2.base
557
+ });
558
+ const receipt = await publicClient.waitForTransactionReceipt({
559
+ hash,
560
+ confirmations: 1
561
+ });
562
+ if (receipt.status !== "success") {
563
+ throw new Error(`SurfLiquid USDC approval reverted (tx ${hash})`);
564
+ }
565
+ }
566
+ // --- IAgent: lifecycle ---
567
+ async disconnect() {
568
+ if (this.surf) {
569
+ await this.surf.disconnectWallet();
570
+ }
571
+ this.connectedAddress = null;
572
+ }
573
+ async activateAgent(state) {
574
+ await this.connect(state);
575
+ }
576
+ // --- IAgent: funds (user-pays; asset is always USDC, callback ignored) ---
577
+ async deposit(state, _chainId, amount, _asset, _depositCallback) {
578
+ const surf = await this.connect(state);
579
+ const human = (0, import_viem6.formatUnits)(BigInt(amount), SURFLIQUID_USDC_DECIMALS);
580
+ const smartWallet = await this.resolveVaultAddress(
581
+ surf,
582
+ state.walletAddress
583
+ );
584
+ await this.ensureVaultAllowance(state, smartWallet, BigInt(amount));
585
+ const tx = await surf.deposit({
586
+ asset: SURFLIQUID_USDC_ADDRESS,
587
+ amount: human
588
+ });
589
+ await tx.wait();
590
+ return { txHash: tx.hash, smartWallet, amount };
591
+ }
592
+ async withdraw(state, _chainId, _token, amount) {
593
+ const surf = await this.connect(state);
594
+ const human = amount != null ? (0, import_viem6.formatUnits)(BigInt(amount), SURFLIQUID_USDC_DECIMALS) : void 0;
595
+ try {
596
+ const tx = await surf.withdraw({
597
+ asset: SURFLIQUID_USDC_ADDRESS,
598
+ amount: human
599
+ });
600
+ await tx.wait();
601
+ return {
602
+ txHash: tx.hash,
603
+ type: amount != null ? "partial" : "full",
604
+ amount: amount ?? "0"
605
+ };
606
+ } catch (error) {
607
+ throw new OwneyError(
608
+ "WITHDRAW_FAILED",
609
+ error instanceof Error ? error.message : "SurfLiquid withdraw failed.",
610
+ { chainId: SURFLIQUID_CHAIN_ID, amount },
611
+ this.id
612
+ );
613
+ }
614
+ }
615
+ // --- IAgent: portfolio reads ---
616
+ async getBalances(state, chainId) {
617
+ const surf = await this.connect(state);
618
+ const vault = await surf.getVault(state.walletAddress);
619
+ const morphoVaultAddresses = (vault.assets ?? []).filter((asset) => asset.assetSymbol === "USDC" && asset.chainId === chainId).map((asset) => asset.morphoVaultAddress).filter((address) => Boolean(address));
620
+ const morphoStats = await fetchMorphoStatsByVault(
621
+ morphoVaultAddresses,
622
+ chainId
623
+ );
624
+ return mapBalances2(vault, chainId, morphoStats);
625
+ }
626
+ async getEarnings(state) {
627
+ const surf = await this.connect(state);
628
+ const vault = await surf.getVault(state.walletAddress);
629
+ return mapEarnings2(vault, vault.userVaultAddress ?? state.walletAddress);
630
+ }
631
+ async getAccountApy(state, _chainId, days, _tokenSymbol) {
632
+ const surf = await this.connect(state);
633
+ const vault = await surf.getVault(state.walletAddress);
634
+ return mapAccountApy(vault, days);
635
+ }
636
+ async getHistory(state, chainId, options) {
637
+ const surf = await this.connect(state);
638
+ const page = options?.cursor ? Number(options.cursor) : 1;
639
+ const limit = options?.limit ?? 10;
640
+ const result = await surf.getAgentMessages(
641
+ state.walletAddress,
642
+ page,
643
+ limit,
644
+ options?.fromDate,
645
+ options?.toDate
646
+ );
647
+ return mapHistory(result, chainId);
648
+ }
649
+ async getUserProfile(state) {
650
+ const surf = await this.connect(state);
651
+ const vault = await surf.getVault(state.walletAddress);
652
+ return mapUserProfile2(vault, state.walletAddress);
653
+ }
654
+ // --- IAgent: discovery (no wallet) ---
655
+ async getAgentApy(days, _options) {
656
+ const surf = this.ensureSurf();
657
+ const assets = await surf.getSupportedAssets(SURFLIQUID_CHAIN_ID);
658
+ const usdc = assets.find((asset) => asset.assetSymbol === "USDC");
659
+ if (!usdc) return { averageApy: 0 };
660
+ const windowed = usdc[APY_WINDOW_KEY2[days]];
661
+ return { averageApy: windowed != null ? windowed : usdc.currentAPY };
662
+ }
663
+ };
664
+ }
665
+ });
666
+
20
667
  // src/index.ts
21
668
  var index_exports = {};
22
669
  __export(index_exports, {
@@ -89,56 +736,8 @@ var SupportedAssets = [
89
736
  }
90
737
  ];
91
738
 
92
- // src/errors.ts
93
- var OwneyError = class extends Error {
94
- code;
95
- details;
96
- agentId;
97
- constructor(code, message, details, agentId) {
98
- const prefix = agentId ? `[${code}][agent:${agentId}]` : `[${code}]`;
99
- super(`${prefix} ${message}`);
100
- this.name = "OwneyError";
101
- this.code = code;
102
- this.details = details;
103
- this.agentId = agentId;
104
- }
105
- };
106
- var AgentNotFoundError = class extends OwneyError {
107
- constructor(agentId, available) {
108
- super(
109
- "AGENT_NOT_FOUND",
110
- `Unknown agent "${agentId}". Available agents: ${available.join(", ")}`,
111
- { agentId, available },
112
- agentId
113
- );
114
- this.name = "AgentNotFoundError";
115
- }
116
- };
117
- var NotConnectedError = class extends OwneyError {
118
- constructor() {
119
- super("NOT_CONNECTED", "Not connected. Call sdk.connect(provider) first.");
120
- this.name = "NotConnectedError";
121
- }
122
- };
123
- var AgentChainIncompatibleError = class extends OwneyError {
124
- incompatibleAgents;
125
- connectedChainId;
126
- constructor(incompatibleAgents, connectedChainId) {
127
- const details = incompatibleAgents.map(
128
- ({ agentId, supportedChainIds }) => `"${agentId}" supports chains [${supportedChainIds.join(", ")}]`
129
- ).join("; ");
130
- super(
131
- "AGENT_CHAIN_INCOMPATIBLE",
132
- `Chain ${connectedChainId} is not supported by the following agents: ${details}`,
133
- { incompatibleAgents, connectedChainId }
134
- );
135
- this.name = "AgentChainIncompatibleError";
136
- this.incompatibleAgents = incompatibleAgents;
137
- this.connectedChainId = connectedChainId;
138
- }
139
- };
140
-
141
739
  // src/lib/utils.ts
740
+ init_errors();
142
741
  var isValidChainId = (chainId) => {
143
742
  if (!SUPPORTED_CHAIN_IDS.includes(chainId)) {
144
743
  throw new OwneyError(
@@ -149,6 +748,12 @@ var isValidChainId = (chainId) => {
149
748
  }
150
749
  return chainId;
151
750
  };
751
+ function isSameAsset(tokenSymbol, asset) {
752
+ const token = tokenSymbol.toLowerCase();
753
+ const target = asset.toLowerCase();
754
+ if (token === target) return true;
755
+ return target === "weth" && token === "eth" || target === "eth" && token === "weth";
756
+ }
152
757
  function hexToDecimal(hex, decimals = 6) {
153
758
  const normalized = hex.startsWith("0x") || hex.startsWith("0X") ? hex : `0x${hex}`;
154
759
  const parsed = BigInt(normalized);
@@ -182,6 +787,10 @@ var DECIMALS_BY_SYMBOL = {
182
787
  wbtc: 8,
183
788
  cbbtc: 8
184
789
  };
790
+ function canonicalSymbol(symbol) {
791
+ const upper = symbol.toUpperCase();
792
+ return upper === "ETH" ? "WETH" : upper;
793
+ }
185
794
  function tokenDecimals(symbol, explicit) {
186
795
  if (typeof explicit === "number" && Number.isFinite(explicit))
187
796
  return explicit;
@@ -246,55 +855,89 @@ function mapPendingAllocations(raw) {
246
855
  function mapBalances(raw, _chainId, smartWallet) {
247
856
  const portfolio = raw.portfolio;
248
857
  const portfolioByChain = portfolio.portfolioByChain ?? {};
858
+ const rawPositions = portfolio.positions ?? [];
859
+ const detailByKey = /* @__PURE__ */ new Map();
860
+ for (const p of rawPositions) {
861
+ const cid = resolveChainId(p.chain ?? "");
862
+ const symbol = canonicalSymbol(p.token_symbol ?? "");
863
+ if (Number.isFinite(cid) && symbol) detailByKey.set(`${cid}:${symbol}`, p);
864
+ }
249
865
  let totalBalance = 0;
250
866
  const tokens = [];
867
+ const positions = [];
251
868
  for (const [chainIdKey, tokensBySymbol] of Object.entries(portfolioByChain)) {
252
869
  const positionChainId = Number(chainIdKey);
870
+ const chainName2 = CHAIN_ID_TO_NAME[positionChainId] ?? "BASE";
253
871
  for (const [
254
872
  symbol,
255
873
  { balance, balanceWithFee, decimals }
256
874
  ] of Object.entries(tokensBySymbol ?? {})) {
257
- const amount = hexToDecimal(balanceWithFee ?? balance ?? "0", decimals);
875
+ const amount = hexToDecimal(
876
+ balanceWithFee ?? balance ?? "0",
877
+ tokenDecimals(symbol, decimals)
878
+ );
258
879
  totalBalance += amount;
259
880
  tokens.push({
260
- chain: CHAIN_ID_TO_NAME[positionChainId] ?? "BASE",
881
+ chain: chainName2,
261
882
  chainId: positionChainId,
262
883
  asset: symbol,
263
884
  amount: String(amount)
264
885
  });
886
+ const detail = detailByKey.get(
887
+ `${positionChainId}:${canonicalSymbol(symbol)}`
888
+ );
889
+ positions.push({
890
+ chain: chainName2,
891
+ protocol: detail?.protocol_name ?? detail?.protocol_id ?? "",
892
+ protocolId: detail?.protocol_id,
893
+ pool: detail?.pool,
894
+ // Report the token the way the rest of owney spells it ("WETH", as the
895
+ // pre-portfolioByChain mapper did), not Zyfai's internal "eth".
896
+ asset: canonicalSymbol(symbol),
897
+ amount: String(amount),
898
+ apy: detail?.pool_apy,
899
+ tvl: detail?.pool_tvl,
900
+ // PositionSlot has no `liquidity` field yet; Zyfai will add it. Narrow
901
+ // structural read keeps it undefined today and auto-populates later.
902
+ liquidity: detail?.liquidity
903
+ });
265
904
  }
266
905
  }
267
- const positions = (portfolio.positions ?? []).map((p) => ({
268
- chain: p.chain ?? "",
269
- protocol: p.protocol_name ?? p.protocol_id ?? "",
270
- protocolId: p.protocol_id,
271
- pool: p.pool,
272
- asset: p.token_symbol ?? "",
273
- // Fee-adjusted underlying amounts are decimal base-unit strings. The
274
- // legacy `amount` fallback is hex. Runtime
275
- // getBalances payloads often carry one (e.g. 18 for WETH) — prefer that
276
- // when present, otherwise fall back to the per-symbol lookup.
277
- amount: String(
278
- p.underlyingAmountWithFee ?? p.underlyingAmount ? normalizeDecimalPlace(
279
- p.underlyingAmountWithFee ?? p.underlyingAmount ?? "0",
280
- tokenDecimals(
281
- p.token_symbol,
282
- p.decimals
283
- )
284
- ) : hexToDecimal(
285
- p.amount || "0",
286
- tokenDecimals(
287
- p.token_symbol,
288
- p.decimals
289
- )
290
- )
291
- ),
292
- apy: p.pool_apy,
293
- tvl: p.pool_tvl,
294
- // PositionSlot has no `liquidity` field yet; Zyfai will add it. Narrow
295
- // structural read keeps it undefined today and auto-populates later.
296
- liquidity: p.liquidity
297
- }));
906
+ if (positions.length === 0) {
907
+ positions.push(
908
+ ...(portfolio.positions ?? []).map((p) => ({
909
+ chain: p.chain ?? "",
910
+ protocol: p.protocol_name ?? p.protocol_id ?? "",
911
+ protocolId: p.protocol_id,
912
+ pool: p.pool,
913
+ asset: p.token_symbol ?? "",
914
+ // Fee-adjusted underlying amounts are decimal base-unit strings; the
915
+ // legacy `amount` fallback is hex. Runtime getBalances payloads often
916
+ // carry `decimals` (e.g. 18 for WETH) — prefer it when present,
917
+ // otherwise fall back to the per-symbol lookup.
918
+ amount: String(
919
+ p.underlyingAmountWithFee ?? p.underlyingAmount ? normalizeDecimalPlace(
920
+ p.underlyingAmountWithFee ?? p.underlyingAmount ?? "0",
921
+ tokenDecimals(
922
+ p.token_symbol,
923
+ p.decimals
924
+ )
925
+ ) : hexToDecimal(
926
+ p.amount || "0",
927
+ tokenDecimals(
928
+ p.token_symbol,
929
+ p.decimals
930
+ )
931
+ )
932
+ ),
933
+ apy: p.pool_apy,
934
+ tvl: p.pool_tvl,
935
+ // PositionSlot has no `liquidity` field yet; Zyfai will add it. Narrow
936
+ // structural read keeps it undefined today and auto-populates later.
937
+ liquidity: p.liquidity
938
+ }))
939
+ );
940
+ }
298
941
  const pendingAllocations = mapPendingAllocations(
299
942
  portfolio.staleBalances
300
943
  );
@@ -589,6 +1232,9 @@ function decodeHistoryCursor(token) {
589
1232
  return parsed;
590
1233
  }
591
1234
 
1235
+ // src/agents/zyfai/zyfai.agent.ts
1236
+ init_errors();
1237
+
592
1238
  // src/agents/zyfai/zyfai.auth-cache.ts
593
1239
  var KEY_PREFIX = "owney.zyfai.session";
594
1240
  var storage = () => {
@@ -1377,6 +2023,37 @@ var ZyfaiAgent = class {
1377
2023
  );
1378
2024
  }
1379
2025
  }
2026
+ /**
2027
+ * Records the deposit with Zyfai, retrying transient failures.
2028
+ *
2029
+ * This runs AFTER the transfer has already landed on-chain, so it must never
2030
+ * fail the deposit — the funds moved. But it is also the ONLY source of the
2031
+ * "Top up wallet" entry the history is built from: Zyfai auto-deploys the
2032
+ * balance it detects either way, so when this call is lost the user's deposit
2033
+ * never appears in Activity (an earlier withdrawal stays the newest row) and
2034
+ * nothing ever backfills it. Retry, then log loudly enough to be recoverable.
2035
+ */
2036
+ async logDepositWithRetry(chainId, txHash, amount, tokenAddress) {
2037
+ const ATTEMPTS = 3;
2038
+ const RETRY_DELAY_MS = 1e3;
2039
+ for (let attempt = 1; attempt <= ATTEMPTS; attempt++) {
2040
+ try {
2041
+ await (tokenAddress ? this.sdk.logDeposit(chainId, txHash, amount, tokenAddress) : this.sdk.logDeposit(chainId, txHash, amount));
2042
+ return;
2043
+ } catch (logError) {
2044
+ if (attempt === ATTEMPTS) {
2045
+ console.error(
2046
+ "[owney-sdk] Deposit landed on-chain but logDeposit failed \u2014 it will be missing from Zyfai history:",
2047
+ { txHash, chainId, amount, tokenAddress, error: logError }
2048
+ );
2049
+ return;
2050
+ }
2051
+ await new Promise(
2052
+ (resolve) => setTimeout(resolve, RETRY_DELAY_MS * attempt)
2053
+ );
2054
+ }
2055
+ }
2056
+ }
1380
2057
  /**
1381
2058
  * True when `smartWallet` is the backend-managed pool wallet Zyfai assigned
1382
2059
  * to this EOA — the "new wallet" kind that is predeployed on the chains it
@@ -1680,14 +2357,12 @@ var ZyfaiAgent = class {
1680
2357
  );
1681
2358
  const txHash = await depositCallback(smartWallet, validChainId, amount);
1682
2359
  const tokenAddress = asset === "WETH" ? WETH_ADDRESS_BY_CHAIN[validChainId] : void 0;
1683
- try {
1684
- await (tokenAddress ? this.sdk.logDeposit(validChainId, txHash, amount, tokenAddress) : this.sdk.logDeposit(validChainId, txHash, amount));
1685
- } catch (logError) {
1686
- console.warn(
1687
- "[owney-sdk] Deposit landed on-chain but logDeposit failed (non-fatal):",
1688
- logError
1689
- );
1690
- }
2360
+ await this.logDepositWithRetry(
2361
+ validChainId,
2362
+ txHash,
2363
+ amount,
2364
+ tokenAddress
2365
+ );
1691
2366
  return { txHash, smartWallet, amount };
1692
2367
  }
1693
2368
  await this.ensureWalletDeployed(this.getAddress(), validChainId);
@@ -1842,7 +2517,11 @@ var ZyfaiAgent = class {
1842
2517
  }
1843
2518
  };
1844
2519
 
2520
+ // src/client.ts
2521
+ init_errors();
2522
+
1845
2523
  // src/lib/routing-api.ts
2524
+ init_errors();
1846
2525
  var ROUTING_API_BASE_URL = "https://owney-routing-api-243946518160.europe-west4.run.app";
1847
2526
  async function fetchOrgAgentConfig(apiKey) {
1848
2527
  const url = `${ROUTING_API_BASE_URL}/api/v1/agent/org-config`;
@@ -1907,6 +2586,7 @@ async function fetchAgentKeys(apiKey) {
1907
2586
  }
1908
2587
 
1909
2588
  // src/lib/health-report.ts
2589
+ init_errors();
1910
2590
  var ROUTING_API_BASE_URL2 = "https://owney-routing-api-243946518160.europe-west4.run.app";
1911
2591
  async function reportAgentFailure(apiKey, agentType, errorCode) {
1912
2592
  try {
@@ -1942,11 +2622,10 @@ async function withFailureReporting(apiKey, agentType, fn) {
1942
2622
  // src/lib/helpers/withdraw-helper.ts
1943
2623
  var import_viem2 = require("viem");
1944
2624
  function projectAgentBalancesForAsset(agents, aggregated, chainId, asset, decimals) {
1945
- const target = asset.toUpperCase();
1946
2625
  return agents.map((agent) => {
1947
2626
  const agentBalance = aggregated[agent.id];
1948
2627
  const tokenBalance = agentBalance?.tokens.find(
1949
- (t) => t.chainId === chainId && t.asset.toUpperCase() === target
2628
+ (t) => t.chainId === chainId && isSameAsset(t.asset, asset)
1950
2629
  );
1951
2630
  if (!tokenBalance) return { agent, balance: 0n };
1952
2631
  return { agent, balance: (0, import_viem2.parseUnits)(tokenBalance.amount, decimals) };
@@ -2056,8 +2735,11 @@ function aggregateApyByChainAndAsset(agentApys, agentBalances) {
2056
2735
  }
2057
2736
 
2058
2737
  // src/client.ts
2059
- var import_viem6 = require("viem");
2060
- var import_chains2 = require("viem/chains");
2738
+ var import_viem7 = require("viem");
2739
+ var import_chains3 = require("viem/chains");
2740
+
2741
+ // src/lib/sponsored-deposit.ts
2742
+ init_errors();
2061
2743
 
2062
2744
  // src/lib/transfer-auth.ts
2063
2745
  var import_viem3 = require("viem");
@@ -2096,12 +2778,13 @@ function randomAuthNonce() {
2096
2778
  }
2097
2779
 
2098
2780
  // src/lib/sponsor-client.ts
2781
+ init_errors();
2099
2782
  var ROUTING_API_BASE_URL3 = "https://owney-routing-api-243946518160.europe-west4.run.app";
2100
2783
  async function postSponsorTransferAuth(input) {
2101
- const base3 = input.baseUrl ?? ROUTING_API_BASE_URL3;
2784
+ const base4 = input.baseUrl ?? ROUTING_API_BASE_URL3;
2102
2785
  let res;
2103
2786
  try {
2104
- res = await fetch(`${base3}/api/v1/sponsor/erc20-transfer-auth`, {
2787
+ res = await fetch(`${base4}/api/v1/sponsor/erc20-transfer-auth`, {
2105
2788
  method: "POST",
2106
2789
  headers: {
2107
2790
  "content-type": "application/json",
@@ -2138,10 +2821,10 @@ async function postSponsorTransferAuth(input) {
2138
2821
  return parsed.data;
2139
2822
  }
2140
2823
  async function postSponsorPermit2Transfer(input) {
2141
- const base3 = input.baseUrl ?? ROUTING_API_BASE_URL3;
2824
+ const base4 = input.baseUrl ?? ROUTING_API_BASE_URL3;
2142
2825
  let res;
2143
2826
  try {
2144
- res = await fetch(`${base3}/api/v1/sponsor/permit2-transfer`, {
2827
+ res = await fetch(`${base4}/api/v1/sponsor/permit2-transfer`, {
2145
2828
  method: "POST",
2146
2829
  headers: {
2147
2830
  "content-type": "application/json",
@@ -2176,11 +2859,11 @@ async function postSponsorPermit2Transfer(input) {
2176
2859
  return parsed.data;
2177
2860
  }
2178
2861
  async function getSponsorRelayerAddress(input) {
2179
- const base3 = input.baseUrl ?? ROUTING_API_BASE_URL3;
2862
+ const base4 = input.baseUrl ?? ROUTING_API_BASE_URL3;
2180
2863
  let res;
2181
2864
  try {
2182
2865
  res = await fetch(
2183
- `${base3}/api/v1/sponsor/relayer-address?chainId=${input.chainId}`,
2866
+ `${base4}/api/v1/sponsor/relayer-address?chainId=${input.chainId}`,
2184
2867
  {
2185
2868
  headers: { "x-owney-api-key": input.apiKey }
2186
2869
  }
@@ -2212,85 +2895,11 @@ async function getSponsorRelayerAddress(input) {
2212
2895
  return parsed.data.relayer;
2213
2896
  }
2214
2897
 
2215
- // src/lib/permit2.ts
2216
- var import_viem4 = require("viem");
2217
- var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
2218
- var MAX_UINT256 = 2n ** 256n - 1n;
2219
- var ERC20_ALLOWANCE_ABI = [
2220
- {
2221
- type: "function",
2222
- name: "allowance",
2223
- stateMutability: "view",
2224
- inputs: [
2225
- { name: "owner", type: "address" },
2226
- { name: "spender", type: "address" }
2227
- ],
2228
- outputs: [{ name: "", type: "uint256" }]
2229
- },
2230
- {
2231
- type: "function",
2232
- name: "approve",
2233
- stateMutability: "nonpayable",
2234
- inputs: [
2235
- { name: "spender", type: "address" },
2236
- { name: "amount", type: "uint256" }
2237
- ],
2238
- outputs: [{ name: "", type: "bool" }]
2239
- },
2240
- {
2241
- type: "function",
2242
- name: "balanceOf",
2243
- stateMutability: "view",
2244
- inputs: [{ name: "account", type: "address" }],
2245
- outputs: [{ name: "", type: "uint256" }]
2246
- }
2247
- ];
2248
- function buildPermitTransferFromTypedData(input) {
2249
- return {
2250
- domain: {
2251
- name: "Permit2",
2252
- chainId: input.chainId,
2253
- verifyingContract: PERMIT2_ADDRESS
2254
- },
2255
- types: {
2256
- PermitTransferFrom: [
2257
- { name: "permitted", type: "TokenPermissions" },
2258
- { name: "spender", type: "address" },
2259
- { name: "nonce", type: "uint256" },
2260
- { name: "deadline", type: "uint256" }
2261
- ],
2262
- TokenPermissions: [
2263
- { name: "token", type: "address" },
2264
- { name: "amount", type: "uint256" }
2265
- ]
2266
- },
2267
- primaryType: "PermitTransferFrom",
2268
- message: input.message
2269
- };
2270
- }
2271
- function randomPermit2Nonce() {
2272
- const bytes = new Uint8Array(32);
2273
- globalThis.crypto.getRandomValues(bytes);
2274
- return BigInt((0, import_viem4.bytesToHex)(bytes));
2275
- }
2276
- async function readPermit2Allowance(publicClient, token, owner) {
2277
- return publicClient.readContract({
2278
- address: token,
2279
- abi: ERC20_ALLOWANCE_ABI,
2280
- functionName: "allowance",
2281
- args: [owner, PERMIT2_ADDRESS]
2282
- });
2283
- }
2284
- async function readErc20Balance(publicClient, token, owner) {
2285
- return publicClient.readContract({
2286
- address: token,
2287
- abi: ERC20_ALLOWANCE_ABI,
2288
- functionName: "balanceOf",
2289
- args: [owner]
2290
- });
2291
- }
2898
+ // src/lib/sponsored-deposit.ts
2899
+ init_permit2();
2292
2900
 
2293
2901
  // src/lib/chain-guard.ts
2902
+ init_errors();
2294
2903
  var CHAIN_NAMES = {
2295
2904
  1: "Ethereum",
2296
2905
  8453: "Base",
@@ -2404,6 +3013,8 @@ function makeSponsoredDepositCallback(deps) {
2404
3013
  }
2405
3014
 
2406
3015
  // src/lib/sponsored-weth-deposit.ts
3016
+ init_errors();
3017
+ init_permit2();
2407
3018
  var PERMIT_WINDOW_SECONDS = 15 * 60;
2408
3019
  function makeSponsoredWethCallback(deps) {
2409
3020
  const get = deps.httpGet ?? getSponsorRelayerAddress;
@@ -2488,6 +3099,7 @@ function makeSponsoredWethCallback(deps) {
2488
3099
 
2489
3100
  // src/lib/sponsored-calls-deposit.ts
2490
3101
  var import_viem5 = require("viem");
3102
+ init_errors();
2491
3103
  var DEFAULT_POLL_INTERVAL_MS = 1500;
2492
3104
  var DEFAULT_MAX_POLLS = 30;
2493
3105
  async function paymasterSupported(provider, owner, chainId) {
@@ -2578,6 +3190,7 @@ function makeSponsoredCallsCallback(deps) {
2578
3190
  }
2579
3191
 
2580
3192
  // src/client.ts
3193
+ init_permit2();
2581
3194
  function encodeMultiAgentCursor(map) {
2582
3195
  return Buffer.from(JSON.stringify(map), "utf8").toString("base64");
2583
3196
  }
@@ -2604,10 +3217,11 @@ var SPONSORED_USDC_BY_CHAIN = {
2604
3217
  1: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
2605
3218
  };
2606
3219
  var VIEM_CHAIN2 = {
2607
- 8453: import_chains2.base,
2608
- 42161: import_chains2.arbitrum,
2609
- 1: import_chains2.mainnet
3220
+ 8453: import_chains3.base,
3221
+ 42161: import_chains3.arbitrum,
3222
+ 1: import_chains3.mainnet
2610
3223
  };
3224
+ var AGENT_ELIGIBILITY_ORDER = ["surfliquid", "zyfai", "sail"];
2611
3225
  var SPONSORED_WETH_BY_CHAIN = {
2612
3226
  8453: "0x4200000000000000000000000000000000000006",
2613
3227
  42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
@@ -2636,6 +3250,7 @@ var OwneySDK = class {
2636
3250
  zyfaiRpcUrls;
2637
3251
  routingApiBaseUrl;
2638
3252
  referralSource;
3253
+ surfliquidApiBaseUrl;
2639
3254
  cachedSponsoredCallback = null;
2640
3255
  cachedWethSponsoredCallback = null;
2641
3256
  paymasterServiceUrl;
@@ -2648,6 +3263,7 @@ var OwneySDK = class {
2648
3263
  this.routingApiBaseUrl = config.routingApiBaseUrl;
2649
3264
  this.paymasterServiceUrl = config.paymasterServiceUrl;
2650
3265
  this.referralSource = config.referralSource;
3266
+ this.surfliquidApiBaseUrl = config.surfliquidApiBaseUrl;
2651
3267
  }
2652
3268
  /**
2653
3269
  * Establish connection state for all subsequent SDK calls.
@@ -2747,14 +3363,14 @@ var OwneySDK = class {
2747
3363
  // Casts work around viem's chain-narrowed Client vs the generic
2748
3364
  // PublicClient/WalletClient param types — structurally identical at
2749
3365
  // runtime, but the two share a name TS treats as unrelated.
2750
- getPublicClient: (cid) => (0, import_viem6.createPublicClient)({
3366
+ getPublicClient: (cid) => (0, import_viem7.createPublicClient)({
2751
3367
  chain: VIEM_CHAIN2[cid],
2752
- transport: (0, import_viem6.custom)(provider)
3368
+ transport: (0, import_viem7.custom)(provider)
2753
3369
  }),
2754
- getWalletClient: (cid) => (0, import_viem6.createWalletClient)({
3370
+ getWalletClient: (cid) => (0, import_viem7.createWalletClient)({
2755
3371
  account: owner,
2756
3372
  chain: VIEM_CHAIN2[cid],
2757
- transport: (0, import_viem6.custom)(provider)
3373
+ transport: (0, import_viem7.custom)(provider)
2758
3374
  })
2759
3375
  });
2760
3376
  if (!onApproved) this.cachedSponsoredCallback = callback;
@@ -2800,14 +3416,14 @@ var OwneySDK = class {
2800
3416
  // Casts work around viem's chain-narrowed Client vs the generic
2801
3417
  // PublicClient/WalletClient param types — structurally identical at
2802
3418
  // runtime, but the two share a name TS treats as unrelated.
2803
- getPublicClient: (cid) => (0, import_viem6.createPublicClient)({
3419
+ getPublicClient: (cid) => (0, import_viem7.createPublicClient)({
2804
3420
  chain: VIEM_CHAIN2[cid],
2805
- transport: (0, import_viem6.custom)(provider)
3421
+ transport: (0, import_viem7.custom)(provider)
2806
3422
  }),
2807
- getWalletClient: (cid) => (0, import_viem6.createWalletClient)({
3423
+ getWalletClient: (cid) => (0, import_viem7.createWalletClient)({
2808
3424
  account: owner,
2809
3425
  chain: VIEM_CHAIN2[cid],
2810
- transport: (0, import_viem6.custom)(provider)
3426
+ transport: (0, import_viem7.custom)(provider)
2811
3427
  })
2812
3428
  });
2813
3429
  if (!onApproved) this.cachedWethSponsoredCallback = callback;
@@ -2877,8 +3493,8 @@ var OwneySDK = class {
2877
3493
  this.initializingAgentsPromise = (async () => {
2878
3494
  const agentKeys = await fetchAgentKeys(this.apiKey);
2879
3495
  this.disabledAgents.clear();
2880
- for (const { key: key2, agent_type, is_enabled } of agentKeys) {
2881
- const agent = this.createAgent(agent_type, key2);
3496
+ for (const { key: key2, agent_type, project_name, is_enabled } of agentKeys) {
3497
+ const agent = await this.createAgent(agent_type, key2, project_name);
2882
3498
  if (!agent) continue;
2883
3499
  this.agents.set(agent_type, agent);
2884
3500
  if (is_enabled === false) {
@@ -2899,10 +3515,22 @@ var OwneySDK = class {
2899
3515
  this.initializingAgentsPromise = null;
2900
3516
  }
2901
3517
  }
2902
- createAgent(agentId, key2) {
3518
+ async createAgent(agentId, key2, projectName) {
2903
3519
  if (agentId === "zyfai") {
2904
3520
  return new ZyfaiAgent(key2, this.zyfaiRpcUrls, this.referralSource);
2905
3521
  }
3522
+ if (agentId === "surfliquid") {
3523
+ if (!projectName) return null;
3524
+ const { SurfLiquidAgent: SurfLiquidAgent2 } = await Promise.resolve().then(() => (init_surfliquid_agent(), surfliquid_agent_exports));
3525
+ return new SurfLiquidAgent2({
3526
+ projectName,
3527
+ appId: key2,
3528
+ // Same-origin proxy override — SurfLiquid auth is an httpOnly cookie,
3529
+ // which WebKit (iOS) discards when it is third-party. See
3530
+ // OwneySDKConfig.surfliquidApiBaseUrl.
3531
+ apiBaseUrl: this.surfliquidApiBaseUrl
3532
+ });
3533
+ }
2906
3534
  return null;
2907
3535
  }
2908
3536
  // --- Account lifecycle ---
@@ -3057,18 +3685,30 @@ var OwneySDK = class {
3057
3685
  );
3058
3686
  }
3059
3687
  const agentResults = {};
3688
+ const agentErrors = {};
3060
3689
  for (const { agent, amount: agentAmount } of agentAmounts) {
3061
- agentResults[agent.id] = await this.depositWithFallback(
3062
- agent,
3063
- state,
3064
- chainId,
3065
- agentAmount,
3066
- asset,
3067
- effectiveCallback,
3068
- depositCallback
3690
+ try {
3691
+ agentResults[agent.id] = await this.depositWithFallback(
3692
+ agent,
3693
+ state,
3694
+ chainId,
3695
+ agentAmount,
3696
+ asset,
3697
+ effectiveCallback,
3698
+ depositCallback
3699
+ );
3700
+ } catch (error) {
3701
+ agentErrors[agent.id] = error instanceof Error ? error.message : String(error);
3702
+ }
3703
+ }
3704
+ if (Object.keys(agentResults).length === 0) {
3705
+ throw new OwneyError(
3706
+ "DEPOSIT_ALL_FAILED",
3707
+ `All ${agentAmounts.length} agent deposit(s) failed.`,
3708
+ { agentErrors }
3069
3709
  );
3070
3710
  }
3071
- return { agentResults };
3711
+ return Object.keys(agentErrors).length > 0 ? { agentResults, agentErrors } : { agentResults };
3072
3712
  }
3073
3713
  /**
3074
3714
  * Invokes `agent.deposit` with the resolved sponsored callback, composing
@@ -3184,9 +3824,8 @@ var OwneySDK = class {
3184
3824
  async hasExistingBalance(agent, state, chainId, asset) {
3185
3825
  try {
3186
3826
  const balance = await agent.getBalances(state, chainId);
3187
- const target = asset.toLowerCase();
3188
3827
  const token = balance.tokens.find(
3189
- (t) => t.chainId === chainId && t.asset.toLowerCase() === target
3828
+ (t) => t.chainId === chainId && isSameAsset(t.asset, asset)
3190
3829
  );
3191
3830
  return !!token && Number(token.amount) > 0;
3192
3831
  } catch {
@@ -3225,6 +3864,34 @@ var OwneySDK = class {
3225
3864
  }
3226
3865
  return eligible;
3227
3866
  }
3867
+ /**
3868
+ * Agent ids the routing API provisioned for this org that support the given
3869
+ * chain + asset, ordered by preference ({@link AGENT_ELIGIBILITY_ORDER},
3870
+ * surfliquid first). Returns `[]` when the org has no compatible agent — never
3871
+ * throws on an empty org. Loads agent keys on first call (apiKey only, no
3872
+ * wallet), so the UI can resolve which agent to use before the user connects.
3873
+ *
3874
+ * This is the source of truth for agent availability: an agent appears here
3875
+ * iff the routing API returned its key. No per-app feature flags.
3876
+ */
3877
+ async getEligibleAgentIds(chainId, asset) {
3878
+ try {
3879
+ await this.ensureAgentsInitialized();
3880
+ } catch (error) {
3881
+ if (error instanceof OwneyError && error.code === "API_NO_AGENTS") {
3882
+ return [];
3883
+ }
3884
+ throw error;
3885
+ }
3886
+ return [...this.agents.values()].filter((agent) => {
3887
+ const chainAssets = agent.supportedAssets.find(
3888
+ (sa) => sa.chainId === chainId
3889
+ );
3890
+ return chainAssets?.assets.some((a) => a.symbol === asset) ?? false;
3891
+ }).map((agent) => agent.id).sort(
3892
+ (a, b) => AGENT_ELIGIBILITY_ORDER.indexOf(a) - AGENT_ELIGIBILITY_ORDER.indexOf(b)
3893
+ );
3894
+ }
3228
3895
  // --- Fund operations ---
3229
3896
  /**
3230
3897
  * Withdraw funds from a specific agent, or all agents that support the active chain+asset if agentId is omitted.
@@ -3260,9 +3927,17 @@ var OwneySDK = class {
3260
3927
  }
3261
3928
  const eligibleAgents = this.getEligibleAgents(chainId, asset);
3262
3929
  if (!amount) {
3930
+ const aggregated2 = await this.getBalances();
3931
+ const funded = projectAgentBalancesForAsset(
3932
+ eligibleAgents,
3933
+ aggregated2.agentBalances,
3934
+ chainId,
3935
+ asset,
3936
+ assetInfo.decimals
3937
+ ).filter((b) => b.balance > 0n);
3263
3938
  const results2 = {};
3264
3939
  const agentErrors2 = {};
3265
- for (const agent of eligibleAgents) {
3940
+ for (const { agent } of funded) {
3266
3941
  try {
3267
3942
  results2[agent.id] = await agent.withdraw(state, chainId, token);
3268
3943
  } catch (err) {
@@ -3279,9 +3954,10 @@ var OwneySDK = class {
3279
3954
  { asset, agentErrors: agentErrors2 }
3280
3955
  );
3281
3956
  }
3957
+ const totalWithdrawn = funded.filter(({ agent }) => results2[agent.id] !== void 0).reduce((sum, { balance }) => sum + balance, 0n);
3282
3958
  return {
3283
3959
  agentResult: results2,
3284
- totalWithdrawn: sumWithdrawnAmount(results2).toString()
3960
+ totalWithdrawn: totalWithdrawn.toString()
3285
3961
  };
3286
3962
  }
3287
3963
  const requested = BigInt(amount);
@@ -3673,10 +4349,10 @@ var OwneySDK = class {
3673
4349
  );
3674
4350
  }
3675
4351
  const provider = this.requireConnectedProvider();
3676
- const wallet = (0, import_viem6.createWalletClient)({
4352
+ const wallet = (0, import_viem7.createWalletClient)({
3677
4353
  account: state.walletAddress,
3678
4354
  chain: VIEM_CHAIN2[chainId],
3679
- transport: (0, import_viem6.custom)(provider)
4355
+ transport: (0, import_viem7.custom)(provider)
3680
4356
  });
3681
4357
  const hash = await wallet.writeContract({
3682
4358
  address: token,
@@ -3686,9 +4362,9 @@ var OwneySDK = class {
3686
4362
  account: state.walletAddress,
3687
4363
  chain: VIEM_CHAIN2[chainId]
3688
4364
  });
3689
- const publicClient = (0, import_viem6.createPublicClient)({
4365
+ const publicClient = (0, import_viem7.createPublicClient)({
3690
4366
  chain: VIEM_CHAIN2[chainId],
3691
- transport: (0, import_viem6.custom)(provider)
4367
+ transport: (0, import_viem7.custom)(provider)
3692
4368
  });
3693
4369
  const receipt = await publicClient.waitForTransactionReceipt({
3694
4370
  hash,
@@ -3796,8 +4472,11 @@ var OwneySDK = class {
3796
4472
  }
3797
4473
  };
3798
4474
 
4475
+ // src/index.ts
4476
+ init_errors();
4477
+
3799
4478
  // src/agents/zyfai/zyfai.siwx.ts
3800
- var import_viem7 = require("viem");
4479
+ var import_viem8 = require("viem");
3801
4480
  var import_siwe = require("siwe");
3802
4481
  var import_sdk2 = require("@zyfai/sdk");
3803
4482
 
@@ -3924,7 +4603,7 @@ function buildSIWXConfig(deps) {
3924
4603
  issuedAt,
3925
4604
  toString() {
3926
4605
  return new import_siwe.SiweMessage({
3927
- address: (0, import_viem7.getAddress)(accountAddress),
4606
+ address: (0, import_viem8.getAddress)(accountAddress),
3928
4607
  chainId: numericChainId(chainId),
3929
4608
  domain,
3930
4609
  uri,