@defisaver/positions-sdk 0.0.96 → 0.0.98

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 (65) hide show
  1. package/README.md +63 -63
  2. package/cjs/markets/compound/marketsAssets.js +1 -1
  3. package/cjs/morphoBlue/index.d.ts +2 -1
  4. package/cjs/morphoBlue/index.js +29 -1
  5. package/esm/markets/compound/marketsAssets.js +1 -1
  6. package/esm/morphoBlue/index.d.ts +2 -1
  7. package/esm/morphoBlue/index.js +28 -1
  8. package/package.json +40 -40
  9. package/src/aaveV2/index.ts +227 -227
  10. package/src/aaveV3/index.ts +558 -558
  11. package/src/assets/index.ts +60 -60
  12. package/src/chickenBonds/index.ts +123 -123
  13. package/src/compoundV2/index.ts +219 -219
  14. package/src/compoundV3/index.ts +266 -266
  15. package/src/config/contracts.js +848 -848
  16. package/src/constants/index.ts +5 -5
  17. package/src/contracts.ts +128 -128
  18. package/src/curveUsd/index.ts +229 -229
  19. package/src/exchange/index.ts +17 -17
  20. package/src/helpers/aaveHelpers/index.ts +134 -134
  21. package/src/helpers/chickenBondsHelpers/index.ts +23 -23
  22. package/src/helpers/compoundHelpers/index.ts +181 -181
  23. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  24. package/src/helpers/index.ts +7 -7
  25. package/src/helpers/llamaLendHelpers/index.ts +45 -45
  26. package/src/helpers/makerHelpers/index.ts +94 -94
  27. package/src/helpers/morphoBlueHelpers/index.ts +56 -56
  28. package/src/helpers/sparkHelpers/index.ts +106 -106
  29. package/src/index.ts +46 -46
  30. package/src/liquity/index.ts +116 -116
  31. package/src/llamaLend/index.ts +268 -268
  32. package/src/maker/index.ts +117 -117
  33. package/src/markets/aave/index.ts +80 -80
  34. package/src/markets/aave/marketAssets.ts +24 -24
  35. package/src/markets/compound/index.ts +142 -142
  36. package/src/markets/compound/marketsAssets.ts +50 -50
  37. package/src/markets/curveUsd/index.ts +69 -69
  38. package/src/markets/index.ts +5 -5
  39. package/src/markets/llamaLend/contractAddresses.ts +95 -95
  40. package/src/markets/llamaLend/index.ts +150 -150
  41. package/src/markets/morphoBlue/index.ts +611 -611
  42. package/src/markets/spark/index.ts +29 -29
  43. package/src/markets/spark/marketAssets.ts +10 -10
  44. package/src/moneymarket/moneymarketCommonService.ts +76 -76
  45. package/src/morphoAaveV2/index.ts +256 -256
  46. package/src/morphoAaveV3/index.ts +612 -612
  47. package/src/morphoBlue/index.ts +199 -162
  48. package/src/multicall/index.ts +22 -22
  49. package/src/services/dsrService.ts +15 -15
  50. package/src/services/priceService.ts +21 -21
  51. package/src/services/utils.ts +51 -51
  52. package/src/setup.ts +8 -8
  53. package/src/spark/index.ts +424 -424
  54. package/src/staking/staking.ts +187 -187
  55. package/src/types/aave.ts +256 -256
  56. package/src/types/chickenBonds.ts +45 -45
  57. package/src/types/common.ts +84 -84
  58. package/src/types/compound.ts +128 -128
  59. package/src/types/curveUsd.ts +118 -118
  60. package/src/types/index.ts +8 -8
  61. package/src/types/liquity.ts +30 -30
  62. package/src/types/llamaLend.ts +143 -143
  63. package/src/types/maker.ts +50 -50
  64. package/src/types/morphoBlue.ts +139 -139
  65. package/src/types/spark.ts +106 -106
