@defisaver/positions-sdk 0.0.59 → 0.0.61-dev

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.
Files changed (105) hide show
  1. package/README.md +63 -63
  2. package/cjs/aaveV3/index.js +2 -1
  3. package/cjs/config/contracts.d.ts +20 -189
  4. package/cjs/config/contracts.js +16 -26
  5. package/cjs/llamaLend/index.js +28 -32
  6. package/cjs/markets/llamaLend/contractAddresses.d.ts +9 -0
  7. package/cjs/markets/llamaLend/contractAddresses.js +88 -0
  8. package/cjs/markets/llamaLend/index.d.ts +12 -10
  9. package/cjs/markets/llamaLend/index.js +41 -57
  10. package/cjs/types/contracts/generated/{LlamaLendCRVCrvUSDController.d.ts → LlamaLendControllerAbi.d.ts} +3 -3
  11. package/cjs/types/contracts/generated/LlamaLendView.d.ts +5 -0
  12. package/cjs/types/contracts/generated/index.d.ts +1 -4
  13. package/cjs/types/llamaLend.d.ts +35 -9
  14. package/cjs/types/llamaLend.js +26 -8
  15. package/esm/aaveV3/index.js +2 -1
  16. package/esm/config/contracts.d.ts +20 -189
  17. package/esm/config/contracts.js +16 -26
  18. package/esm/llamaLend/index.js +31 -35
  19. package/esm/markets/llamaLend/contractAddresses.d.ts +9 -0
  20. package/esm/markets/llamaLend/contractAddresses.js +84 -0
  21. package/esm/markets/llamaLend/index.d.ts +12 -10
  22. package/esm/markets/llamaLend/index.js +31 -55
  23. package/esm/types/contracts/generated/{LlamaLendCrvUSDCRVController.d.ts → LlamaLendControllerAbi.d.ts} +3 -3
  24. package/esm/types/contracts/generated/LlamaLendView.d.ts +5 -0
  25. package/esm/types/contracts/generated/index.d.ts +1 -4
  26. package/esm/types/llamaLend.d.ts +35 -9
  27. package/esm/types/llamaLend.js +25 -7
  28. package/package.json +40 -40
  29. package/src/aaveV2/index.ts +227 -227
  30. package/src/aaveV3/index.ts +562 -561
  31. package/src/assets/index.ts +60 -60
  32. package/src/chickenBonds/index.ts +123 -123
  33. package/src/compoundV2/index.ts +219 -219
  34. package/src/compoundV3/index.ts +273 -273
  35. package/src/config/contracts.js +841 -851
  36. package/src/constants/index.ts +5 -5
  37. package/src/contracts.ts +128 -127
  38. package/src/curveUsd/index.ts +229 -229
  39. package/src/exchange/index.ts +17 -17
  40. package/src/helpers/aaveHelpers/index.ts +134 -134
  41. package/src/helpers/chickenBondsHelpers/index.ts +23 -23
  42. package/src/helpers/compoundHelpers/index.ts +181 -181
  43. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  44. package/src/helpers/index.ts +7 -7
  45. package/src/helpers/llamaLendHelpers/index.ts +45 -45
  46. package/src/helpers/makerHelpers/index.ts +94 -94
  47. package/src/helpers/morphoBlueHelpers/index.ts +56 -56
  48. package/src/helpers/sparkHelpers/index.ts +106 -106
  49. package/src/index.ts +46 -46
  50. package/src/liquity/index.ts +116 -116
  51. package/src/llamaLend/index.ts +268 -279
  52. package/src/maker/index.ts +117 -117
  53. package/src/markets/aave/index.ts +80 -80
  54. package/src/markets/aave/marketAssets.ts +24 -24
  55. package/src/markets/compound/index.ts +141 -141
  56. package/src/markets/compound/marketsAssets.ts +48 -48
  57. package/src/markets/curveUsd/index.ts +69 -69
  58. package/src/markets/index.ts +5 -5
  59. package/src/markets/llamaLend/contractAddresses.ts +93 -0
  60. package/src/markets/llamaLend/index.ts +150 -65
  61. package/src/markets/morphoBlue/index.ts +262 -262
  62. package/src/markets/spark/index.ts +29 -29
  63. package/src/markets/spark/marketAssets.ts +10 -10
  64. package/src/moneymarket/moneymarketCommonService.ts +75 -75
  65. package/src/morphoAaveV2/index.ts +256 -256
  66. package/src/morphoAaveV3/index.ts +619 -619
  67. package/src/morphoBlue/index.ts +177 -177
  68. package/src/multicall/index.ts +22 -22
  69. package/src/services/dsrService.ts +15 -15
  70. package/src/services/priceService.ts +21 -21
  71. package/src/services/utils.ts +51 -51
  72. package/src/setup.ts +8 -8
  73. package/src/spark/index.ts +422 -422
  74. package/src/staking/staking.ts +174 -174
  75. package/src/types/aave.ts +256 -256
  76. package/src/types/chickenBonds.ts +45 -45
  77. package/src/types/common.ts +84 -84
  78. package/src/types/compound.ts +128 -128
  79. package/src/types/contracts/generated/{LlamaLendCRVCrvUSDController.ts → LlamaLendControllerAbi.ts} +3 -3
  80. package/src/types/contracts/generated/LlamaLendView.ts +9 -0
  81. package/src/types/contracts/generated/index.ts +1 -4
  82. package/src/types/curveUsd.ts +118 -118
  83. package/src/types/index.ts +8 -8
  84. package/src/types/liquity.ts +30 -30
  85. package/src/types/llamaLend.ts +143 -119
  86. package/src/types/maker.ts +50 -50
  87. package/src/types/morphoBlue.ts +107 -107
  88. package/src/types/spark.ts +106 -106
  89. package/cjs/types/contracts/generated/LlamaLendCrvUSDCRVController.d.ts +0 -209
  90. package/cjs/types/contracts/generated/LlamaLendCrvUSDCRVController.js +0 -5
  91. package/cjs/types/contracts/generated/LlamaLendTBTCCrvUSDController.d.ts +0 -209
  92. package/cjs/types/contracts/generated/LlamaLendTBTCCrvUSDController.js +0 -5
  93. package/cjs/types/contracts/generated/LlamaLendWstETHCrvUSDController.d.ts +0 -209
  94. package/cjs/types/contracts/generated/LlamaLendWstETHCrvUSDController.js +0 -5
  95. package/esm/types/contracts/generated/LlamaLendCRVCrvUSDController.d.ts +0 -209
  96. package/esm/types/contracts/generated/LlamaLendCrvUSDCRVController.js +0 -4
  97. package/esm/types/contracts/generated/LlamaLendTBTCCrvUSDController.d.ts +0 -209
  98. package/esm/types/contracts/generated/LlamaLendTBTCCrvUSDController.js +0 -4
  99. package/esm/types/contracts/generated/LlamaLendWstETHCrvUSDController.d.ts +0 -209
  100. package/esm/types/contracts/generated/LlamaLendWstETHCrvUSDController.js +0 -4
  101. package/src/types/contracts/generated/LlamaLendCrvUSDCRVController.ts +0 -416
  102. package/src/types/contracts/generated/LlamaLendTBTCCrvUSDController.ts +0 -416
  103. package/src/types/contracts/generated/LlamaLendWstETHCrvUSDController.ts +0 -416
  104. /package/cjs/types/contracts/generated/{LlamaLendCRVCrvUSDController.js → LlamaLendControllerAbi.js} +0 -0
  105. /package/esm/types/contracts/generated/{LlamaLendCRVCrvUSDController.js → LlamaLendControllerAbi.js} +0 -0
