@defisaver/positions-sdk 0.0.69 → 0.0.71

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 (82) hide show
  1. package/README.md +63 -63
  2. package/cjs/aaveV3/index.js +1 -1
  3. package/cjs/markets/aave/marketAssets.js +1 -1
  4. package/cjs/markets/llamaLend/index.js +1 -1
  5. package/cjs/markets/morphoBlue/index.d.ts +24 -2
  6. package/cjs/markets/morphoBlue/index.js +221 -40
  7. package/cjs/morphoBlue/index.js +2 -1
  8. package/cjs/staking/staking.js +11 -4
  9. package/cjs/types/llamaLend.js +2 -2
  10. package/cjs/types/morphoBlue.d.ts +18 -2
  11. package/cjs/types/morphoBlue.js +19 -2
  12. package/esm/aaveV3/index.js +1 -1
  13. package/esm/markets/aave/marketAssets.js +1 -1
  14. package/esm/markets/llamaLend/index.js +1 -1
  15. package/esm/markets/morphoBlue/index.d.ts +24 -2
  16. package/esm/markets/morphoBlue/index.js +209 -39
  17. package/esm/morphoBlue/index.js +2 -1
  18. package/esm/staking/staking.js +11 -4
  19. package/esm/types/llamaLend.js +2 -2
  20. package/esm/types/morphoBlue.d.ts +18 -2
  21. package/esm/types/morphoBlue.js +18 -1
  22. package/package.json +41 -41
  23. package/src/aaveV2/index.ts +227 -227
  24. package/src/aaveV3/index.ts +562 -562
  25. package/src/assets/index.ts +60 -60
  26. package/src/chickenBonds/index.ts +123 -123
  27. package/src/compoundV2/index.ts +219 -219
  28. package/src/compoundV3/index.ts +273 -273
  29. package/src/config/contracts.js +845 -845
  30. package/src/constants/index.ts +5 -5
  31. package/src/contracts.ts +128 -128
  32. package/src/curveUsd/index.ts +229 -229
  33. package/src/exchange/index.ts +17 -17
  34. package/src/helpers/aaveHelpers/index.ts +134 -134
  35. package/src/helpers/chickenBondsHelpers/index.ts +23 -23
  36. package/src/helpers/compoundHelpers/index.ts +181 -181
  37. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  38. package/src/helpers/index.ts +7 -7
  39. package/src/helpers/llamaLendHelpers/index.ts +45 -45
  40. package/src/helpers/makerHelpers/index.ts +94 -94
  41. package/src/helpers/morphoBlueHelpers/index.ts +56 -56
  42. package/src/helpers/sparkHelpers/index.ts +106 -106
  43. package/src/index.ts +46 -46
  44. package/src/liquity/index.ts +116 -116
  45. package/src/llamaLend/index.ts +268 -268
  46. package/src/maker/index.ts +117 -117
  47. package/src/markets/aave/index.ts +80 -80
  48. package/src/markets/aave/marketAssets.ts +25 -25
  49. package/src/markets/compound/index.ts +142 -142
  50. package/src/markets/compound/marketsAssets.ts +50 -50
  51. package/src/markets/curveUsd/index.ts +69 -69
  52. package/src/markets/index.ts +5 -5
  53. package/src/markets/llamaLend/contractAddresses.ts +95 -95
  54. package/src/markets/llamaLend/index.ts +150 -150
  55. package/src/markets/morphoBlue/index.ts +431 -262
  56. package/src/markets/spark/index.ts +29 -29
  57. package/src/markets/spark/marketAssets.ts +10 -10
  58. package/src/moneymarket/moneymarketCommonService.ts +75 -75
  59. package/src/morphoAaveV2/index.ts +256 -256
  60. package/src/morphoAaveV3/index.ts +619 -619
  61. package/src/morphoBlue/index.ts +178 -177
  62. package/src/multicall/index.ts +22 -22
  63. package/src/services/dsrService.ts +15 -15
  64. package/src/services/priceService.ts +21 -21
  65. package/src/services/utils.ts +51 -51
  66. package/src/setup.ts +8 -8
  67. package/src/spark/index.ts +422 -422
  68. package/src/staking/staking.ts +198 -198
  69. package/src/types/aave.ts +256 -256
  70. package/src/types/chickenBonds.ts +45 -45
  71. package/src/types/common.ts +84 -84
  72. package/src/types/compound.ts +128 -128
  73. package/src/types/curveUsd.ts +118 -118
  74. package/src/types/index.ts +8 -8
  75. package/src/types/liquity.ts +30 -30
  76. package/src/types/llamaLend.ts +143 -143
  77. package/src/types/maker.ts +50 -50
  78. package/src/types/morphoBlue.ts +125 -107
  79. package/src/types/spark.ts +106 -106
  80. package/.yarn/releases/yarn-1.22.1.cjs +0 -147386
  81. package/.yarn/releases/yarn-1.22.1.js +0 -147386
  82. package/.yarnrc.yml +0 -1
