@owney/sdk 0.6.3 → 0.6.4

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
@@ -264,30 +264,22 @@ function mapWeightedApyByChain(raw) {
264
264
  }
265
265
  return Object.keys(out).length > 0 ? out : void 0;
266
266
  }
267
- function weightedFinalApy(entry) {
268
- const finalByToken = entry.final_weighted_apy ?? {};
269
- const balanceByToken = {};
270
- for (const p of entry.positions ?? []) {
271
- const token = p.tokenSymbol ?? "";
272
- balanceByToken[token] = (balanceByToken[token] ?? 0) + (p.balance ?? 0);
273
- }
267
+ function rawPoolApyForChain(entry, chainId) {
274
268
  let weightedSum = 0;
275
269
  let totalBalance = 0;
276
- for (const [token, apy] of Object.entries(finalByToken)) {
277
- const balance = balanceByToken[token] ?? 0;
278
- weightedSum += apy * balance;
270
+ for (const p of entry.positions ?? []) {
271
+ if (p.chainId !== chainId)
272
+ continue;
273
+ const balance = p.balance ?? 0;
274
+ if (balance <= 0 || typeof p.apy !== "number")
275
+ continue;
276
+ weightedSum += p.apy * balance;
279
277
  totalBalance += balance;
280
278
  }
281
- if (totalBalance > 0)
282
- return weightedSum / totalBalance;
283
- const values = Object.values(finalByToken);
284
- return values.length ? values.reduce((sum, v) => sum + v, 0) / values.length : 0;
279
+ return totalBalance > 0 ? weightedSum / totalBalance : null;
285
280
  }
286
- function mapApyHistory(raw) {
287
- const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({
288
- date,
289
- apy: weightedFinalApy(entry)
290
- })).sort((a, b) => a.date.localeCompare(b.date));
281
+ function mapApyHistory(raw, chainId) {
282
+ const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({ date, apy: rawPoolApyForChain(entry, chainId) })).filter((p) => p.apy !== null).sort((a, b) => a.date.localeCompare(b.date));
291
283
  return {
292
284
  walletAddress: raw.walletAddress,
293
285
  weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0,
@@ -868,7 +860,7 @@ var ZyfaiAgent = class {
868
860
  async getAccountApy(state, chainId, days) {
869
861
  const { smartWallet } = await this.resolveSmartWallet(state, chainId);
870
862
  const raw = await this.sdk.getDailyApyHistory(smartWallet, days);
871
- return mapApyHistory(raw);
863
+ return mapApyHistory(raw, chainId);
872
864
  }
873
865
  async getHistory(state, chainId, options) {
874
866
  const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
package/dist/index.js CHANGED
@@ -232,30 +232,22 @@ function mapWeightedApyByChain(raw) {
232
232
  }
233
233
  return Object.keys(out).length > 0 ? out : void 0;
234
234
  }
235
- function weightedFinalApy(entry) {
236
- const finalByToken = entry.final_weighted_apy ?? {};
237
- const balanceByToken = {};
238
- for (const p of entry.positions ?? []) {
239
- const token = p.tokenSymbol ?? "";
240
- balanceByToken[token] = (balanceByToken[token] ?? 0) + (p.balance ?? 0);
241
- }
235
+ function rawPoolApyForChain(entry, chainId) {
242
236
  let weightedSum = 0;
243
237
  let totalBalance = 0;
244
- for (const [token, apy] of Object.entries(finalByToken)) {
245
- const balance = balanceByToken[token] ?? 0;
246
- weightedSum += apy * balance;
238
+ for (const p of entry.positions ?? []) {
239
+ if (p.chainId !== chainId)
240
+ continue;
241
+ const balance = p.balance ?? 0;
242
+ if (balance <= 0 || typeof p.apy !== "number")
243
+ continue;
244
+ weightedSum += p.apy * balance;
247
245
  totalBalance += balance;
248
246
  }
249
- if (totalBalance > 0)
250
- return weightedSum / totalBalance;
251
- const values = Object.values(finalByToken);
252
- return values.length ? values.reduce((sum, v) => sum + v, 0) / values.length : 0;
247
+ return totalBalance > 0 ? weightedSum / totalBalance : null;
253
248
  }
254
- function mapApyHistory(raw) {
255
- const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({
256
- date,
257
- apy: weightedFinalApy(entry)
258
- })).sort((a, b) => a.date.localeCompare(b.date));
249
+ function mapApyHistory(raw, chainId) {
250
+ const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({ date, apy: rawPoolApyForChain(entry, chainId) })).filter((p) => p.apy !== null).sort((a, b) => a.date.localeCompare(b.date));
259
251
  return {
260
252
  walletAddress: raw.walletAddress,
261
253
  weightedApyAfterFee: raw.weightedApyAfterFee ? sumTokenValues(raw.weightedApyAfterFee) : void 0,
@@ -836,7 +828,7 @@ var ZyfaiAgent = class {
836
828
  async getAccountApy(state, chainId, days) {
837
829
  const { smartWallet } = await this.resolveSmartWallet(state, chainId);
838
830
  const raw = await this.sdk.getDailyApyHistory(smartWallet, days);
839
- return mapApyHistory(raw);
831
+ return mapApyHistory(raw, chainId);
840
832
  }
841
833
  async getHistory(state, chainId, options) {
842
834
  const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",