@defisaver/positions-sdk 1.0.11-fluid-dev11 → 1.0.11-fluid-dev13

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 (104) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +69 -69
  4. package/cjs/config/contracts.d.ts +113 -53
  5. package/cjs/config/contracts.js +10 -0
  6. package/cjs/contracts.d.ts +1 -0
  7. package/cjs/contracts.js +2 -1
  8. package/cjs/fluid/index.js +80 -28
  9. package/cjs/helpers/fluidHelpers/index.js +21 -2
  10. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  11. package/cjs/markets/fluid/index.d.ts +0 -2
  12. package/cjs/markets/fluid/index.js +26 -24
  13. package/cjs/services/priceService.d.ts +2 -0
  14. package/cjs/services/priceService.js +13 -1
  15. package/cjs/types/contracts/generated/BTCPriceFeed.d.ts +135 -0
  16. package/cjs/types/contracts/generated/BTCPriceFeed.js +5 -0
  17. package/cjs/types/contracts/generated/index.d.ts +1 -0
  18. package/cjs/types/fluid.d.ts +4 -0
  19. package/esm/config/contracts.d.ts +113 -53
  20. package/esm/config/contracts.js +10 -0
  21. package/esm/contracts.d.ts +1 -0
  22. package/esm/contracts.js +1 -0
  23. package/esm/fluid/index.js +81 -29
  24. package/esm/helpers/fluidHelpers/index.js +21 -2
  25. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  26. package/esm/markets/fluid/index.d.ts +0 -2
  27. package/esm/markets/fluid/index.js +24 -21
  28. package/esm/services/priceService.d.ts +2 -0
  29. package/esm/services/priceService.js +11 -1
  30. package/esm/types/contracts/generated/BTCPriceFeed.d.ts +135 -0
  31. package/esm/types/contracts/generated/BTCPriceFeed.js +4 -0
  32. package/esm/types/contracts/generated/index.d.ts +1 -0
  33. package/esm/types/fluid.d.ts +4 -0
  34. package/package.json +54 -54
  35. package/src/aaveV2/index.ts +227 -227
  36. package/src/aaveV3/index.ts +624 -624
  37. package/src/assets/index.ts +60 -60
  38. package/src/chickenBonds/index.ts +123 -123
  39. package/src/compoundV2/index.ts +220 -220
  40. package/src/compoundV3/index.ts +291 -291
  41. package/src/config/contracts.js +1165 -1155
  42. package/src/constants/index.ts +6 -6
  43. package/src/contracts.ts +136 -135
  44. package/src/curveUsd/index.ts +239 -239
  45. package/src/eulerV2/index.ts +303 -303
  46. package/src/exchange/index.ts +17 -17
  47. package/src/fluid/index.ts +1320 -1261
  48. package/src/helpers/aaveHelpers/index.ts +203 -203
  49. package/src/helpers/chickenBondsHelpers/index.ts +23 -23
  50. package/src/helpers/compoundHelpers/index.ts +248 -248
  51. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  52. package/src/helpers/eulerHelpers/index.ts +234 -234
  53. package/src/helpers/fluidHelpers/index.ts +325 -294
  54. package/src/helpers/index.ts +11 -11
  55. package/src/helpers/liquityV2Helpers/index.ts +80 -80
  56. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  57. package/src/helpers/makerHelpers/index.ts +94 -94
  58. package/src/helpers/morphoBlueHelpers/index.ts +367 -367
  59. package/src/helpers/sparkHelpers/index.ts +154 -154
  60. package/src/index.ts +52 -52
  61. package/src/liquity/index.ts +116 -116
  62. package/src/liquityV2/index.ts +295 -295
  63. package/src/llamaLend/index.ts +275 -275
  64. package/src/maker/index.ts +117 -117
  65. package/src/markets/aave/index.ts +152 -152
  66. package/src/markets/aave/marketAssets.ts +46 -46
  67. package/src/markets/compound/index.ts +213 -213
  68. package/src/markets/compound/marketsAssets.ts +82 -82
  69. package/src/markets/curveUsd/index.ts +69 -69
  70. package/src/markets/euler/index.ts +26 -26
  71. package/src/markets/fluid/index.ts +2456 -2454
  72. package/src/markets/index.ts +27 -27
  73. package/src/markets/liquityV2/index.ts +54 -54
  74. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  75. package/src/markets/llamaLend/index.ts +235 -235
  76. package/src/markets/morphoBlue/index.ts +895 -895
  77. package/src/markets/spark/index.ts +29 -29
  78. package/src/markets/spark/marketAssets.ts +10 -10
  79. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  80. package/src/morphoAaveV2/index.ts +256 -256
  81. package/src/morphoAaveV3/index.ts +630 -630
  82. package/src/morphoBlue/index.ts +202 -202
  83. package/src/multicall/index.ts +33 -33
  84. package/src/services/priceService.ts +143 -130
  85. package/src/services/utils.ts +59 -59
  86. package/src/setup.ts +8 -8
  87. package/src/spark/index.ts +460 -460
  88. package/src/staking/staking.ts +217 -217
  89. package/src/types/aave.ts +275 -275
  90. package/src/types/chickenBonds.ts +45 -45
  91. package/src/types/common.ts +84 -84
  92. package/src/types/compound.ts +133 -133
  93. package/src/types/contracts/generated/BTCPriceFeed.ts +202 -0
  94. package/src/types/contracts/generated/index.ts +1 -0
  95. package/src/types/curveUsd.ts +119 -119
  96. package/src/types/euler.ts +173 -173
  97. package/src/types/fluid.ts +330 -325
  98. package/src/types/index.ts +11 -11
  99. package/src/types/liquity.ts +30 -30
  100. package/src/types/liquityV2.ts +119 -119
  101. package/src/types/llamaLend.ts +155 -155
  102. package/src/types/maker.ts +50 -50
  103. package/src/types/morphoBlue.ts +194 -194
  104. package/src/types/spark.ts +135 -135
