@defisaver/positions-sdk 1.0.26-dev-portfolio-4 → 1.0.26-dev-portfolio-6

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.
@@ -24,3 +24,4 @@ export declare const getApyAfterValuesEstimationEulerV2: (actions: {
24
24
  asset: string;
25
25
  vaultAddress: EthAddress;
26
26
  }[], provider: EthereumProvider, network: NetworkNumber) => Promise<any>;
27
+ export declare const getEulerV2SubAccounts: (address: EthAddress) => EthAddress[];
@@ -23,7 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getApyAfterValuesEstimationEulerV2 = exports.getEulerV2SupplyRate = exports.getUtilizationRate = exports.getEulerV2BorrowRate = exports.getEulerV2AggregatedData = exports.calculateNetApy = exports.isLeveragedPos = void 0;
26
+ exports.getEulerV2SubAccounts = exports.getApyAfterValuesEstimationEulerV2 = exports.getEulerV2SupplyRate = exports.getUtilizationRate = exports.getEulerV2BorrowRate = exports.getEulerV2AggregatedData = exports.calculateNetApy = exports.isLeveragedPos = void 0;
27
27
  const decimal_js_1 = __importDefault(require("decimal.js"));
28
28
  const tokens_1 = require("@defisaver/tokens");
29
29
  const moneymarket_1 = require("../../moneymarket");
@@ -228,3 +228,23 @@ const getApyAfterValuesEstimationEulerV2 = (actions, provider, network) => __awa
228
228
  return data;
229
229
  });
230
230
  exports.getApyAfterValuesEstimationEulerV2 = getApyAfterValuesEstimationEulerV2;
231
+ const xorLastByte = (address, xorValue) => {
232
+ // Extract the last byte (2 hex characters)
233
+ const lastByte = address.slice(-2);
234
+ // XOR the last byte with the given xorValue
235
+ // eslint-disable-next-line no-bitwise
236
+ const xorResult = [...lastByte].map((char, i) => (parseInt(char, 16) ^ parseInt(xorValue[i], 16)).toString(16)).join('');
237
+ // Return the full address with the last byte XORed
238
+ return `0x${address.slice(0, -2)}${xorResult.padStart(2, '0')}`;
239
+ };
240
+ const getEulerV2SubAccounts = (address) => {
241
+ // Clean the address by removing "0x"
242
+ const cleanAddress = address.toLowerCase().replace(/^0x/, '');
243
+ // XOR the last byte with 0x01, 0x02, and 0x03
244
+ const xorWith01 = xorLastByte(cleanAddress, '01');
245
+ const xorWith02 = xorLastByte(cleanAddress, '02');
246
+ const xorWith03 = xorLastByte(cleanAddress, '03');
247
+ // Return an array with all three modified addresses
248
+ return [xorWith01, xorWith02, xorWith03];
249
+ };
250
+ exports.getEulerV2SubAccounts = getEulerV2SubAccounts;
@@ -105,7 +105,7 @@ const _getMakerCdpData = (provider, network, cdp) => __awaiter(void 0, void 0, v
105
105
  dogContract.read.chop([cdp.ilk]),
106
106
  ]);
107
107
  const collInfo = (0, makerHelpers_1.parseCollateralInfo)(cdp.ilk, par.toString(), mat.toString(), artGlobal.toString(), rate.toString(), spot.toString(), line.toString(), duty.toString(), futureRate.toString(), chop.toString());
108
- const collateral = (0, tokens_1.assetAmountInEth)(ink.toString(), cdp.asset);
108
+ const collateral = (0, tokens_1.assetAmountInEth)(ink.toString(), `MCD-${cdp.asset}`);
109
109
  const collateralUsd = new decimal_js_1.default(collateral).mul(collInfo.assetPrice).toString();
110
110
  const debt = new decimal_js_1.default(art).times(collInfo.currentRate).div(1e27).floor()
111
111
  .toString();
@@ -33,6 +33,7 @@ const viem_1 = require("../services/viem");
33
33
  const liquity_1 = require("../liquity");
34
34
  const liquityV2_1 = require("../liquityV2");
35
35
  const fluid_1 = require("../fluid");
