@owney/sdk 0.2.17 → 0.2.18
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 +12 -0
- package/dist/index.cjs +66 -2
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +66 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -280,12 +280,18 @@ console.log(apy.walletAddress); // "0x..."
|
|
|
280
280
|
console.log(apy.weightedApyAfterFee); // 5.2
|
|
281
281
|
// Per chain + token breakdown, when the agent reports it
|
|
282
282
|
console.log(apy.weightedApyAfterFeeDetails?.apyPerAsset[8453]?.USDC); // 5.4
|
|
283
|
+
// Same per-(chain, asset) breakdown surfaced under a unified key
|
|
284
|
+
console.log(apy.apyByChainAndAsset?.[8453]?.USDC); // 5.4
|
|
283
285
|
|
|
284
286
|
// All agents (balance-weighted)
|
|
285
287
|
const allApy = await sdk.getAccountApy({ days: "30D" });
|
|
286
288
|
console.log(allApy.totalApy); // "5.6"
|
|
287
289
|
console.log(allApy.agentApy.zyfai.weightedApyAfterFee); // 6.0
|
|
288
290
|
console.log(allApy.agentApy.sail.weightedApyAfterFee); // 4.0
|
|
291
|
+
// Per (chain, asset), balance-weighted across all agents.
|
|
292
|
+
// Cells with 0/missing APY for a given agent are excluded from that cell.
|
|
293
|
+
console.log(allApy.apyByChainAndAsset[8453]?.USDC); // 5.3
|
|
294
|
+
console.log(allApy.apyByChainAndAsset[42161]?.USDT); // 4.7
|
|
289
295
|
```
|
|
290
296
|
|
|
291
297
|
### `getHistory({ agentId?, filters? })`
|
|
@@ -560,15 +566,21 @@ interface OwneyEarnings {
|
|
|
560
566
|
agentEarnings: Record<AgentId, AgentEarnings>;
|
|
561
567
|
}
|
|
562
568
|
|
|
569
|
+
type ApyByChainAndAsset = Partial<
|
|
570
|
+
Record<OwneySupportedChainId, Partial<Record<OwneySupportedTokens, number>>>
|
|
571
|
+
>;
|
|
572
|
+
|
|
563
573
|
interface AccountAgentApy {
|
|
564
574
|
walletAddress: string;
|
|
565
575
|
weightedApyAfterFee?: number;
|
|
566
576
|
weightedApyAfterFeeDetails?: AgentApyDetails;
|
|
577
|
+
apyByChainAndAsset?: ApyByChainAndAsset;
|
|
567
578
|
}
|
|
568
579
|
|
|
569
580
|
interface OwneyAccountApy {
|
|
570
581
|
totalApy: string;
|
|
571
582
|
agentApy: Record<AgentId, AccountAgentApy>;
|
|
583
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
572
584
|
}
|
|
573
585
|
|
|
574
586
|
type HistoryAction = "Rebalance" | "Deposit" | "Top up" | "Withdraw" | "Earned";
|
package/dist/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ var P_SUPPORTED_CHAINS = {
|
|
|
36
36
|
chainIds: [8453, 42161],
|
|
37
37
|
chains: ["BASE", "ARBITRUM"]
|
|
38
38
|
};
|
|
39
|
+
var SUPPORTED_TOKENS = ["USDC", "USDT"];
|
|
39
40
|
var SUPPORTED_CHAIN_IDS = P_SUPPORTED_CHAINS.chainIds;
|
|
40
41
|
var SUPPORTED_CHAINS = P_SUPPORTED_CHAINS.chains;
|
|
41
42
|
var SupportedAssets = [
|
|
@@ -209,10 +210,31 @@ function mapEarnings(raw, smartWallet) {
|
|
|
209
210
|
tokens
|
|
210
211
|
};
|
|
211
212
|
}
|
|
213
|
+
function mapWeightedApyByChain(raw) {
|
|
214
|
+
if (!raw)
|
|
215
|
+
return void 0;
|
|
216
|
+
const out = {};
|
|
217
|
+
for (const [chainKey, tokenApy] of Object.entries(raw)) {
|
|
218
|
+
const chainId = Number(chainKey);
|
|
219
|
+
if (!SUPPORTED_CHAIN_IDS.includes(chainId))
|
|
220
|
+
continue;
|
|
221
|
+
const perAsset = {};
|
|
222
|
+
for (const [symbol, apy] of Object.entries(tokenApy)) {
|
|
223
|
+
const token = symbol;
|
|
224
|
+
if (!SUPPORTED_TOKENS.includes(token))
|
|
225
|
+
continue;
|
|
226
|
+
perAsset[token] = apy;
|
|
227
|
+
}
|
|
228
|
+
if (Object.keys(perAsset).length > 0)
|
|
229
|
+
out[chainId] = perAsset;
|
|
230
|
+
}
|
|
231
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
232
|
+
}
|
|
212
233
|
function mapApyHistory(raw) {
|
|
213
234
|
return {
|
|
214
235
|
walletAddress: raw.walletAddress,
|
|
215
|
-
weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0
|
|
236
|
+
weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0,
|
|
237
|
+
apyByChainAndAsset: mapWeightedApyByChain(raw.weightedApyAfterFeeByChain)
|
|
216
238
|
};
|
|
217
239
|
}
|
|
218
240
|
function mapHistory(raw, chainId) {
|
|
@@ -612,6 +634,46 @@ function sumWithdrawnAmount(results) {
|
|
|
612
634
|
return Object.values(results).reduce((sum, r) => sum + BigInt(r.amount), 0n);
|
|
613
635
|
}
|
|
614
636
|
|
|
637
|
+
// src/lib/helpers/account-apy-helper.ts
|
|
638
|
+
function aggregateApyByChainAndAsset(agentApys, agentBalances) {
|
|
639
|
+
const sums = {};
|
|
640
|
+
const weights = {};
|
|
641
|
+
for (const id of Object.keys(agentApys)) {
|
|
642
|
+
const cells = agentApys[id].apyByChainAndAsset;
|
|
643
|
+
const balance = agentBalances[id] ?? 0;
|
|
644
|
+
if (!cells || balance <= 0)
|
|
645
|
+
continue;
|
|
646
|
+
for (const [chainKey, perAsset] of Object.entries(cells)) {
|
|
647
|
+
if (!perAsset)
|
|
648
|
+
continue;
|
|
649
|
+
const chainId = Number(chainKey);
|
|
650
|
+
for (const [asset, apyValue] of Object.entries(perAsset)) {
|
|
651
|
+
const apy = Number(apyValue ?? 0);
|
|
652
|
+
if (apy === 0)
|
|
653
|
+
continue;
|
|
654
|
+
sums[chainId] ??= {};
|
|
655
|
+
weights[chainId] ??= {};
|
|
656
|
+
sums[chainId][asset] = (sums[chainId][asset] ?? 0) + apy * balance;
|
|
657
|
+
weights[chainId][asset] = (weights[chainId][asset] ?? 0) + balance;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
const out = {};
|
|
662
|
+
for (const chainKey of Object.keys(sums)) {
|
|
663
|
+
const chainId = Number(chainKey);
|
|
664
|
+
const perAssetOut = {};
|
|
665
|
+
for (const asset of Object.keys(sums[chainId])) {
|
|
666
|
+
const w = weights[chainId][asset];
|
|
667
|
+
if (w > 0)
|
|
668
|
+
perAssetOut[asset] = sums[chainId][asset] / w;
|
|
669
|
+
}
|
|
670
|
+
if (Object.keys(perAssetOut).length > 0) {
|
|
671
|
+
out[chainId] = perAssetOut;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return out;
|
|
675
|
+
}
|
|
676
|
+
|
|
615
677
|
// src/client.ts
|
|
616
678
|
var import_viem = require("viem");
|
|
617
679
|
var OwneySDK = class {
|
|
@@ -1184,9 +1246,11 @@ var OwneySDK = class {
|
|
|
1184
1246
|
totalApy += agentApy * weight;
|
|
1185
1247
|
}
|
|
1186
1248
|
}
|
|
1249
|
+
const apyByChainAndAsset = aggregateApyByChainAndAsset(results, balances);
|
|
1187
1250
|
return {
|
|
1188
1251
|
totalApy: String(totalApy),
|
|
1189
|
-
agentApy: results
|
|
1252
|
+
agentApy: results,
|
|
1253
|
+
apyByChainAndAsset
|
|
1190
1254
|
};
|
|
1191
1255
|
}
|
|
1192
1256
|
/**
|
package/dist/index.d.cts
CHANGED
|
@@ -139,14 +139,17 @@ interface OwneyEarnings {
|
|
|
139
139
|
totalEarnings: string;
|
|
140
140
|
agentEarnings: Record<AgentId, AgentEarnings>;
|
|
141
141
|
}
|
|
142
|
+
type ApyByChainAndAsset = Partial<Record<OwneySupportedChainId, Partial<Record<OwneySupportedTokens, number>>>>;
|
|
142
143
|
interface AccountAgentApy {
|
|
143
144
|
walletAddress: string;
|
|
144
145
|
weightedApyAfterFee?: number;
|
|
145
146
|
weightedApyAfterFeeDetails?: AgentApyDetails;
|
|
147
|
+
apyByChainAndAsset?: ApyByChainAndAsset;
|
|
146
148
|
}
|
|
147
149
|
interface OwneyAccountApy {
|
|
148
150
|
totalApy: string;
|
|
149
151
|
agentApy: Record<AgentId, AccountAgentApy>;
|
|
152
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
150
153
|
}
|
|
151
154
|
type HistoryAction = "Rebalance" | "Deposit" | "Top up" | "Withdraw" | "Earned";
|
|
152
155
|
interface HistoryTransaction {
|
|
@@ -371,4 +374,4 @@ declare class AgentChainIncompatibleError extends OwneyError {
|
|
|
371
374
|
}>, connectedChainId: number);
|
|
372
375
|
}
|
|
373
376
|
|
|
374
|
-
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
|
|
377
|
+
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -139,14 +139,17 @@ interface OwneyEarnings {
|
|
|
139
139
|
totalEarnings: string;
|
|
140
140
|
agentEarnings: Record<AgentId, AgentEarnings>;
|
|
141
141
|
}
|
|
142
|
+
type ApyByChainAndAsset = Partial<Record<OwneySupportedChainId, Partial<Record<OwneySupportedTokens, number>>>>;
|
|
142
143
|
interface AccountAgentApy {
|
|
143
144
|
walletAddress: string;
|
|
144
145
|
weightedApyAfterFee?: number;
|
|
145
146
|
weightedApyAfterFeeDetails?: AgentApyDetails;
|
|
147
|
+
apyByChainAndAsset?: ApyByChainAndAsset;
|
|
146
148
|
}
|
|
147
149
|
interface OwneyAccountApy {
|
|
148
150
|
totalApy: string;
|
|
149
151
|
agentApy: Record<AgentId, AccountAgentApy>;
|
|
152
|
+
apyByChainAndAsset: ApyByChainAndAsset;
|
|
150
153
|
}
|
|
151
154
|
type HistoryAction = "Rebalance" | "Deposit" | "Top up" | "Withdraw" | "Earned";
|
|
152
155
|
interface HistoryTransaction {
|
|
@@ -371,4 +374,4 @@ declare class AgentChainIncompatibleError extends OwneyError {
|
|
|
371
374
|
}>, connectedChainId: number);
|
|
372
375
|
}
|
|
373
376
|
|
|
374
|
-
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
|
|
377
|
+
export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@ var P_SUPPORTED_CHAINS = {
|
|
|
6
6
|
chainIds: [8453, 42161],
|
|
7
7
|
chains: ["BASE", "ARBITRUM"]
|
|
8
8
|
};
|
|
9
|
+
var SUPPORTED_TOKENS = ["USDC", "USDT"];
|
|
9
10
|
var SUPPORTED_CHAIN_IDS = P_SUPPORTED_CHAINS.chainIds;
|
|
10
11
|
var SUPPORTED_CHAINS = P_SUPPORTED_CHAINS.chains;
|
|
11
12
|
var SupportedAssets = [
|
|
@@ -179,10 +180,31 @@ function mapEarnings(raw, smartWallet) {
|
|
|
179
180
|
tokens
|
|
180
181
|
};
|
|
181
182
|
}
|
|
183
|
+
function mapWeightedApyByChain(raw) {
|
|
184
|
+
if (!raw)
|
|
185
|
+
return void 0;
|
|
186
|
+
const out = {};
|
|
187
|
+
for (const [chainKey, tokenApy] of Object.entries(raw)) {
|
|
188
|
+
const chainId = Number(chainKey);
|
|
189
|
+
if (!SUPPORTED_CHAIN_IDS.includes(chainId))
|
|
190
|
+
continue;
|
|
191
|
+
const perAsset = {};
|
|
192
|
+
for (const [symbol, apy] of Object.entries(tokenApy)) {
|
|
193
|
+
const token = symbol;
|
|
194
|
+
if (!SUPPORTED_TOKENS.includes(token))
|
|
195
|
+
continue;
|
|
196
|
+
perAsset[token] = apy;
|
|
197
|
+
}
|
|
198
|
+
if (Object.keys(perAsset).length > 0)
|
|
199
|
+
out[chainId] = perAsset;
|
|
200
|
+
}
|
|
201
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
202
|
+
}
|
|
182
203
|
function mapApyHistory(raw) {
|
|
183
204
|
return {
|
|
184
205
|
walletAddress: raw.walletAddress,
|
|
185
|
-
weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0
|
|
206
|
+
weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0,
|
|
207
|
+
apyByChainAndAsset: mapWeightedApyByChain(raw.weightedApyAfterFeeByChain)
|
|
186
208
|
};
|
|
187
209
|
}
|
|
188
210
|
function mapHistory(raw, chainId) {
|
|
@@ -582,6 +604,46 @@ function sumWithdrawnAmount(results) {
|
|
|
582
604
|
return Object.values(results).reduce((sum, r) => sum + BigInt(r.amount), 0n);
|
|
583
605
|
}
|
|
584
606
|
|
|
607
|
+
// src/lib/helpers/account-apy-helper.ts
|
|
608
|
+
function aggregateApyByChainAndAsset(agentApys, agentBalances) {
|
|
609
|
+
const sums = {};
|
|
610
|
+
const weights = {};
|
|
611
|
+
for (const id of Object.keys(agentApys)) {
|
|
612
|
+
const cells = agentApys[id].apyByChainAndAsset;
|
|
613
|
+
const balance = agentBalances[id] ?? 0;
|
|
614
|
+
if (!cells || balance <= 0)
|
|
615
|
+
continue;
|
|
616
|
+
for (const [chainKey, perAsset] of Object.entries(cells)) {
|
|
617
|
+
if (!perAsset)
|
|
618
|
+
continue;
|
|
619
|
+
const chainId = Number(chainKey);
|
|
620
|
+
for (const [asset, apyValue] of Object.entries(perAsset)) {
|
|
621
|
+
const apy = Number(apyValue ?? 0);
|
|
622
|
+
if (apy === 0)
|
|
623
|
+
continue;
|
|
624
|
+
sums[chainId] ??= {};
|
|
625
|
+
weights[chainId] ??= {};
|
|
626
|
+
sums[chainId][asset] = (sums[chainId][asset] ?? 0) + apy * balance;
|
|
627
|
+
weights[chainId][asset] = (weights[chainId][asset] ?? 0) + balance;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
const out = {};
|
|
632
|
+
for (const chainKey of Object.keys(sums)) {
|
|
633
|
+
const chainId = Number(chainKey);
|
|
634
|
+
const perAssetOut = {};
|
|
635
|
+
for (const asset of Object.keys(sums[chainId])) {
|
|
636
|
+
const w = weights[chainId][asset];
|
|
637
|
+
if (w > 0)
|
|
638
|
+
perAssetOut[asset] = sums[chainId][asset] / w;
|
|
639
|
+
}
|
|
640
|
+
if (Object.keys(perAssetOut).length > 0) {
|
|
641
|
+
out[chainId] = perAssetOut;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return out;
|
|
645
|
+
}
|
|
646
|
+
|
|
585
647
|
// src/client.ts
|
|
586
648
|
import { parseUnits } from "viem";
|
|
587
649
|
var OwneySDK = class {
|
|
@@ -1154,9 +1216,11 @@ var OwneySDK = class {
|
|
|
1154
1216
|
totalApy += agentApy * weight;
|
|
1155
1217
|
}
|
|
1156
1218
|
}
|
|
1219
|
+
const apyByChainAndAsset = aggregateApyByChainAndAsset(results, balances);
|
|
1157
1220
|
return {
|
|
1158
1221
|
totalApy: String(totalApy),
|
|
1159
|
-
agentApy: results
|
|
1222
|
+
agentApy: results,
|
|
1223
|
+
apyByChainAndAsset
|
|
1160
1224
|
};
|
|
1161
1225
|
}
|
|
1162
1226
|
/**
|