@@ -1,162 +1,199 @@
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
- FeedRegistryContract,
7
- MorphoBlueViewContract,
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 { wethToEth } from '../services/utils';
15
- import { getMorphoBlueAggregatedPositionData } from '../helpers/morphoBlueHelpers';
16
-
17
-
18
- const compound = (ratePerSeconds: string) => {
19
- const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
20
- const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
21
- return new Dec(apyNumber).mul(WAD).floor().toString();
22
- };
23
-
24
- const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
25
- if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
26
- return 0;
27
- }
28
- const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
29
- .toString();
30
- const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
31
- .toString();
32
- const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
33
- .toString();
34
- return compound(ratePerSecond);
35
- };
36
-
37
- const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
38
- if (totalBorrowShares === '0') {
39
- return 0;
40
- }
41
- return compound(borrowRate);
42
- };
43
-
44
-
45
- export async function getMorphoBlueMarketData(web3: Web3, network: NetworkNumber, selectedMarket: MorphoBlueMarketData, mainnetWeb3: Web3): Promise<MorphoBlueMarketInfo> {
46
- const {
47
- loanToken, collateralToken, oracle, irm, lltv, oracleType,
48
- } = selectedMarket;
49
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
50
- const loanTokenInfo = getAssetInfoByAddress(loanToken, network);
51
- const collateralTokenInfo = getAssetInfoByAddress(collateralToken, network);
52
- let loanTokenFeedAddress = loanTokenInfo.addresses[NetworkNumber.Eth];
53
- if (loanTokenInfo.symbol === 'WETH') {
54
- const ethAddress = getAssetInfo('ETH').address;
55
- loanTokenFeedAddress = ethAddress;
56
- }
57
-
58
- const feedRegistryContract = FeedRegistryContract(mainnetWeb3, NetworkNumber.Eth);
59
- const morphoBlueViewContract = MorphoBlueViewContract(web3, network);
60
-
61
- const [loanTokenPrice, marketInfo] = await Promise.all([
62
- loanTokenInfo.symbol === 'USDA' ? '100000000' : feedRegistryContract.methods.latestAnswer(loanTokenFeedAddress, USD_QUOTE).call(),
63
- morphoBlueViewContract.methods.getMarketInfoNotTuple(loanToken, collateralToken, oracle, irm, lltvInWei).call(),
64
- ]);
65
-
66
- const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets, marketInfo.totalBorrowAssets, marketInfo.borrowRate, marketInfo.fee);
67
- const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate, marketInfo.totalBorrowShares);
68
- const utillization = new Dec(marketInfo.totalBorrowAssets).div(marketInfo.totalSupplyAssets).mul(100).toString();
69
-
70
- const oracleScaleFactor = new Dec(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
71
- const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
72
-
73
- const scale = new Dec(10).pow(loanTokenInfo.decimals).toString();
74
-
75
- const oracleRate = new Dec(marketInfo.oracle).div(oracleScale).toString();
76
- const assetsData: MorphoBlueAssetsData = {};
77
- assetsData[wethToEth(loanTokenInfo.symbol)] = {
78
- symbol: wethToEth(loanTokenInfo.symbol),
79
- address: loanToken,
80
- price: new Dec(loanTokenPrice).div(1e8).toString(),
81
- supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
82
- borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
83
- totalSupply: new Dec(marketInfo.totalSupplyAssets).div(scale).toString(),
84
- totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(scale).toString(),
85
- canBeSupplied: true,
86
- canBeBorrowed: true,
87
- };
88
-
89
- assetsData[wethToEth(collateralTokenInfo.symbol)] = {
90
- symbol: wethToEth(collateralTokenInfo.symbol),
91
- address: collateralToken,
92
- price: new Dec(assetsData[wethToEth(loanTokenInfo.symbol)].price).mul(oracleRate).toString(),
93
- supplyRate: '0',
94
- borrowRate: '0',
95
- canBeSupplied: true,
96
- canBeBorrowed: false,
97
- };
98
- if (STAKING_ASSETS.includes(collateralTokenInfo.symbol)) {
99
- assetsData[collateralTokenInfo.symbol].incentiveSupplyApy = await getStakingApy(collateralTokenInfo.symbol, mainnetWeb3);
100
- assetsData[collateralTokenInfo.symbol].incentiveSupplyToken = collateralTokenInfo.symbol;
101
- }
102
-
103
- return {
104
- id: marketInfo.id,
105
- fee: new Dec(marketInfo.fee).div(WAD).toString(),
106
- loanToken: wethToEth(loanTokenInfo.symbol),
107
- collateralToken: wethToEth(collateralTokenInfo.symbol),
108
- utillization,
109
- oracle: oracleRate,
110
- oracleType,
111
- lltv: new Dec(lltv).toString(),
112
- minRatio: new Dec(1).div(lltv).mul(100).toString(),
113
- assetsData,
114
- };
115
- }
116
-
117
- export async function getMorphoBlueAccountData(web3: Web3, network: NetworkNumber, account: string, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
118
- const {
119
- loanToken, collateralToken, oracle, irm, lltv,
120
- } = selectedMarket;
121
- const lltvInWei = new Dec(lltv).mul(WAD).toString();
122
- const marketObject = {
123
- loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
124
- };
125
- const viewContract = MorphoBlueViewContract(web3, network);
126
- const loanInfo = await viewContract.methods.getUserInfo(marketObject, account).call();
127
- const usedAssets: MMUsedAssets = {};
128
-
129
- const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
130
- const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets, marketInfo.loanToken);
131
- const loanTokenBorrowed = assetAmountInEth(loanInfo.borrowedInAssets, marketInfo.loanToken);
132
- usedAssets[marketInfo.loanToken] = {
133
- symbol: loanTokenInfo.symbol,
134
- supplied: loanTokenSupplied,
135
- borrowed: loanTokenBorrowed,
136
- isSupplied: new Dec(loanInfo.suppliedInAssets).gt(0),
137
- isBorrowed: new Dec(loanInfo.borrowedInAssets).gt(0),
138
- collateral: false,
139
- suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
140
- borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
141
- };
142
-
143
- const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
144
- const collateralTokenSupplied = assetAmountInEth(loanInfo.collateral, marketInfo.collateralToken);
145
- usedAssets[marketInfo.collateralToken] = {
146
- symbol: collateralTokenInfo.symbol,
147
- supplied: collateralTokenSupplied,
148
- borrowed: '0',
149
- isSupplied: new Dec(loanInfo.collateral).gt(0),
150
- isBorrowed: false,
151
- collateral: true,
152
- suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
153
- borrowedUsd: '0',
154
- };
155
-
156
- return {
157
- supplyShares: loanInfo.supplyShares,
158
- borrowShares: loanInfo.borrowShares,
159
- usedAssets,
160
- ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, marketInfo }),
161
- };
162
- }
1
+ import Web3 from 'web3';
2
+ import Dec from 'decimal.js';
3
+ import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
4
+ import {
5
+ Blockish, EthAddress, MMUsedAssets, NetworkNumber, PositionBalances,
6
+ } from '../types/common';
7
+ import {
8
+ FeedRegistryContract,
9
+ MorphoBlueViewContract,
10
+ } from '../contracts';
11
+ import {
12
+ MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo, MorphoBluePositionData,
13
+ } from '../types';
14
+ import { WAD, SECONDS_PER_YEAR, USD_QUOTE } from '../constants';
15
+ import { getStakingApy, STAKING_ASSETS } from '../staking';
16
+ import { wethToEth } from '../services/utils';
17
+ import { getMorphoBlueAggregatedPositionData } from '../helpers/morphoBlueHelpers';
18
+
19
+
20
+ const compound = (ratePerSeconds: string) => {
21
+ const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
22
+ const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
23
+ return new Dec(apyNumber).mul(WAD).floor().toString();
24
+ };
25
+
26
+ const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
27
+ if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
28
+ return 0;
29
+ }
30
+ const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
31
+ .toString();
32
+ const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
33
+ .toString();
34
+ const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
35
+ .toString();
36
+ return compound(ratePerSecond);
37
+ };
38
+
39
+ const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
40
+ if (totalBorrowShares === '0') {
41
+ return 0;
42
+ }
43
+ return compound(borrowRate);
44
+ };
45
+
46
+
47
+ export async function getMorphoBlueMarketData(web3: Web3, network: NetworkNumber, selectedMarket: MorphoBlueMarketData, mainnetWeb3: Web3): Promise<MorphoBlueMarketInfo> {
48
+ const {
49
+ loanToken, collateralToken, oracle, irm, lltv, oracleType,
50
+ } = selectedMarket;
51
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
52
+ const loanTokenInfo = getAssetInfoByAddress(loanToken, network);
53
+ const collateralTokenInfo = getAssetInfoByAddress(collateralToken, network);
54
+ let loanTokenFeedAddress = loanTokenInfo.addresses[NetworkNumber.Eth];
55
+ if (loanTokenInfo.symbol === 'WETH') {
56
+ const ethAddress = getAssetInfo('ETH').address;
57
+ loanTokenFeedAddress = ethAddress;
58
+ }
59
+
60
+ const feedRegistryContract = FeedRegistryContract(mainnetWeb3, NetworkNumber.Eth);
61
+ const morphoBlueViewContract = MorphoBlueViewContract(web3, network);
62
+
63
+ const [loanTokenPrice, marketInfo] = await Promise.all([
64
+ loanTokenInfo.symbol === 'USDA' ? '100000000' : feedRegistryContract.methods.latestAnswer(loanTokenFeedAddress, USD_QUOTE).call(),
65
+ morphoBlueViewContract.methods.getMarketInfoNotTuple(loanToken, collateralToken, oracle, irm, lltvInWei).call(),
66
+ ]);
67
+
68
+ const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets, marketInfo.totalBorrowAssets, marketInfo.borrowRate, marketInfo.fee);
69
+ const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate, marketInfo.totalBorrowShares);
70
+ const utillization = new Dec(marketInfo.totalBorrowAssets).div(marketInfo.totalSupplyAssets).mul(100).toString();
71
+
72
+ const oracleScaleFactor = new Dec(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
73
+ const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
74
+
75
+ const scale = new Dec(10).pow(loanTokenInfo.decimals).toString();
76
+
77
+ const oracleRate = new Dec(marketInfo.oracle).div(oracleScale).toString();
78
+ const assetsData: MorphoBlueAssetsData = {};
79
+ assetsData[wethToEth(loanTokenInfo.symbol)] = {
80
+ symbol: wethToEth(loanTokenInfo.symbol),
81
+ address: loanToken,
82
+ price: new Dec(loanTokenPrice).div(1e8).toString(),
83
+ supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
84
+ borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
85
+ totalSupply: new Dec(marketInfo.totalSupplyAssets).div(scale).toString(),
86
+ totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(scale).toString(),
87
+ canBeSupplied: true,
88
+ canBeBorrowed: true,
89
+ };
90
+
91
+ assetsData[wethToEth(collateralTokenInfo.symbol)] = {
92
+ symbol: wethToEth(collateralTokenInfo.symbol),
93
+ address: collateralToken,
94
+ price: new Dec(assetsData[wethToEth(loanTokenInfo.symbol)].price).mul(oracleRate).toString(),
95
+ supplyRate: '0',
96
+ borrowRate: '0',
97
+ canBeSupplied: true,
98
+ canBeBorrowed: false,
99
+ };
100
+ if (STAKING_ASSETS.includes(collateralTokenInfo.symbol)) {
101
+ assetsData[collateralTokenInfo.symbol].incentiveSupplyApy = await getStakingApy(collateralTokenInfo.symbol, mainnetWeb3);
102
+ assetsData[collateralTokenInfo.symbol].incentiveSupplyToken = collateralTokenInfo.symbol;
103
+ }
104
+
105
+ return {
106
+ id: marketInfo.id,
107
+ fee: new Dec(marketInfo.fee).div(WAD).toString(),
108
+ loanToken: wethToEth(loanTokenInfo.symbol),
109
+ collateralToken: wethToEth(collateralTokenInfo.symbol),
110
+ utillization,
111
+ oracle: oracleRate,
112
+ oracleType,
113
+ lltv: new Dec(lltv).toString(),
114
+ minRatio: new Dec(1).div(lltv).mul(100).toString(),
115
+ assetsData,
116
+ };
117
+ }
118
+
119
+ export const getMorphoBlueAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, selectedMarket: MorphoBlueMarketData): Promise<PositionBalances> => {
120
+ let balances: PositionBalances = {
121
+ collateral: {},
122
+ debt: {},
123
+ };
124
+
125
+ if (!address) {
126
+ return balances;
127
+ }
128
+
129
+ const viewContract = MorphoBlueViewContract(web3, network, block);
130
+ const {
131
+ loanToken, collateralToken, oracle, irm, lltv,
132
+ } = selectedMarket;
133
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
134
+ const marketObject = {
135
+ loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
136
+ };
137
+
138
+ const loanInfo = await viewContract.methods.getUserInfo(marketObject, address).call({}, block);
139
+ const loanTokenInfo = getAssetInfoByAddress(selectedMarket.loanToken, network);
140
+ const collateralTokenInfo = getAssetInfoByAddress(selectedMarket.collateralToken, network);
141
+
142
+ balances = {
143
+ collateral: {
144
+ [addressMapping ? collateralTokenInfo.address.toLowerCase() : wethToEth(collateralTokenInfo.symbol)]: assetAmountInEth(loanInfo.collateral, collateralTokenInfo.symbol),
145
+ },
146
+ debt: {
147
+ [addressMapping ? loanTokenInfo.address.toLowerCase() : wethToEth(loanTokenInfo.symbol)]: assetAmountInEth(loanInfo.borrowedInAssets, loanTokenInfo.symbol),
148
+ },
149
+ };
150
+
151
+ return balances;
152
+ };
153
+
154
+ export async function getMorphoBlueAccountData(web3: Web3, network: NetworkNumber, account: string, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
155
+ const {
156
+ loanToken, collateralToken, oracle, irm, lltv,
157
+ } = selectedMarket;
158
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
159
+ const marketObject = {
160
+ loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
161
+ };
162
+ const viewContract = MorphoBlueViewContract(web3, network);
163
+ const loanInfo = await viewContract.methods.getUserInfo(marketObject, account).call();
164
+ const usedAssets: MMUsedAssets = {};
165
+
166
+ const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
167
+ const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets, marketInfo.loanToken);
168
+ const loanTokenBorrowed = assetAmountInEth(loanInfo.borrowedInAssets, marketInfo.loanToken);
169
+ usedAssets[marketInfo.loanToken] = {
170
+ symbol: loanTokenInfo.symbol,
171
+ supplied: loanTokenSupplied,
172
+ borrowed: loanTokenBorrowed,
173
+ isSupplied: new Dec(loanInfo.suppliedInAssets).gt(0),
174
+ isBorrowed: new Dec(loanInfo.borrowedInAssets).gt(0),
175
+ collateral: false,
176
+ suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
177
+ borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
178
+ };
179
+
180
+ const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
181
+ const collateralTokenSupplied = assetAmountInEth(loanInfo.collateral, marketInfo.collateralToken);
182
+ usedAssets[marketInfo.collateralToken] = {
183
+ symbol: collateralTokenInfo.symbol,
184
+ supplied: collateralTokenSupplied,
185
+ borrowed: '0',
186
+ isSupplied: new Dec(loanInfo.collateral).gt(0),
187
+ isBorrowed: false,
188
+ collateral: true,
189
+ suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
190
+ borrowedUsd: '0',
191
+ };
192
+
193
+ return {
194
+ supplyShares: loanInfo.supplyShares,
195
+ borrowShares: loanInfo.borrowShares,
196
+ usedAssets,
197
+ ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, marketInfo }),
198
+ };
199
+ }
@@ -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
+ });