@defisaver/positions-sdk 0.0.163 → 0.0.164

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.
@@ -167,26 +167,30 @@ const getEulerV2SupplyRate = (borrowRate, totalBorrows, totalAssets, _interestFe
167
167
  return new decimal_js_1.default(borrowRate).mul(utilizationRate).mul(fee).toString();
168
168
  };
169
169
  exports.getEulerV2SupplyRate = getEulerV2SupplyRate;
170
+ const getLiquidityChanges = (action, amount, isBorrowOperation) => {
171
+ let liquidityAdded;
172
+ let liquidityRemoved;
173
+ if (isBorrowOperation) {
174
+ liquidityAdded = action === 'payback' ? amount : '0';
175
+ liquidityRemoved = action === 'borrow' ? amount : '0';
176
+ }
177
+ else {
178
+ liquidityAdded = action === 'collateral' ? amount : '0';
179
+ liquidityRemoved = action === 'withdraw' ? amount : '0';
180
+ }
181
+ return { liquidityAdded, liquidityRemoved };
182
+ };
170
183
  const getApyAfterValuesEstimationEulerV2 = (actions, web3, network) => __awaiter(void 0, void 0, void 0, function* () {
171
184
  const eulerV2ViewContract = (0, contracts_1.EulerV2ViewContract)(web3, network);
172
185
  const multicallData = [];
173
186
  const apyAfterValuesEstimationParams = [];
174
187
  actions.forEach(({ action, amount, asset, vaultAddress, }) => {
175
- const isBorrowOperation = constants_1.borrowOperations.includes(action);
176
188
  const amountInWei = (0, tokens_1.assetAmountInWei)(amount, asset);
177
- let liquidityAdded;
178
- let liquidityRemoved;
179
- if (isBorrowOperation) {
180
- liquidityAdded = action === 'payback' ? amountInWei : '0';
181
- liquidityRemoved = action === 'borrow' ? amountInWei : '0';
182
- }
183
- else {
184
- liquidityAdded = action === 'collateral' ? amountInWei : '0';
185
- liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
186
- }
189
+ const isBorrowOperation = constants_1.borrowOperations.includes(action);
190
+ const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amountInWei, isBorrowOperation);
187
191
  apyAfterValuesEstimationParams.push([
188
192
  vaultAddress,
189
- isBorrowOperation,
193
+ constants_1.borrowOperations.includes(action),
190
194
  liquidityAdded,
191
195
  liquidityRemoved,
192
196
  ]);
@@ -207,10 +211,17 @@ const getApyAfterValuesEstimationEulerV2 = (actions, web3, network) => __awaiter
207
211
  for (let i = 0; i < numOfActions; i += 1) {
208
212
  const _interestRate = multicallRes[numOfActions].estimatedBorrowRates[i];
209
213
  const vaultInfo = multicallRes[i][0];
214
+ const decimals = vaultInfo.decimals;
210
215
  const borrowRate = (0, exports.getEulerV2BorrowRate)(_interestRate);
216
+ const amount = (0, tokens_1.assetAmountInWei)(actions[i].amount, decimals);
217
+ const action = actions[i].action;
218
+ const isBorrowOperation = constants_1.borrowOperations.includes(action);
219
+ const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amount, isBorrowOperation);
220
+ const totalBorrows = new decimal_js_1.default(vaultInfo.totalBorrows).add(isBorrowOperation ? liquidityRemoved : '0').sub(isBorrowOperation ? liquidityAdded : '0').toString();
221
+ const totalAssets = new decimal_js_1.default(vaultInfo.totalAssets).add(isBorrowOperation ? '0' : liquidityAdded).sub(isBorrowOperation ? '0' : liquidityRemoved).toString();
211
222
  data[vaultInfo.vaultAddr.toLowerCase()] = {
212
223
  borrowRate,
213
- supplyRate: (0, exports.getEulerV2SupplyRate)(borrowRate, vaultInfo.totalBorrows, vaultInfo.totalAssets, vaultInfo.interestFee),
224
+ supplyRate: (0, exports.getEulerV2SupplyRate)(borrowRate, totalBorrows, totalAssets, vaultInfo.interestFee),
214
225
  };
215
226
  }
216
227
  return data;
@@ -156,26 +156,30 @@ export const getEulerV2SupplyRate = (borrowRate, totalBorrows, totalAssets, _int
156
156
  const fee = new Dec(1).minus(interestFee);
157
157
  return new Dec(borrowRate).mul(utilizationRate).mul(fee).toString();
158
158
  };
159
+ const getLiquidityChanges = (action, amount, isBorrowOperation) => {
160
+ let liquidityAdded;
161
+ let liquidityRemoved;
162
+ if (isBorrowOperation) {
163
+ liquidityAdded = action === 'payback' ? amount : '0';
164
+ liquidityRemoved = action === 'borrow' ? amount : '0';
165
+ }
166
+ else {
167
+ liquidityAdded = action === 'collateral' ? amount : '0';
168
+ liquidityRemoved = action === 'withdraw' ? amount : '0';
169
+ }
170
+ return { liquidityAdded, liquidityRemoved };
171
+ };
159
172
  export const getApyAfterValuesEstimationEulerV2 = (actions, web3, network) => __awaiter(void 0, void 0, void 0, function* () {
160
173
  const eulerV2ViewContract = EulerV2ViewContract(web3, network);
161
174
  const multicallData = [];
162
175
  const apyAfterValuesEstimationParams = [];
163
176
  actions.forEach(({ action, amount, asset, vaultAddress, }) => {
164
- const isBorrowOperation = borrowOperations.includes(action);
165
177
  const amountInWei = assetAmountInWei(amount, asset);
166
- let liquidityAdded;
167
- let liquidityRemoved;
168
- if (isBorrowOperation) {
169
- liquidityAdded = action === 'payback' ? amountInWei : '0';
170
- liquidityRemoved = action === 'borrow' ? amountInWei : '0';
171
- }
172
- else {
173
- liquidityAdded = action === 'collateral' ? amountInWei : '0';
174
- liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
175
- }
178
+ const isBorrowOperation = borrowOperations.includes(action);
179
+ const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amountInWei, isBorrowOperation);
176
180
  apyAfterValuesEstimationParams.push([
177
181
  vaultAddress,
178
- isBorrowOperation,
182
+ borrowOperations.includes(action),
179
183
  liquidityAdded,
180
184
  liquidityRemoved,
181
185
  ]);
@@ -196,10 +200,17 @@ export const getApyAfterValuesEstimationEulerV2 = (actions, web3, network) => __
196
200
  for (let i = 0; i < numOfActions; i += 1) {
197
201
  const _interestRate = multicallRes[numOfActions].estimatedBorrowRates[i];
198
202
  const vaultInfo = multicallRes[i][0];
203
+ const decimals = vaultInfo.decimals;
199
204
  const borrowRate = getEulerV2BorrowRate(_interestRate);
205
+ const amount = assetAmountInWei(actions[i].amount, decimals);
206
+ const action = actions[i].action;
207
+ const isBorrowOperation = borrowOperations.includes(action);
208
+ const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amount, isBorrowOperation);
209
+ const totalBorrows = new Dec(vaultInfo.totalBorrows).add(isBorrowOperation ? liquidityRemoved : '0').sub(isBorrowOperation ? liquidityAdded : '0').toString();
210
+ const totalAssets = new Dec(vaultInfo.totalAssets).add(isBorrowOperation ? '0' : liquidityAdded).sub(isBorrowOperation ? '0' : liquidityRemoved).toString();
200
211
  data[vaultInfo.vaultAddr.toLowerCase()] = {
201
212
  borrowRate,
202
- supplyRate: getEulerV2SupplyRate(borrowRate, vaultInfo.totalBorrows, vaultInfo.totalAssets, vaultInfo.interestFee),
213
+ supplyRate: getEulerV2SupplyRate(borrowRate, totalBorrows, totalAssets, vaultInfo.interestFee),
203
214
  };
204
215
  }
205
216
  return data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "0.0.163",
3
+ "version": "0.0.164",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -163,6 +163,20 @@ export const getEulerV2SupplyRate = (borrowRate: string, totalBorrows: string, t
163
163
  const fee = new Dec(1).minus(interestFee);
164
164
  return new Dec(borrowRate).mul(utilizationRate).mul(fee).toString();
165
165
  };
166
+
167
+ const getLiquidityChanges = (action: string, amount: string, isBorrowOperation: boolean) => {
168
+ let liquidityAdded;
169
+ let liquidityRemoved;
170
+ if (isBorrowOperation) {
171
+ liquidityAdded = action === 'payback' ? amount : '0';
172
+ liquidityRemoved = action === 'borrow' ? amount : '0';
173
+ } else {
174
+ liquidityAdded = action === 'collateral' ? amount : '0';
175
+ liquidityRemoved = action === 'withdraw' ? amount : '0';
176
+ }
177
+ return { liquidityAdded, liquidityRemoved };
178
+ };
179
+
166
180
  export const getApyAfterValuesEstimationEulerV2 = async (actions: { action: string, amount: string, asset: string, vaultAddress: EthAddress }[], web3: Web3, network: NetworkNumber) => {
167
181
  const eulerV2ViewContract = EulerV2ViewContract(web3, network);
168
182
  const multicallData: any[] = [];
@@ -170,20 +184,12 @@ export const getApyAfterValuesEstimationEulerV2 = async (actions: { action: stri
170
184
  actions.forEach(({
171
185
  action, amount, asset, vaultAddress,
172
186
  }) => {
173
- const isBorrowOperation = borrowOperations.includes(action);
174
187
  const amountInWei = assetAmountInWei(amount, asset);
175
- let liquidityAdded;
176
- let liquidityRemoved;
177
- if (isBorrowOperation) {
178
- liquidityAdded = action === 'payback' ? amountInWei : '0';
179
- liquidityRemoved = action === 'borrow' ? amountInWei : '0';
180
- } else {
181
- liquidityAdded = action === 'collateral' ? amountInWei : '0';
182
- liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
183
- }
188
+ const isBorrowOperation = borrowOperations.includes(action);
189
+ const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amountInWei, isBorrowOperation);
184
190
  apyAfterValuesEstimationParams.push([
185
191
  vaultAddress,
186
- isBorrowOperation,
192
+ borrowOperations.includes(action),
187
193
  liquidityAdded,
188
194
  liquidityRemoved,
189
195
  ]);
@@ -204,10 +210,19 @@ export const getApyAfterValuesEstimationEulerV2 = async (actions: { action: stri
204
210
  for (let i = 0; i < numOfActions; i += 1) {
205
211
  const _interestRate = multicallRes[numOfActions].estimatedBorrowRates[i];
206
212
  const vaultInfo = multicallRes[i][0];
213
+ const decimals = vaultInfo.decimals;
207
214
  const borrowRate = getEulerV2BorrowRate(_interestRate);
215
+
216
+ const amount = assetAmountInWei(actions[i].amount, decimals);
217
+ const action = actions[i].action;
218
+ const isBorrowOperation = borrowOperations.includes(action);
219
+ const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amount, isBorrowOperation);
220
+
221
+ const totalBorrows = new Dec(vaultInfo.totalBorrows).add(isBorrowOperation ? liquidityRemoved : '0').sub(isBorrowOperation ? liquidityAdded : '0').toString();
222
+ const totalAssets = new Dec(vaultInfo.totalAssets).add(isBorrowOperation ? '0' : liquidityAdded).sub(isBorrowOperation ? '0' : liquidityRemoved).toString();
208
223
  data[vaultInfo.vaultAddr.toLowerCase()] = {
209
224
  borrowRate,
210
- supplyRate: getEulerV2SupplyRate(borrowRate, vaultInfo.totalBorrows, vaultInfo.totalAssets, vaultInfo.interestFee),
225
+ supplyRate: getEulerV2SupplyRate(borrowRate, totalBorrows, totalAssets, vaultInfo.interestFee),
211
226
  };
212
227
  }
213
228
  return data;