36
+ const eulerHelpers_1 = require("../helpers/eulerHelpers");
36
37
  function getPortfolioData(provider, network, defaultProvider, addresses) {
37
38
  return __awaiter(this, void 0, void 0, function* () {
38
39
  const isMainnet = network === common_1.NetworkNumber.Eth;
@@ -136,12 +137,19 @@ function getPortfolioData(provider, network, defaultProvider, addresses) {
136
137
  if (new decimal_js_1.default(accData.suppliedUsd).gt(0))
137
138
  positions[address.toLowerCase()].spark[market.value] = accData;
138
139
  }))).flat(),
139
- ...eulerV2Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
140
- const accData = yield (0, eulerV2_1._getEulerV2AccountData)(client, network, address, address, Object.assign({ selectedMarket: market }, eulerV2MarketsData[market.value]));
141
- if (new decimal_js_1.default(accData.suppliedUsd).gt(0) || new decimal_js_1.default(accData.borrowedUsd).gt(0)) {
142
- positions[address.toLowerCase()].eulerV2[market.value] = accData;
143
- }
144
- }))).flat(),
140
+ ...eulerV2Markets.map((market) => addresses.map((address) => {
141
+ const eulerV2SubAccounts = (0, eulerHelpers_1.getEulerV2SubAccounts)(address);
142
+ const eulerV2Addresses = [address, ...eulerV2SubAccounts];
143
+ return Promise.all(eulerV2Addresses.map((eulerAddress) => __awaiter(this, void 0, void 0, function* () {
144
+ const accData = yield (0, eulerV2_1._getEulerV2AccountData)(client, network, eulerAddress, eulerAddress, Object.assign({ selectedMarket: market }, eulerV2MarketsData[market.value]));
145
+ if (new decimal_js_1.default(accData.suppliedUsd).gt(0) || new decimal_js_1.default(accData.borrowedUsd).gt(0)) {
146
+ if (!positions[address.toLowerCase()].eulerV2[market.value]) {
147
+ positions[address.toLowerCase()].eulerV2[market.value] = {};
148
+ }
149
+ positions[address.toLowerCase()].eulerV2[market.value][eulerAddress.toLowerCase()] = accData;
150
+ }
151
+ })));
152
+ }).flat()).flat(),
145
153
  ...addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
146
154
  var _a;
147
155
  return (_a = makerCdps[address.toLowerCase()]) === null || _a === void 0 ? void 0 : _a.map((cdpInfo) => __awaiter(this, void 0, void 0, function* () {
@@ -24,7 +24,7 @@ export interface PortfolioPositionsDataForAddress {
24
24
  [key in SparkVersions]?: SparkPositionData;
25
25
  };
26
26
  eulerV2: {
27
- [key in EulerV2Versions]?: EulerV2PositionData;
27
+ [key in EulerV2Versions]?: Record<EthAddress, EulerV2PositionData>;
28
28
  };
29
29
  maker: {
30
30
  [key: string]: CdpData;
@@ -24,3 +24,4 @@ export declare const getApyAfterValuesEstimationEulerV2: (actions: {
24
24
  asset: string;
25
25
  vaultAddress: EthAddress;
26
26
  }[], provider: EthereumProvider, network: NetworkNumber) => Promise<any>;
27
+ export declare const getEulerV2SubAccounts: (address: EthAddress) => EthAddress[];
@@ -215,3 +215,22 @@ export const getApyAfterValuesEstimationEulerV2 = (actions, provider, network) =
215
215
  }
216
216
  return data;
217
217
  });
218
+ const xorLastByte = (address, xorValue) => {
219
+ // Extract the last byte (2 hex characters)
220
+ const lastByte = address.slice(-2);
221
+ // XOR the last byte with the given xorValue
222
+ // eslint-disable-next-line no-bitwise
223
+ const xorResult = [...lastByte].map((char, i) => (parseInt(char, 16) ^ parseInt(xorValue[i], 16)).toString(16)).join('');
224
+ // Return the full address with the last byte XORed
225
+ return `0x${address.slice(0, -2)}${xorResult.padStart(2, '0')}`;
226
+ };
227
+ export const getEulerV2SubAccounts = (address) => {
228
+ // Clean the address by removing "0x"
229
+ const cleanAddress = address.toLowerCase().replace(/^0x/, '');
230
+ // XOR the last byte with 0x01, 0x02, and 0x03
231
+ const xorWith01 = xorLastByte(cleanAddress, '01');
232
+ const xorWith02 = xorLastByte(cleanAddress, '02');
233
+ const xorWith03 = xorLastByte(cleanAddress, '03');
234
+ // Return an array with all three modified addresses
235
+ return [xorWith01, xorWith02, xorWith03];
236
+ };
@@ -95,7 +95,7 @@ export const _getMakerCdpData = (provider, network, cdp) => __awaiter(void 0, vo
95
95
  dogContract.read.chop([cdp.ilk]),
96
96
  ]);