@@ -1,175 +1,175 @@
1
- import Dec from 'decimal.js';
2
- import Web3 from 'web3';
3
- import {
4
- CbEthContract, LidoContract, PotContract, REthContract, wstETHContract,
5
- } from '../contracts';
6
- import { MMAssetsData, MMUsedAssets, NetworkNumber } from '../types/common';
7
- import { ContractEventLog } from '../types/contracts/generated/types';
8
- import { BLOCKS_IN_A_YEAR, SECONDS_PER_YEAR, AVG_BLOCK_TIME } from '../constants';
9
- import { multicall } from '../multicall';
10
-
11
- export const getStETHApr = async (web3: Web3, fromBlock = 17900000, blockNumber: 'latest' | number = 'latest') => {
12
- try {
13
- const tokenRebasedEvents: ContractEventLog<{ [key: string]: any }>[] = await LidoContract(web3, NetworkNumber.Eth).getPastEvents('TokenRebased', { fromBlock, toBlock: blockNumber });
14
- tokenRebasedEvents.sort((a, b) => b.blockNumber - a.blockNumber); // sort from highest to lowest block number
15
- const movingAverage = 7;
16
- const aprs = tokenRebasedEvents.slice(0, movingAverage).map(({ returnValues: event }) => {
17
- const preShareRate = new Dec(event.preTotalEther.toString()).div(event.preTotalShares.toString());
18
- const postShareRate = new Dec(event.postTotalEther.toString()).div(event.postTotalShares.toString());
19
- return new Dec(SECONDS_PER_YEAR).mul(new Dec(postShareRate).sub(preShareRate).div(preShareRate))
20
- .div(event.timeElapsed.toString()).mul(100)
21
- .toNumber();
22
- });
23
- return aprs.reduce((a, b) => a + b, 0) / aprs.length;
24
- } catch (e) {
25
- console.warn('Failed to fetch stETH APY from events, falling back to Lido API');
26
- const res = await fetch('https://eth-api.lido.fi/v1/protocol/steth/apr/sma');
27
- const data = await res.json();
28
- return data.data.smaApr;
29
- }
30
- };
31
-
32
-
33
- export const getCbETHApr = async (web3: Web3, blockNumber: 'latest' | number = 'latest') => {
34
- let currentBlock = blockNumber;
35
- if (blockNumber === 'latest') currentBlock = await web3.eth.getBlockNumber();
36
- const blockDiff = 6 * 24 * 60 * 60 / AVG_BLOCK_TIME;
37
- const pastBlock = (currentBlock as number) - blockDiff;
38
- const contract = CbEthContract(web3, NetworkNumber.Eth);
39
- const [pastRate, currentRate] = await Promise.all([
40
- contract.methods.exchangeRate().call({}, pastBlock),
41
- contract.methods.exchangeRate().call({}, currentBlock),
42
- ]);
43
- const apr = new Dec(currentRate.toString()).sub(pastRate.toString()).div(currentRate.toString())
44
- .mul(BLOCKS_IN_A_YEAR / blockDiff)
45
- .mul(100)
46
- .toString();
47
- return apr;
48
- };
49
-
50
-
51
- export const getREthApr = async (web3: Web3, blockNumber: 'latest' | number = 'latest') => {
52
- let currentBlock = blockNumber;
53
- if (blockNumber === 'latest') currentBlock = await web3.eth.getBlockNumber();
54
- const blockDiff = 8 * 24 * 60 * 60 / AVG_BLOCK_TIME;
55
- const pastBlock = (currentBlock as number) - blockDiff;
56
- const contract = REthContract(web3, NetworkNumber.Eth);
57
- const [pastRate, currentRate] = await Promise.all([
58
- contract.methods.getExchangeRate().call({}, pastBlock),
59
- contract.methods.getExchangeRate().call({}, currentBlock),
60
- ]);
61
- const apr = new Dec(currentRate.toString()).sub(pastRate.toString()).div(currentRate.toString())
62
- .mul(BLOCKS_IN_A_YEAR / blockDiff)
63
- .mul(100)
64
- .toString();
65
-
66
- return apr;
67
- };
68
-
69
- export const getDsrApy = async (web3: Web3, blockNumber: 'latest' | number = 'latest') => {
70
- const potContract = PotContract(web3, NetworkNumber.Eth);
71
- return new Dec(await potContract.methods.dsr().call())
72
- .div(new Dec(1e27))
73
- .pow(SECONDS_PER_YEAR)
74
- .sub(1)
75
- .mul(100)
76
- .toString();
77
- };
78
-
79
- export const getSUSDeApy = async () => {
80
- const res = await fetch('https://app.defisaver.com/api/staking/apy?asset=sUSDe');
81
- const data = await res.json();
82
- return data.apy;
83
- };
84
-
85
- export const getStakingApy = (asset: string, web3: Web3, blockNumber: 'latest' | number = 'latest', fromBlock: number | undefined = undefined) => {
86
- if (asset === 'stETH' || asset === 'wstETH') return getStETHApr(web3, fromBlock, blockNumber);
87
- if (asset === 'cbETH') return getCbETHApr(web3, blockNumber);
88
- if (asset === 'rETH') return getREthApr(web3, blockNumber);
89
- if (asset === 'sDAI') return getDsrApy(web3);
90
- if (asset === 'sUSDe') return getSUSDeApy();
91
- };
92
-
93
- export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
94
- let interval = 1;
95
-
96
- if (+interest === 0) return 0;
97
-
98
- if (type === 'month') interval = 1 / 12;
99
- if (type === 'week') interval = 1 / 52.1429;
100
-
101
- if (apy) {
102
- // interest rate already compounded
103
- return (+principal * (1 + (+interest / 100 * interval))) - +principal;
104
- }
105
-
106
- return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
107
- };
108
-
109
- export const calculateNetApy = (usedAssets: MMUsedAssets, assetsData: MMAssetsData, isMorpho = false) => {
110
- const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
111
- const acc = { ..._acc };
112
- const assetData = assetsData[usedAsset.symbol];
113
-
114
- if (usedAsset.isSupplied) {
115
- const amount = usedAsset.suppliedUsd;
116
- acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
117
- const rate = isMorpho
118
- ? usedAsset.supplyRate === '0' ? assetData.supplyRateP2P : usedAsset.supplyRate
119
- : assetData.supplyRate;
120
- const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
121
- acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
122
- if (assetData.incentiveSupplyApy) {
123
- // take COMP/AAVE yield into account
124
- const incentiveInterest = calculateInterestEarned(amount, assetData.incentiveSupplyApy, 'year', true);
125
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
126
- }
127
- }
128
-
129
- if (usedAsset.isBorrowed) {
130
- const amount = usedAsset.borrowedUsd;
131
- acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
132
- const rate = isMorpho
133
- ? usedAsset.borrowRate === '0' ? assetData.borrowRateP2P : usedAsset.borrowRate
134
- : usedAsset.symbol === 'GHO'
135
- ? usedAsset.discountedBorrowRate
136
- : (usedAsset?.interestMode === '1' ? usedAsset.stableBorrowRate : assetData.borrowRate);
137
- const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
138
- acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
139
- if (assetData.incentiveBorrowApy) {
140
- // take COMP/AAVE yield into account
141
- const incentiveInterest = calculateInterestEarned(amount, assetData.incentiveBorrowApy, 'year', true);
142
- acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
143
- }
144
- }
145
-
146
- return acc;
147
- }, {
148
- borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
149
- });
150
-
151
- const {
152
- borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
153
- } = sumValues;
154
-
155
- const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
156
- const balance = new Dec(suppliedUsd).sub(borrowedUsd);
157
- const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
158
-
159
- return { netApy, totalInterestUsd, incentiveUsd };
160
- };
161
-
162
- export const getWstETHByStETH = async (stETHAmount: string | number, web3: Web3) => wstETHContract(web3, NetworkNumber.Eth).methods.getWstETHByStETH(stETHAmount).call();
163
-
164
- export const getStETHByWstETH = async (wstETHAmount: string | number, web3: Web3) => wstETHContract(web3, NetworkNumber.Eth).methods.getStETHByWstETH(wstETHAmount).call();
165
-
166
- export const getStETHByWstETHMultiple = async (wstEthAmounts: string[] | number[], web3: Web3) => {
167
- const contract = wstETHContract(web3, NetworkNumber.Eth);
168
- const calls = wstEthAmounts.map((amount) => ({
169
- target: contract.options.address,
170
- abiItem: contract.options.jsonInterface.find((i) => i.name === 'getStETHByWstETH'),
171
- params: [amount],
172
- }));
173
- const stEthAmounts = await multicall(calls, web3);
174
- return stEthAmounts.map((arr) => arr[0]);
1
+ import Dec from 'decimal.js';
2
+ import Web3 from 'web3';
3
+ import {
4
+ CbEthContract, LidoContract, PotContract, REthContract, wstETHContract,
5
+ } from '../contracts';
6
+ import { MMAssetsData, MMUsedAssets, NetworkNumber } from '../types/common';
7
+ import { ContractEventLog } from '../types/contracts/generated/types';
8
+ import { BLOCKS_IN_A_YEAR, SECONDS_PER_YEAR, AVG_BLOCK_TIME } from '../constants';
9
+ import { multicall } from '../multicall';
10
+
11
+ export const getStETHApr = async (web3: Web3, fromBlock = 17900000, blockNumber: 'latest' | number = 'latest') => {
12
+ try {
13
+ const tokenRebasedEvents: ContractEventLog<{ [key: string]: any }>[] = await LidoContract(web3, NetworkNumber.Eth).getPastEvents('TokenRebased', { fromBlock, toBlock: blockNumber });
14
+ tokenRebasedEvents.sort((a, b) => b.blockNumber - a.blockNumber); // sort from highest to lowest block number
15
+ const movingAverage = 7;
16
+ const aprs = tokenRebasedEvents.slice(0, movingAverage).map(({ returnValues: event }) => {
17
+ const preShareRate = new Dec(event.preTotalEther.toString()).div(event.preTotalShares.toString());
18
+ const postShareRate = new Dec(event.postTotalEther.toString()).div(event.postTotalShares.toString());
19
+ return new Dec(SECONDS_PER_YEAR).mul(new Dec(postShareRate).sub(preShareRate).div(preShareRate))
20
+ .div(event.timeElapsed.toString()).mul(100)
21
+ .toNumber();
22
+ });
23
+ return aprs.reduce((a, b) => a + b, 0) / aprs.length;
24
+ } catch (e) {
25
+ console.warn('Failed to fetch stETH APY from events, falling back to Lido API');
26
+ const res = await fetch('https://eth-api.lido.fi/v1/protocol/steth/apr/sma');
27
+ const data = await res.json();
28
+ return data.data.smaApr;
29
+ }
30
+ };
31
+
32
+
33
+ export const getCbETHApr = async (web3: Web3, blockNumber: 'latest' | number = 'latest') => {
34
+ let currentBlock = blockNumber;
35
+ if (blockNumber === 'latest') currentBlock = await web3.eth.getBlockNumber();
36
+ const blockDiff = 6 * 24 * 60 * 60 / AVG_BLOCK_TIME;
37
+ const pastBlock = (currentBlock as number) - blockDiff;
38
+ const contract = CbEthContract(web3, NetworkNumber.Eth);
39
+ const [pastRate, currentRate] = await Promise.all([
40
+ contract.methods.exchangeRate().call({}, pastBlock),
41
+ contract.methods.exchangeRate().call({}, currentBlock),
42
+ ]);
43
+ const apr = new Dec(currentRate.toString()).sub(pastRate.toString()).div(currentRate.toString())
44
+ .mul(BLOCKS_IN_A_YEAR / blockDiff)
45
+ .mul(100)
46
+ .toString();
47
+ return apr;
48
+ };
49
+
50
+
51
+ export const getREthApr = async (web3: Web3, blockNumber: 'latest' | number = 'latest') => {
52
+ let currentBlock = blockNumber;
53
+ if (blockNumber === 'latest') currentBlock = await web3.eth.getBlockNumber();
54
+ const blockDiff = 8 * 24 * 60 * 60 / AVG_BLOCK_TIME;
55
+ const pastBlock = (currentBlock as number) - blockDiff;
56
+ const contract = REthContract(web3, NetworkNumber.Eth);
57
+ const [pastRate, currentRate] = await Promise.all([
58
+ contract.methods.getExchangeRate().call({}, pastBlock),
59
+ contract.methods.getExchangeRate().call({}, currentBlock),
60
+ ]);
61
+ const apr = new Dec(currentRate.toString()).sub(pastRate.toString()).div(currentRate.toString())
62
+ .mul(BLOCKS_IN_A_YEAR / blockDiff)
63
+ .mul(100)
64
+ .toString();
65
+
66
+ return apr;
67
+ };
68
+
69
+ export const getDsrApy = async (web3: Web3, blockNumber: 'latest' | number = 'latest') => {
70
+ const potContract = PotContract(web3, NetworkNumber.Eth);
71
+ return new Dec(await potContract.methods.dsr().call())
72
+ .div(new Dec(1e27))
73
+ .pow(SECONDS_PER_YEAR)
74
+ .sub(1)
75
+ .mul(100)
76
+ .toString();
77
+ };
78
+
79
+ export const getSUSDeApy = async () => {
80
+ const res = await fetch('https://app.defisaver.com/api/staking/apy?asset=sUSDe');
81
+ const data = await res.json();
82
+ return data.apy;
83
+ };
84
+
85
+ export const getStakingApy = (asset: string, web3: Web3, blockNumber: 'latest' | number = 'latest', fromBlock: number | undefined = undefined) => {
86
+ if (asset === 'stETH' || asset === 'wstETH') return getStETHApr(web3, fromBlock, blockNumber);
87
+ if (asset === 'cbETH') return getCbETHApr(web3, blockNumber);
88
+ if (asset === 'rETH') return getREthApr(web3, blockNumber);
89
+ if (asset === 'sDAI') return getDsrApy(web3);
90
+ if (asset === 'sUSDe') return getSUSDeApy();
91
+ };
92
+
93
+ export const calculateInterestEarned = (principal: string, interest: string, type: string, apy = false) => {
94
+ let interval = 1;
95
+
96
+ if (+interest === 0) return 0;
97
+
98
+ if (type === 'month') interval = 1 / 12;
99
+ if (type === 'week') interval = 1 / 52.1429;
100
+
101
+ if (apy) {
102
+ // interest rate already compounded
103
+ return (+principal * (1 + (+interest / 100 * interval))) - +principal;
104
+ }
105
+
106
+ return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
107
+ };
108
+
109
+ export const calculateNetApy = (usedAssets: MMUsedAssets, assetsData: MMAssetsData, isMorpho = false) => {
110
+ const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
111
+ const acc = { ..._acc };
112
+ const assetData = assetsData[usedAsset.symbol];
113
+
114
+ if (usedAsset.isSupplied) {
115
+ const amount = usedAsset.suppliedUsd;
116
+ acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
117
+ const rate = isMorpho
118
+ ? usedAsset.supplyRate === '0' ? assetData.supplyRateP2P : usedAsset.supplyRate
119
+ : assetData.supplyRate;
120
+ const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
121
+ acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
122
+ if (assetData.incentiveSupplyApy) {
123
+ // take COMP/AAVE yield into account
124
+ const incentiveInterest = calculateInterestEarned(amount, assetData.incentiveSupplyApy, 'year', true);
125
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
126
+ }
127
+ }
128
+
129
+ if (usedAsset.isBorrowed) {
130
+ const amount = usedAsset.borrowedUsd;
131
+ acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
132
+ const rate = isMorpho
133
+ ? usedAsset.borrowRate === '0' ? assetData.borrowRateP2P : usedAsset.borrowRate
134
+ : usedAsset.symbol === 'GHO'
135
+ ? usedAsset.discountedBorrowRate
136
+ : (usedAsset?.interestMode === '1' ? usedAsset.stableBorrowRate : assetData.borrowRate);
137
+ const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
138
+ acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
139
+ if (assetData.incentiveBorrowApy) {
140
+ // take COMP/AAVE yield into account
141
+ const incentiveInterest = calculateInterestEarned(amount, assetData.incentiveBorrowApy, 'year', true);
142
+ acc.incentiveUsd = new Dec(acc.incentiveUsd).add(incentiveInterest).toString();
143
+ }
144
+ }
145
+
146
+ return acc;
147
+ }, {
148
+ borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
149
+ });
150
+
151
+ const {
152
+ borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
153
+ } = sumValues;
154
+
155
+ const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
156
+ const balance = new Dec(suppliedUsd).sub(borrowedUsd);
157
+ const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
158
+
159
+ return { netApy, totalInterestUsd, incentiveUsd };
160
+ };
161
+
162
+ export const getWstETHByStETH = async (stETHAmount: string | number, web3: Web3) => wstETHContract(web3, NetworkNumber.Eth).methods.getWstETHByStETH(stETHAmount).call();
163
+
164
+ export const getStETHByWstETH = async (wstETHAmount: string | number, web3: Web3) => wstETHContract(web3, NetworkNumber.Eth).methods.getStETHByWstETH(wstETHAmount).call();
165
+
166
+ export const getStETHByWstETHMultiple = async (wstEthAmounts: string[] | number[], web3: Web3) => {
167
+ const contract = wstETHContract(web3, NetworkNumber.Eth);
168
+ const calls = wstEthAmounts.map((amount) => ({
169
+ target: contract.options.address,
170
+ abiItem: contract.options.jsonInterface.find((i) => i.name === 'getStETHByWstETH'),
171
+ params: [amount],
172
+ }));
173
+ const stEthAmounts = await multicall(calls, web3);
174
+ return stEthAmounts.map((arr) => arr[0]);
175
175
  };