@@ -1,130 +1,143 @@
1
- import Web3 from 'web3';
2
- import Dec from 'decimal.js';
3
- import { getAssetInfo } from '@defisaver/tokens';
4
- import {
5
- COMPPriceFeedContract,
6
- ETHPriceFeedContract,
7
- USDCPriceFeedContract, WeETHPriceFeedContract,
8
- WstETHPriceFeedContract,
9
- } from '../contracts';
10
- import { NetworkNumber } from '../types/common';
11
- import { multicall } from '../multicall';
12
- import { getEthAmountForDecimals } from './utils';
13
-
14
- export const getEthPrice = async (web3: Web3) => {
15
- const contract = ETHPriceFeedContract(web3, NetworkNumber.Eth);
16
- const price = await contract.methods.latestAnswer().call();
17
- return new Dec(price).div(1e8).toString();
18
- };
19
-
20
- export const getUSDCPrice = async (web3: Web3) => {
21
- const contract = USDCPriceFeedContract(web3, NetworkNumber.Eth);
22
- const price = await contract.methods.latestAnswer().call();
23
- return new Dec(price).div(1e8).toString();
24
- };
25
-
26
- export const getCompPrice = async (web3: Web3) => {
27
- const contract = COMPPriceFeedContract(web3, NetworkNumber.Eth);
28
- const price = await contract.methods.latestAnswer().call();
29
- return new Dec(price).div(1e8).toString();
30
- };
31
-
32
- export const getWstETHPrice = async (web3: Web3) => {
33
- const wstETHFeedContract = WstETHPriceFeedContract(web3, NetworkNumber.Eth);
34
- const ethFeedContract = ETHPriceFeedContract(web3, NetworkNumber.Eth);
35
- const calls = [
36
- {
37
- target: ethFeedContract.options.address,
38
- abiItem: ethFeedContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
39
- params: [],
40
- },
41
- {
42
- target: wstETHFeedContract.options.address,
43
- abiItem: wstETHFeedContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
44
- params: [],
45
- },
46
- ];
47
-
48
- const multicallRes = await multicall(calls, web3);
49
-
50
- const ethPrice = new Dec(multicallRes[0][0]).div(1e8);
51
-
52
- const wstETHRate = new Dec(multicallRes[1].answer).div(1e8);
53
-
54
- return new Dec(ethPrice).mul(wstETHRate).toString();
55
- };
56
-
57
- export const getWstETHChainLinkPriceCalls = (web3: Web3, network: NetworkNumber) => {
58
- const wstETHFeedContract = WstETHPriceFeedContract(web3, network);
59
- const ethFeedContract = ETHPriceFeedContract(web3, network);
60
- const calls = [
61
- {
62
- target: ethFeedContract.options.address,
63
- abiItem: ethFeedContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
64
- params: [],
65
- },
66
- {
67
- target: wstETHFeedContract.options.address,
68
- abiItem: wstETHFeedContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
69
- params: [],
70
- },
71
- {
72
- target: wstETHFeedContract.options.address,
73
- abiItem: wstETHFeedContract.options.jsonInterface.find(({ name }) => name === 'decimals'),
74
- params: [],
75
- },
76
- ];
77
- return calls;
78
- };
79
-
80
- export const getWeETHChainLinkPriceCalls = (web3: Web3, network: NetworkNumber) => {
81
- const weETHFeedContract = WeETHPriceFeedContract(web3, network);
82
- const ethFeedContract = ETHPriceFeedContract(web3, network);
83
- const calls = [
84
- {
85
- target: ethFeedContract.options.address,
86
- abiItem: ethFeedContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
87
- params: [],
88
- },
89
- {
90
- target: weETHFeedContract.options.address,
91
- abiItem: weETHFeedContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
92
- params: [],
93
- },
94
- {
95
- target: weETHFeedContract.options.address,
96
- abiItem: weETHFeedContract.options.jsonInterface.find(({ name }) => name === 'decimals'),
97
- params: [],
98
- },
99
- ];
100
- return calls;
101
- };
102
-
103
- export const parseWstETHPriceCalls = (_ethPrice: string, wstETHrate: { answer: string }, decimals: string) => {
104
- const ethPrice = new Dec(_ethPrice).div(1e8);
105
- const wstETHRate = getEthAmountForDecimals(wstETHrate.answer, decimals);
106
- return { ethPrice, wstETHRate };
107
- };
108
-
109
- export const parseWeETHPriceCalls = (_ethPrice: string, weETHrate: { answer: string }, decimals: string) => {
110
- const ethPrice = new Dec(_ethPrice).div(1e8);
111
- const weETHRate = getEthAmountForDecimals(weETHrate.answer, decimals);
112
- return { ethPrice, weETHRate };
113
- };
114
-
115
- // this is a fixed version, the original version is above but requires to refactor comp v3 function, so it's easier to just copy the function for now
116
- export const getWstETHPriceFluid = async (web3: Web3, network: NetworkNumber) => {
117
- const calls = getWstETHChainLinkPriceCalls(web3, network);
118
- const multicallRes = await multicall(calls, web3, network);
119
- const { ethPrice, wstETHRate } = parseWstETHPriceCalls(multicallRes[0][0], multicallRes[1], multicallRes[2][0]);
120
-
121
- return new Dec(ethPrice).mul(wstETHRate).toString();
122
- };
123
-
124
- // chainlink price feed available only on mainnet
125
- export const getChainlinkAssetAddress = (symbol: string, network: NetworkNumber) => {
126
- // Chainlink only has BTC/USD feed so we use that for BTC derivatives
127
- if (['WBTC', 'RENBTC'].includes(symbol?.toUpperCase())) return '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB';
128
- if (symbol?.toUpperCase() === 'WETH') return getAssetInfo('ETH').addresses[network];
129
- return getAssetInfo(symbol).addresses[network];
130
- };
1
+ import Web3 from 'web3';
2
+ import Dec from 'decimal.js';
3
+ import { getAssetInfo } from '@defisaver/tokens';
4
+ import {
5
+ COMPPriceFeedContract,
6
+ ETHPriceFeedContract,
7
+ BTCPriceFeedContract,
8
+ USDCPriceFeedContract, WeETHPriceFeedContract,
9
+ WstETHPriceFeedContract,
10
+ } from '../contracts';
11
+ import { NetworkNumber } from '../types/common';
12
+ import { multicall } from '../multicall';
13
+ import { getEthAmountForDecimals } from './utils';
14
+
15
+ export const getEthPrice = async (web3: Web3) => {
16
+ const contract = ETHPriceFeedContract(web3, NetworkNumber.Eth);
17
+ const price = await contract.methods.latestAnswer().call();
18
+ return new Dec(price).div(1e8).toString();
19
+ };
20
+
21
+ export const getUSDCPrice = async (web3: Web3) => {
22
+ const contract = USDCPriceFeedContract(web3, NetworkNumber.Eth);
23
+ const price = await contract.methods.latestAnswer().call();
24
+ return new Dec(price).div(1e8).toString();
25
+ };
26
+
27
+ export const getCompPrice = async (web3: Web3) => {
28
+ const contract = COMPPriceFeedContract(web3, NetworkNumber.Eth);
29
+ const price = await contract.methods.latestAnswer().call();
30
+ return new Dec(price).div(1e8).toString();
31
+ };
32
+
33
+ export const getWstETHPrice = async (web3: Web3) => {
34
+ const wstETHFeedContract = WstETHPriceFeedContract(web3, NetworkNumber.Eth);
35
+ const ethFeedContract = ETHPriceFeedContract(web3, NetworkNumber.Eth);
36
+ const calls = [
37
+ {
38
+ target: ethFeedContract.options.address,
39
+ abiItem: ethFeedContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
40
+ params: [],
41
+ },
42
+ {
43
+ target: wstETHFeedContract.options.address,
44
+ abiItem: wstETHFeedContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
45
+ params: [],
46
+ },
47
+ ];
48
+
49
+ const multicallRes = await multicall(calls, web3);
50
+
51
+ const ethPrice = new Dec(multicallRes[0][0]).div(1e8);
52
+
53
+ const wstETHRate = new Dec(multicallRes[1].answer).div(1e8);
54
+
55
+ return new Dec(ethPrice).mul(wstETHRate).toString();
56
+ };
57
+
58
+ export const getWstETHChainLinkPriceCalls = (web3: Web3, network: NetworkNumber) => {
59
+ const wstETHFeedContract = WstETHPriceFeedContract(web3, network);
60
+ const ethFeedContract = ETHPriceFeedContract(web3, network);
61
+ const calls = [
62
+ {
63
+ target: ethFeedContract.options.address,
64
+ abiItem: ethFeedContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
65
+ params: [],
66
+ },
67
+ {
68
+ target: wstETHFeedContract.options.address,
69
+ abiItem: wstETHFeedContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
70
+ params: [],
71
+ },
72
+ {
73
+ target: wstETHFeedContract.options.address,
74
+ abiItem: wstETHFeedContract.options.jsonInterface.find(({ name }) => name === 'decimals'),
75
+ params: [],
76
+ },
77
+ ];
78
+ return calls;
79
+ };
80
+
81
+ export const getWeETHChainLinkPriceCalls = (web3: Web3, network: NetworkNumber) => {
82
+ const weETHFeedContract = WeETHPriceFeedContract(web3, network);
83
+ const ethFeedContract = ETHPriceFeedContract(web3, network);
84
+ const calls = [
85
+ {
86
+ target: ethFeedContract.options.address,
87
+ abiItem: ethFeedContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
88
+ params: [],
89
+ },
90
+ {
91
+ target: weETHFeedContract.options.address,
92
+ abiItem: weETHFeedContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
93
+ params: [],
94
+ },
95
+ {
96
+ target: weETHFeedContract.options.address,
97
+ abiItem: weETHFeedContract.options.jsonInterface.find(({ name }) => name === 'decimals'),
98
+ params: [],
99
+ },
100
+ ];
101
+ return calls;
102
+ };
103
+
104
+ export const parseWstETHPriceCalls = (_ethPrice: string, wstETHrate: { answer: string }, decimals: string) => {
105
+ const ethPrice = new Dec(_ethPrice).div(1e8);
106
+ const wstETHRate = getEthAmountForDecimals(wstETHrate.answer, decimals);
107
+ return { ethPrice, wstETHRate };
108
+ };
109
+
110
+ export const parseWeETHPriceCalls = (_ethPrice: string, weETHrate: { answer: string }, decimals: string) => {
111
+ const ethPrice = new Dec(_ethPrice).div(1e8);
112
+ const weETHRate = getEthAmountForDecimals(weETHrate.answer, decimals);
113
+ return { ethPrice, weETHRate };
114
+ };
115
+
116
+ // this is a fixed version, the original version is above but requires to refactor comp v3 function, so it's easier to just copy the function for now
117
+ export const getWstETHPriceFluid = async (web3: Web3, network: NetworkNumber) => {
118
+ const calls = getWstETHChainLinkPriceCalls(web3, network);
119
+ const multicallRes = await multicall(calls, web3, network);
120
+ const { ethPrice, wstETHRate } = parseWstETHPriceCalls(multicallRes[0][0], multicallRes[1], multicallRes[2][0]);
121
+
122
+ return new Dec(ethPrice).mul(wstETHRate).toString();
123
+ };
124
+
125
+ export const getBTCPriceForFluid = async (web3: Web3, network: NetworkNumber) => {
126
+ const contract = BTCPriceFeedContract(web3, network);
127
+ const price = await contract.methods.latestAnswer().call();
128
+ return new Dec(price).div(1e8).toString();
129
+ };
130
+
131
+ export const getEthPriceForFluid = async (web3: Web3, network: NetworkNumber) => {
132
+ const contract = ETHPriceFeedContract(web3, network);
133
+ const price = await contract.methods.latestAnswer().call();
134
+ return new Dec(price).div(1e8).toString();
135
+ };
136
+
137
+ // chainlink price feed available only on mainnet
138
+ export const getChainlinkAssetAddress = (symbol: string, network: NetworkNumber) => {
139
+ // Chainlink only has BTC/USD feed so we use that for BTC derivatives
140
+ if (['WBTC', 'RENBTC'].includes(symbol?.toUpperCase())) return '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB';
141
+ if (symbol?.toUpperCase() === 'WETH') return getAssetInfo('ETH').addresses[network];
142
+ return getAssetInfo(symbol).addresses[network];
143
+ };
@@ -1,59 +1,59 @@
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
-
52
- // eslint-disable-next-line no-bitwise
53
- export const isEnabledOnBitmap = (bitmap: number, assetId: number) => (BigInt(bitmap) >> BigInt(assetId)) & BigInt(1);
54
-
55
- export const MAXUINT:string = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
56
-
57
- export const isMaxuint = (amount: string) => compareAddresses(MAXUINT, amount);
58
-
59
- export const isMainnetNetwork = (network: NetworkNumber) => network === NetworkNumber.Eth;
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
+
52
+ // eslint-disable-next-line no-bitwise
53
+ export const isEnabledOnBitmap = (bitmap: number, assetId: number) => (BigInt(bitmap) >> BigInt(assetId)) & BigInt(1);
54
+
55
+ export const MAXUINT:string = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
56
+
57
+ export const isMaxuint = (amount: string) => compareAddresses(MAXUINT, amount);
58
+
59
+ export const isMainnetNetwork = (network: NetworkNumber) => network === NetworkNumber.Eth;
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
+ });