97
97
  const collInfo = parseCollateralInfo(cdp.ilk, par.toString(), mat.toString(), artGlobal.toString(), rate.toString(), spot.toString(), line.toString(), duty.toString(), futureRate.toString(), chop.toString());
98
- const collateral = assetAmountInEth(ink.toString(), cdp.asset);
98
+ const collateral = assetAmountInEth(ink.toString(), `MCD-${cdp.asset}`);
99
99
  const collateralUsd = new Dec(collateral).mul(collInfo.assetPrice).toString();
100
100
  const debt = new Dec(art).times(collInfo.currentRate).div(1e27).floor()
101
101
  .toString();
@@ -26,6 +26,7 @@ import { getViemProvider } from '../services/viem';
26
26
  import { _getLiquityTroveInfo } from '../liquity';
27
27
  import { _getLiquityV2MarketData, _getLiquityV2TroveData, _getLiquityV2UserTroveIds } from '../liquityV2';
28
28
  import { _getUserPositions } from '../fluid';
29
+ import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
29
30
  export function getPortfolioData(provider, network, defaultProvider, addresses) {
30
31
  return __awaiter(this, void 0, void 0, function* () {
31
32
  const isMainnet = network === NetworkNumber.Eth;
@@ -129,12 +130,19 @@ export function getPortfolioData(provider, network, defaultProvider, addresses)
129
130
  if (new Dec(accData.suppliedUsd).gt(0))
130
131
  positions[address.toLowerCase()].spark[market.value] = accData;
131
132
  }))).flat(),
132
- ...eulerV2Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
133
- const accData = yield _getEulerV2AccountData(client, network, address, address, Object.assign({ selectedMarket: market }, eulerV2MarketsData[market.value]));
134
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
135
- positions[address.toLowerCase()].eulerV2[market.value] = accData;
136
- }
137
- }))).flat(),
133
+ ...eulerV2Markets.map((market) => addresses.map((address) => {
134
+ const eulerV2SubAccounts = getEulerV2SubAccounts(address);
135
+ const eulerV2Addresses = [address, ...eulerV2SubAccounts];
136
+ return Promise.all(eulerV2Addresses.map((eulerAddress) => __awaiter(this, void 0, void 0, function* () {
137
+ const accData = yield _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, Object.assign({ selectedMarket: market }, eulerV2MarketsData[market.value]));
138
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
139
+ if (!positions[address.toLowerCase()].eulerV2[market.value]) {
140
+ positions[address.toLowerCase()].eulerV2[market.value] = {};
141
+ }
142
+ positions[address.toLowerCase()].eulerV2[market.value][eulerAddress.toLowerCase()] = accData;
143
+ }
144
+ })));
145
+ }).flat()).flat(),
138
146
  ...addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
139
147
  var _a;
