@owney/sdk 0.2.11 → 0.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -244,11 +244,17 @@ Returns: `AgentEarnings` (single agent) or `OwneyEarnings` (all agents)
244
244
  const earnings = await sdk.getEarnings("zyfai");
245
245
  console.log(earnings.smartWallet); // "0x..."
246
246
  console.log(earnings.lifetimeEarnings); // 42.5
247
+ console.log(earnings.tokens);
248
+ // [
249
+ // { chain: "BASE", chainId: 8453, asset: "USDC", amount: "30.0" },
250
+ // { chain: "ARBITRUM", chainId: 42161, asset: "USDC", amount: "12.5" },
251
+ // ]
247
252
 
248
253
  // All agents
249
254
  const allEarnings = await sdk.getEarnings();
250
255
  console.log(allEarnings.totalEarnings); // "85.0"
251
256
  console.log(allEarnings.agentEarnings.zyfai.lifetimeEarnings); // 42.5
257
+ console.log(allEarnings.agentEarnings.zyfai.tokens); // per-chain, per-asset breakdown
252
258
  ```
253
259
 
254
260
  ### `getAccountApy({ agentId?, days })`
@@ -535,6 +541,7 @@ interface OwneyBalances {
535
541
  interface AgentEarnings {
536
542
  smartWallet: `0x${string}`;
537
543
  lifetimeEarnings: number;
544
+ tokens: OwneyToken[];
538
545
  }
539
546
 
540
547
  interface OwneyEarnings {
@@ -718,7 +725,3 @@ The three convenience subclasses extend `OwneyError` and remain backwards-compat
718
725
  | `NotConnectedError` | `NOT_CONNECTED` | — |
719
726
  | `AgentNotFoundError` | `AGENT_NOT_FOUND` | — |
720
727
  | `AgentChainIncompatibleError` | `AGENT_CHAIN_INCOMPATIBLE` | `incompatibleAgents`, `connectedChainId` |
721
-
722
- ## License
723
-
724
- MIT
package/dist/index.cjs CHANGED
@@ -128,15 +128,6 @@ function hexToDecimal(hex, decimals = 6) {
128
128
  const remainder = parsed % divisor;
129
129
  return Number(whole) + Number(remainder) / Number(divisor);
130
130
  }
131
- function normalizeDecimalPlace(value, decimals = 6) {
132
- const parsed = BigInt(value);
133
- if (decimals <= 0)
134
- return Number(parsed);
135
- const divisor = BigInt(10 ** decimals);
136
- const whole = parsed / divisor;
137
- const remainder = parsed % divisor;
138
- return Number(whole) + Number(remainder) / Number(divisor);
139
- }
140
131
  var DayFilterMapping = {
141
132
  "30D": 30,
142
133
  "14D": 14,
@@ -162,34 +153,25 @@ var CHAIN_ID_TO_NAME = {
162
153
  8453: "BASE",
163
154
  42161: "ARBITRUM"
164
155
  };
165
- var CHAIN_NAME_TO_ID = {
166
- BASE: 8453,
167
- ARBITRUM: 42161
168
- };
169
- function mapBalances(raw, chainId, smartWallet) {
156
+ function mapBalances(raw, _chainId, smartWallet) {
170
157
  const portfolio = raw.portfolio;
171
- const tokenMap = /* @__PURE__ */ new Map();
172
- for (const p of portfolio.positions || []) {
173
- const symbol = p.token_symbol || "USDC";
174
- const positionChainId = (p.chain ? CHAIN_NAME_TO_ID[p.chain.toUpperCase()] : void 0) ?? chainId;
175
- const key = `${symbol}:${positionChainId}`;
176
- const current = tokenMap.get(key);
177
- tokenMap.set(key, {
178
- symbol,
179
- chainId: positionChainId,
180
- amount: (current?.amount ?? 0) + normalizeDecimalPlace(p.underlyingAmount || "0")
181
- });
182
- }
158
+ const portfolioByChain = portfolio.portfolioByChain ?? {};
183
159
  let totalBalance = 0;
184
160
  const tokens = [];
185
- for (const [, entry] of tokenMap) {
186
- totalBalance += entry.amount;
187
- tokens.push({
188
- chain: CHAIN_ID_TO_NAME[entry.chainId] ?? "BASE",
189
- chainId: entry.chainId,
190
- asset: entry.symbol,
191
- amount: String(entry.amount)
192
- });
161
+ for (const [chainIdKey, tokensBySymbol] of Object.entries(portfolioByChain)) {
162
+ const positionChainId = Number(chainIdKey);
163
+ for (const [symbol, { balance, decimals }] of Object.entries(
164
+ tokensBySymbol ?? {}
165
+ )) {
166
+ const amount = hexToDecimal(balance || "0", decimals);
167
+ totalBalance += amount;
168
+ tokens.push({
169
+ chain: CHAIN_ID_TO_NAME[positionChainId] ?? "BASE",
170
+ chainId: positionChainId,
171
+ asset: symbol,
172
+ amount: String(amount)
173
+ });
174
+ }
193
175
  }
194
176
  return {
195
177
  smartWallet,
@@ -204,9 +186,26 @@ function sumTokenEarnings(tokens) {
204
186
  return Object.values(tokens).reduce((sum, val) => sum + Number(val), 0);
205
187
  }
206
188
  function mapEarnings(raw, smartWallet) {
189
+ const totalEarningsByChain = raw.data.totalEarningsByChain;
190
+ const tokens = [];
191
+ for (const [chainIdKey, tokensBySymbol] of Object.entries(
192
+ totalEarningsByChain
193
+ )) {
194
+ const positionChainId = Number(chainIdKey);
195
+ for (const [symbol, earnings] of Object.entries(tokensBySymbol ?? {})) {
196
+ const amount = Number(earnings);
197
+ tokens.push({
198
+ chain: CHAIN_ID_TO_NAME[positionChainId] ?? "BASE",
199
+ chainId: positionChainId,
200
+ asset: symbol,
201
+ amount: String(amount)
202
+ });
203
+ }
204
+ }
207
205
  return {
208
206
  smartWallet,
209
- lifetimeEarnings: sumTokenEarnings(raw.data.totalEarningsByToken)
207
+ lifetimeEarnings: sumTokenEarnings(raw.data.totalEarningsByToken),
208
+ tokens
210
209
  };
211
210
  }
212
211
  function mapApyHistory(raw) {
@@ -215,7 +214,7 @@ function mapApyHistory(raw) {
215
214
  weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0
216
215
  };
217
216
  }
218
- function mapHistory(raw) {
217
+ function mapHistory(raw, chainId) {
219
218
  const actionMap = {
220
219
  "Deposit to protocol": "Deposit",
221
220
  "Top up wallet": "Top up",
@@ -224,7 +223,7 @@ function mapHistory(raw) {
224
223
  };
225
224
  return {
226
225
  total: raw.total,
227
- data: raw.data.map((entry) => {
226
+ data: raw.data.filter((d) => d.chainId === chainId).map((entry) => {
228
227
  const rawLog = entry.rebalanceLog;
229
228
  const positions = entry.positions ?? [];
230
229
  let rebalanceLog = [];
@@ -257,7 +256,9 @@ function mapHistory(raw) {
257
256
  txHashes: entry.transactionHash ? [entry.transactionHash] : [],
258
257
  chainId: entry.chainId,
259
258
  tokenSymbol: entry?.positions?.[0]?.token_symbol,
260
- amount: String(hexToDecimal(entry?.positions?.[0]?.amount || "0x"))
259
+ amount: String(
260
+ hexToDecimal(entry?.positions?.[0]?.amount || "0x")
261
+ )
261
262
  }
262
263
  ],
263
264
  rebalanceLog
@@ -483,7 +484,7 @@ var ZyfaiAgent = class {
483
484
  async getHistory(state, chainId, options) {
484
485
  const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
485
486
  const raw = await this.sdk.getHistory(smartWallet, validChainId, options);
486
- return mapHistory(raw);
487
+ return mapHistory(raw, validChainId);
487
488
  }
488
489
  // --- IAgent: Agent control ---
489
490
  async pause(state, chainId) {
@@ -792,7 +793,7 @@ var OwneySDK = class {
792
793
  }
793
794
  /**
794
795
  * Deposit funds into a specific agent, or split equally across all agents if agentId is omitted.
795
- * Validates that the asset is supported and amount meets minimums for the target agent(s).
796
+ * Validates that the asset is supported and amount meets minimums for the target agent(s).
796
797
  * @param options - Deposit parameters
797
798
  * @param options.amount - Amount to deposit in smallest unit (e.g. "100000000" for 100 USDC)
798
799
  * @param options.asset - Asset symbol to deposit (e.g. "USDC")
@@ -851,7 +852,10 @@ var OwneySDK = class {
851
852
  if (valid.length === agents.length) {
852
853
  return splits.map((s) => ({ agent: s.agent, amount: String(s.amount) }));
853
854
  }
854
- return this.splitDepositAmount(totalAmount, valid.map((s) => s.agent));
855
+ return this.splitDepositAmount(
856
+ totalAmount,
857
+ valid.map((s) => s.agent)
858
+ );
855
859
  }
856
860
  validateMinDepositAmount(agent, amount) {
857
861
  const minDepositAmount = BigInt(agent.minDepositAmount);
package/dist/index.d.cts CHANGED
@@ -130,6 +130,7 @@ interface OwneyBalances {
130
130
  interface AgentEarnings {
131
131
  smartWallet: `0x${string}`;
132
132
  lifetimeEarnings: number;
133
+ tokens: OwneyToken[];
133
134
  }
134
135
  interface OwneyEarnings {
135
136
  totalEarnings: string;
@@ -249,7 +250,7 @@ declare class OwneySDK {
249
250
  activateAgent(chainId: number, agentId?: AgentId[]): Promise<void>;
250
251
  /**
251
252
  * Deposit funds into a specific agent, or split equally across all agents if agentId is omitted.
252
- * Validates that the asset is supported and amount meets minimums for the target agent(s).
253
+ * Validates that the asset is supported and amount meets minimums for the target agent(s).
253
254
  * @param options - Deposit parameters
254
255
  * @param options.amount - Amount to deposit in smallest unit (e.g. "100000000" for 100 USDC)
255
256
  * @param options.asset - Asset symbol to deposit (e.g. "USDC")
package/dist/index.d.ts CHANGED
@@ -130,6 +130,7 @@ interface OwneyBalances {
130
130
  interface AgentEarnings {
131
131
  smartWallet: `0x${string}`;
132
132
  lifetimeEarnings: number;
133
+ tokens: OwneyToken[];
133
134
  }
134
135
  interface OwneyEarnings {
135
136
  totalEarnings: string;
@@ -249,7 +250,7 @@ declare class OwneySDK {
249
250
  activateAgent(chainId: number, agentId?: AgentId[]): Promise<void>;
250
251
  /**
251
252
  * Deposit funds into a specific agent, or split equally across all agents if agentId is omitted.
252
- * Validates that the asset is supported and amount meets minimums for the target agent(s).
253
+ * Validates that the asset is supported and amount meets minimums for the target agent(s).
253
254
  * @param options - Deposit parameters
254
255
  * @param options.amount - Amount to deposit in smallest unit (e.g. "100000000" for 100 USDC)
255
256
  * @param options.asset - Asset symbol to deposit (e.g. "USDC")
package/dist/index.js CHANGED
@@ -98,15 +98,6 @@ function hexToDecimal(hex, decimals = 6) {
98
98
  const remainder = parsed % divisor;
99
99
  return Number(whole) + Number(remainder) / Number(divisor);
100
100
  }
101
- function normalizeDecimalPlace(value, decimals = 6) {
102
- const parsed = BigInt(value);
103
- if (decimals <= 0)
104
- return Number(parsed);
105
- const divisor = BigInt(10 ** decimals);
106
- const whole = parsed / divisor;
107
- const remainder = parsed % divisor;
108
- return Number(whole) + Number(remainder) / Number(divisor);
109
- }
110
101
  var DayFilterMapping = {
111
102
  "30D": 30,
112
103
  "14D": 14,
@@ -132,34 +123,25 @@ var CHAIN_ID_TO_NAME = {
132
123
  8453: "BASE",
133
124
  42161: "ARBITRUM"
134
125
  };
135
- var CHAIN_NAME_TO_ID = {
136
- BASE: 8453,
137
- ARBITRUM: 42161
138
- };
139
- function mapBalances(raw, chainId, smartWallet) {
126
+ function mapBalances(raw, _chainId, smartWallet) {
140
127
  const portfolio = raw.portfolio;
141
- const tokenMap = /* @__PURE__ */ new Map();
142
- for (const p of portfolio.positions || []) {
143
- const symbol = p.token_symbol || "USDC";
144
- const positionChainId = (p.chain ? CHAIN_NAME_TO_ID[p.chain.toUpperCase()] : void 0) ?? chainId;
145
- const key = `${symbol}:${positionChainId}`;
146
- const current = tokenMap.get(key);
147
- tokenMap.set(key, {
148
- symbol,
149
- chainId: positionChainId,
150
- amount: (current?.amount ?? 0) + normalizeDecimalPlace(p.underlyingAmount || "0")
151
- });
152
- }
128
+ const portfolioByChain = portfolio.portfolioByChain ?? {};
153
129
  let totalBalance = 0;
154
130
  const tokens = [];
155
- for (const [, entry] of tokenMap) {
156
- totalBalance += entry.amount;
157
- tokens.push({
158
- chain: CHAIN_ID_TO_NAME[entry.chainId] ?? "BASE",
159
- chainId: entry.chainId,
160
- asset: entry.symbol,
161
- amount: String(entry.amount)
162
- });
131
+ for (const [chainIdKey, tokensBySymbol] of Object.entries(portfolioByChain)) {
132
+ const positionChainId = Number(chainIdKey);
133
+ for (const [symbol, { balance, decimals }] of Object.entries(
134
+ tokensBySymbol ?? {}
135
+ )) {
136
+ const amount = hexToDecimal(balance || "0", decimals);
137
+ totalBalance += amount;
138
+ tokens.push({
139
+ chain: CHAIN_ID_TO_NAME[positionChainId] ?? "BASE",
140
+ chainId: positionChainId,
141
+ asset: symbol,
142
+ amount: String(amount)
143
+ });
144
+ }
163
145
  }
164
146
  return {
165
147
  smartWallet,
@@ -174,9 +156,26 @@ function sumTokenEarnings(tokens) {
174
156
  return Object.values(tokens).reduce((sum, val) => sum + Number(val), 0);
175
157
  }
176
158
  function mapEarnings(raw, smartWallet) {
159
+ const totalEarningsByChain = raw.data.totalEarningsByChain;
160
+ const tokens = [];
161
+ for (const [chainIdKey, tokensBySymbol] of Object.entries(
162
+ totalEarningsByChain
163
+ )) {
164
+ const positionChainId = Number(chainIdKey);
165
+ for (const [symbol, earnings] of Object.entries(tokensBySymbol ?? {})) {
166
+ const amount = Number(earnings);
167
+ tokens.push({
168
+ chain: CHAIN_ID_TO_NAME[positionChainId] ?? "BASE",
169
+ chainId: positionChainId,
170
+ asset: symbol,
171
+ amount: String(amount)
172
+ });
173
+ }
174
+ }
177
175
  return {
178
176
  smartWallet,
179
- lifetimeEarnings: sumTokenEarnings(raw.data.totalEarningsByToken)
177
+ lifetimeEarnings: sumTokenEarnings(raw.data.totalEarningsByToken),
178
+ tokens
180
179
  };
181
180
  }
182
181
  function mapApyHistory(raw) {
@@ -185,7 +184,7 @@ function mapApyHistory(raw) {
185
184
  weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0
186
185
  };
187
186
  }
188
- function mapHistory(raw) {
187
+ function mapHistory(raw, chainId) {
189
188
  const actionMap = {
190
189
  "Deposit to protocol": "Deposit",
191
190
  "Top up wallet": "Top up",
@@ -194,7 +193,7 @@ function mapHistory(raw) {
194
193
  };
195
194
  return {
196
195
  total: raw.total,
197
- data: raw.data.map((entry) => {
196
+ data: raw.data.filter((d) => d.chainId === chainId).map((entry) => {
198
197
  const rawLog = entry.rebalanceLog;
199
198
  const positions = entry.positions ?? [];
200
199
  let rebalanceLog = [];
@@ -227,7 +226,9 @@ function mapHistory(raw) {
227
226
  txHashes: entry.transactionHash ? [entry.transactionHash] : [],
228
227
  chainId: entry.chainId,
229
228
  tokenSymbol: entry?.positions?.[0]?.token_symbol,
230
- amount: String(hexToDecimal(entry?.positions?.[0]?.amount || "0x"))
229
+ amount: String(
230
+ hexToDecimal(entry?.positions?.[0]?.amount || "0x")
231
+ )
231
232
  }
232
233
  ],
233
234
  rebalanceLog
@@ -453,7 +454,7 @@ var ZyfaiAgent = class {
453
454
  async getHistory(state, chainId, options) {
454
455
  const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
455
456
  const raw = await this.sdk.getHistory(smartWallet, validChainId, options);
456
- return mapHistory(raw);
457
+ return mapHistory(raw, validChainId);
457
458
  }
458
459
  // --- IAgent: Agent control ---
459
460
  async pause(state, chainId) {
@@ -762,7 +763,7 @@ var OwneySDK = class {
762
763
  }
763
764
  /**
764
765
  * Deposit funds into a specific agent, or split equally across all agents if agentId is omitted.
765
- * Validates that the asset is supported and amount meets minimums for the target agent(s).
766
+ * Validates that the asset is supported and amount meets minimums for the target agent(s).
766
767
  * @param options - Deposit parameters
767
768
  * @param options.amount - Amount to deposit in smallest unit (e.g. "100000000" for 100 USDC)
768
769
  * @param options.asset - Asset symbol to deposit (e.g. "USDC")
@@ -821,7 +822,10 @@ var OwneySDK = class {
821
822
  if (valid.length === agents.length) {
822
823
  return splits.map((s) => ({ agent: s.agent, amount: String(s.amount) }));
823
824
  }
824
- return this.splitDepositAmount(totalAmount, valid.map((s) => s.agent));
825
+ return this.splitDepositAmount(
826
+ totalAmount,
827
+ valid.map((s) => s.agent)
828
+ );
825
829
  }
826
830
  validateMinDepositAmount(agent, amount) {
827
831
  const minDepositAmount = BigInt(agent.minDepositAmount);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "dev": "tsup --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@zyfai/sdk": "^0.2.30",
23
+ "@zyfai/sdk": "^0.2.32",
24
24
  "viem": "^2.48.1"
25
25
  },
26
26
  "devDependencies": {