@defisaver/positions-sdk 0.0.30 → 0.0.32

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 (113) hide show
  1. package/README.md +63 -63
  2. package/cjs/config/contracts.d.ts +115 -0
  3. package/cjs/config/contracts.js +14 -0
  4. package/cjs/constants/index.d.ts +2 -0
  5. package/cjs/constants/index.js +3 -1
  6. package/cjs/contracts.d.ts +2 -0
  7. package/cjs/contracts.js +3 -1
  8. package/cjs/helpers/index.d.ts +1 -0
  9. package/cjs/helpers/index.js +2 -1
  10. package/cjs/helpers/morphoBlueHelpers/index.d.ts +7 -0
  11. package/cjs/helpers/morphoBlueHelpers/index.js +41 -0
  12. package/cjs/index.d.ts +2 -1
  13. package/cjs/index.js +3 -1
  14. package/cjs/markets/index.d.ts +1 -0
  15. package/cjs/markets/index.js +3 -1
  16. package/cjs/markets/morphoBlue/index.d.ts +6 -0
  17. package/cjs/markets/morphoBlue/index.js +24 -0
  18. package/cjs/morphoBlue/index.d.ts +5 -0
  19. package/cjs/morphoBlue/index.js +153 -0
  20. package/cjs/types/common.d.ts +1 -1
  21. package/cjs/types/contracts/generated/FeedRegistry.d.ts +182 -0
  22. package/cjs/types/contracts/generated/FeedRegistry.js +5 -0
  23. package/cjs/types/contracts/generated/MorphoBlueView.d.ts +122 -0
  24. package/cjs/types/contracts/generated/MorphoBlueView.js +5 -0
  25. package/cjs/types/contracts/generated/index.d.ts +2 -0
  26. package/cjs/types/index.d.ts +1 -0
  27. package/cjs/types/index.js +1 -0
  28. package/cjs/types/morphoBlue.d.ts +74 -0
  29. package/cjs/types/morphoBlue.js +7 -0
  30. package/esm/config/contracts.d.ts +115 -0
  31. package/esm/config/contracts.js +14 -0
  32. package/esm/constants/index.d.ts +2 -0
  33. package/esm/constants/index.js +2 -0
  34. package/esm/contracts.d.ts +2 -0
  35. package/esm/contracts.js +2 -0
  36. package/esm/helpers/index.d.ts +1 -0
  37. package/esm/helpers/index.js +1 -0
  38. package/esm/helpers/morphoBlueHelpers/index.d.ts +7 -0
  39. package/esm/helpers/morphoBlueHelpers/index.js +34 -0
  40. package/esm/index.d.ts +2 -1
  41. package/esm/index.js +2 -1
  42. package/esm/markets/index.d.ts +1 -0
  43. package/esm/markets/index.js +1 -0
  44. package/esm/markets/morphoBlue/index.d.ts +6 -0
  45. package/esm/markets/morphoBlue/index.js +19 -0
  46. package/esm/morphoBlue/index.d.ts +5 -0
  47. package/esm/morphoBlue/index.js +145 -0
  48. package/esm/types/common.d.ts +1 -1
  49. package/esm/types/contracts/generated/FeedRegistry.d.ts +182 -0
  50. package/esm/types/contracts/generated/FeedRegistry.js +4 -0
  51. package/esm/types/contracts/generated/MorphoBlueView.d.ts +122 -0
  52. package/esm/types/contracts/generated/MorphoBlueView.js +4 -0
  53. package/esm/types/contracts/generated/index.d.ts +2 -0
  54. package/esm/types/index.d.ts +1 -0
  55. package/esm/types/index.js +1 -0
  56. package/esm/types/morphoBlue.d.ts +74 -0
  57. package/esm/types/morphoBlue.js +4 -0
  58. package/package.json +40 -40
  59. package/src/aaveV2/index.ts +226 -226
  60. package/src/aaveV3/index.ts +561 -561
  61. package/src/assets/index.ts +60 -60
  62. package/src/chickenBonds/index.ts +123 -123
  63. package/src/compoundV2/index.ts +219 -219
  64. package/src/compoundV3/index.ts +275 -275
  65. package/src/config/contracts.js +690 -676
  66. package/src/constants/index.ts +6 -4
  67. package/src/contracts.ts +131 -127
  68. package/src/curveUsd/index.ts +228 -228
  69. package/src/exchange/index.ts +17 -17
  70. package/src/helpers/aaveHelpers/index.ts +134 -134
  71. package/src/helpers/chickenBondsHelpers/index.ts +23 -23
  72. package/src/helpers/compoundHelpers/index.ts +181 -181
  73. package/src/helpers/curveUsdHelpers/index.ts +32 -32
  74. package/src/helpers/index.ts +7 -6
  75. package/src/helpers/makerHelpers/index.ts +94 -94
  76. package/src/helpers/morphoBlueHelpers/index.ts +47 -0
  77. package/src/helpers/sparkHelpers/index.ts +106 -106
  78. package/src/index.ts +42 -40
  79. package/src/liquity/index.ts +116 -116
  80. package/src/maker/index.ts +101 -101
  81. package/src/markets/aave/index.ts +80 -80
  82. package/src/markets/aave/marketAssets.ts +24 -24
  83. package/src/markets/compound/index.ts +141 -141
  84. package/src/markets/compound/marketsAssets.ts +48 -48
  85. package/src/markets/curveUsd/index.ts +69 -69
  86. package/src/markets/index.ts +5 -4
  87. package/src/markets/morphoBlue/index.ts +21 -0
  88. package/src/markets/spark/index.ts +29 -29
  89. package/src/markets/spark/marketAssets.ts +10 -10
  90. package/src/moneymarket/moneymarketCommonService.ts +75 -75
  91. package/src/morphoAaveV2/index.ts +255 -255
  92. package/src/morphoAaveV3/index.ts +619 -619
  93. package/src/morphoBlue/index.ts +166 -0
  94. package/src/multicall/index.ts +22 -22
  95. package/src/services/dsrService.ts +15 -15
  96. package/src/services/priceService.ts +21 -21
  97. package/src/services/utils.ts +34 -34
  98. package/src/spark/index.ts +422 -422
  99. package/src/staking/staking.ts +167 -167
  100. package/src/types/aave.ts +256 -256
  101. package/src/types/chickenBonds.ts +45 -45
  102. package/src/types/common.ts +83 -83
  103. package/src/types/compound.ts +128 -128
  104. package/src/types/contracts/generated/FeedRegistry.ts +337 -0
  105. package/src/types/contracts/generated/MorphoBlueView.ts +171 -0
  106. package/src/types/contracts/generated/index.ts +2 -0
  107. package/src/types/curveUsd.ts +112 -112
  108. package/src/types/index.ts +8 -7
  109. package/src/types/liquity.ts +30 -30
  110. package/src/types/maker.ts +50 -50
  111. package/src/types/morphoBlue.ts +80 -0
  112. package/src/types/spark.ts +106 -106
  113. package/src/morpho/markets.ts +0 -39