140
148
  return (_a = makerCdps[address.toLowerCase()]) === null || _a === void 0 ? void 0 : _a.map((cdpInfo) => __awaiter(this, void 0, void 0, function* () {
@@ -24,7 +24,7 @@ export interface PortfolioPositionsDataForAddress {
24
24
  [key in SparkVersions]?: SparkPositionData;
25
25
  };
26
26
  eulerV2: {
27
- [key in EulerV2Versions]?: EulerV2PositionData;
27
+ [key in EulerV2Versions]?: Record<EthAddress, EulerV2PositionData>;
28
28
  };
29
29
  maker: {
30
30
  [key: string]: CdpData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "1.0.26-dev-portfolio-4",
3
+ "version": "1.0.26-dev-portfolio-6",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -12,7 +12,7 @@
12
12
  "dev": "tsc -p tsconfig.json --watch",
13
13
  "lint": "eslint src/ --fix",
14
14
  "lint-check": "eslint src/",
15
- "test": "mocha tests/maker.ts",
15
+ "test": "mocha tests/portfolio.ts",
16
16
  "test-single": "mocha ./tests/$npm_config_name.ts",
17
17
  "test:debugger": "mocha --inspect-brk tests/*",
18
18
  "build-test": "npm run build && npm run test",
@@ -230,4 +230,31 @@ export const getApyAfterValuesEstimationEulerV2 = async (actions: { action: stri
230
230
  };
231
231
  }
232
232
  return data;
233
+ };
234
+
235
+ const xorLastByte = (address: string, xorValue: string): EthAddress => {
236
+ // Extract the last byte (2 hex characters)
237
+ const lastByte = address.slice(-2);
238
+
239
+ // XOR the last byte with the given xorValue
240
+
241
+ // eslint-disable-next-line no-bitwise
242
+ const xorResult = [...lastByte].map((char, i) => (parseInt(char, 16) ^ parseInt(xorValue[i], 16)).toString(16),
243
+ ).join('');
244
+
245
+ // Return the full address with the last byte XORed
246
+ return `0x${address.slice(0, -2)}${xorResult.padStart(2, '0')}`;
247
+ };
248
+
249
+ export const getEulerV2SubAccounts = (address: EthAddress): EthAddress[] => {
250
+ // Clean the address by removing "0x"
251
+ const cleanAddress = address.toLowerCase().replace(/^0x/, '');
252
+
253
+ // XOR the last byte with 0x01, 0x02, and 0x03
254
+ const xorWith01 = xorLastByte(cleanAddress, '01');
255
+ const xorWith02 = xorLastByte(cleanAddress, '02');
256
+ const xorWith03 = xorLastByte(cleanAddress, '03');
257
+
258
+ // Return an array with all three modified addresses
259
+ return [xorWith01, xorWith02, xorWith03];
233
260
  };
@@ -167,7 +167,7 @@ export const _getMakerCdpData = async (provider: Client, network: NetworkNumber,
167
167
  chop.toString(),
168
168
  );
169
169
 
170
- const collateral = assetAmountInEth(ink.toString(), cdp.asset);
170
+ const collateral = assetAmountInEth(ink.toString(), `MCD-${cdp.asset}`);
171
171
 
172
172
  const collateralUsd = new Dec(collateral).mul(collInfo.assetPrice).toString();
173
173
  const debt = new Dec(art).times(collInfo.currentRate).div(1e27).floor()
@@ -37,6 +37,7 @@ import { getViemProvider } from '../services/viem';
37
37
  import { _getLiquityTroveInfo } from '../liquity';
38
38
  import { _getLiquityV2MarketData, _getLiquityV2TroveData, _getLiquityV2UserTroveIds } from '../liquityV2';
39
39
  import { _getUserPositions } from '../fluid';
40
+ import { getEulerV2SubAccounts } from '../helpers/eulerHelpers';
40
41
 
41
42
  export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[]): Promise<PortfolioPositionsData> {
42
43
  const isMainnet = network === NetworkNumber.Eth;
@@ -141,12 +142,19 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
141
142
  const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: sparkMarketsData[market.value].assetsData });
142
143
  if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].spark[market.value] = accData;
143
144
  })).flat(),
144
- ...eulerV2Markets.map((market) => addresses.map(async (address) => {
145
- const accData = await _getEulerV2AccountData(client, network, address, address, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
146
- if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
147
- positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = accData;
148
- }
149
- })).flat(),
145
+ ...eulerV2Markets.map((market) => addresses.map((address) => {
146
+ const eulerV2SubAccounts = getEulerV2SubAccounts(address);
147
+ const eulerV2Addresses = [address, ...eulerV2SubAccounts];
148
+ return Promise.all(eulerV2Addresses.map(async (eulerAddress) => {
149
+ const accData = await _getEulerV2AccountData(client, network, eulerAddress, eulerAddress, { selectedMarket: market, ...eulerV2MarketsData[market.value] });
150
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
151
+ if (!positions[address.toLowerCase() as EthAddress].eulerV2[market.value]) {
152
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value] = {};
153
+ }
154
+ positions[address.toLowerCase() as EthAddress].eulerV2[market.value]![eulerAddress.toLowerCase() as EthAddress] = accData;
155
+ }
156
+ }));
157
+ }).flat()).flat(),
150
158
  ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
151
159
  const cdpData = await _getMakerCdpData(client, network, cdpInfo);
152
160
  if (cdpData) {
@@ -25,7 +25,7 @@ export interface PortfolioPositionsDataForAddress {
25
25
  [key in SparkVersions]?: SparkPositionData;
26
26
  }
27
27
  eulerV2: {
28
- [key in EulerV2Versions]?: EulerV2PositionData;
28
+ [key in EulerV2Versions]?: Record<EthAddress, EulerV2PositionData>;
29
29
  };
30
30
  maker: {
31
31
  [key: string]: CdpData;