@@ -1,177 +1,178 @@
1
- import Web3 from 'web3';
2
- import Dec from 'decimal.js';
3
- import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
4
- import { MMUsedAssets, NetworkNumber } from '../types/common';
5
- import {
6
- MorphoBlueViewContract,
7
- getConfigContractAbi, getConfigContractAddress,
8
- } from '../contracts';
9
- import {
10
- MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo, MorphoBluePositionData,
11
- } from '../types';
12
- import { WAD, SECONDS_PER_YEAR, USD_QUOTE } from '../constants';
13
- import { getStakingApy, STAKING_ASSETS } from '../staking';
14
- import { getAbiItem, wethToEth } from '../services/utils';
15
- import { multicall } from '../multicall';
16
- import { getMorphoBlueAggregatedPositionData } from '../helpers/morphoBlueHelpers';
17
-
18
-
19
- const compound = (ratePerSeconds: string) => {
20
- const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
21
- const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
22
- return new Dec(apyNumber).mul(WAD).floor().toString();
23
- };
24
-
25
- const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
26
- if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
27
- return 0;
28
- }
29
- const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
30
- .toString();
31
- const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
32
- .toString();
33
- const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
34
- .toString();
35
- return compound(ratePerSecond);
36
- };
37
-
38
- const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
39
- if (totalBorrowShares === '0') {
40
- return 0;
41
- }
42
- return compound(borrowRate);
43
- };
44
-
45
-
46
- export async function getMorphoBlueMarketData(web3: Web3, network: NetworkNumber, selectedMarket: MorphoBlueMarketData, mainnetWeb3: Web3): Promise<MorphoBlueMarketInfo> {
47
- const {
48
- loanToken, collateralToken, oracle, irm, lltv,
49
- } = selectedMarket;
50
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
51
- const loanTokenInfo = getAssetInfoByAddress(loanToken);
52
- const collateralTokenInfo = getAssetInfoByAddress(collateralToken);
53
- let loanTokenFeedAddress = loanToken;
54
- if (loanTokenInfo.symbol === 'WETH') {
55
- const ethAddress = getAssetInfo('ETH').address;
56
- loanTokenFeedAddress = ethAddress;
57
- }
58
-
59
- const FeedRegistryAddress = getConfigContractAddress('FeedRegistry', network);
60
- const FeedRegistryAbi = getConfigContractAbi('FeedRegistry');
61
-
62
- const viewContractAddress = getConfigContractAddress('MorphoBlueView', network);
63
- const viewContractAbi = getConfigContractAbi('MorphoBlueView');
64
-
65
- const multicallCallsObject = [
66
- {
67
- target: FeedRegistryAddress,
68
- abiItem: getAbiItem(FeedRegistryAbi, 'latestAnswer'),
69
- params: [loanTokenFeedAddress, USD_QUOTE],
70
- },
71
- {
72
- target: viewContractAddress,
73
- abiItem: getAbiItem(viewContractAbi, 'getMarketInfoNotTuple'),
74
- params: [loanToken, collateralToken, oracle, irm, lltvInWei],
75
- },
76
- ];
77
-
78
- const multicallData = await multicall(multicallCallsObject, web3, network);
79
- const loanTokenPrice = multicallData[0][0];
80
- const marketInfo = multicallData[1][0];
81
-
82
- const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets, marketInfo.totalBorrowAssets, marketInfo.borrowRate, marketInfo.fee);
83
- const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate, marketInfo.totalBorrowShares);
84
- const utillization = new Dec(marketInfo.totalBorrowAssets).div(marketInfo.totalSupplyAssets).mul(100).toString();
85
-
86
- const oracleScaleFactor = new Dec(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
87
- const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
88
-
89
- const scale = new Dec(10).pow(loanTokenInfo.decimals).toString();
90
-
91
- const oracleRate = new Dec(marketInfo.oracle).div(oracleScale).toString();
92
- const assetsData: MorphoBlueAssetsData = {};
93
- assetsData[wethToEth(loanTokenInfo.symbol)] = {
94
- symbol: wethToEth(loanTokenInfo.symbol),
95
- address: loanToken,
96
- price: new Dec(loanTokenPrice).div(1e8).toString(),
97
- supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
98
- borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
99
- totalSupply: new Dec(marketInfo.totalSupplyAssets).div(scale).toString(),
100
- totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(scale).toString(),
101
- canBeSupplied: true,
102
- canBeBorrowed: true,
103
- };
104
-
105
- assetsData[wethToEth(collateralTokenInfo.symbol)] = {
106
- symbol: wethToEth(collateralTokenInfo.symbol),
107
- address: collateralToken,
108
- price: new Dec(loanTokenPrice).div(1e8).mul(oracleRate).toString(),
109
- supplyRate: '0',
110
- borrowRate: '0',
111
- canBeSupplied: true,
112
- canBeBorrowed: false,
113
- };
114
- if (STAKING_ASSETS.includes(collateralTokenInfo.symbol)) {
115
- assetsData[collateralTokenInfo.symbol].incentiveSupplyApy = await getStakingApy(collateralTokenInfo.symbol, mainnetWeb3);
116
- assetsData[collateralTokenInfo.symbol].incentiveSupplyToken = collateralTokenInfo.symbol;
117
- }
118
-
119
- return {
120
- id: marketInfo.id,
121
- fee: new Dec(marketInfo.fee).div(WAD).toString(),
122
- loanToken: wethToEth(loanTokenInfo.symbol),
123
- collateralToken: wethToEth(collateralTokenInfo.symbol),
124
- utillization,
125
- oracle: oracleRate,
126
- lltv: new Dec(lltv).toString(),
127
- minRatio: new Dec(1).div(lltv).mul(100).toString(),
128
- assetsData,
129
- };
130
- }
131
-
132
- export async function getMorphoBlueAccountData(web3: Web3, network: NetworkNumber, account: string, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
133
- const {
134
- loanToken, collateralToken, oracle, irm, lltv,
135
- } = selectedMarket;
136
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
137
- const marketObject = {
138
- loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
139
- };
140
- const viewContract = MorphoBlueViewContract(web3, network);
141
- const loanInfo = await viewContract.methods.getUserInfo(marketObject, account).call();
142
- const usedAssets: MMUsedAssets = {};
143
-
144
- const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
145
- const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets, marketInfo.loanToken);
146
- const loanTokenBorrowed = assetAmountInEth(loanInfo.borrowedInAssets, marketInfo.loanToken);
147
- usedAssets[marketInfo.loanToken] = {
148
- symbol: loanTokenInfo.symbol,
149
- supplied: loanTokenSupplied,
150
- borrowed: loanTokenBorrowed,
151
- isSupplied: new Dec(loanInfo.suppliedInAssets).gt(0),
152
- isBorrowed: new Dec(loanInfo.borrowedInAssets).gt(0),
153
- collateral: false,
154
- suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
155
- borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
156
- };
157
-
158
- const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
159
- const collateralTokenSupplied = assetAmountInEth(loanInfo.collateral, marketInfo.collateralToken);
160
- usedAssets[marketInfo.collateralToken] = {
161
- symbol: collateralTokenInfo.symbol,
162
- supplied: collateralTokenSupplied,
163
- borrowed: '0',
164
- isSupplied: new Dec(loanInfo.collateral).gt(0),
165
- isBorrowed: false,
166
- collateral: true,
167
- suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
168
- borrowedUsd: '0',
169
- };
170
-
171
- return {
172
- supplyShares: loanInfo.supplyShares,
173
- borrowShares: loanInfo.borrowShares,
174
- usedAssets,
175
- ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, marketInfo }),
176
- };
177
- }
1
+ import Web3 from 'web3';
2
+ import Dec from 'decimal.js';
3
+ import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
4
+ import { MMUsedAssets, NetworkNumber } from '../types/common';
5
+ import {
6
+ MorphoBlueViewContract,
7
+ getConfigContractAbi, getConfigContractAddress,
8
+ } from '../contracts';
9
+ import {
10
+ MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo, MorphoBluePositionData,
11
+ } from '../types';
12
+ import { WAD, SECONDS_PER_YEAR, USD_QUOTE } from '../constants';
13
+ import { getStakingApy, STAKING_ASSETS } from '../staking';
14
+ import { getAbiItem, wethToEth } from '../services/utils';
15
+ import { multicall } from '../multicall';
16
+ import { getMorphoBlueAggregatedPositionData } from '../helpers/morphoBlueHelpers';
17
+
18
+
19
+ const compound = (ratePerSeconds: string) => {
20
+ const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
21
+ const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
22
+ return new Dec(apyNumber).mul(WAD).floor().toString();
23
+ };
24
+
25
+ const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
26
+ if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
27
+ return 0;
28
+ }
29
+ const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
30
+ .toString();
31
+ const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
32
+ .toString();
33
+ const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
34
+ .toString();
35
+ return compound(ratePerSecond);
36
+ };
37
+
38
+ const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
39
+ if (totalBorrowShares === '0') {
40
+ return 0;
41
+ }
42
+ return compound(borrowRate);
43
+ };
44
+
45
+
46
+ export async function getMorphoBlueMarketData(web3: Web3, network: NetworkNumber, selectedMarket: MorphoBlueMarketData, mainnetWeb3: Web3): Promise<MorphoBlueMarketInfo> {
47
+ const {
48
+ loanToken, collateralToken, oracle, irm, lltv, oracleType,
49
+ } = selectedMarket;
50
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
51
+ const loanTokenInfo = getAssetInfoByAddress(loanToken);
52
+ const collateralTokenInfo = getAssetInfoByAddress(collateralToken);
53
+ let loanTokenFeedAddress = loanToken;
54
+ if (loanTokenInfo.symbol === 'WETH') {
55
+ const ethAddress = getAssetInfo('ETH').address;
56
+ loanTokenFeedAddress = ethAddress;
57
+ }
58
+
59
+ const FeedRegistryAddress = getConfigContractAddress('FeedRegistry', network);
60
+ const FeedRegistryAbi = getConfigContractAbi('FeedRegistry');
61
+
62
+ const viewContractAddress = getConfigContractAddress('MorphoBlueView', network);
63
+ const viewContractAbi = getConfigContractAbi('MorphoBlueView');
64
+
65
+ const multicallCallsObject = [
66
+ {
67
+ target: FeedRegistryAddress,
68
+ abiItem: getAbiItem(FeedRegistryAbi, 'latestAnswer'),
69
+ params: [loanTokenFeedAddress, USD_QUOTE],
70
+ },
71
+ {
72
+ target: viewContractAddress,
73
+ abiItem: getAbiItem(viewContractAbi, 'getMarketInfoNotTuple'),
74
+ params: [loanToken, collateralToken, oracle, irm, lltvInWei],
75
+ },
76
+ ];
77
+
78
+ const multicallData = await multicall(multicallCallsObject, web3, network);
79
+ const loanTokenPrice = multicallData[0][0];
80
+ const marketInfo = multicallData[1][0];
81
+
82
+ const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets, marketInfo.totalBorrowAssets, marketInfo.borrowRate, marketInfo.fee);
83
+ const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate, marketInfo.totalBorrowShares);
84
+ const utillization = new Dec(marketInfo.totalBorrowAssets).div(marketInfo.totalSupplyAssets).mul(100).toString();
85
+
86
+ const oracleScaleFactor = new Dec(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
87
+ const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
88
+
89
+ const scale = new Dec(10).pow(loanTokenInfo.decimals).toString();
90
+
91
+ const oracleRate = new Dec(marketInfo.oracle).div(oracleScale).toString();
92
+ const assetsData: MorphoBlueAssetsData = {};
93
+ assetsData[wethToEth(loanTokenInfo.symbol)] = {
94
+ symbol: wethToEth(loanTokenInfo.symbol),
95
+ address: loanToken,
96
+ price: new Dec(loanTokenPrice).div(1e8).toString(),
97
+ supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
98
+ borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
99
+ totalSupply: new Dec(marketInfo.totalSupplyAssets).div(scale).toString(),
100
+ totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(scale).toString(),
101
+ canBeSupplied: true,
102
+ canBeBorrowed: true,
103
+ };
104
+
105
+ assetsData[wethToEth(collateralTokenInfo.symbol)] = {
106
+ symbol: wethToEth(collateralTokenInfo.symbol),
107
+ address: collateralToken,
108
+ price: new Dec(loanTokenPrice).div(1e8).mul(oracleRate).toString(),
109
+ supplyRate: '0',
110
+ borrowRate: '0',
111
+ canBeSupplied: true,
112
+ canBeBorrowed: false,
113
+ };
114
+ if (STAKING_ASSETS.includes(collateralTokenInfo.symbol)) {
115
+ assetsData[collateralTokenInfo.symbol].incentiveSupplyApy = await getStakingApy(collateralTokenInfo.symbol, mainnetWeb3);
116
+ assetsData[collateralTokenInfo.symbol].incentiveSupplyToken = collateralTokenInfo.symbol;
117
+ }
118
+
119
+ return {
120
+ id: marketInfo.id,
121
+ fee: new Dec(marketInfo.fee).div(WAD).toString(),
122
+ loanToken: wethToEth(loanTokenInfo.symbol),
123
+ collateralToken: wethToEth(collateralTokenInfo.symbol),
124
+ utillization,
125
+ oracle: oracleRate,
126
+ oracleType,
127
+ lltv: new Dec(lltv).toString(),
128
+ minRatio: new Dec(1).div(lltv).mul(100).toString(),
129
+ assetsData,
130
+ };
131
+ }
132
+
133
+ export async function getMorphoBlueAccountData(web3: Web3, network: NetworkNumber, account: string, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
134
+ const {
135
+ loanToken, collateralToken, oracle, irm, lltv,
136
+ } = selectedMarket;
137
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
138
+ const marketObject = {
139
+ loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
140
+ };
141
+ const viewContract = MorphoBlueViewContract(web3, network);
142
+ const loanInfo = await viewContract.methods.getUserInfo(marketObject, account).call();
143
+ const usedAssets: MMUsedAssets = {};
144
+
145
+ const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
146
+ const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets, marketInfo.loanToken);
147
+ const loanTokenBorrowed = assetAmountInEth(loanInfo.borrowedInAssets, marketInfo.loanToken);
148
+ usedAssets[marketInfo.loanToken] = {
149
+ symbol: loanTokenInfo.symbol,
150
+ supplied: loanTokenSupplied,
151
+ borrowed: loanTokenBorrowed,
152
+ isSupplied: new Dec(loanInfo.suppliedInAssets).gt(0),
153
+ isBorrowed: new Dec(loanInfo.borrowedInAssets).gt(0),
154
+ collateral: false,
155
+ suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
156
+ borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
157
+ };
158
+
159
+ const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
160
+ const collateralTokenSupplied = assetAmountInEth(loanInfo.collateral, marketInfo.collateralToken);
161
+ usedAssets[marketInfo.collateralToken] = {
162
+ symbol: collateralTokenInfo.symbol,
163
+ supplied: collateralTokenSupplied,
164
+ borrowed: '0',
165
+ isSupplied: new Dec(loanInfo.collateral).gt(0),
166
+ isBorrowed: false,
167
+ collateral: true,
168
+ suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
169
+ borrowedUsd: '0',
170
+ };
171
+
172
+ return {
173
+ supplyShares: loanInfo.supplyShares,
174
+ borrowShares: loanInfo.borrowShares,
175
+ usedAssets,
176
+ ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, marketInfo }),
177
+ };
178
+ }
@@ -1,23 +1,23 @@
1
- import Web3 from 'web3';
2
- import { UniMulticallContract } from '../contracts';
3
- import { NetworkNumber } from '../types/common';
4
-
5
- export const multicall = async (calls: any[], web3: Web3, network: NetworkNumber = NetworkNumber.Eth, blockNumber: 'latest' | number = 'latest') => {
6
- const multicallContract = UniMulticallContract(web3, network);
7
- const formattedCalls = calls.map((call) => {
8
- const callData = web3.eth.abi.encodeFunctionCall(call.abiItem, call.params);
9
- return { callData, target: call.target || '0x0', gasLimit: call.gasLimit || 1e6 };
10
- });
11
- const callResult = await multicallContract.methods.multicall(formattedCalls.filter(item => item.target !== '0x0')).call({}, blockNumber);
12
-
13
- let formattedResult: any[] = [];
14
-
15
- callResult.returnData.forEach(([success, gasUsed, result], i) => {
16
- const formattedRes = result !== '0x'
17
- ? web3.eth.abi.decodeParameters(calls[i].abiItem.outputs, result)
18
- : undefined;
19
- formattedResult = [...formattedResult, formattedRes];
20
- });
21
-
22
- return formattedResult;
1
+ import Web3 from 'web3';
2
+ import { UniMulticallContract } from '../contracts';
3
+ import { NetworkNumber } from '../types/common';
4
+
5
+ export const multicall = async (calls: any[], web3: Web3, network: NetworkNumber = NetworkNumber.Eth, blockNumber: 'latest' | number = 'latest') => {
6
+ const multicallContract = UniMulticallContract(web3, network);
7
+ const formattedCalls = calls.map((call) => {
8
+ const callData = web3.eth.abi.encodeFunctionCall(call.abiItem, call.params);
9
+ return { callData, target: call.target || '0x0', gasLimit: call.gasLimit || 1e6 };
10
+ });
11
+ const callResult = await multicallContract.methods.multicall(formattedCalls.filter(item => item.target !== '0x0')).call({}, blockNumber);
12
+
13
+ let formattedResult: any[] = [];
14
+
15
+ callResult.returnData.forEach(([success, gasUsed, result], i) => {
16
+ const formattedRes = result !== '0x'
17
+ ? web3.eth.abi.decodeParameters(calls[i].abiItem.outputs, result)
18
+ : undefined;
19
+ formattedResult = [...formattedResult, formattedRes];
20
+ });
21
+
22
+ return formattedResult;
23
23
  };
@@ -1,16 +1,16 @@
1
- import Dec from 'decimal.js';
2
- import Web3 from 'web3';
3
- import { NetworkNumber } from '../types/common';
4
- import { SECONDS_PER_YEAR } from '../constants';
5
- import { PotContract } from '../contracts';
6
-
7
-
8
- export const getDsrApy = async (web3: Web3, network: NetworkNumber) => {
9
- const potContract = PotContract(web3, network);
10
- return new Dec(await potContract.methods.dsr().call())
11
- .div(new Dec(1e27))
12
- .pow(SECONDS_PER_YEAR)
13
- .sub(1)
14
- .mul(100)
15
- .toString();
1
+ import Dec from 'decimal.js';
2
+ import Web3 from 'web3';
3
+ import { NetworkNumber } from '../types/common';
4
+ import { SECONDS_PER_YEAR } from '../constants';
5
+ import { PotContract } from '../contracts';
6
+
7
+
8
+ export const getDsrApy = async (web3: Web3, network: NetworkNumber) => {
9
+ const potContract = PotContract(web3, network);
10
+ return new Dec(await potContract.methods.dsr().call())
11
+ .div(new Dec(1e27))
12
+ .pow(SECONDS_PER_YEAR)
13
+ .sub(1)
14
+ .mul(100)
15
+ .toString();
16
16
  };
@@ -1,22 +1,22 @@
1
- import Web3 from 'web3';
2
- import Dec from 'decimal.js';
3
- import { COMPPriceFeedContract, ETHPriceFeedContract, USDCPriceFeedContract } from '../contracts';
4
- import { NetworkNumber } from '../types/common';
5
-
6
- export const getEthPrice = async (web3: Web3) => {
7
- const contract = ETHPriceFeedContract(web3, NetworkNumber.Eth);
8
- const price = await contract.methods.latestAnswer().call();
9
- return new Dec(price).div(1e8).toString();
10
- };
11
-
12
- export const getUSDCPrice = async (web3: Web3) => {
13
- const contract = USDCPriceFeedContract(web3, NetworkNumber.Eth);
14
- const price = await contract.methods.latestAnswer().call();
15
- return new Dec(price).div(1e8).toString();
16
- };
17
-
18
- export const getCompPrice = async (web3: Web3) => {
19
- const contract = COMPPriceFeedContract(web3, NetworkNumber.Eth);
20
- const price = await contract.methods.latestAnswer().call();
21
- return new Dec(price).div(1e8).toString();
1
+ import Web3 from 'web3';
2
+ import Dec from 'decimal.js';
3
+ import { COMPPriceFeedContract, ETHPriceFeedContract, USDCPriceFeedContract } from '../contracts';
4
+ import { NetworkNumber } from '../types/common';
5
+
6
+ export const getEthPrice = async (web3: Web3) => {
7
+ const contract = ETHPriceFeedContract(web3, NetworkNumber.Eth);
8
+ const price = await contract.methods.latestAnswer().call();
9
+ return new Dec(price).div(1e8).toString();
10
+ };
11
+
12
+ export const getUSDCPrice = async (web3: Web3) => {
13
+ const contract = USDCPriceFeedContract(web3, NetworkNumber.Eth);
14
+ const price = await contract.methods.latestAnswer().call();
15
+ return new Dec(price).div(1e8).toString();
16
+ };
17
+
18
+ export const getCompPrice = async (web3: Web3) => {
19
+ const contract = COMPPriceFeedContract(web3, NetworkNumber.Eth);
20
+ const price = await contract.methods.latestAnswer().call();
21
+ return new Dec(price).div(1e8).toString();
22
22
  };
@@ -1,51 +1,51 @@
1
- import Dec from 'decimal.js';
2
- import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
3
- import { NetworkNumber } from '../types/common';
4
-
5
- export const isLayer2Network = (networkId: NetworkNumber) => [10, 42161, 8453].includes(+networkId);
6
-
7
- export const addToObjectIf = (condition: any, item: any) => (condition ? item : {});
8
-
9
- export const ethToWeth = (maybeEth: any) => maybeEth?.replace(/^ETH$/, 'WETH');
10
-
11
- export const wethToEth = (maybeWeth: any) => maybeWeth?.replace(/^WETH$/, 'ETH');
12
-
13
- export const stEthToWstEth = (maybeStEth: any) => maybeStEth?.replace(/^stETH$/, 'wstETH');
14
-
15
- export const wstEthToStEth = (maybeStEth: any) => maybeStEth?.replace(/^wstETH$/, 'stETH');
16
-
17
- export const getAbiItem = (abi: any, methodName: string) => abi.find((i: any) => i.name === methodName);
18
-
19
- export const ADDRESS_REGEX = /0x[0-9a-fA-F]{40}/;
20
- export const isAddress = (address: string) => typeof address === 'string' && (new RegExp(ADDRESS_REGEX).test(address));
21
-
22
- export const compareAddresses = (addr1 = '', addr2 = '') => addr1.toLowerCase() === addr2.toLowerCase();
23
-
24
- export const getWeiAmountForDecimals = (amount: string | number, decimals: number) => new Dec(amount).mul(10 ** decimals).floor().toString();
25
-
26
- export const getEthAmountForDecimals = (amount: string | number, decimals: string | number) => new Dec(amount).div(10 ** +decimals).toString();
27
-
28
- export const handleWbtcLegacy = (asset: string) => (asset === 'WBTC Legacy' ? 'WBTC' : asset);
29
-
30
- export const wethToEthByAddress = (maybeWethAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address;
31
-
32
- export const ethToWethByAddress = (maybeEthAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(ethToWeth(getAssetInfoByAddress(maybeEthAddr, chainId).symbol), chainId).address;
33
-
34
- export const bytesToString = (hex: string) => Buffer.from(hex.replace(/^0x/, ''), 'hex')
35
- .toString()
36
- // eslint-disable-next-line no-control-regex
37
- .replace(/\x00/g, '');
38
-
39
- /**
40
- * Map an input value from one range (minInput, maxInput) to a value in another range (minOutput, maxOutput)
41
- */
42
- export const mapRange = (input: number | string, minInput: number | string, maxInput: number | string, minOutput:number | string, maxOutput: number | string) => {
43
- // slope = 1.0 * (output_end - output_start) / (input_end - input_start)
44
- const inputDiff = new Dec(maxInput).minus(minInput);
45
- const outputDiff = new Dec(maxOutput).minus(minOutput);
46
- const slope = new Dec(outputDiff).div(inputDiff);
47
-
48
- // output = output_start + slope * (input - input_start)
49
- return new Dec(minOutput).plus(new Dec(slope).mul(new Dec(input).minus(minInput))).toDP(2).toNumber();
50
- };
51
-
1
+ import Dec from 'decimal.js';
2
+ import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
3
+ import { NetworkNumber } from '../types/common';
4
+
5
+ export const isLayer2Network = (networkId: NetworkNumber) => [10, 42161, 8453].includes(+networkId);
6
+
7
+ export const addToObjectIf = (condition: any, item: any) => (condition ? item : {});
8
+
9
+ export const ethToWeth = (maybeEth: any) => maybeEth?.replace(/^ETH$/, 'WETH');
10
+
11
+ export const wethToEth = (maybeWeth: any) => maybeWeth?.replace(/^WETH$/, 'ETH');
12
+
13
+ export const stEthToWstEth = (maybeStEth: any) => maybeStEth?.replace(/^stETH$/, 'wstETH');
14
+
15
+ export const wstEthToStEth = (maybeStEth: any) => maybeStEth?.replace(/^wstETH$/, 'stETH');
16
+
17
+ export const getAbiItem = (abi: any, methodName: string) => abi.find((i: any) => i.name === methodName);
18
+
19
+ export const ADDRESS_REGEX = /0x[0-9a-fA-F]{40}/;
20
+ export const isAddress = (address: string) => typeof address === 'string' && (new RegExp(ADDRESS_REGEX).test(address));
21
+
22
+ export const compareAddresses = (addr1 = '', addr2 = '') => addr1.toLowerCase() === addr2.toLowerCase();
23
+
24
+ export const getWeiAmountForDecimals = (amount: string | number, decimals: number) => new Dec(amount).mul(10 ** decimals).floor().toString();
25
+
26
+ export const getEthAmountForDecimals = (amount: string | number, decimals: string | number) => new Dec(amount).div(10 ** +decimals).toString();
27
+
28
+ export const handleWbtcLegacy = (asset: string) => (asset === 'WBTC Legacy' ? 'WBTC' : asset);
29
+
30
+ export const wethToEthByAddress = (maybeWethAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address;
31
+
32
+ export const ethToWethByAddress = (maybeEthAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(ethToWeth(getAssetInfoByAddress(maybeEthAddr, chainId).symbol), chainId).address;
33
+
34
+ export const bytesToString = (hex: string) => Buffer.from(hex.replace(/^0x/, ''), 'hex')
35
+ .toString()
36
+ // eslint-disable-next-line no-control-regex
37
+ .replace(/\x00/g, '');
38
+
39
+ /**
40
+ * Map an input value from one range (minInput, maxInput) to a value in another range (minOutput, maxOutput)
41
+ */
42
+ export const mapRange = (input: number | string, minInput: number | string, maxInput: number | string, minOutput:number | string, maxOutput: number | string) => {
43
+ // slope = 1.0 * (output_end - output_start) / (input_end - input_start)
44
+ const inputDiff = new Dec(maxInput).minus(minInput);
45
+ const outputDiff = new Dec(maxOutput).minus(minOutput);
46
+ const slope = new Dec(outputDiff).div(inputDiff);
47
+
48
+ // output = output_start + slope * (input - input_start)
49
+ return new Dec(minOutput).plus(new Dec(slope).mul(new Dec(input).minus(minInput))).toDP(2).toNumber();
50
+ };
51
+
package/src/setup.ts CHANGED
@@ -1,8 +1,8 @@
1
- import Decimal from 'decimal.js';
2
-
3
- Decimal.set({
4
- rounding: Decimal.ROUND_DOWN,
5
- toExpPos: 9e15,
6
- toExpNeg: -9e15,
7
- precision: 50,
8
- });
1
+ import Decimal from 'decimal.js';
2
+
3
+ Decimal.set({
4
+ rounding: Decimal.ROUND_DOWN,
5
+ toExpPos: 9e15,
6
+ toExpNeg: -9e15,
7
+ precision: 50,
8
+ });