@owney/sdk 0.5.1 → 0.5.3-beta.1
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 +20 -67
- package/dist/index.cjs +113 -44
- package/dist/index.d.cts +104 -102
- package/dist/index.d.ts +104 -102
- package/dist/index.js +113 -44
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @owney/sdk
|
|
2
2
|
|
|
3
|
-
A TypeScript SDK for interacting with DeFi yield agents. Supports multiple
|
|
3
|
+
A TypeScript SDK for interacting with DeFi yield agents. Supports multiple agents through a unified interface.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -117,7 +117,7 @@ Activate the user's smart wallet for the specified agents, or all chain-compatib
|
|
|
117
117
|
|
|
118
118
|
```typescript
|
|
119
119
|
// Activate specific agents on Base
|
|
120
|
-
await sdk.activateAgent(8453, ["zyfai"
|
|
120
|
+
await sdk.activateAgent(8453, ["zyfai"]);
|
|
121
121
|
|
|
122
122
|
// Activate all agents that support Arbitrum
|
|
123
123
|
await sdk.activateAgent(42161);
|
|
@@ -159,8 +159,7 @@ const results = await sdk.deposit({
|
|
|
159
159
|
return await transferTokens(smartWallet, amount);
|
|
160
160
|
},
|
|
161
161
|
});
|
|
162
|
-
console.log(results.agentResults.zyfai.amount); // "
|
|
163
|
-
console.log(results.agentResults.sail.amount); // "50000000"
|
|
162
|
+
console.log(results.agentResults.zyfai.amount); // "100000000"
|
|
164
163
|
```
|
|
165
164
|
|
|
166
165
|
### `withdraw(options)`
|
|
@@ -199,15 +198,13 @@ console.log(result.type); // "partial"
|
|
|
199
198
|
console.log(result.amount); // "50000000"
|
|
200
199
|
|
|
201
200
|
// Partial withdrawal split proportionally across all agents
|
|
202
|
-
// e.g. zyfai=$5,
|
|
201
|
+
// e.g. zyfai=$5, amount=$4 -> zyfai withdraws $4
|
|
203
202
|
const split = await sdk.withdraw({ asset: "USDC", amount: "4000000" });
|
|
204
|
-
console.log(split.agentResult.zyfai.amount); // "
|
|
205
|
-
console.log(split.agentResult.sail.amount); // "1000000"
|
|
203
|
+
console.log(split.agentResult.zyfai.amount); // "4000000"
|
|
206
204
|
|
|
207
205
|
// Full withdrawal from all agents
|
|
208
206
|
const results = await sdk.withdraw({ asset: "USDC" });
|
|
209
207
|
console.log(results.agentResult.zyfai.type); // "full"
|
|
210
|
-
console.log(results.agentResult.sail.type); // "full"
|
|
211
208
|
```
|
|
212
209
|
|
|
213
210
|
### `getBalances(agentId?)`
|
|
@@ -216,7 +213,7 @@ Get the user's balances for a specific agent, or aggregated across all agents.
|
|
|
216
213
|
|
|
217
214
|
| Param | Type | Description |
|
|
218
215
|
| --------- | ------------------------------ | ------------------------------------ |
|
|
219
|
-
| `agentId` | `"zyfai"
|
|
216
|
+
| `agentId` | `"zyfai"` (optional) | Agent to query. Omit for all agents. |
|
|
220
217
|
|
|
221
218
|
Returns: `AgentBalance` (single agent) or `OwneyBalances` (all agents)
|
|
222
219
|
|
|
@@ -229,9 +226,8 @@ console.log(balance.tokens); // [{ chain: "BASE", chainId: 8453, asset: "USDC",
|
|
|
229
226
|
|
|
230
227
|
// All agents
|
|
231
228
|
const allBalances = await sdk.getBalances();
|
|
232
|
-
console.log(allBalances.totalBalance); // "
|
|
229
|
+
console.log(allBalances.totalBalance); // "150.25"
|
|
233
230
|
console.log(allBalances.agentBalances.zyfai.totalBalance); // "150.25"
|
|
234
|
-
console.log(allBalances.agentBalances.sail.totalBalance); // "150.25"
|
|
235
231
|
```
|
|
236
232
|
|
|
237
233
|
### `getEarnings(agentId?)`
|
|
@@ -240,7 +236,7 @@ Get the user's on-chain earnings for a specific agent, or aggregated across all
|
|
|
240
236
|
|
|
241
237
|
| Param | Type | Description |
|
|
242
238
|
| --------- | ------------------------------ | ------------------------------------ |
|
|
243
|
-
| `agentId` | `"zyfai"
|
|
239
|
+
| `agentId` | `"zyfai"` (optional) | Agent to query. Omit for all agents. |
|
|
244
240
|
|
|
245
241
|
Returns: `AgentEarnings` (single agent) or `OwneyEarnings` (all agents)
|
|
246
242
|
|
|
@@ -268,7 +264,7 @@ Get the weighted APY for the user's account. When querying all agents, the total
|
|
|
268
264
|
|
|
269
265
|
| Param | Type | Description |
|
|
270
266
|
| --------- | ------------------------------ | ------------------------------------------------ |
|
|
271
|
-
| `agentId` | `"zyfai"
|
|
267
|
+
| `agentId` | `"zyfai"` (optional) | Agent to query. Omit for balance-weighted total. |
|
|
272
268
|
| `days` | `"7D" \| "14D" \| "30D"` | Lookback period |
|
|
273
269
|
|
|
274
270
|
Returns: `AccountAgentApy` (single agent) or `OwneyAccountApy` (all agents)
|
|
@@ -287,7 +283,6 @@ console.log(apy.apyByChainAndAsset?.[8453]?.USDC); // 5.4
|
|
|
287
283
|
const allApy = await sdk.getAccountApy({ days: "30D" });
|
|
288
284
|
console.log(allApy.totalApy); // "5.6"
|
|
289
285
|
console.log(allApy.agentApy.zyfai.weightedApyAfterFee); // 6.0
|
|
290
|
-
console.log(allApy.agentApy.sail.weightedApyAfterFee); // 4.0
|
|
291
286
|
// Per (chain, asset), balance-weighted across all agents.
|
|
292
287
|
// Cells with 0/missing APY for a given agent are excluded from that cell.
|
|
293
288
|
console.log(allApy.apyByChainAndAsset[8453]?.USDC); // 5.3
|
|
@@ -304,7 +299,7 @@ Results are **cursor-paginated**. The first call (no `cursor`) anchors a stable
|
|
|
304
299
|
|
|
305
300
|
| Param | Type | Description |
|
|
306
301
|
| ------------------ | ------------------------------ | -------------------------------------------------------- |
|
|
307
|
-
| `agentId` | `"zyfai"
|
|
302
|
+
| `agentId` | `"zyfai"` (optional) | Agent to query. Omit for all agents. |
|
|
308
303
|
| `filters.fromDate` | `string` (optional) | Start date (`YYYY-MM-DD`) |
|
|
309
304
|
| `filters.toDate` | `string` (optional) | End date (`YYYY-MM-DD`) |
|
|
310
305
|
| `filters.limit` | `number` (optional) | Max entries returned per call. Defaults to `10`. |
|
|
@@ -336,7 +331,7 @@ console.log(history.data[0].rebalanceLog[0].status); // "success"
|
|
|
336
331
|
// All agents (merged and sorted by date)
|
|
337
332
|
const allHistory = await sdk.getHistory();
|
|
338
333
|
console.log(allHistory.hasMore); // true | false
|
|
339
|
-
console.log(allHistory.data[0].agent); // "
|
|
334
|
+
console.log(allHistory.data[0].agent); // "zyfai" (most recent entry)
|
|
340
335
|
console.log(allHistory.data[0].action); // "Rebalance"
|
|
341
336
|
|
|
342
337
|
// Paginate through all history (per-chain, anchored to a single snapshot)
|
|
@@ -351,44 +346,13 @@ do {
|
|
|
351
346
|
} while (cursor);
|
|
352
347
|
```
|
|
353
348
|
|
|
354
|
-
### `pauseAgent(agentId)`
|
|
355
|
-
|
|
356
|
-
Pause an agent's automated operations. The agent will stop rebalancing until resumed.
|
|
357
|
-
|
|
358
|
-
| Param | Type | Description |
|
|
359
|
-
| --------- | ------------------- | -------------- |
|
|
360
|
-
| `agentId` | `"zyfai" \| "sail"` | Agent to pause |
|
|
361
|
-
|
|
362
|
-
Returns: `OwneyAgentStatus`
|
|
363
|
-
|
|
364
|
-
```typescript
|
|
365
|
-
const status = await sdk.pauseAgent("zyfai");
|
|
366
|
-
console.log(status.success); // true
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
### `resumeAgent(agentId)`
|
|
370
|
-
|
|
371
|
-
Resume an agent's automated operations.
|
|
372
|
-
|
|
373
|
-
| Param | Type | Description |
|
|
374
|
-
| --------- | ------------------- | --------------- |
|
|
375
|
-
| `agentId` | `"zyfai" \| "sail"` | Agent to resume |
|
|
376
|
-
|
|
377
|
-
Returns: `OwneyAgentStatus`
|
|
378
|
-
|
|
379
|
-
```typescript
|
|
380
|
-
const status = await sdk.resumeAgent("zyfai");
|
|
381
|
-
console.log(status.success); // true
|
|
382
|
-
console.log(status.protocols); // ["aave", "compound"]
|
|
383
|
-
```
|
|
384
|
-
|
|
385
349
|
### `getUserProfile(agentId?)`
|
|
386
350
|
|
|
387
351
|
Get the user's profile for a specific agent, or all agents.
|
|
388
352
|
|
|
389
353
|
| Param | Type | Description |
|
|
390
354
|
| --------- | ------------------------------ | ------------------------------------ |
|
|
391
|
-
| `agentId` | `"zyfai"
|
|
355
|
+
| `agentId` | `"zyfai"` (optional) | Agent to query. Omit for all agents. |
|
|
392
356
|
|
|
393
357
|
Returns: `AgentUserProfile` (single agent) or `OwneyUserProfile` (all agents)
|
|
394
358
|
|
|
@@ -404,7 +368,6 @@ console.log(profile.protocols); // ["aave", "compound"]
|
|
|
404
368
|
// All agents
|
|
405
369
|
const allProfiles = await sdk.getUserProfile();
|
|
406
370
|
console.log(allProfiles.agentUserProfile.zyfai.smartWallet); // "0x..."
|
|
407
|
-
console.log(allProfiles.agentUserProfile.sail.smartWallet); // "0x..."
|
|
408
371
|
```
|
|
409
372
|
|
|
410
373
|
### `getAgentApy({ agentId?, days, tokenSymbol?, chainId? })`
|
|
@@ -413,14 +376,14 @@ Get the agent's average APY performance over a time period. Does not require a w
|
|
|
413
376
|
|
|
414
377
|
| Param | Type | Description |
|
|
415
378
|
| ------------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
416
|
-
| `agentId` | `"zyfai"
|
|
379
|
+
| `agentId` | `"zyfai"` (optional) | Agent to query. Omit for all agents. |
|
|
417
380
|
| `days` | `"7D" \| "14D" \| "30D"` | Lookback period |
|
|
418
|
-
| `tokenSymbol` | `string` (optional) | Asset symbol (e.g. `"USDC"
|
|
419
|
-
| `chainId` | `number` (optional) | Chain id. Typically paired with `tokenSymbol` for per-asset+chain APY.
|
|
381
|
+
| `tokenSymbol` | `string` (optional) | Asset symbol (e.g. `"USDC"`). Scopes the upstream request to a specific asset when the agent's backend supports it. |
|
|
382
|
+
| `chainId` | `number` (optional) | Chain id. Typically paired with `tokenSymbol` for per-asset+chain APY. |
|
|
420
383
|
|
|
421
384
|
Returns: `AgentApy` (single agent) or `OwneyAgentApy` (all agents)
|
|
422
385
|
|
|
423
|
-
|
|
386
|
+
The agent populates `detailedApys.apyPerAsset` with a per-chain, per-token
|
|
424
387
|
breakdown when available. `tokenSymbol` / `chainId` only affect whether the
|
|
425
388
|
upstream query is scoped — the breakdown is built from whatever the agent
|
|
426
389
|
returns.
|
|
@@ -434,10 +397,8 @@ console.log(apy.detailedApys?.apyPerAsset[8453]?.USDC); // 8.7
|
|
|
434
397
|
// All agents
|
|
435
398
|
const allApy = await sdk.getAgentApy({ days: "30D" });
|
|
436
399
|
console.log(allApy.agentApy.zyfai.averageApy); // 8.5
|
|
437
|
-
console.log(allApy.agentApy.sail.averageApy); // 6.2
|
|
438
|
-
console.log(allApy.agentApy.sail.detailedApys?.apyPerAsset[42161]?.USDT); // 6.4
|
|
439
400
|
|
|
440
|
-
// Scope the upstream query to USDC on Base
|
|
401
|
+
// Scope the upstream query to USDC on Base
|
|
441
402
|
const usdcBaseApy = await sdk.getAgentApy({
|
|
442
403
|
days: "30D",
|
|
443
404
|
tokenSymbol: "USDC",
|
|
@@ -454,7 +415,7 @@ Returns: `OwneyAgentAllocation`
|
|
|
454
415
|
|
|
455
416
|
```typescript
|
|
456
417
|
const allocation = await sdk.getAgentAllocation();
|
|
457
|
-
console.log(allocation.agentAllocation); // { zyfai:
|
|
418
|
+
console.log(allocation.agentAllocation); // { zyfai: 100 }
|
|
458
419
|
```
|
|
459
420
|
|
|
460
421
|
## Types
|
|
@@ -462,7 +423,7 @@ console.log(allocation.agentAllocation); // { zyfai: 75, sail: 25 }
|
|
|
462
423
|
### Configuration
|
|
463
424
|
|
|
464
425
|
```typescript
|
|
465
|
-
type AgentId = "zyfai"
|
|
426
|
+
type AgentId = "zyfai";
|
|
466
427
|
type Asset = string;
|
|
467
428
|
type DailyApyDays = "7D" | "14D" | "30D";
|
|
468
429
|
type OwneySupportedChainId = 8453 | 42161;
|
|
@@ -639,12 +600,6 @@ interface OwneyAgentHistory {
|
|
|
639
600
|
nextCursor?: string;
|
|
640
601
|
}
|
|
641
602
|
|
|
642
|
-
interface OwneyAgentStatus {
|
|
643
|
-
success: boolean;
|
|
644
|
-
strategy?: string;
|
|
645
|
-
protocols?: string[];
|
|
646
|
-
}
|
|
647
|
-
|
|
648
603
|
interface AgentUserProfile {
|
|
649
604
|
address: string;
|
|
650
605
|
smartWallet: string;
|
|
@@ -738,7 +693,7 @@ type OwneyErrorCode =
|
|
|
738
693
|
| "ASSET_NO_COMPATIBLE_AGENTS" // No agents support the asset on the active chain
|
|
739
694
|
// Deposit
|
|
740
695
|
| "DEPOSIT_AMOUNT_BELOW_MINIMUM" // Amount below an agent's required minimum deposit for the chain+asset
|
|
741
|
-
| "DEPOSIT_CALLBACK_REQUIRED" //
|
|
696
|
+
| "DEPOSIT_CALLBACK_REQUIRED" // Agent requires a depositCallback
|
|
742
697
|
| "DEPOSIT_CALLBACK_INVALID" // depositCallback didn't return a tx hash
|
|
743
698
|
| "DEPOSIT_NO_PERMITTED_TOKENS" // No permitted tokens available for deposit
|
|
744
699
|
// Withdraw
|
|
@@ -750,8 +705,6 @@ type OwneyErrorCode =
|
|
|
750
705
|
// API
|
|
751
706
|
| "API_ROUTING_ERROR" // Routing API returned a non-OK HTTP status
|
|
752
707
|
| "API_ROUTING_FAILED" // Routing API returned { success: false }
|
|
753
|
-
| "API_SAIL_ERROR" // Sail API returned a non-OK HTTP status
|
|
754
|
-
| "API_SAIL_TIMEOUT" // Sail API request timed out
|
|
755
708
|
| "API_NO_AGENTS" // No supported agents returned by routing API
|
|
756
709
|
// Aggregation
|
|
757
710
|
| "BALANCE_ALL_FAILED" // All agents failed to return balances
|
package/dist/index.cjs
CHANGED
|
@@ -157,6 +157,18 @@ var CHAIN_ID_TO_NAME = {
|
|
|
157
157
|
8453: "BASE",
|
|
158
158
|
42161: "ARBITRUM"
|
|
159
159
|
};
|
|
160
|
+
var NAME_TO_CHAIN_ID = Object.entries(
|
|
161
|
+
CHAIN_ID_TO_NAME
|
|
162
|
+
).reduce((acc, [id, name]) => {
|
|
163
|
+
acc[name.toLowerCase()] = Number(id);
|
|
164
|
+
return acc;
|
|
165
|
+
}, {});
|
|
166
|
+
function resolveChainId(chain) {
|
|
167
|
+
const asNum = Number(chain);
|
|
168
|
+
if (Number.isFinite(asNum) && asNum > 0)
|
|
169
|
+
return asNum;
|
|
170
|
+
return NAME_TO_CHAIN_ID[chain.trim().toLowerCase()] ?? NaN;
|
|
171
|
+
}
|
|
160
172
|
function mapBalances(raw, _chainId, smartWallet) {
|
|
161
173
|
const portfolio = raw.portfolio;
|
|
162
174
|
const portfolioByChain = portfolio.portfolioByChain ?? {};
|
|
@@ -305,13 +317,6 @@ function mapEntries(rawEntries, chainId) {
|
|
|
305
317
|
};
|
|
306
318
|
});
|
|
307
319
|
}
|
|
308
|
-
function mapAgentStatus(raw) {
|
|
309
|
-
return {
|
|
310
|
-
success: raw.success,
|
|
311
|
-
strategy: raw.strategy,
|
|
312
|
-
protocols: raw.protocols
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
320
|
function mapUserProfile(raw, userAddress) {
|
|
316
321
|
return {
|
|
317
322
|
address: userAddress,
|
|
@@ -347,6 +352,46 @@ function mapApyByStrategy(raw) {
|
|
|
347
352
|
detailedApys: hasDetails ? { apyPerAsset } : void 0
|
|
348
353
|
};
|
|
349
354
|
}
|
|
355
|
+
function computeAllocationApy(positions) {
|
|
356
|
+
let weightedSum = 0;
|
|
357
|
+
let totalValue = 0;
|
|
358
|
+
const buckets = {};
|
|
359
|
+
for (const p of positions) {
|
|
360
|
+
const apy = p.apy;
|
|
361
|
+
const value = Number(p.amount);
|
|
362
|
+
if (typeof apy !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
weightedSum += apy * value;
|
|
366
|
+
totalValue += value;
|
|
367
|
+
const chainId = resolveChainId(p.chain);
|
|
368
|
+
const token = p.asset;
|
|
369
|
+
if (!SUPPORTED_CHAIN_IDS.includes(chainId))
|
|
370
|
+
continue;
|
|
371
|
+
if (!SUPPORTED_TOKENS.includes(token))
|
|
372
|
+
continue;
|
|
373
|
+
const perAsset = buckets[chainId] ?? {};
|
|
374
|
+
const cell = perAsset[token] ?? { weightedSum: 0, value: 0 };
|
|
375
|
+
cell.weightedSum += apy * value;
|
|
376
|
+
cell.value += value;
|
|
377
|
+
perAsset[token] = cell;
|
|
378
|
+
buckets[chainId] = perAsset;
|
|
379
|
+
}
|
|
380
|
+
const apyByChainAndAsset = {};
|
|
381
|
+
for (const [chainKey, perAsset] of Object.entries(buckets)) {
|
|
382
|
+
const chainId = Number(chainKey);
|
|
383
|
+
const out = {};
|
|
384
|
+
for (const [token, cell] of Object.entries(perAsset ?? {})) {
|
|
385
|
+
if (cell && cell.value > 0) {
|
|
386
|
+
out[token] = cell.weightedSum / cell.value;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (Object.keys(out).length > 0)
|
|
390
|
+
apyByChainAndAsset[chainId] = out;
|
|
391
|
+
}
|
|
392
|
+
const totalApy = totalValue > 0 ? String(weightedSum / totalValue) : "0";
|
|
393
|
+
return { totalApy, apyByChainAndAsset };
|
|
394
|
+
}
|
|
350
395
|
|
|
351
396
|
// src/agents/zyfai/zyfai.history-cursor.ts
|
|
352
397
|
var InvalidHistoryCursorError = class extends Error {
|
|
@@ -729,17 +774,6 @@ var ZyfaiAgent = class {
|
|
|
729
774
|
nextCursor
|
|
730
775
|
};
|
|
731
776
|
}
|
|
732
|
-
// --- IAgent: Agent control ---
|
|
733
|
-
async pause(state, chainId) {
|
|
734
|
-
await this.connectAuth(state, chainId);
|
|
735
|
-
const raw = await this.sdk.pauseAgent();
|
|
736
|
-
return mapAgentStatus(raw);
|
|
737
|
-
}
|
|
738
|
-
async resume(state, chainId) {
|
|
739
|
-
await this.connectAuth(state, chainId);
|
|
740
|
-
const raw = await this.sdk.resumeAgent();
|
|
741
|
-
return mapAgentStatus(raw);
|
|
742
|
-
}
|
|
743
777
|
async getUserProfile(state, chainId) {
|
|
744
778
|
await this.connectAuth(state, chainId);
|
|
745
779
|
const validChainId = isValidChainId(chainId);
|
|
@@ -1100,7 +1134,10 @@ var SPONSORED_USDC_BY_CHAIN = {
|
|
|
1100
1134
|
8453: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
|
|
1101
1135
|
42161: "0xaf88d065e77c8cc2239327c5edb3a432268e5831"
|
|
1102
1136
|
};
|
|
1103
|
-
var VIEM_CHAIN = {
|
|
1137
|
+
var VIEM_CHAIN = {
|
|
1138
|
+
8453: import_chains.base,
|
|
1139
|
+
42161: import_chains.arbitrum
|
|
1140
|
+
};
|
|
1104
1141
|
var OwneySDK = class {
|
|
1105
1142
|
agents = /* @__PURE__ */ new Map();
|
|
1106
1143
|
activeAgents = /* @__PURE__ */ new Set();
|
|
@@ -1620,10 +1657,7 @@ var OwneySDK = class {
|
|
|
1620
1657
|
disabledBalances,
|
|
1621
1658
|
requested
|
|
1622
1659
|
);
|
|
1623
|
-
const enabledTotal = enabledBalances.reduce(
|
|
1624
|
-
(s, b) => s + b.balance,
|
|
1625
|
-
0n
|
|
1626
|
-
);
|
|
1660
|
+
const enabledTotal = enabledBalances.reduce((s, b) => s + b.balance, 0n);
|
|
1627
1661
|
const enabledPlans = afterDrain > 0n ? planProportionalShares(enabledBalances, afterDrain, enabledTotal) : enabledBalances.map(({ agent, balance }) => ({
|
|
1628
1662
|
agent,
|
|
1629
1663
|
balance,
|
|
@@ -1894,27 +1928,6 @@ var OwneySDK = class {
|
|
|
1894
1928
|
nextCursor: hasMore ? encodeMultiAgentCursor(nextCursorMap) : void 0
|
|
1895
1929
|
};
|
|
1896
1930
|
}
|
|
1897
|
-
// --- Agent control ---
|
|
1898
|
-
/**
|
|
1899
|
-
* Pause an agent's automated operations. The agent will stop rebalancing until resumed.
|
|
1900
|
-
* @param agentId - The agent to pause (e.g. "zyfai")
|
|
1901
|
-
* @returns OwneyAgentStatus with success flag
|
|
1902
|
-
*/
|
|
1903
|
-
async pauseAgent(agentId) {
|
|
1904
|
-
const state = this.requireState();
|
|
1905
|
-
const chainId = this.requireChainId();
|
|
1906
|
-
return this.getAgent(agentId).pause(state, chainId);
|
|
1907
|
-
}
|
|
1908
|
-
/**
|
|
1909
|
-
* Resume an agent's automated operations.
|
|
1910
|
-
* @param agentId - The agent to resume (e.g. "zyfai")
|
|
1911
|
-
* @returns OwneyAgentStatus with success flag
|
|
1912
|
-
*/
|
|
1913
|
-
async resumeAgent(agentId) {
|
|
1914
|
-
const state = this.requireState();
|
|
1915
|
-
const chainId = this.requireChainId();
|
|
1916
|
-
return this.getAgent(agentId).resume(state, chainId);
|
|
1917
|
-
}
|
|
1918
1931
|
/**
|
|
1919
1932
|
* Get the user's profile for a specific agent, or all agents.
|
|
1920
1933
|
* @param agentId - Optional. Agent to query. Omit for all agents.
|
|
@@ -2044,6 +2057,62 @@ var OwneySDK = class {
|
|
|
2044
2057
|
agentPositions: positionsByAgent
|
|
2045
2058
|
};
|
|
2046
2059
|
}
|
|
2060
|
+
/**
|
|
2061
|
+
* Allocation-weighted APY from the current portfolio: each pool's APY
|
|
2062
|
+
* weighted by the user's position value in it. More accurate per-user than
|
|
2063
|
+
* the historical 30D APY. (ROUT-228)
|
|
2064
|
+
* @param options.agentId - Optional. Scope to one agent. Omit to aggregate
|
|
2065
|
+
* positions across all active agents.
|
|
2066
|
+
*/
|
|
2067
|
+
async getAllocationApy({
|
|
2068
|
+
agentId
|
|
2069
|
+
} = {}) {
|
|
2070
|
+
const state = this.requireState();
|
|
2071
|
+
const chainId = this.requireChainId();
|
|
2072
|
+
const activeAgents = agentId ? /* @__PURE__ */ new Map([[agentId, this.getAgent(agentId)]]) : this.getActiveAgents();
|
|
2073
|
+
const entries = [...activeAgents.entries()];
|
|
2074
|
+
const balanceResults = await Promise.allSettled(
|
|
2075
|
+
entries.map(async ([id, agent]) => {
|
|
2076
|
+
const b = await agent.getBalances(state, chainId);
|
|
2077
|
+
return [id, b.positions ?? []];
|
|
2078
|
+
})
|
|
2079
|
+
);
|
|
2080
|
+
const agentApy = {};
|
|
2081
|
+
const allPositions = [];
|
|
2082
|
+
let successCount = 0;
|
|
2083
|
+
const agentErrors = {};
|
|
2084
|
+
for (let i = 0; i < balanceResults.length; i++) {
|
|
2085
|
+
const settled = balanceResults[i];
|
|
2086
|
+
const [aid] = entries[i];
|
|
2087
|
+
if (settled.status === "fulfilled") {
|
|
2088
|
+
const [id, positions] = settled.value;
|
|
2089
|
+
const per = computeAllocationApy(positions);
|
|
2090
|
+
agentApy[id] = {
|
|
2091
|
+
totalApy: per.totalApy,
|
|
2092
|
+
apyByChainAndAsset: per.apyByChainAndAsset
|
|
2093
|
+
};
|
|
2094
|
+
allPositions.push(...positions);
|
|
2095
|
+
successCount += 1;
|
|
2096
|
+
} else {
|
|
2097
|
+
const reason = settled.reason;
|
|
2098
|
+
agentErrors[aid] = reason instanceof Error ? reason.message : String(reason);
|
|
2099
|
+
console.error(`getAllocationApy agent "${aid}" failed:`, reason);
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
if (successCount === 0) {
|
|
2103
|
+
throw new OwneyError(
|
|
2104
|
+
"ALLOCATION_ALL_FAILED",
|
|
2105
|
+
"Failed to fetch allocation APY: all agents failed.",
|
|
2106
|
+
{ agentErrors }
|
|
2107
|
+
);
|
|
2108
|
+
}
|
|
2109
|
+
const overall = computeAllocationApy(allPositions);
|
|
2110
|
+
return {
|
|
2111
|
+
totalApy: overall.totalApy,
|
|
2112
|
+
apyByChainAndAsset: overall.apyByChainAndAsset,
|
|
2113
|
+
agentApy
|
|
2114
|
+
};
|
|
2115
|
+
}
|
|
2047
2116
|
};
|
|
2048
2117
|
|
|
2049
2118
|
// src/agents/zyfai/zyfai.siwx.ts
|
package/dist/index.d.cts
CHANGED
|
@@ -21,90 +21,6 @@ type OwneySupportedChainId = (typeof SUPPORTED_CHAIN_IDS)[number];
|
|
|
21
21
|
type OwneySupportedChains = (typeof SUPPORTED_CHAINS)[number];
|
|
22
22
|
type OwneySupportedTokens = (typeof SUPPORTED_TOKENS)[number];
|
|
23
23
|
|
|
24
|
-
type DepositCallback = (smartWalletAddress: string, chainId: number, amount: string) => Promise<`0x${string}`> | `0x${string}`;
|
|
25
|
-
interface IAgent {
|
|
26
|
-
readonly id: string;
|
|
27
|
-
readonly supportedChainIds: readonly OwneySupportedChainId[];
|
|
28
|
-
readonly supportedAssets: readonly AgentSupportedAssets[];
|
|
29
|
-
disconnect(): Promise<void>;
|
|
30
|
-
activateAgent(state: ConnectionState, chainId: number): Promise<void>;
|
|
31
|
-
deposit(state: ConnectionState, chainId: number, amount: string, depositCallback?: DepositCallback): Promise<OwneyDepositResult>;
|
|
32
|
-
withdraw(state: ConnectionState, chainId: number, token: OwneySupportedTokens, amount?: string): Promise<AgentWithdrawResult>;
|
|
33
|
-
getBalances(state: ConnectionState, chainId: number): Promise<AgentBalance>;
|
|
34
|
-
getEarnings(state: ConnectionState, chainId: number): Promise<AgentEarnings>;
|
|
35
|
-
getAccountApy(state: ConnectionState, chainId: number, days: DailyApyDays): Promise<AccountAgentApy>;
|
|
36
|
-
getHistory(state: ConnectionState, chainId: number, options?: HistoryFilters): Promise<OwneyAgentHistory>;
|
|
37
|
-
pause(state: ConnectionState, chainId: number): Promise<OwneyAgentStatus>;
|
|
38
|
-
resume(state: ConnectionState, chainId: number): Promise<OwneyAgentStatus>;
|
|
39
|
-
getUserProfile(state: ConnectionState, chainId: number): Promise<AgentUserProfile>;
|
|
40
|
-
getAgentApy(days: DailyApyDays, options?: AgentApyOptions): Promise<AgentApy>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Optional filters for per-agent APY lookups.
|
|
44
|
-
* - `tokenSymbol` / `chainId` scope the APY to a specific asset+chain when
|
|
45
|
-
* the underlying provider supports it (Zyfai). Agents whose backends do
|
|
46
|
-
* not yet support per-asset APY will ignore these options.
|
|
47
|
-
*/
|
|
48
|
-
type AgentApyOptions = {
|
|
49
|
-
tokenSymbol?: string;
|
|
50
|
-
chainId?: number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
type AgentId = "zyfai" | "sail";
|
|
54
|
-
type Asset = string;
|
|
55
|
-
type AgentSupportedAsset = {
|
|
56
|
-
readonly symbol: string;
|
|
57
|
-
readonly minDepositAmount: string;
|
|
58
|
-
};
|
|
59
|
-
type AgentSupportedAssets = {
|
|
60
|
-
readonly chainId: number;
|
|
61
|
-
readonly chain?: string;
|
|
62
|
-
readonly assets: readonly AgentSupportedAsset[];
|
|
63
|
-
};
|
|
64
|
-
type DailyApyDays = "7D" | "14D" | "30D";
|
|
65
|
-
type HistoryFilters = {
|
|
66
|
-
fromDate?: string;
|
|
67
|
-
toDate?: string;
|
|
68
|
-
/** Max entries returned per call. Defaults to 10. */
|
|
69
|
-
limit?: number;
|
|
70
|
-
/** Opaque cursor returned by a previous getHistory call. */
|
|
71
|
-
cursor?: string;
|
|
72
|
-
};
|
|
73
|
-
type HistoryOptions = {
|
|
74
|
-
agentId?: AgentId;
|
|
75
|
-
filters?: HistoryFilters;
|
|
76
|
-
};
|
|
77
|
-
type WithdrawOptions = {
|
|
78
|
-
asset: Asset;
|
|
79
|
-
amount?: string;
|
|
80
|
-
agentId?: AgentId;
|
|
81
|
-
};
|
|
82
|
-
type AccountApyOptions = {
|
|
83
|
-
agentId?: AgentId;
|
|
84
|
-
days: DailyApyDays;
|
|
85
|
-
};
|
|
86
|
-
type AgentsApyOptions = {
|
|
87
|
-
agentId?: AgentId;
|
|
88
|
-
days: DailyApyDays;
|
|
89
|
-
/**
|
|
90
|
-
* Optional asset symbol (e.g. "USDC", "WETH") to scope the APY to a
|
|
91
|
-
* specific asset+chain. Ignored by agents whose backends do not yet
|
|
92
|
-
* support per-asset APY.
|
|
93
|
-
*/
|
|
94
|
-
tokenSymbol?: string;
|
|
95
|
-
/**
|
|
96
|
-
* Optional chain id for per-asset APY lookups. Typically paired with
|
|
97
|
-
* `tokenSymbol`.
|
|
98
|
-
*/
|
|
99
|
-
chainId?: number;
|
|
100
|
-
};
|
|
101
|
-
type DepositOptions = {
|
|
102
|
-
amount: string;
|
|
103
|
-
asset: Asset;
|
|
104
|
-
depositCallback?: DepositCallback;
|
|
105
|
-
agentId?: AgentId;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
24
|
interface OwneyDepositResult {
|
|
109
25
|
txHash: string;
|
|
110
26
|
smartWallet: string;
|
|
@@ -177,6 +93,17 @@ interface OwneyAccountApy {
|
|
|
177
93
|
agentApy: Record<AgentId, AccountAgentApy>;
|
|
178
94
|
apyByChainAndAsset: ApyByChainAndAsset;
|
|
179
95
|
}
|
|
96
|
+
interface AllocationAgentApy {
|
|
97
|
+
/** Account-wide weighted APY for this agent, as a decimal string. */
|
|
98
|
+
totalApy: string;
|
|
99
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
100
|
+
}
|
|
101
|
+
interface OwneyAllocationApy {
|
|
102
|
+
/** Account-wide weighted APY across all active agents, as a decimal string. */
|
|
103
|
+
totalApy: string;
|
|
104
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
105
|
+
agentApy: Record<AgentId, AllocationAgentApy>;
|
|
106
|
+
}
|
|
180
107
|
type HistoryAction = "Rebalance" | "Deposit" | "Top up" | "Withdraw" | "Earned";
|
|
181
108
|
interface HistoryTransaction {
|
|
182
109
|
txHashes: string[];
|
|
@@ -214,11 +141,6 @@ interface OwneyAgentHistory {
|
|
|
214
141
|
/** Opaque cursor for the next page. Present iff `hasMore === true`. */
|
|
215
142
|
nextCursor?: string;
|
|
216
143
|
}
|
|
217
|
-
interface OwneyAgentStatus {
|
|
218
|
-
success: boolean;
|
|
219
|
-
strategy?: string;
|
|
220
|
-
protocols?: string[];
|
|
221
|
-
}
|
|
222
144
|
interface AgentUserProfile {
|
|
223
145
|
address: string;
|
|
224
146
|
smartWallet: string;
|
|
@@ -246,6 +168,88 @@ interface OwneyAgentAllocation {
|
|
|
246
168
|
agentPositions: Record<AgentId, OwneyPosition[]>;
|
|
247
169
|
}
|
|
248
170
|
|
|
171
|
+
type DepositCallback = (smartWalletAddress: string, chainId: number, amount: string) => Promise<`0x${string}`> | `0x${string}`;
|
|
172
|
+
interface IAgent {
|
|
173
|
+
readonly id: string;
|
|
174
|
+
readonly supportedChainIds: readonly OwneySupportedChainId[];
|
|
175
|
+
readonly supportedAssets: readonly AgentSupportedAssets[];
|
|
176
|
+
disconnect(): Promise<void>;
|
|
177
|
+
activateAgent(state: ConnectionState, chainId: number): Promise<void>;
|
|
178
|
+
deposit(state: ConnectionState, chainId: number, amount: string, depositCallback?: DepositCallback): Promise<OwneyDepositResult>;
|
|
179
|
+
withdraw(state: ConnectionState, chainId: number, token: OwneySupportedTokens, amount?: string): Promise<AgentWithdrawResult>;
|
|
180
|
+
getBalances(state: ConnectionState, chainId: number): Promise<AgentBalance>;
|
|
181
|
+
getEarnings(state: ConnectionState, chainId: number): Promise<AgentEarnings>;
|
|
182
|
+
getAccountApy(state: ConnectionState, chainId: number, days: DailyApyDays): Promise<AccountAgentApy>;
|
|
183
|
+
getHistory(state: ConnectionState, chainId: number, options?: HistoryFilters): Promise<OwneyAgentHistory>;
|
|
184
|
+
getUserProfile(state: ConnectionState, chainId: number): Promise<AgentUserProfile>;
|
|
185
|
+
getAgentApy(days: DailyApyDays, options?: AgentApyOptions): Promise<AgentApy>;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Optional filters for per-agent APY lookups.
|
|
189
|
+
* - `tokenSymbol` / `chainId` scope the APY to a specific asset+chain when
|
|
190
|
+
* the underlying provider supports it (Zyfai). Agents whose backends do
|
|
191
|
+
* not yet support per-asset APY will ignore these options.
|
|
192
|
+
*/
|
|
193
|
+
type AgentApyOptions = {
|
|
194
|
+
tokenSymbol?: string;
|
|
195
|
+
chainId?: number;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
type AgentId = "zyfai" | "sail";
|
|
199
|
+
type Asset = string;
|
|
200
|
+
type AgentSupportedAsset = {
|
|
201
|
+
readonly symbol: string;
|
|
202
|
+
readonly minDepositAmount: string;
|
|
203
|
+
};
|
|
204
|
+
type AgentSupportedAssets = {
|
|
205
|
+
readonly chainId: number;
|
|
206
|
+
readonly chain?: string;
|
|
207
|
+
readonly assets: readonly AgentSupportedAsset[];
|
|
208
|
+
};
|
|
209
|
+
type DailyApyDays = "7D" | "14D" | "30D";
|
|
210
|
+
type HistoryFilters = {
|
|
211
|
+
fromDate?: string;
|
|
212
|
+
toDate?: string;
|
|
213
|
+
/** Max entries returned per call. Defaults to 10. */
|
|
214
|
+
limit?: number;
|
|
215
|
+
/** Opaque cursor returned by a previous getHistory call. */
|
|
216
|
+
cursor?: string;
|
|
217
|
+
};
|
|
218
|
+
type HistoryOptions = {
|
|
219
|
+
agentId?: AgentId;
|
|
220
|
+
filters?: HistoryFilters;
|
|
221
|
+
};
|
|
222
|
+
type WithdrawOptions = {
|
|
223
|
+
asset: Asset;
|
|
224
|
+
amount?: string;
|
|
225
|
+
agentId?: AgentId;
|
|
226
|
+
};
|
|
227
|
+
type AccountApyOptions = {
|
|
228
|
+
agentId?: AgentId;
|
|
229
|
+
days: DailyApyDays;
|
|
230
|
+
};
|
|
231
|
+
type AgentsApyOptions = {
|
|
232
|
+
agentId?: AgentId;
|
|
233
|
+
days: DailyApyDays;
|
|
234
|
+
/**
|
|
235
|
+
* Optional asset symbol (e.g. "USDC", "WETH") to scope the APY to a
|
|
236
|
+
* specific asset+chain. Ignored by agents whose backends do not yet
|
|
237
|
+
* support per-asset APY.
|
|
238
|
+
*/
|
|
239
|
+
tokenSymbol?: string;
|
|
240
|
+
/**
|
|
241
|
+
* Optional chain id for per-asset APY lookups. Typically paired with
|
|
242
|
+
* `tokenSymbol`.
|
|
243
|
+
*/
|
|
244
|
+
chainId?: number;
|
|
245
|
+
};
|
|
246
|
+
type DepositOptions = {
|
|
247
|
+
amount: string;
|
|
248
|
+
asset: Asset;
|
|
249
|
+
depositCallback?: DepositCallback;
|
|
250
|
+
agentId?: AgentId;
|
|
251
|
+
};
|
|
252
|
+
|
|
249
253
|
declare class OwneySDK {
|
|
250
254
|
private agents;
|
|
251
255
|
private activeAgents;
|
|
@@ -382,18 +386,6 @@ declare class OwneySDK {
|
|
|
382
386
|
* @returns {OwneyAgentHistory} `data` (up to `limit` entries), `hasMore`, `nextCursor`.
|
|
383
387
|
*/
|
|
384
388
|
getHistory(params?: HistoryOptions): Promise<OwneyAgentHistory>;
|
|
385
|
-
/**
|
|
386
|
-
* Pause an agent's automated operations. The agent will stop rebalancing until resumed.
|
|
387
|
-
* @param agentId - The agent to pause (e.g. "zyfai")
|
|
388
|
-
* @returns OwneyAgentStatus with success flag
|
|
389
|
-
*/
|
|
390
|
-
pauseAgent(agentId: AgentId): Promise<OwneyAgentStatus>;
|
|
391
|
-
/**
|
|
392
|
-
* Resume an agent's automated operations.
|
|
393
|
-
* @param agentId - The agent to resume (e.g. "zyfai")
|
|
394
|
-
* @returns OwneyAgentStatus with success flag
|
|
395
|
-
*/
|
|
396
|
-
resumeAgent(agentId: AgentId): Promise<OwneyAgentStatus>;
|
|
397
389
|
/**
|
|
398
390
|
* Get the user's profile for a specific agent, or all agents.
|
|
399
391
|
* @param agentId - Optional. Agent to query. Omit for all agents.
|
|
@@ -418,6 +410,16 @@ declare class OwneySDK {
|
|
|
418
410
|
* @returns Object with agent names as keys and allocation percentages as values
|
|
419
411
|
*/
|
|
420
412
|
getAgentAllocation(): Promise<OwneyAgentAllocation>;
|
|
413
|
+
/**
|
|
414
|
+
* Allocation-weighted APY from the current portfolio: each pool's APY
|
|
415
|
+
* weighted by the user's position value in it. More accurate per-user than
|
|
416
|
+
* the historical 30D APY. (ROUT-228)
|
|
417
|
+
* @param options.agentId - Optional. Scope to one agent. Omit to aggregate
|
|
418
|
+
* positions across all active agents.
|
|
419
|
+
*/
|
|
420
|
+
getAllocationApy({ agentId, }?: {
|
|
421
|
+
agentId?: AgentId;
|
|
422
|
+
}): Promise<OwneyAllocationApy>;
|
|
421
423
|
}
|
|
422
424
|
|
|
423
425
|
type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "AGENT_DISABLED" | "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" | "SPONSOR_REQUEST_FAILED" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
|
|
@@ -463,4 +465,4 @@ type OwneySIWXConfig = {
|
|
|
463
465
|
*/
|
|
464
466
|
declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
|
|
465
467
|
|
|
466
|
-
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory,
|
|
468
|
+
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, AllocationAgentApy, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAllocationApy, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneyPosition, OwneySDK, OwneySDKConfig, OwneySIWXConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions, createOwneySIWX };
|
package/dist/index.d.ts
CHANGED
|
@@ -21,90 +21,6 @@ type OwneySupportedChainId = (typeof SUPPORTED_CHAIN_IDS)[number];
|
|
|
21
21
|
type OwneySupportedChains = (typeof SUPPORTED_CHAINS)[number];
|
|
22
22
|
type OwneySupportedTokens = (typeof SUPPORTED_TOKENS)[number];
|
|
23
23
|
|
|
24
|
-
type DepositCallback = (smartWalletAddress: string, chainId: number, amount: string) => Promise<`0x${string}`> | `0x${string}`;
|
|
25
|
-
interface IAgent {
|
|
26
|
-
readonly id: string;
|
|
27
|
-
readonly supportedChainIds: readonly OwneySupportedChainId[];
|
|
28
|
-
readonly supportedAssets: readonly AgentSupportedAssets[];
|
|
29
|
-
disconnect(): Promise<void>;
|
|
30
|
-
activateAgent(state: ConnectionState, chainId: number): Promise<void>;
|
|
31
|
-
deposit(state: ConnectionState, chainId: number, amount: string, depositCallback?: DepositCallback): Promise<OwneyDepositResult>;
|
|
32
|
-
withdraw(state: ConnectionState, chainId: number, token: OwneySupportedTokens, amount?: string): Promise<AgentWithdrawResult>;
|
|
33
|
-
getBalances(state: ConnectionState, chainId: number): Promise<AgentBalance>;
|
|
34
|
-
getEarnings(state: ConnectionState, chainId: number): Promise<AgentEarnings>;
|
|
35
|
-
getAccountApy(state: ConnectionState, chainId: number, days: DailyApyDays): Promise<AccountAgentApy>;
|
|
36
|
-
getHistory(state: ConnectionState, chainId: number, options?: HistoryFilters): Promise<OwneyAgentHistory>;
|
|
37
|
-
pause(state: ConnectionState, chainId: number): Promise<OwneyAgentStatus>;
|
|
38
|
-
resume(state: ConnectionState, chainId: number): Promise<OwneyAgentStatus>;
|
|
39
|
-
getUserProfile(state: ConnectionState, chainId: number): Promise<AgentUserProfile>;
|
|
40
|
-
getAgentApy(days: DailyApyDays, options?: AgentApyOptions): Promise<AgentApy>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Optional filters for per-agent APY lookups.
|
|
44
|
-
* - `tokenSymbol` / `chainId` scope the APY to a specific asset+chain when
|
|
45
|
-
* the underlying provider supports it (Zyfai). Agents whose backends do
|
|
46
|
-
* not yet support per-asset APY will ignore these options.
|
|
47
|
-
*/
|
|
48
|
-
type AgentApyOptions = {
|
|
49
|
-
tokenSymbol?: string;
|
|
50
|
-
chainId?: number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
type AgentId = "zyfai" | "sail";
|
|
54
|
-
type Asset = string;
|
|
55
|
-
type AgentSupportedAsset = {
|
|
56
|
-
readonly symbol: string;
|
|
57
|
-
readonly minDepositAmount: string;
|
|
58
|
-
};
|
|
59
|
-
type AgentSupportedAssets = {
|
|
60
|
-
readonly chainId: number;
|
|
61
|
-
readonly chain?: string;
|
|
62
|
-
readonly assets: readonly AgentSupportedAsset[];
|
|
63
|
-
};
|
|
64
|
-
type DailyApyDays = "7D" | "14D" | "30D";
|
|
65
|
-
type HistoryFilters = {
|
|
66
|
-
fromDate?: string;
|
|
67
|
-
toDate?: string;
|
|
68
|
-
/** Max entries returned per call. Defaults to 10. */
|
|
69
|
-
limit?: number;
|
|
70
|
-
/** Opaque cursor returned by a previous getHistory call. */
|
|
71
|
-
cursor?: string;
|
|
72
|
-
};
|
|
73
|
-
type HistoryOptions = {
|
|
74
|
-
agentId?: AgentId;
|
|
75
|
-
filters?: HistoryFilters;
|
|
76
|
-
};
|
|
77
|
-
type WithdrawOptions = {
|
|
78
|
-
asset: Asset;
|
|
79
|
-
amount?: string;
|
|
80
|
-
agentId?: AgentId;
|
|
81
|
-
};
|
|
82
|
-
type AccountApyOptions = {
|
|
83
|
-
agentId?: AgentId;
|
|
84
|
-
days: DailyApyDays;
|
|
85
|
-
};
|
|
86
|
-
type AgentsApyOptions = {
|
|
87
|
-
agentId?: AgentId;
|
|
88
|
-
days: DailyApyDays;
|
|
89
|
-
/**
|
|
90
|
-
* Optional asset symbol (e.g. "USDC", "WETH") to scope the APY to a
|
|
91
|
-
* specific asset+chain. Ignored by agents whose backends do not yet
|
|
92
|
-
* support per-asset APY.
|
|
93
|
-
*/
|
|
94
|
-
tokenSymbol?: string;
|
|
95
|
-
/**
|
|
96
|
-
* Optional chain id for per-asset APY lookups. Typically paired with
|
|
97
|
-
* `tokenSymbol`.
|
|
98
|
-
*/
|
|
99
|
-
chainId?: number;
|
|
100
|
-
};
|
|
101
|
-
type DepositOptions = {
|
|
102
|
-
amount: string;
|
|
103
|
-
asset: Asset;
|
|
104
|
-
depositCallback?: DepositCallback;
|
|
105
|
-
agentId?: AgentId;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
24
|
interface OwneyDepositResult {
|
|
109
25
|
txHash: string;
|
|
110
26
|
smartWallet: string;
|
|
@@ -177,6 +93,17 @@ interface OwneyAccountApy {
|
|
|
177
93
|
agentApy: Record<AgentId, AccountAgentApy>;
|
|
178
94
|
apyByChainAndAsset: ApyByChainAndAsset;
|
|
179
95
|
}
|
|
96
|
+
interface AllocationAgentApy {
|
|
97
|
+
/** Account-wide weighted APY for this agent, as a decimal string. */
|
|
98
|
+
totalApy: string;
|
|
99
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
100
|
+
}
|
|
101
|
+
interface OwneyAllocationApy {
|
|
102
|
+
/** Account-wide weighted APY across all active agents, as a decimal string. */
|
|
103
|
+
totalApy: string;
|
|
104
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
105
|
+
agentApy: Record<AgentId, AllocationAgentApy>;
|
|
106
|
+
}
|
|
180
107
|
type HistoryAction = "Rebalance" | "Deposit" | "Top up" | "Withdraw" | "Earned";
|
|
181
108
|
interface HistoryTransaction {
|
|
182
109
|
txHashes: string[];
|
|
@@ -214,11 +141,6 @@ interface OwneyAgentHistory {
|
|
|
214
141
|
/** Opaque cursor for the next page. Present iff `hasMore === true`. */
|
|
215
142
|
nextCursor?: string;
|
|
216
143
|
}
|
|
217
|
-
interface OwneyAgentStatus {
|
|
218
|
-
success: boolean;
|
|
219
|
-
strategy?: string;
|
|
220
|
-
protocols?: string[];
|
|
221
|
-
}
|
|
222
144
|
interface AgentUserProfile {
|
|
223
145
|
address: string;
|
|
224
146
|
smartWallet: string;
|
|
@@ -246,6 +168,88 @@ interface OwneyAgentAllocation {
|
|
|
246
168
|
agentPositions: Record<AgentId, OwneyPosition[]>;
|
|
247
169
|
}
|
|
248
170
|
|
|
171
|
+
type DepositCallback = (smartWalletAddress: string, chainId: number, amount: string) => Promise<`0x${string}`> | `0x${string}`;
|
|
172
|
+
interface IAgent {
|
|
173
|
+
readonly id: string;
|
|
174
|
+
readonly supportedChainIds: readonly OwneySupportedChainId[];
|
|
175
|
+
readonly supportedAssets: readonly AgentSupportedAssets[];
|
|
176
|
+
disconnect(): Promise<void>;
|
|
177
|
+
activateAgent(state: ConnectionState, chainId: number): Promise<void>;
|
|
178
|
+
deposit(state: ConnectionState, chainId: number, amount: string, depositCallback?: DepositCallback): Promise<OwneyDepositResult>;
|
|
179
|
+
withdraw(state: ConnectionState, chainId: number, token: OwneySupportedTokens, amount?: string): Promise<AgentWithdrawResult>;
|
|
180
|
+
getBalances(state: ConnectionState, chainId: number): Promise<AgentBalance>;
|
|
181
|
+
getEarnings(state: ConnectionState, chainId: number): Promise<AgentEarnings>;
|
|
182
|
+
getAccountApy(state: ConnectionState, chainId: number, days: DailyApyDays): Promise<AccountAgentApy>;
|
|
183
|
+
getHistory(state: ConnectionState, chainId: number, options?: HistoryFilters): Promise<OwneyAgentHistory>;
|
|
184
|
+
getUserProfile(state: ConnectionState, chainId: number): Promise<AgentUserProfile>;
|
|
185
|
+
getAgentApy(days: DailyApyDays, options?: AgentApyOptions): Promise<AgentApy>;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Optional filters for per-agent APY lookups.
|
|
189
|
+
* - `tokenSymbol` / `chainId` scope the APY to a specific asset+chain when
|
|
190
|
+
* the underlying provider supports it (Zyfai). Agents whose backends do
|
|
191
|
+
* not yet support per-asset APY will ignore these options.
|
|
192
|
+
*/
|
|
193
|
+
type AgentApyOptions = {
|
|
194
|
+
tokenSymbol?: string;
|
|
195
|
+
chainId?: number;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
type AgentId = "zyfai" | "sail";
|
|
199
|
+
type Asset = string;
|
|
200
|
+
type AgentSupportedAsset = {
|
|
201
|
+
readonly symbol: string;
|
|
202
|
+
readonly minDepositAmount: string;
|
|
203
|
+
};
|
|
204
|
+
type AgentSupportedAssets = {
|
|
205
|
+
readonly chainId: number;
|
|
206
|
+
readonly chain?: string;
|
|
207
|
+
readonly assets: readonly AgentSupportedAsset[];
|
|
208
|
+
};
|
|
209
|
+
type DailyApyDays = "7D" | "14D" | "30D";
|
|
210
|
+
type HistoryFilters = {
|
|
211
|
+
fromDate?: string;
|
|
212
|
+
toDate?: string;
|
|
213
|
+
/** Max entries returned per call. Defaults to 10. */
|
|
214
|
+
limit?: number;
|
|
215
|
+
/** Opaque cursor returned by a previous getHistory call. */
|
|
216
|
+
cursor?: string;
|
|
217
|
+
};
|
|
218
|
+
type HistoryOptions = {
|
|
219
|
+
agentId?: AgentId;
|
|
220
|
+
filters?: HistoryFilters;
|
|
221
|
+
};
|
|
222
|
+
type WithdrawOptions = {
|
|
223
|
+
asset: Asset;
|
|
224
|
+
amount?: string;
|
|
225
|
+
agentId?: AgentId;
|
|
226
|
+
};
|
|
227
|
+
type AccountApyOptions = {
|
|
228
|
+
agentId?: AgentId;
|
|
229
|
+
days: DailyApyDays;
|
|
230
|
+
};
|
|
231
|
+
type AgentsApyOptions = {
|
|
232
|
+
agentId?: AgentId;
|
|
233
|
+
days: DailyApyDays;
|
|
234
|
+
/**
|
|
235
|
+
* Optional asset symbol (e.g. "USDC", "WETH") to scope the APY to a
|
|
236
|
+
* specific asset+chain. Ignored by agents whose backends do not yet
|
|
237
|
+
* support per-asset APY.
|
|
238
|
+
*/
|
|
239
|
+
tokenSymbol?: string;
|
|
240
|
+
/**
|
|
241
|
+
* Optional chain id for per-asset APY lookups. Typically paired with
|
|
242
|
+
* `tokenSymbol`.
|
|
243
|
+
*/
|
|
244
|
+
chainId?: number;
|
|
245
|
+
};
|
|
246
|
+
type DepositOptions = {
|
|
247
|
+
amount: string;
|
|
248
|
+
asset: Asset;
|
|
249
|
+
depositCallback?: DepositCallback;
|
|
250
|
+
agentId?: AgentId;
|
|
251
|
+
};
|
|
252
|
+
|
|
249
253
|
declare class OwneySDK {
|
|
250
254
|
private agents;
|
|
251
255
|
private activeAgents;
|
|
@@ -382,18 +386,6 @@ declare class OwneySDK {
|
|
|
382
386
|
* @returns {OwneyAgentHistory} `data` (up to `limit` entries), `hasMore`, `nextCursor`.
|
|
383
387
|
*/
|
|
384
388
|
getHistory(params?: HistoryOptions): Promise<OwneyAgentHistory>;
|
|
385
|
-
/**
|
|
386
|
-
* Pause an agent's automated operations. The agent will stop rebalancing until resumed.
|
|
387
|
-
* @param agentId - The agent to pause (e.g. "zyfai")
|
|
388
|
-
* @returns OwneyAgentStatus with success flag
|
|
389
|
-
*/
|
|
390
|
-
pauseAgent(agentId: AgentId): Promise<OwneyAgentStatus>;
|
|
391
|
-
/**
|
|
392
|
-
* Resume an agent's automated operations.
|
|
393
|
-
* @param agentId - The agent to resume (e.g. "zyfai")
|
|
394
|
-
* @returns OwneyAgentStatus with success flag
|
|
395
|
-
*/
|
|
396
|
-
resumeAgent(agentId: AgentId): Promise<OwneyAgentStatus>;
|
|
397
389
|
/**
|
|
398
390
|
* Get the user's profile for a specific agent, or all agents.
|
|
399
391
|
* @param agentId - Optional. Agent to query. Omit for all agents.
|
|
@@ -418,6 +410,16 @@ declare class OwneySDK {
|
|
|
418
410
|
* @returns Object with agent names as keys and allocation percentages as values
|
|
419
411
|
*/
|
|
420
412
|
getAgentAllocation(): Promise<OwneyAgentAllocation>;
|
|
413
|
+
/**
|
|
414
|
+
* Allocation-weighted APY from the current portfolio: each pool's APY
|
|
415
|
+
* weighted by the user's position value in it. More accurate per-user than
|
|
416
|
+
* the historical 30D APY. (ROUT-228)
|
|
417
|
+
* @param options.agentId - Optional. Scope to one agent. Omit to aggregate
|
|
418
|
+
* positions across all active agents.
|
|
419
|
+
*/
|
|
420
|
+
getAllocationApy({ agentId, }?: {
|
|
421
|
+
agentId?: AgentId;
|
|
422
|
+
}): Promise<OwneyAllocationApy>;
|
|
421
423
|
}
|
|
422
424
|
|
|
423
425
|
type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "AGENT_DISABLED" | "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" | "SPONSOR_REQUEST_FAILED" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
|
|
@@ -463,4 +465,4 @@ type OwneySIWXConfig = {
|
|
|
463
465
|
*/
|
|
464
466
|
declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
|
|
465
467
|
|
|
466
|
-
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory,
|
|
468
|
+
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, AllocationAgentApy, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAllocationApy, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneyPosition, OwneySDK, OwneySDKConfig, OwneySIWXConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions, createOwneySIWX };
|
package/dist/index.js
CHANGED
|
@@ -125,6 +125,18 @@ var CHAIN_ID_TO_NAME = {
|
|
|
125
125
|
8453: "BASE",
|
|
126
126
|
42161: "ARBITRUM"
|
|
127
127
|
};
|
|
128
|
+
var NAME_TO_CHAIN_ID = Object.entries(
|
|
129
|
+
CHAIN_ID_TO_NAME
|
|
130
|
+
).reduce((acc, [id, name]) => {
|
|
131
|
+
acc[name.toLowerCase()] = Number(id);
|
|
132
|
+
return acc;
|
|
133
|
+
}, {});
|
|
134
|
+
function resolveChainId(chain) {
|
|
135
|
+
const asNum = Number(chain);
|
|
136
|
+
if (Number.isFinite(asNum) && asNum > 0)
|
|
137
|
+
return asNum;
|
|
138
|
+
return NAME_TO_CHAIN_ID[chain.trim().toLowerCase()] ?? NaN;
|
|
139
|
+
}
|
|
128
140
|
function mapBalances(raw, _chainId, smartWallet) {
|
|
129
141
|
const portfolio = raw.portfolio;
|
|
130
142
|
const portfolioByChain = portfolio.portfolioByChain ?? {};
|
|
@@ -273,13 +285,6 @@ function mapEntries(rawEntries, chainId) {
|
|
|
273
285
|
};
|
|
274
286
|
});
|
|
275
287
|
}
|
|
276
|
-
function mapAgentStatus(raw) {
|
|
277
|
-
return {
|
|
278
|
-
success: raw.success,
|
|
279
|
-
strategy: raw.strategy,
|
|
280
|
-
protocols: raw.protocols
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
288
|
function mapUserProfile(raw, userAddress) {
|
|
284
289
|
return {
|
|
285
290
|
address: userAddress,
|
|
@@ -315,6 +320,46 @@ function mapApyByStrategy(raw) {
|
|
|
315
320
|
detailedApys: hasDetails ? { apyPerAsset } : void 0
|
|
316
321
|
};
|
|
317
322
|
}
|
|
323
|
+
function computeAllocationApy(positions) {
|
|
324
|
+
let weightedSum = 0;
|
|
325
|
+
let totalValue = 0;
|
|
326
|
+
const buckets = {};
|
|
327
|
+
for (const p of positions) {
|
|
328
|
+
const apy = p.apy;
|
|
329
|
+
const value = Number(p.amount);
|
|
330
|
+
if (typeof apy !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
weightedSum += apy * value;
|
|
334
|
+
totalValue += value;
|
|
335
|
+
const chainId = resolveChainId(p.chain);
|
|
336
|
+
const token = p.asset;
|
|
337
|
+
if (!SUPPORTED_CHAIN_IDS.includes(chainId))
|
|
338
|
+
continue;
|
|
339
|
+
if (!SUPPORTED_TOKENS.includes(token))
|
|
340
|
+
continue;
|
|
341
|
+
const perAsset = buckets[chainId] ?? {};
|
|
342
|
+
const cell = perAsset[token] ?? { weightedSum: 0, value: 0 };
|
|
343
|
+
cell.weightedSum += apy * value;
|
|
344
|
+
cell.value += value;
|
|
345
|
+
perAsset[token] = cell;
|
|
346
|
+
buckets[chainId] = perAsset;
|
|
347
|
+
}
|
|
348
|
+
const apyByChainAndAsset = {};
|
|
349
|
+
for (const [chainKey, perAsset] of Object.entries(buckets)) {
|
|
350
|
+
const chainId = Number(chainKey);
|
|
351
|
+
const out = {};
|
|
352
|
+
for (const [token, cell] of Object.entries(perAsset ?? {})) {
|
|
353
|
+
if (cell && cell.value > 0) {
|
|
354
|
+
out[token] = cell.weightedSum / cell.value;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (Object.keys(out).length > 0)
|
|
358
|
+
apyByChainAndAsset[chainId] = out;
|
|
359
|
+
}
|
|
360
|
+
const totalApy = totalValue > 0 ? String(weightedSum / totalValue) : "0";
|
|
361
|
+
return { totalApy, apyByChainAndAsset };
|
|
362
|
+
}
|
|
318
363
|
|
|
319
364
|
// src/agents/zyfai/zyfai.history-cursor.ts
|
|
320
365
|
var InvalidHistoryCursorError = class extends Error {
|
|
@@ -697,17 +742,6 @@ var ZyfaiAgent = class {
|
|
|
697
742
|
nextCursor
|
|
698
743
|
};
|
|
699
744
|
}
|
|
700
|
-
// --- IAgent: Agent control ---
|
|
701
|
-
async pause(state, chainId) {
|
|
702
|
-
await this.connectAuth(state, chainId);
|
|
703
|
-
const raw = await this.sdk.pauseAgent();
|
|
704
|
-
return mapAgentStatus(raw);
|
|
705
|
-
}
|
|
706
|
-
async resume(state, chainId) {
|
|
707
|
-
await this.connectAuth(state, chainId);
|
|
708
|
-
const raw = await this.sdk.resumeAgent();
|
|
709
|
-
return mapAgentStatus(raw);
|
|
710
|
-
}
|
|
711
745
|
async getUserProfile(state, chainId) {
|
|
712
746
|
await this.connectAuth(state, chainId);
|
|
713
747
|
const validChainId = isValidChainId(chainId);
|
|
@@ -1072,7 +1106,10 @@ var SPONSORED_USDC_BY_CHAIN = {
|
|
|
1072
1106
|
8453: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
|
|
1073
1107
|
42161: "0xaf88d065e77c8cc2239327c5edb3a432268e5831"
|
|
1074
1108
|
};
|
|
1075
|
-
var VIEM_CHAIN = {
|
|
1109
|
+
var VIEM_CHAIN = {
|
|
1110
|
+
8453: base,
|
|
1111
|
+
42161: arbitrum
|
|
1112
|
+
};
|
|
1076
1113
|
var OwneySDK = class {
|
|
1077
1114
|
agents = /* @__PURE__ */ new Map();
|
|
1078
1115
|
activeAgents = /* @__PURE__ */ new Set();
|
|
@@ -1592,10 +1629,7 @@ var OwneySDK = class {
|
|
|
1592
1629
|
disabledBalances,
|
|
1593
1630
|
requested
|
|
1594
1631
|
);
|
|
1595
|
-
const enabledTotal = enabledBalances.reduce(
|
|
1596
|
-
(s, b) => s + b.balance,
|
|
1597
|
-
0n
|
|
1598
|
-
);
|
|
1632
|
+
const enabledTotal = enabledBalances.reduce((s, b) => s + b.balance, 0n);
|
|
1599
1633
|
const enabledPlans = afterDrain > 0n ? planProportionalShares(enabledBalances, afterDrain, enabledTotal) : enabledBalances.map(({ agent, balance }) => ({
|
|
1600
1634
|
agent,
|
|
1601
1635
|
balance,
|
|
@@ -1866,27 +1900,6 @@ var OwneySDK = class {
|
|
|
1866
1900
|
nextCursor: hasMore ? encodeMultiAgentCursor(nextCursorMap) : void 0
|
|
1867
1901
|
};
|
|
1868
1902
|
}
|
|
1869
|
-
// --- Agent control ---
|
|
1870
|
-
/**
|
|
1871
|
-
* Pause an agent's automated operations. The agent will stop rebalancing until resumed.
|
|
1872
|
-
* @param agentId - The agent to pause (e.g. "zyfai")
|
|
1873
|
-
* @returns OwneyAgentStatus with success flag
|
|
1874
|
-
*/
|
|
1875
|
-
async pauseAgent(agentId) {
|
|
1876
|
-
const state = this.requireState();
|
|
1877
|
-
const chainId = this.requireChainId();
|
|
1878
|
-
return this.getAgent(agentId).pause(state, chainId);
|
|
1879
|
-
}
|
|
1880
|
-
/**
|
|
1881
|
-
* Resume an agent's automated operations.
|
|
1882
|
-
* @param agentId - The agent to resume (e.g. "zyfai")
|
|
1883
|
-
* @returns OwneyAgentStatus with success flag
|
|
1884
|
-
*/
|
|
1885
|
-
async resumeAgent(agentId) {
|
|
1886
|
-
const state = this.requireState();
|
|
1887
|
-
const chainId = this.requireChainId();
|
|
1888
|
-
return this.getAgent(agentId).resume(state, chainId);
|
|
1889
|
-
}
|
|
1890
1903
|
/**
|
|
1891
1904
|
* Get the user's profile for a specific agent, or all agents.
|
|
1892
1905
|
* @param agentId - Optional. Agent to query. Omit for all agents.
|
|
@@ -2016,6 +2029,62 @@ var OwneySDK = class {
|
|
|
2016
2029
|
agentPositions: positionsByAgent
|
|
2017
2030
|
};
|
|
2018
2031
|
}
|
|
2032
|
+
/**
|
|
2033
|
+
* Allocation-weighted APY from the current portfolio: each pool's APY
|
|
2034
|
+
* weighted by the user's position value in it. More accurate per-user than
|
|
2035
|
+
* the historical 30D APY. (ROUT-228)
|
|
2036
|
+
* @param options.agentId - Optional. Scope to one agent. Omit to aggregate
|
|
2037
|
+
* positions across all active agents.
|
|
2038
|
+
*/
|
|
2039
|
+
async getAllocationApy({
|
|
2040
|
+
agentId
|
|
2041
|
+
} = {}) {
|
|
2042
|
+
const state = this.requireState();
|
|
2043
|
+
const chainId = this.requireChainId();
|
|
2044
|
+
const activeAgents = agentId ? /* @__PURE__ */ new Map([[agentId, this.getAgent(agentId)]]) : this.getActiveAgents();
|
|
2045
|
+
const entries = [...activeAgents.entries()];
|
|
2046
|
+
const balanceResults = await Promise.allSettled(
|
|
2047
|
+
entries.map(async ([id, agent]) => {
|
|
2048
|
+
const b = await agent.getBalances(state, chainId);
|
|
2049
|
+
return [id, b.positions ?? []];
|
|
2050
|
+
})
|
|
2051
|
+
);
|
|
2052
|
+
const agentApy = {};
|
|
2053
|
+
const allPositions = [];
|
|
2054
|
+
let successCount = 0;
|
|
2055
|
+
const agentErrors = {};
|
|
2056
|
+
for (let i = 0; i < balanceResults.length; i++) {
|
|
2057
|
+
const settled = balanceResults[i];
|
|
2058
|
+
const [aid] = entries[i];
|
|
2059
|
+
if (settled.status === "fulfilled") {
|
|
2060
|
+
const [id, positions] = settled.value;
|
|
2061
|
+
const per = computeAllocationApy(positions);
|
|
2062
|
+
agentApy[id] = {
|
|
2063
|
+
totalApy: per.totalApy,
|
|
2064
|
+
apyByChainAndAsset: per.apyByChainAndAsset
|
|
2065
|
+
};
|
|
2066
|
+
allPositions.push(...positions);
|
|
2067
|
+
successCount += 1;
|
|
2068
|
+
} else {
|
|
2069
|
+
const reason = settled.reason;
|
|
2070
|
+
agentErrors[aid] = reason instanceof Error ? reason.message : String(reason);
|
|
2071
|
+
console.error(`getAllocationApy agent "${aid}" failed:`, reason);
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
if (successCount === 0) {
|
|
2075
|
+
throw new OwneyError(
|
|
2076
|
+
"ALLOCATION_ALL_FAILED",
|
|
2077
|
+
"Failed to fetch allocation APY: all agents failed.",
|
|
2078
|
+
{ agentErrors }
|
|
2079
|
+
);
|
|
2080
|
+
}
|
|
2081
|
+
const overall = computeAllocationApy(allPositions);
|
|
2082
|
+
return {
|
|
2083
|
+
totalApy: overall.totalApy,
|
|
2084
|
+
apyByChainAndAsset: overall.apyByChainAndAsset,
|
|
2085
|
+
agentApy
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2019
2088
|
};
|
|
2020
2089
|
|
|
2021
2090
|
// src/agents/zyfai/zyfai.siwx.ts
|