@@ -0,0 +1,166 @@
1
+ import Web3 from 'web3';
2
+ import Dec from 'decimal.js';
3
+ import { 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 } 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 oracleRate = new Dec(marketInfo.oracle).div(1e36).toString();
87
+ const assetsData: MorphoBlueAssetsData = {};
88
+ assetsData[wethToEth(loanTokenInfo.symbol)] = {
89
+ symbol: wethToEth(loanTokenInfo.symbol),
90
+ address: loanToken,
91
+ price: new Dec(loanTokenPrice).div(1e8).toString(),
92
+ supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
93
+ borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
94
+ totalSupply: new Dec(marketInfo.totalSupplyAssets).div(WAD).toString(),
95
+ totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(WAD).toString(),
96
+ };
97
+
98
+ assetsData[wethToEth(collateralTokenInfo.symbol)] = {
99
+ symbol: wethToEth(collateralTokenInfo.symbol),
100
+ address: collateralToken,
101
+ price: new Dec(loanTokenPrice).div(1e8).mul(oracleRate).toString(),
102
+ supplyRate: '0',
103
+ borrowRate: '0',
104
+ };
105
+
106
+ if (['wstETH', 'cbETH', 'rETH'].includes(collateralTokenInfo.symbol)) {
107
+ assetsData[collateralTokenInfo.symbol].incentiveSupplyApy = await getStakingApy(collateralTokenInfo.symbol, mainnetWeb3);
108
+ assetsData[collateralTokenInfo.symbol].incentiveSupplyToken = collateralTokenInfo.symbol;
109
+ }
110
+
111
+ return {
112
+ id: marketInfo.id,
113
+ fee: new Dec(marketInfo.fee).div(WAD).toString(),
114
+ loanToken: wethToEth(loanTokenInfo.symbol),
115
+ collateralToken: wethToEth(collateralTokenInfo.symbol),
116
+ utillization,
117
+ oracle: oracleRate,
118
+ lltv: new Dec(lltv).toString(),
119
+ assetsData,
120
+ };
121
+ }
122
+
123
+ export async function getMorphoBlueAccountData(web3: Web3, network: NetworkNumber, account: string, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData> {
124
+ const {
125
+ loanToken, collateralToken, oracle, irm, lltv,
126
+ } = selectedMarket;
127
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
128
+ const marketObject = {
129
+ loanToken, collateralToken, oracle, irm, lltv: lltvInWei,
130
+ };
131
+ const viewContract = MorphoBlueViewContract(web3, network);
132
+ const loanInfo = await viewContract.methods.getUserInfo(marketObject, account).call();
133
+ const usedAssets: MMUsedAssets = {};
134
+
135
+ const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
136
+ const loanTokenSupplied = new Dec(loanInfo.suppliedInAssets).div(WAD).toString();
137
+ const loanTokenBorrowed = new Dec(loanInfo.borrowedInAssets).div(WAD).toString();
138
+ usedAssets[marketInfo.loanToken] = {
139
+ symbol: loanTokenInfo.symbol,
140
+ supplied: loanTokenSupplied,
141
+ borrowed: loanTokenBorrowed,
142
+ isSupplied: new Dec(loanInfo.suppliedInAssets).gt(0),
143
+ isBorrowed: new Dec(loanInfo.borrowedInAssets).gt(0),
144
+ collateral: false,
145
+ suppliedUsd: new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString(),
146
+ borrowedUsd: new Dec(loanTokenBorrowed).mul(loanTokenInfo.price).toString(),
147
+ };
148
+
149
+ const collateralTokenInfo = marketInfo.assetsData[marketInfo.collateralToken];
150
+ const collateralTokenSupplied = new Dec(loanInfo.collateral).div(WAD).toString();
151
+ usedAssets[marketInfo.collateralToken] = {
152
+ symbol: collateralTokenInfo.symbol,
153
+ supplied: collateralTokenSupplied,
154
+ borrowed: '0',
155
+ isSupplied: new Dec(loanInfo.collateral).gt(0),
156
+ isBorrowed: false,
157
+ collateral: true,
158
+ suppliedUsd: new Dec(collateralTokenSupplied).mul(collateralTokenInfo.price).toString(),
159
+ borrowedUsd: '0',
160
+ };
161
+
162
+ return {
163
+ usedAssets,
164
+ ...getMorphoBlueAggregatedPositionData({ usedAssets, assetsData: marketInfo.assetsData, lltv: marketInfo.lltv }),
165
+ };
166
+ }
@@ -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,35 +1,35 @@
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 getEthAmountForDecimals = (amount: string | number, decimals: string | number) => new Dec(amount).div(10 ** +decimals).toString();
25
-
26
- export const handleWbtcLegacy = (asset: string) => (asset === 'WBTC Legacy' ? 'WBTC' : asset);
27
-
28
- export const wethToEthByAddress = (maybeWethAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address;
29
-
30
- export const ethToWethByAddress = (maybeEthAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(ethToWeth(getAssetInfoByAddress(maybeEthAddr, chainId).symbol), chainId).address;
31
-
32
- export const bytesToString = (hex: string) => Buffer.from(hex.replace(/^0x/, ''), 'hex')
33
- .toString()
34
- // eslint-disable-next-line no-control-regex
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 getEthAmountForDecimals = (amount: string | number, decimals: string | number) => new Dec(amount).div(10 ** +decimals).toString();
25
+
26
+ export const handleWbtcLegacy = (asset: string) => (asset === 'WBTC Legacy' ? 'WBTC' : asset);
27
+
28
+ export const wethToEthByAddress = (maybeWethAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address;
29
+
30
+ export const ethToWethByAddress = (maybeEthAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(ethToWeth(getAssetInfoByAddress(maybeEthAddr, chainId).symbol), chainId).address;
31
+
32
+ export const bytesToString = (hex: string) => Buffer.from(hex.replace(/^0x/, ''), 'hex')
33
+ .toString()
34
+ // eslint-disable-next-line no-control-regex
35
35
  .replace(/\x00/g, '');