@metamask/transaction-controller 23.1.0 → 25.0.0

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 (278) hide show
  1. package/CHANGELOG.md +134 -1
  2. package/dist/TransactionController.js +38 -1816
  3. package/dist/TransactionController.js.map +1 -1
  4. package/dist/TransactionController.mjs +39 -0
  5. package/dist/TransactionController.mjs.map +1 -0
  6. package/dist/chunk-2K7J3EY3.mjs +170 -0
  7. package/dist/chunk-2K7J3EY3.mjs.map +1 -0
  8. package/dist/chunk-5XBULBP2.js +399 -0
  9. package/dist/chunk-5XBULBP2.js.map +1 -0
  10. package/dist/chunk-6MYNWYJK.mjs +158 -0
  11. package/dist/chunk-6MYNWYJK.mjs.map +1 -0
  12. package/dist/chunk-7APMBUKB.js +158 -0
  13. package/dist/chunk-7APMBUKB.js.map +1 -0
  14. package/dist/chunk-7LXE4KHV.js +40 -0
  15. package/dist/chunk-7LXE4KHV.js.map +1 -0
  16. package/dist/chunk-7MZ57ILQ.mjs +62 -0
  17. package/dist/chunk-7MZ57ILQ.mjs.map +1 -0
  18. package/dist/chunk-BJEESIBE.js +2313 -0
  19. package/dist/chunk-BJEESIBE.js.map +1 -0
  20. package/dist/chunk-C67QD5PV.mjs +320 -0
  21. package/dist/chunk-C67QD5PV.mjs.map +1 -0
  22. package/dist/chunk-DE3MZYVY.mjs +2313 -0
  23. package/dist/chunk-DE3MZYVY.mjs.map +1 -0
  24. package/dist/chunk-DEKM6PVG.mjs +46 -0
  25. package/dist/chunk-DEKM6PVG.mjs.map +1 -0
  26. package/dist/chunk-DQP6X25N.mjs +208 -0
  27. package/dist/chunk-DQP6X25N.mjs.map +1 -0
  28. package/dist/chunk-DTDTOMTB.js +238 -0
  29. package/dist/chunk-DTDTOMTB.js.map +1 -0
  30. package/dist/chunk-DTEDYRTL.js +242 -0
  31. package/dist/chunk-DTEDYRTL.js.map +1 -0
  32. package/dist/chunk-EQ3RRHB7.mjs +211 -0
  33. package/dist/chunk-EQ3RRHB7.mjs.map +1 -0
  34. package/dist/chunk-FDJPXQTF.js +46 -0
  35. package/dist/chunk-FDJPXQTF.js.map +1 -0
  36. package/dist/chunk-FRKQ3Z2L.mjs +40 -0
  37. package/dist/chunk-FRKQ3Z2L.mjs.map +1 -0
  38. package/dist/chunk-FS7FRO7B.mjs +90 -0
  39. package/dist/chunk-FS7FRO7B.mjs.map +1 -0
  40. package/dist/chunk-GKTIFXPN.js +170 -0
  41. package/dist/chunk-GKTIFXPN.js.map +1 -0
  42. package/dist/chunk-H4M66BA3.js +62 -0
  43. package/dist/chunk-H4M66BA3.js.map +1 -0
  44. package/dist/chunk-HPNXIKFY.js +76 -0
  45. package/dist/chunk-HPNXIKFY.js.map +1 -0
  46. package/dist/chunk-HS277C77.js +75 -0
  47. package/dist/chunk-HS277C77.js.map +1 -0
  48. package/dist/chunk-I5YZ7QUK.js +121 -0
  49. package/dist/chunk-I5YZ7QUK.js.map +1 -0
  50. package/dist/chunk-IC233ZQS.js +211 -0
  51. package/dist/chunk-IC233ZQS.js.map +1 -0
  52. package/dist/chunk-IUBAETUH.js +137 -0
  53. package/dist/chunk-IUBAETUH.js.map +1 -0
  54. package/dist/chunk-J56A7UCK.mjs +123 -0
  55. package/dist/chunk-J56A7UCK.mjs.map +1 -0
  56. package/dist/chunk-JR6HDRNV.mjs +242 -0
  57. package/dist/chunk-JR6HDRNV.mjs.map +1 -0
  58. package/dist/chunk-JRBREX22.mjs +75 -0
  59. package/dist/chunk-JRBREX22.mjs.map +1 -0
  60. package/dist/chunk-JRQHIBG5.mjs +399 -0
  61. package/dist/chunk-JRQHIBG5.mjs.map +1 -0
  62. package/dist/chunk-LM4NUNMT.mjs +76 -0
  63. package/dist/chunk-LM4NUNMT.mjs.map +1 -0
  64. package/dist/chunk-M7455RU7.js +320 -0
  65. package/dist/chunk-M7455RU7.js.map +1 -0
  66. package/dist/chunk-MHM5LRRF.mjs +122 -0
  67. package/dist/chunk-MHM5LRRF.mjs.map +1 -0
  68. package/dist/chunk-NHRBO3LU.mjs +50 -0
  69. package/dist/chunk-NHRBO3LU.mjs.map +1 -0
  70. package/dist/chunk-NM6OYEPP.mjs +182 -0
  71. package/dist/chunk-NM6OYEPP.mjs.map +1 -0
  72. package/dist/chunk-NUOBUW7C.js +85 -0
  73. package/dist/chunk-NUOBUW7C.js.map +1 -0
  74. package/dist/chunk-QP75SWIQ.js +53 -0
  75. package/dist/chunk-QP75SWIQ.js.map +1 -0
  76. package/dist/chunk-RI6MVJJN.js +122 -0
  77. package/dist/chunk-RI6MVJJN.js.map +1 -0
  78. package/dist/chunk-S6VGOPUY.js +14 -0
  79. package/dist/chunk-S6VGOPUY.js.map +1 -0
  80. package/dist/chunk-UGFBA4GV.js +123 -0
  81. package/dist/chunk-UGFBA4GV.js.map +1 -0
  82. package/dist/chunk-UKYY2RVS.mjs +137 -0
  83. package/dist/chunk-UKYY2RVS.mjs.map +1 -0
  84. package/dist/chunk-UM4ORJ5B.mjs +121 -0
  85. package/dist/chunk-UM4ORJ5B.mjs.map +1 -0
  86. package/dist/chunk-UQQWZT6C.mjs +14 -0
  87. package/dist/chunk-UQQWZT6C.mjs.map +1 -0
  88. package/dist/chunk-VH47Q6TS.js +182 -0
  89. package/dist/chunk-VH47Q6TS.js.map +1 -0
  90. package/dist/chunk-XGRAHX6T.mjs +53 -0
  91. package/dist/chunk-XGRAHX6T.mjs.map +1 -0
  92. package/dist/chunk-XUI43LEZ.mjs +30 -0
  93. package/dist/chunk-XUI43LEZ.mjs.map +1 -0
  94. package/dist/chunk-Y734U4V6.mjs +85 -0
  95. package/dist/chunk-Y734U4V6.mjs.map +1 -0
  96. package/dist/chunk-Y7ENNK7L.mjs +238 -0
  97. package/dist/chunk-Y7ENNK7L.mjs.map +1 -0
  98. package/dist/chunk-Z4BLTVTB.js +30 -0
  99. package/dist/chunk-Z4BLTVTB.js.map +1 -0
  100. package/dist/chunk-ZCQRDZ36.js +208 -0
  101. package/dist/chunk-ZCQRDZ36.js.map +1 -0
  102. package/dist/chunk-ZJLZSFOZ.js +90 -0
  103. package/dist/chunk-ZJLZSFOZ.js.map +1 -0
  104. package/dist/chunk-ZNZEJDOE.js +50 -0
  105. package/dist/chunk-ZNZEJDOE.js.map +1 -0
  106. package/dist/constants.js +15 -110
  107. package/dist/constants.js.map +1 -1
  108. package/dist/constants.mjs +16 -0
  109. package/dist/constants.mjs.map +1 -0
  110. package/dist/gas-flows/DefaultGasFeeFlow.js +14 -77
  111. package/dist/gas-flows/DefaultGasFeeFlow.js.map +1 -1
  112. package/dist/gas-flows/DefaultGasFeeFlow.mjs +15 -0
  113. package/dist/gas-flows/DefaultGasFeeFlow.mjs.map +1 -0
  114. package/dist/gas-flows/LineaGasFeeFlow.js +15 -110
  115. package/dist/gas-flows/LineaGasFeeFlow.js.map +1 -1
  116. package/dist/gas-flows/LineaGasFeeFlow.mjs +16 -0
  117. package/dist/gas-flows/LineaGasFeeFlow.mjs.map +1 -0
  118. package/dist/helpers/EtherscanRemoteTransactionSource.js +11 -145
  119. package/dist/helpers/EtherscanRemoteTransactionSource.js.map +1 -1
  120. package/dist/helpers/EtherscanRemoteTransactionSource.mjs +12 -0
  121. package/dist/helpers/EtherscanRemoteTransactionSource.mjs.map +1 -0
  122. package/dist/helpers/GasFeePoller.js +10 -143
  123. package/dist/helpers/GasFeePoller.js.map +1 -1
  124. package/dist/helpers/GasFeePoller.mjs +11 -0
  125. package/dist/helpers/GasFeePoller.mjs.map +1 -0
  126. package/dist/helpers/IncomingTransactionHelper.js +8 -205
  127. package/dist/helpers/IncomingTransactionHelper.js.map +1 -1
  128. package/dist/helpers/IncomingTransactionHelper.mjs +9 -0
  129. package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -0
  130. package/dist/helpers/MultichainTrackingHelper.js +12 -291
  131. package/dist/helpers/MultichainTrackingHelper.js.map +1 -1
  132. package/dist/helpers/MultichainTrackingHelper.mjs +13 -0
  133. package/dist/helpers/MultichainTrackingHelper.mjs.map +1 -0
  134. package/dist/helpers/PendingTransactionTracker.js +9 -360
  135. package/dist/helpers/PendingTransactionTracker.js.map +1 -1
  136. package/dist/helpers/PendingTransactionTracker.mjs +10 -0
  137. package/dist/helpers/PendingTransactionTracker.mjs.map +1 -0
  138. package/dist/index.js +56 -25
  139. package/dist/index.js.map +1 -1
  140. package/dist/index.mjs +57 -0
  141. package/dist/index.mjs.map +1 -0
  142. package/dist/logger.js +11 -8
  143. package/dist/logger.js.map +1 -1
  144. package/dist/logger.mjs +12 -0
  145. package/dist/logger.mjs.map +1 -0
  146. package/dist/tsconfig.build.tsbuildinfo +1 -0
  147. package/dist/{TransactionController.d.ts → types/TransactionController.d.ts} +235 -46
  148. package/dist/types/TransactionController.d.ts.map +1 -0
  149. package/dist/{constants.d.ts → types/constants.d.ts} +8 -3
  150. package/dist/types/constants.d.ts.map +1 -0
  151. package/dist/types/gas-flows/DefaultGasFeeFlow.d.ts.map +1 -0
  152. package/dist/types/gas-flows/LineaGasFeeFlow.d.ts.map +1 -0
  153. package/dist/types/helpers/EtherscanRemoteTransactionSource.d.ts.map +1 -0
  154. package/dist/types/helpers/GasFeePoller.d.ts.map +1 -0
  155. package/dist/types/helpers/IncomingTransactionHelper.d.ts.map +1 -0
  156. package/dist/types/helpers/MultichainTrackingHelper.d.ts.map +1 -0
  157. package/dist/types/helpers/PendingTransactionTracker.d.ts.map +1 -0
  158. package/dist/types/index.d.ts +9 -0
  159. package/dist/types/index.d.ts.map +1 -0
  160. package/dist/types/logger.d.ts.map +1 -0
  161. package/dist/{types.d.ts → types/types.d.ts} +63 -65
  162. package/dist/types/types.d.ts.map +1 -0
  163. package/dist/types/utils/etherscan.d.ts.map +1 -0
  164. package/dist/types/utils/external-transactions.d.ts.map +1 -0
  165. package/dist/types/utils/gas-fees.d.ts.map +1 -0
  166. package/dist/types/utils/gas-flow.d.ts.map +1 -0
  167. package/dist/types/utils/gas.d.ts.map +1 -0
  168. package/dist/types/utils/history.d.ts +20 -0
  169. package/dist/types/utils/history.d.ts.map +1 -0
  170. package/dist/types/utils/nonce.d.ts.map +1 -0
  171. package/dist/types/utils/simulation-api.d.ts +99 -0
  172. package/dist/types/utils/simulation-api.d.ts.map +1 -0
  173. package/dist/types/utils/simulation.d.ts +21 -0
  174. package/dist/types/utils/simulation.d.ts.map +1 -0
  175. package/dist/{utils → types/utils}/swaps.d.ts +8 -5
  176. package/dist/types/utils/swaps.d.ts.map +1 -0
  177. package/dist/types/utils/transaction-type.d.ts.map +1 -0
  178. package/dist/{utils → types/utils}/utils.d.ts +9 -1
  179. package/dist/types/utils/utils.d.ts.map +1 -0
  180. package/dist/types/utils/validation.d.ts.map +1 -0
  181. package/dist/types.js +19 -170
  182. package/dist/types.js.map +1 -1
  183. package/dist/types.mjs +20 -0
  184. package/dist/types.mjs.map +1 -0
  185. package/dist/utils/etherscan.js +13 -118
  186. package/dist/utils/etherscan.js.map +1 -1
  187. package/dist/utils/etherscan.mjs +14 -0
  188. package/dist/utils/etherscan.mjs.map +1 -0
  189. package/dist/utils/external-transactions.js +8 -35
  190. package/dist/utils/external-transactions.js.map +1 -1
  191. package/dist/utils/external-transactions.mjs +9 -0
  192. package/dist/utils/external-transactions.mjs.map +1 -0
  193. package/dist/utils/gas-fees.js +15 -200
  194. package/dist/utils/gas-fees.js.map +1 -1
  195. package/dist/utils/gas-fees.mjs +16 -0
  196. package/dist/utils/gas-fees.mjs.map +1 -0
  197. package/dist/utils/gas-flow.js +10 -52
  198. package/dist/utils/gas-flow.js.map +1 -1
  199. package/dist/utils/gas-flow.mjs +11 -0
  200. package/dist/utils/gas-flow.mjs.map +1 -0
  201. package/dist/utils/gas.js +19 -133
  202. package/dist/utils/gas.js.map +1 -1
  203. package/dist/utils/gas.mjs +20 -0
  204. package/dist/utils/gas.mjs.map +1 -0
  205. package/dist/utils/history.js +9 -83
  206. package/dist/utils/history.js.map +1 -1
  207. package/dist/utils/history.mjs +10 -0
  208. package/dist/utils/history.mjs.map +1 -0
  209. package/dist/utils/nonce.js +10 -76
  210. package/dist/utils/nonce.js.map +1 -1
  211. package/dist/utils/nonce.mjs +11 -0
  212. package/dist/utils/nonce.mjs.map +1 -0
  213. package/dist/utils/simulation-api.js +10 -0
  214. package/dist/utils/simulation-api.js.map +1 -0
  215. package/dist/utils/simulation-api.mjs +10 -0
  216. package/dist/utils/simulation-api.mjs.map +1 -0
  217. package/dist/utils/simulation.js +12 -0
  218. package/dist/utils/simulation.js.map +1 -0
  219. package/dist/utils/simulation.mjs +12 -0
  220. package/dist/utils/simulation.mjs.map +1 -0
  221. package/dist/utils/swaps.js +23 -256
  222. package/dist/utils/swaps.js.map +1 -1
  223. package/dist/utils/swaps.mjs +24 -0
  224. package/dist/utils/swaps.mjs.map +1 -0
  225. package/dist/utils/transaction-type.js +10 -120
  226. package/dist/utils/transaction-type.js.map +1 -1
  227. package/dist/utils/transaction-type.mjs +11 -0
  228. package/dist/utils/transaction-type.mjs.map +1 -0
  229. package/dist/utils/utils.js +32 -146
  230. package/dist/utils/utils.js.map +1 -1
  231. package/dist/utils/utils.mjs +33 -0
  232. package/dist/utils/utils.mjs.map +1 -0
  233. package/dist/utils/validation.js +11 -258
  234. package/dist/utils/validation.js.map +1 -1
  235. package/dist/utils/validation.mjs +12 -0
  236. package/dist/utils/validation.mjs.map +1 -0
  237. package/package.json +21 -9
  238. package/dist/TransactionController.d.ts.map +0 -1
  239. package/dist/constants.d.ts.map +0 -1
  240. package/dist/gas-flows/DefaultGasFeeFlow.d.ts.map +0 -1
  241. package/dist/gas-flows/LineaGasFeeFlow.d.ts.map +0 -1
  242. package/dist/helpers/EtherscanRemoteTransactionSource.d.ts.map +0 -1
  243. package/dist/helpers/GasFeePoller.d.ts.map +0 -1
  244. package/dist/helpers/IncomingTransactionHelper.d.ts.map +0 -1
  245. package/dist/helpers/MultichainTrackingHelper.d.ts.map +0 -1
  246. package/dist/helpers/PendingTransactionTracker.d.ts.map +0 -1
  247. package/dist/index.d.ts +0 -7
  248. package/dist/index.d.ts.map +0 -1
  249. package/dist/logger.d.ts.map +0 -1
  250. package/dist/types.d.ts.map +0 -1
  251. package/dist/utils/etherscan.d.ts.map +0 -1
  252. package/dist/utils/external-transactions.d.ts.map +0 -1
  253. package/dist/utils/gas-fees.d.ts.map +0 -1
  254. package/dist/utils/gas-flow.d.ts.map +0 -1
  255. package/dist/utils/gas.d.ts.map +0 -1
  256. package/dist/utils/history.d.ts +0 -15
  257. package/dist/utils/history.d.ts.map +0 -1
  258. package/dist/utils/nonce.d.ts.map +0 -1
  259. package/dist/utils/swaps.d.ts.map +0 -1
  260. package/dist/utils/transaction-type.d.ts.map +0 -1
  261. package/dist/utils/utils.d.ts.map +0 -1
  262. package/dist/utils/validation.d.ts.map +0 -1
  263. /package/dist/{gas-flows → types/gas-flows}/DefaultGasFeeFlow.d.ts +0 -0
  264. /package/dist/{gas-flows → types/gas-flows}/LineaGasFeeFlow.d.ts +0 -0
  265. /package/dist/{helpers → types/helpers}/EtherscanRemoteTransactionSource.d.ts +0 -0
  266. /package/dist/{helpers → types/helpers}/GasFeePoller.d.ts +0 -0
  267. /package/dist/{helpers → types/helpers}/IncomingTransactionHelper.d.ts +0 -0
  268. /package/dist/{helpers → types/helpers}/MultichainTrackingHelper.d.ts +0 -0
  269. /package/dist/{helpers → types/helpers}/PendingTransactionTracker.d.ts +0 -0
  270. /package/dist/{logger.d.ts → types/logger.d.ts} +0 -0
  271. /package/dist/{utils → types/utils}/etherscan.d.ts +0 -0
  272. /package/dist/{utils → types/utils}/external-transactions.d.ts +0 -0
  273. /package/dist/{utils → types/utils}/gas-fees.d.ts +0 -0
  274. /package/dist/{utils → types/utils}/gas-flow.d.ts +0 -0
  275. /package/dist/{utils → types/utils}/gas.d.ts +0 -0
  276. /package/dist/{utils → types/utils}/nonce.d.ts +0 -0
  277. /package/dist/{utils → types/utils}/transaction-type.d.ts +0 -0
  278. /package/dist/{utils → types/utils}/validation.d.ts +0 -0
@@ -1,201 +1,16 @@
1
- "use strict";
2
- /* eslint-disable jsdoc/require-jsdoc */
3
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
- return new (P || (P = Promise))(function (resolve, reject) {
6
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
- step((generator = generator.apply(thisArg, _arguments || [])).next());
10
- });
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.gweiDecimalToWeiHex = exports.updateGasFees = void 0;
14
- const controller_utils_1 = require("@metamask/controller-utils");
15
- const utils_1 = require("@metamask/utils");
16
- const logger_1 = require("../logger");
17
- const types_1 = require("../types");
18
- const gas_flow_1 = require("./gas-flow");
19
- const swaps_1 = require("./swaps");
20
- const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'gas-fees');
21
- function updateGasFees(request) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const { txMeta } = request;
24
- const initialParams = Object.assign({}, txMeta.txParams);
25
- const isSwap = swaps_1.SWAP_TRANSACTION_TYPES.includes(txMeta.type);
26
- const savedGasFees = isSwap
27
- ? undefined
28
- : request.getSavedGasFees(txMeta.chainId);
29
- const suggestedGasFees = yield getSuggestedGasFees(request);
30
- log('Suggested gas fees', suggestedGasFees);
31
- const getGasFeeRequest = Object.assign(Object.assign({}, request), { initialParams,
32
- savedGasFees,
33
- suggestedGasFees });
34
- txMeta.txParams.maxFeePerGas = getMaxFeePerGas(getGasFeeRequest);
35
- txMeta.txParams.maxPriorityFeePerGas =
36
- getMaxPriorityFeePerGas(getGasFeeRequest);
37
- txMeta.txParams.gasPrice = getGasPrice(getGasFeeRequest);
38
- txMeta.userFeeLevel = getUserFeeLevel(getGasFeeRequest);
39
- log('Updated gas fee properties', {
40
- maxFeePerGas: txMeta.txParams.maxFeePerGas,
41
- maxPriorityFeePerGas: txMeta.txParams.maxPriorityFeePerGas,
42
- gasPrice: txMeta.txParams.gasPrice,
43
- });
44
- if (txMeta.txParams.maxFeePerGas || txMeta.txParams.maxPriorityFeePerGas) {
45
- delete txMeta.txParams.gasPrice;
46
- }
47
- if (txMeta.txParams.gasPrice) {
48
- delete txMeta.txParams.maxFeePerGas;
49
- delete txMeta.txParams.maxPriorityFeePerGas;
50
- }
51
- updateDefaultGasEstimates(txMeta);
52
- });
53
- }
54
- exports.updateGasFees = updateGasFees;
55
- function gweiDecimalToWeiHex(value) {
56
- return (0, controller_utils_1.toHex)((0, controller_utils_1.gweiDecToWEIBN)(value));
57
- }
58
- exports.gweiDecimalToWeiHex = gweiDecimalToWeiHex;
59
- function getMaxFeePerGas(request) {
60
- const { savedGasFees, eip1559, initialParams, suggestedGasFees } = request;
61
- if (!eip1559) {
62
- return undefined;
63
- }
64
- if (savedGasFees) {
65
- const maxFeePerGas = gweiDecimalToWeiHex(savedGasFees.maxBaseFee);
66
- log('Using maxFeePerGas from savedGasFees', maxFeePerGas);
67
- return maxFeePerGas;
68
- }
69
- if (initialParams.maxFeePerGas) {
70
- log('Using maxFeePerGas from request', initialParams.maxFeePerGas);
71
- return initialParams.maxFeePerGas;
72
- }
73
- if (initialParams.gasPrice && !initialParams.maxPriorityFeePerGas) {
74
- log('Setting maxFeePerGas to gasPrice from request', initialParams.gasPrice);
75
- return initialParams.gasPrice;
76
- }
77
- if (suggestedGasFees.maxFeePerGas) {
78
- log('Using suggested maxFeePerGas', suggestedGasFees.maxFeePerGas);
79
- return suggestedGasFees.maxFeePerGas;
80
- }
81
- if (suggestedGasFees.gasPrice) {
82
- log('Setting maxFeePerGas to suggested gasPrice', suggestedGasFees.gasPrice);
83
- return suggestedGasFees.gasPrice;
84
- }
85
- log('maxFeePerGas not set');
86
- return undefined;
87
- }
88
- function getMaxPriorityFeePerGas(request) {
89
- const { eip1559, initialParams, savedGasFees, suggestedGasFees, txMeta } = request;
90
- if (!eip1559) {
91
- return undefined;
92
- }
93
- if (savedGasFees) {
94
- const maxPriorityFeePerGas = gweiDecimalToWeiHex(savedGasFees.priorityFee);
95
- log('Using maxPriorityFeePerGas from savedGasFees.priorityFee', maxPriorityFeePerGas);
96
- return maxPriorityFeePerGas;
97
- }
98
- if (initialParams.maxPriorityFeePerGas) {
99
- log('Using maxPriorityFeePerGas from request', initialParams.maxPriorityFeePerGas);
100
- return initialParams.maxPriorityFeePerGas;
101
- }
102
- if (initialParams.gasPrice && !initialParams.maxFeePerGas) {
103
- log('Setting maxPriorityFeePerGas to gasPrice from request', initialParams.gasPrice);
104
- return initialParams.gasPrice;
105
- }
106
- if (suggestedGasFees.maxPriorityFeePerGas) {
107
- log('Using suggested maxPriorityFeePerGas', suggestedGasFees.maxPriorityFeePerGas);
108
- return suggestedGasFees.maxPriorityFeePerGas;
109
- }
110
- if (txMeta.txParams.maxFeePerGas) {
111
- log('Setting maxPriorityFeePerGas to maxFeePerGas', txMeta.txParams.maxFeePerGas);
112
- return txMeta.txParams.maxFeePerGas;
113
- }
114
- log('maxPriorityFeePerGas not set');
115
- return undefined;
116
- }
117
- function getGasPrice(request) {
118
- const { eip1559, initialParams, suggestedGasFees } = request;
119
- if (eip1559) {
120
- return undefined;
121
- }
122
- if (initialParams.gasPrice) {
123
- log('Using gasPrice from request', initialParams.gasPrice);
124
- return initialParams.gasPrice;
125
- }
126
- if (suggestedGasFees.maxFeePerGas) {
127
- log('Using suggested maxFeePerGas', suggestedGasFees.maxFeePerGas);
128
- return suggestedGasFees.maxFeePerGas;
129
- }
130
- if (suggestedGasFees.gasPrice) {
131
- log('Using suggested gasPrice', suggestedGasFees.gasPrice);
132
- return suggestedGasFees.gasPrice;
133
- }
134
- log('gasPrice not set');
135
- return undefined;
136
- }
137
- function getUserFeeLevel(request) {
138
- const { eip1559, initialParams, savedGasFees, suggestedGasFees, txMeta } = request;
139
- if (!eip1559) {
140
- return undefined;
141
- }
142
- if (savedGasFees) {
143
- return types_1.UserFeeLevel.CUSTOM;
144
- }
145
- if (!initialParams.maxFeePerGas &&
146
- !initialParams.maxPriorityFeePerGas &&
147
- initialParams.gasPrice) {
148
- return txMeta.origin === controller_utils_1.ORIGIN_METAMASK
149
- ? types_1.UserFeeLevel.CUSTOM
150
- : types_1.UserFeeLevel.DAPP_SUGGESTED;
151
- }
152
- if (!initialParams.maxFeePerGas &&
153
- !initialParams.maxPriorityFeePerGas &&
154
- suggestedGasFees.maxFeePerGas &&
155
- suggestedGasFees.maxPriorityFeePerGas) {
156
- return types_1.UserFeeLevel.MEDIUM;
157
- }
158
- if (txMeta.origin === controller_utils_1.ORIGIN_METAMASK) {
159
- return types_1.UserFeeLevel.MEDIUM;
160
- }
161
- return types_1.UserFeeLevel.DAPP_SUGGESTED;
162
- }
163
- function updateDefaultGasEstimates(txMeta) {
164
- if (!txMeta.defaultGasEstimates) {
165
- txMeta.defaultGasEstimates = {};
166
- }
167
- txMeta.defaultGasEstimates.maxFeePerGas = txMeta.txParams.maxFeePerGas;
168
- txMeta.defaultGasEstimates.maxPriorityFeePerGas =
169
- txMeta.txParams.maxPriorityFeePerGas;
170
- txMeta.defaultGasEstimates.gasPrice = txMeta.txParams.gasPrice;
171
- txMeta.defaultGasEstimates.estimateType = txMeta.userFeeLevel;
172
- }
173
- function getSuggestedGasFees(request) {
174
- return __awaiter(this, void 0, void 0, function* () {
175
- const { eip1559, ethQuery, gasFeeFlows, getGasFeeEstimates, txMeta } = request;
176
- if ((!eip1559 && txMeta.txParams.gasPrice) ||
177
- (eip1559 &&
178
- txMeta.txParams.maxFeePerGas &&
179
- txMeta.txParams.maxPriorityFeePerGas)) {
180
- return {};
181
- }
182
- const gasFeeFlow = (0, gas_flow_1.getGasFeeFlow)(txMeta, gasFeeFlows);
183
- try {
184
- const response = yield gasFeeFlow.getGasFees({
185
- ethQuery,
186
- getGasFeeControllerEstimates: getGasFeeEstimates,
187
- transactionMeta: txMeta,
188
- });
189
- return response.estimates.medium;
190
- }
191
- catch (error) {
192
- log('Failed to get suggested gas fees', error);
193
- }
194
- const gasPriceDecimal = (yield (0, controller_utils_1.query)(ethQuery, 'gasPrice'));
195
- const gasPrice = gasPriceDecimal
196
- ? (0, utils_1.add0x)(gasPriceDecimal.toString(16))
197
- : undefined;
198
- return { gasPrice };
199
- });
200
- }
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+ var _chunkZCQRDZ36js = require('../chunk-ZCQRDZ36.js');
5
+ require('../chunk-IC233ZQS.js');
6
+ require('../chunk-UGFBA4GV.js');
7
+ require('../chunk-H4M66BA3.js');
8
+ require('../chunk-RI6MVJJN.js');
9
+ require('../chunk-S6VGOPUY.js');
10
+ require('../chunk-HPNXIKFY.js');
11
+ require('../chunk-Z4BLTVTB.js');
12
+
13
+
14
+
15
+ exports.gweiDecimalToWeiHex = _chunkZCQRDZ36js.gweiDecimalToWeiHex; exports.updateGasFees = _chunkZCQRDZ36js.updateGasFees;
201
16
  //# sourceMappingURL=gas-fees.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas-fees.js","sourceRoot":"","sources":["../../src/utils/gas-fees.ts"],"names":[],"mappings":";AAAA,wCAAwC;;;;;;;;;;;;AAExC,iEAKoC;AAOpC,2CAA4D;AAE5D,sCAA0C;AAQ1C,oCAAwC;AACxC,yCAA2C;AAC3C,mCAAiD;AAyBjD,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,UAAU,CAAC,CAAC;AAE1D,SAAsB,aAAa,CAAC,OAA6B;;QAC/D,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,MAAM,aAAa,qBAAQ,MAAM,CAAC,QAAQ,CAAE,CAAC;QAE7C,MAAM,MAAM,GAAG,8BAAsB,CAAC,QAAQ,CAC5C,MAAM,CAAC,IAAuB,CAC/B,CAAC;QACF,MAAM,YAAY,GAAG,MAAM;YACzB,CAAC,CAAC,SAAS;YACX,CAAC,CAAC,OAAO,CAAC,eA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,GAAG,eAAe;YAC9B,CAAC,CAAC,IAAA,aAAK,EAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;YACrC,CAAC,CAAC,SAAS,CAAC;QAEd,OAAO,EAAE,QAAQ,EAAE,CAAC;IACtB,CAAC;CAAA","sourcesContent":["/* eslint-disable jsdoc/require-jsdoc */\n\nimport {\n ORIGIN_METAMASK,\n gweiDecToWEIBN,\n query,\n toHex,\n} from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport type {\n FetchGasFeeEstimateOptions,\n GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, createModuleLogger } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type {\n SavedGasFees,\n TransactionParams,\n TransactionMeta,\n TransactionType,\n GasFeeFlow,\n} from '../types';\nimport { UserFeeLevel } from '../types';\nimport { getGasFeeFlow } from './gas-flow';\nimport { SWAP_TRANSACTION_TYPES } from './swaps';\n\nexport type UpdateGasFeesRequest = {\n eip1559: boolean;\n ethQuery: EthQuery;\n gasFeeFlows: GasFeeFlow[];\n getGasFeeEstimates: (\n options: FetchGasFeeEstimateOptions,\n ) => Promise<GasFeeState>;\n getSavedGasFees: (chainId: Hex) => SavedGasFees | undefined;\n txMeta: TransactionMeta;\n};\n\nexport type GetGasFeeRequest = UpdateGasFeesRequest & {\n initialParams: TransactionParams;\n savedGasFees?: SavedGasFees;\n suggestedGasFees: SuggestedGasFees;\n};\n\ntype SuggestedGasFees = {\n maxFeePerGas?: string;\n maxPriorityFeePerGas?: string;\n gasPrice?: string;\n};\n\nconst log = createModuleLogger(projectLogger, 'gas-fees');\n\nexport async function updateGasFees(request: UpdateGasFeesRequest) {\n const { txMeta } = request;\n const initialParams = { ...txMeta.txParams };\n\n const isSwap = SWAP_TRANSACTION_TYPES.includes(\n txMeta.type as TransactionType,\n );\n const savedGasFees = isSwap\n ? undefined\n : request.getSavedGasFees(txMeta.chainId);\n\n const suggestedGasFees = await getSuggestedGasFees(request);\n\n log('Suggested gas fees', suggestedGasFees);\n\n const getGasFeeRequest: GetGasFeeRequest = {\n ...request,\n initialParams,\n savedGasFees,\n suggestedGasFees,\n };\n\n txMeta.txParams.maxFeePerGas = getMaxFeePerGas(getGasFeeRequest);\n\n txMeta.txParams.maxPriorityFeePerGas =\n getMaxPriorityFeePerGas(getGasFeeRequest);\n\n txMeta.txParams.gasPrice = getGasPrice(getGasFeeRequest);\n txMeta.userFeeLevel = getUserFeeLevel(getGasFeeRequest);\n\n log('Updated gas fee properties', {\n maxFeePerGas: txMeta.txParams.maxFeePerGas,\n maxPriorityFeePerGas: txMeta.txParams.maxPriorityFeePerGas,\n gasPrice: txMeta.txParams.gasPrice,\n });\n\n if (txMeta.txParams.maxFeePerGas || txMeta.txParams.maxPriorityFeePerGas) {\n delete txMeta.txParams.gasPrice;\n }\n\n if (txMeta.txParams.gasPrice) {\n delete txMeta.txParams.maxFeePerGas;\n delete txMeta.txParams.maxPriorityFeePerGas;\n }\n\n updateDefaultGasEstimates(txMeta);\n}\n\nexport function gweiDecimalToWeiHex(value: string) {\n return toHex(gweiDecToWEIBN(value));\n}\n\nfunction getMaxFeePerGas(request: GetGasFeeRequest): string | undefined {\n const { savedGasFees, eip1559, initialParams, suggestedGasFees } = request;\n\n if (!eip1559) {\n return undefined;\n }\n\n if (savedGasFees) {\n const maxFeePerGas = gweiDecimalToWeiHex(savedGasFees.maxBaseFee as string);\n log('Using maxFeePerGas from savedGasFees', maxFeePerGas);\n return maxFeePerGas;\n }\n\n if (initialParams.maxFeePerGas) {\n log('Using maxFeePerGas from request', initialParams.maxFeePerGas);\n return initialParams.maxFeePerGas;\n }\n\n if (initialParams.gasPrice && !initialParams.maxPriorityFeePerGas) {\n log(\n 'Setting maxFeePerGas to gasPrice from request',\n initialParams.gasPrice,\n );\n return initialParams.gasPrice;\n }\n\n if (suggestedGasFees.maxFeePerGas) {\n log('Using suggested maxFeePerGas', suggestedGasFees.maxFeePerGas);\n return suggestedGasFees.maxFeePerGas;\n }\n\n if (suggestedGasFees.gasPrice) {\n log(\n 'Setting maxFeePerGas to suggested gasPrice',\n suggestedGasFees.gasPrice,\n );\n return suggestedGasFees.gasPrice;\n }\n\n log('maxFeePerGas not set');\n return undefined;\n}\n\nfunction getMaxPriorityFeePerGas(\n request: GetGasFeeRequest,\n): string | undefined {\n const { eip1559, initialParams, savedGasFees, suggestedGasFees, txMeta } =\n request;\n\n if (!eip1559) {\n return undefined;\n }\n\n if (savedGasFees) {\n const maxPriorityFeePerGas = gweiDecimalToWeiHex(savedGasFees.priorityFee);\n log(\n 'Using maxPriorityFeePerGas from savedGasFees.priorityFee',\n maxPriorityFeePerGas,\n );\n return maxPriorityFeePerGas;\n }\n\n if (initialParams.maxPriorityFeePerGas) {\n log(\n 'Using maxPriorityFeePerGas from request',\n initialParams.maxPriorityFeePerGas,\n );\n return initialParams.maxPriorityFeePerGas;\n }\n\n if (initialParams.gasPrice && !initialParams.maxFeePerGas) {\n log(\n 'Setting maxPriorityFeePerGas to gasPrice from request',\n initialParams.gasPrice,\n );\n return initialParams.gasPrice;\n }\n\n if (suggestedGasFees.maxPriorityFeePerGas) {\n log(\n 'Using suggested maxPriorityFeePerGas',\n suggestedGasFees.maxPriorityFeePerGas,\n );\n return suggestedGasFees.maxPriorityFeePerGas;\n }\n\n if (txMeta.txParams.maxFeePerGas) {\n log(\n 'Setting maxPriorityFeePerGas to maxFeePerGas',\n txMeta.txParams.maxFeePerGas,\n );\n return txMeta.txParams.maxFeePerGas;\n }\n\n log('maxPriorityFeePerGas not set');\n return undefined;\n}\n\nfunction getGasPrice(request: GetGasFeeRequest): string | undefined {\n const { eip1559, initialParams, suggestedGasFees } = request;\n\n if (eip1559) {\n return undefined;\n }\n\n if (initialParams.gasPrice) {\n log('Using gasPrice from request', initialParams.gasPrice);\n return initialParams.gasPrice;\n }\n\n if (suggestedGasFees.maxFeePerGas) {\n log('Using suggested maxFeePerGas', suggestedGasFees.maxFeePerGas);\n return suggestedGasFees.maxFeePerGas;\n }\n\n if (suggestedGasFees.gasPrice) {\n log('Using suggested gasPrice', suggestedGasFees.gasPrice);\n return suggestedGasFees.gasPrice;\n }\n\n log('gasPrice not set');\n return undefined;\n}\n\nfunction getUserFeeLevel(request: GetGasFeeRequest): UserFeeLevel | undefined {\n const { eip1559, initialParams, savedGasFees, suggestedGasFees, txMeta } =\n request;\n\n if (!eip1559) {\n return undefined;\n }\n\n if (savedGasFees) {\n return UserFeeLevel.CUSTOM;\n }\n\n if (\n !initialParams.maxFeePerGas &&\n !initialParams.maxPriorityFeePerGas &&\n initialParams.gasPrice\n ) {\n return txMeta.origin === ORIGIN_METAMASK\n ? UserFeeLevel.CUSTOM\n : UserFeeLevel.DAPP_SUGGESTED;\n }\n\n if (\n !initialParams.maxFeePerGas &&\n !initialParams.maxPriorityFeePerGas &&\n suggestedGasFees.maxFeePerGas &&\n suggestedGasFees.maxPriorityFeePerGas\n ) {\n return UserFeeLevel.MEDIUM;\n }\n\n if (txMeta.origin === ORIGIN_METAMASK) {\n return UserFeeLevel.MEDIUM;\n }\n\n return UserFeeLevel.DAPP_SUGGESTED;\n}\n\nfunction updateDefaultGasEstimates(txMeta: TransactionMeta) {\n if (!txMeta.defaultGasEstimates) {\n txMeta.defaultGasEstimates = {};\n }\n\n txMeta.defaultGasEstimates.maxFeePerGas = txMeta.txParams.maxFeePerGas;\n\n txMeta.defaultGasEstimates.maxPriorityFeePerGas =\n txMeta.txParams.maxPriorityFeePerGas;\n\n txMeta.defaultGasEstimates.gasPrice = txMeta.txParams.gasPrice;\n txMeta.defaultGasEstimates.estimateType = txMeta.userFeeLevel;\n}\n\nasync function getSuggestedGasFees(\n request: UpdateGasFeesRequest,\n): Promise<SuggestedGasFees> {\n const { eip1559, ethQuery, gasFeeFlows, getGasFeeEstimates, txMeta } =\n request;\n\n if (\n (!eip1559 && txMeta.txParams.gasPrice) ||\n (eip1559 &&\n txMeta.txParams.maxFeePerGas &&\n txMeta.txParams.maxPriorityFeePerGas)\n ) {\n return {};\n }\n\n const gasFeeFlow = getGasFeeFlow(txMeta, gasFeeFlows) as GasFeeFlow;\n\n try {\n const response = await gasFeeFlow.getGasFees({\n ethQuery,\n getGasFeeControllerEstimates: getGasFeeEstimates,\n transactionMeta: txMeta,\n });\n\n return response.estimates.medium;\n } catch (error) {\n log('Failed to get suggested gas fees', error);\n }\n\n const gasPriceDecimal = (await query(ethQuery, 'gasPrice')) as number;\n\n const gasPrice = gasPriceDecimal\n ? add0x(gasPriceDecimal.toString(16))\n : undefined;\n\n return { gasPrice };\n}\n"]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import {
2
+ gweiDecimalToWeiHex,
3
+ updateGasFees
4
+ } from "../chunk-DQP6X25N.mjs";
5
+ import "../chunk-EQ3RRHB7.mjs";
6
+ import "../chunk-J56A7UCK.mjs";
7
+ import "../chunk-7MZ57ILQ.mjs";
8
+ import "../chunk-MHM5LRRF.mjs";
9
+ import "../chunk-UQQWZT6C.mjs";
10
+ import "../chunk-LM4NUNMT.mjs";
11
+ import "../chunk-XUI43LEZ.mjs";
12
+ export {
13
+ gweiDecimalToWeiHex,
14
+ updateGasFees
15
+ };
16
+ //# sourceMappingURL=gas-fees.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,53 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeGasFeeEstimates = exports.getGasFeeFlow = void 0;
4
- const controller_utils_1 = require("@metamask/controller-utils");
5
- const gas_fee_controller_1 = require("@metamask/gas-fee-controller");
6
- const types_1 = require("../types");
7
- /**
8
- * Returns the first gas fee flow that matches the transaction.
9
- *
10
- * @param transactionMeta - The transaction metadata to find a gas fee flow for.
11
- * @param gasFeeFlows - The gas fee flows to search.
12
- * @returns The first gas fee flow that matches the transaction, or undefined if none match.
13
- */
14
- function getGasFeeFlow(transactionMeta, gasFeeFlows) {
15
- return gasFeeFlows.find((gasFeeFlow) => gasFeeFlow.matchesTransaction(transactionMeta));
16
- }
17
- exports.getGasFeeFlow = getGasFeeFlow;
18
- /**
19
- * Merge the gas fee estimates from the gas fee controller with the gas fee estimates from a transaction.
20
- * @param request - Data required to merge gas fee estimates.
21
- * @param request.gasFeeControllerEstimateType - Gas fee estimate type from the gas fee controller.
22
- * @param request.gasFeeControllerEstimates - Gas fee estimates from the GasFeeController.
23
- * @param request.transactionGasFeeEstimates - Gas fee estimates from the transaction.
24
- * @returns The merged gas fee estimates.
25
- */
26
- function mergeGasFeeEstimates({ gasFeeControllerEstimateType, gasFeeControllerEstimates, transactionGasFeeEstimates, }) {
27
- if (gasFeeControllerEstimateType === gas_fee_controller_1.GAS_ESTIMATE_TYPES.FEE_MARKET) {
28
- return Object.values(types_1.GasFeeEstimateLevel).reduce((result, level) => (Object.assign(Object.assign({}, result), { [level]: mergeFeeMarketEstimate(gasFeeControllerEstimates[level], transactionGasFeeEstimates[level]) })), Object.assign({}, gasFeeControllerEstimates));
29
- }
30
- if (gasFeeControllerEstimateType === gas_fee_controller_1.GAS_ESTIMATE_TYPES.LEGACY) {
31
- return Object.values(types_1.GasFeeEstimateLevel).reduce((result, level) => (Object.assign(Object.assign({}, result), { [level]: getLegacyEstimate(transactionGasFeeEstimates[level]) })), {});
32
- }
33
- return gasFeeControllerEstimates;
34
- }
35
- exports.mergeGasFeeEstimates = mergeGasFeeEstimates;
36
- /**
37
- * Merge a specific priority level of EIP-1559 gas fee estimates.
38
- * @param gasFeeControllerEstimate - The gas fee estimate from the gas fee controller.
39
- * @param transactionGasFeeEstimate - The gas fee estimate from the transaction.
40
- * @returns The merged gas fee estimate.
41
- */
42
- function mergeFeeMarketEstimate(gasFeeControllerEstimate, transactionGasFeeEstimate) {
43
- return Object.assign(Object.assign({}, gasFeeControllerEstimate), { suggestedMaxFeePerGas: (0, controller_utils_1.weiHexToGweiDec)(transactionGasFeeEstimate.maxFeePerGas), suggestedMaxPriorityFeePerGas: (0, controller_utils_1.weiHexToGweiDec)(transactionGasFeeEstimate.maxPriorityFeePerGas) });
44
- }
45
- /**
46
- * Generate a specific priority level for a legacy gas fee estimate.
47
- * @param transactionGasFeeEstimate - The gas fee estimate from the transaction.
48
- * @returns The legacy gas fee estimate.
49
- */
50
- function getLegacyEstimate(transactionGasFeeEstimate) {
51
- return (0, controller_utils_1.weiHexToGweiDec)(transactionGasFeeEstimate.maxFeePerGas);
52
- }
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+ var _chunkH4M66BA3js = require('../chunk-H4M66BA3.js');
5
+ require('../chunk-HPNXIKFY.js');
6
+ require('../chunk-Z4BLTVTB.js');
7
+
8
+
9
+
10
+ exports.getGasFeeFlow = _chunkH4M66BA3js.getGasFeeFlow; exports.mergeGasFeeEstimates = _chunkH4M66BA3js.mergeGasFeeEstimates;
53
11
  //# sourceMappingURL=gas-flow.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas-flow.js","sourceRoot":"","sources":["../../src/utils/gas-flow.ts"],"names":[],"mappings":";;;AAAA,iEAA6D;AAM7D,qEAGsC;AAEtC,oCAMkB;AAElB;;;;;;GAMG;AACH,SAAgB,aAAa,CAC3B,eAAgC,EAChC,WAAyB;IAEzB,OAAO,WAAW,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CACrC,UAAU,CAAC,kBAAkB,CAAC,eAAe,CAAC,CAC/C,CAAC;AACJ,CAAC;AAPD,sCAOC;AAcD;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAAC,EACnC,4BAA4B,EAC5B,yBAAyB,EACzB,0BAA0B,GAGS;IACnC,IAAI,4BAA4B,KAAK,uCAAkB,CAAC,UAAU,EAAE;QAClE,OAAO,MAAM,CAAC,MAAM,CAAC,2BAAmB,CAAC,CAAC,MAAM,CAC9C,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,iCACd,MAAM,KACT,CAAC,KAAK,CAAC,EAAE,sBAAsB,CAC7B,yBAAyB,CAAC,KAAK,CAAC,EAChC,0BAA0B,CAAC,KAAK,CAAC,CAClC,IACD,EACF,kBAAK,yBAAyB,CAAqB,CACpD,CAAC;KACH;IAED,IAAI,4BAA4B,KAAK,uCAAkB,CAAC,MAAM,EAAE;QAC9D,OAAO,MAAM,CAAC,MAAM,CAAC,2BAAmB,CAAC,CAAC,MAAM,CAC9C,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,iCACd,MAAM,KACT,CAAC,KAAK,CAAC,EAAE,iBAAiB,CAAC,0BAA0B,CAAC,KAAK,CAAC,CAAC,IAC7D,EACF,EAA4B,CAC7B,CAAC;KACH;IAED,OAAO,yBAAyB,CAAC;AACnC,CAAC;AA/BD,oDA+BC;AAED;;;;;GAKG;AACH,SAAS,sBAAsB,CAC7B,wBAAuC,EACvC,yBAAkD;IAElD,uCACK,wBAAwB,KAC3B,qBAAqB,EAAE,IAAA,kCAAe,EACpC,yBAAyB,CAAC,YAAY,CACvC,EACD,6BAA6B,EAAE,IAAA,kCAAe,EAC5C,yBAAyB,CAAC,oBAAoB,CAC/C,IACD;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,iBAAiB,CACxB,yBAAkD;IAElD,OAAO,IAAA,kCAAe,EAAC,yBAAyB,CAAC,YAAY,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { weiHexToGweiDec } from '@metamask/controller-utils';\nimport type {\n Eip1559GasFee,\n GasFeeEstimates,\n LegacyGasPriceEstimate,\n} from '@metamask/gas-fee-controller';\nimport {\n GAS_ESTIMATE_TYPES,\n type GasFeeState,\n} from '@metamask/gas-fee-controller';\n\nimport {\n type GasFeeEstimates as TransactionGasFeeEstimates,\n type GasFeeFlow,\n type TransactionMeta,\n type GasFeeEstimatesForLevel,\n GasFeeEstimateLevel,\n} from '../types';\n\n/**\n * Returns the first gas fee flow that matches the transaction.\n *\n * @param transactionMeta - The transaction metadata to find a gas fee flow for.\n * @param gasFeeFlows - The gas fee flows to search.\n * @returns The first gas fee flow that matches the transaction, or undefined if none match.\n */\nexport function getGasFeeFlow(\n transactionMeta: TransactionMeta,\n gasFeeFlows: GasFeeFlow[],\n): GasFeeFlow | undefined {\n return gasFeeFlows.find((gasFeeFlow) =>\n gasFeeFlow.matchesTransaction(transactionMeta),\n );\n}\n\ntype FeeMarketMergeGasFeeEstimatesRequest = {\n gasFeeControllerEstimateType: 'fee-market';\n gasFeeControllerEstimates: GasFeeEstimates;\n transactionGasFeeEstimates: TransactionGasFeeEstimates;\n};\n\ntype LegacyMergeGasFeeEstimatesRequest = {\n gasFeeControllerEstimateType: 'legacy';\n gasFeeControllerEstimates: LegacyGasPriceEstimate;\n transactionGasFeeEstimates: TransactionGasFeeEstimates;\n};\n\n/**\n * Merge the gas fee estimates from the gas fee controller with the gas fee estimates from a transaction.\n * @param request - Data required to merge gas fee estimates.\n * @param request.gasFeeControllerEstimateType - Gas fee estimate type from the gas fee controller.\n * @param request.gasFeeControllerEstimates - Gas fee estimates from the GasFeeController.\n * @param request.transactionGasFeeEstimates - Gas fee estimates from the transaction.\n * @returns The merged gas fee estimates.\n */\nexport function mergeGasFeeEstimates({\n gasFeeControllerEstimateType,\n gasFeeControllerEstimates,\n transactionGasFeeEstimates,\n}:\n | FeeMarketMergeGasFeeEstimatesRequest\n | LegacyMergeGasFeeEstimatesRequest): GasFeeState['gasFeeEstimates'] {\n if (gasFeeControllerEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n return Object.values(GasFeeEstimateLevel).reduce(\n (result, level) => ({\n ...result,\n [level]: mergeFeeMarketEstimate(\n gasFeeControllerEstimates[level],\n transactionGasFeeEstimates[level],\n ),\n }),\n { ...gasFeeControllerEstimates } as GasFeeEstimates,\n );\n }\n\n if (gasFeeControllerEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {\n return Object.values(GasFeeEstimateLevel).reduce(\n (result, level) => ({\n ...result,\n [level]: getLegacyEstimate(transactionGasFeeEstimates[level]),\n }),\n {} as LegacyGasPriceEstimate,\n );\n }\n\n return gasFeeControllerEstimates;\n}\n\n/**\n * Merge a specific priority level of EIP-1559 gas fee estimates.\n * @param gasFeeControllerEstimate - The gas fee estimate from the gas fee controller.\n * @param transactionGasFeeEstimate - The gas fee estimate from the transaction.\n * @returns The merged gas fee estimate.\n */\nfunction mergeFeeMarketEstimate(\n gasFeeControllerEstimate: Eip1559GasFee,\n transactionGasFeeEstimate: GasFeeEstimatesForLevel,\n): Eip1559GasFee {\n return {\n ...gasFeeControllerEstimate,\n suggestedMaxFeePerGas: weiHexToGweiDec(\n transactionGasFeeEstimate.maxFeePerGas,\n ),\n suggestedMaxPriorityFeePerGas: weiHexToGweiDec(\n transactionGasFeeEstimate.maxPriorityFeePerGas,\n ),\n };\n}\n\n/**\n * Generate a specific priority level for a legacy gas fee estimate.\n * @param transactionGasFeeEstimate - The gas fee estimate from the transaction.\n * @returns The legacy gas fee estimate.\n */\nfunction getLegacyEstimate(\n transactionGasFeeEstimate: GasFeeEstimatesForLevel,\n): string {\n return weiHexToGweiDec(transactionGasFeeEstimate.maxFeePerGas);\n}\n"]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import {
2
+ getGasFeeFlow,
3
+ mergeGasFeeEstimates
4
+ } from "../chunk-7MZ57ILQ.mjs";
5
+ import "../chunk-LM4NUNMT.mjs";
6
+ import "../chunk-XUI43LEZ.mjs";
7
+ export {
8
+ getGasFeeFlow,
9
+ mergeGasFeeEstimates
10
+ };
11
+ //# sourceMappingURL=gas-flow.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/utils/gas.js CHANGED
@@ -1,134 +1,20 @@
1
- "use strict";
2
- /* eslint-disable jsdoc/require-jsdoc */
3
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
- return new (P || (P = Promise))(function (resolve, reject) {
6
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
- step((generator = generator.apply(thisArg, _arguments || [])).next());
10
- });
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.addGasBuffer = exports.estimateGas = exports.updateGas = exports.DEFAULT_GAS_MULTIPLIER = exports.FIXED_GAS = exports.log = void 0;
14
- const controller_utils_1 = require("@metamask/controller-utils");
15
- const utils_1 = require("@metamask/utils");
16
- const constants_1 = require("../constants");
17
- const logger_1 = require("../logger");
18
- exports.log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'gas');
19
- exports.FIXED_GAS = '0x5208';
20
- exports.DEFAULT_GAS_MULTIPLIER = 1.5;
21
- function updateGas(request) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const { txMeta } = request;
24
- const initialParams = Object.assign({}, txMeta.txParams);
25
- const [gas, simulationFails] = yield getGas(request);
26
- txMeta.txParams.gas = gas;
27
- txMeta.simulationFails = simulationFails;
28
- if (!initialParams.gas) {
29
- txMeta.originalGasEstimate = txMeta.txParams.gas;
30
- }
31
- if (!txMeta.defaultGasEstimates) {
32
- txMeta.defaultGasEstimates = {};
33
- }
34
- txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;
35
- });
36
- }
37
- exports.updateGas = updateGas;
38
- function estimateGas(txParams, ethQuery) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- const request = Object.assign({}, txParams);
41
- const { data, value } = request;
42
- const { gasLimit: gasLimitHex, number: blockNumber } = yield getLatestBlock(ethQuery);
43
- const gasLimitBN = (0, controller_utils_1.hexToBN)(gasLimitHex);
44
- request.data = data ? (0, utils_1.add0x)(data) : data;
45
- request.gas = (0, controller_utils_1.BNToHex)((0, controller_utils_1.fractionBN)(gasLimitBN, 19, 20));
46
- request.value = value || '0x0';
47
- let estimatedGas = request.gas;
48
- let simulationFails;
49
- try {
50
- estimatedGas = yield (0, controller_utils_1.query)(ethQuery, 'estimateGas', [request]);
51
- // TODO: Replace `any` with type
52
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- }
54
- catch (error) {
55
- simulationFails = {
56
- reason: error.message,
57
- errorKey: error.errorKey,
58
- debug: {
59
- blockNumber,
60
- blockGasLimit: gasLimitHex,
61
- },
62
- };
63
- (0, exports.log)('Estimation failed', Object.assign(Object.assign({}, simulationFails), { fallback: estimateGas }));
64
- }
65
- return {
66
- blockGasLimit: gasLimitHex,
67
- estimatedGas,
68
- simulationFails,
69
- };
70
- });
71
- }
72
- exports.estimateGas = estimateGas;
73
- function addGasBuffer(estimatedGas, blockGasLimit, multiplier) {
74
- const estimatedGasBN = (0, controller_utils_1.hexToBN)(estimatedGas);
75
- const maxGasBN = (0, controller_utils_1.hexToBN)(blockGasLimit).muln(0.9);
76
- const paddedGasBN = estimatedGasBN.muln(multiplier);
77
- if (estimatedGasBN.gt(maxGasBN)) {
78
- const estimatedGasHex = (0, utils_1.add0x)(estimatedGas);
79
- (0, exports.log)('Using estimated value', estimatedGasHex);
80
- return estimatedGasHex;
81
- }
82
- if (paddedGasBN.lt(maxGasBN)) {
83
- const paddedHex = (0, utils_1.add0x)((0, controller_utils_1.BNToHex)(paddedGasBN));
84
- (0, exports.log)('Using padded estimate', paddedHex, multiplier);
85
- return paddedHex;
86
- }
87
- const maxHex = (0, utils_1.add0x)((0, controller_utils_1.BNToHex)(maxGasBN));
88
- (0, exports.log)('Using 90% of block gas limit', maxHex);
89
- return maxHex;
90
- }
91
- exports.addGasBuffer = addGasBuffer;
92
- function getGas(request) {
93
- var _a;
94
- return __awaiter(this, void 0, void 0, function* () {
95
- const { isCustomNetwork, chainId, txMeta } = request;
96
- if (txMeta.txParams.gas) {
97
- (0, exports.log)('Using value from request', txMeta.txParams.gas);
98
- return [txMeta.txParams.gas];
99
- }
100
- if (yield requiresFixedGas(request)) {
101
- (0, exports.log)('Using fixed value', exports.FIXED_GAS);
102
- return [exports.FIXED_GAS];
103
- }
104
- const { blockGasLimit, estimatedGas, simulationFails } = yield estimateGas(txMeta.txParams, request.ethQuery);
105
- if (isCustomNetwork) {
106
- (0, exports.log)('Using original estimate as custom network');
107
- return [estimatedGas, simulationFails];
108
- }
109
- const bufferMultiplier = (_a = constants_1.GAS_BUFFER_CHAIN_OVERRIDES[chainId]) !== null && _a !== void 0 ? _a : exports.DEFAULT_GAS_MULTIPLIER;
110
- const bufferedGas = addGasBuffer(estimatedGas, blockGasLimit, bufferMultiplier);
111
- return [bufferedGas, simulationFails];
112
- });
113
- }
114
- function requiresFixedGas({ ethQuery, txMeta, isCustomNetwork, }) {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- const { txParams: { to, data }, } = txMeta;
117
- if (isCustomNetwork || !to || data) {
118
- return false;
119
- }
120
- const code = yield getCode(ethQuery, to);
121
- return !code || code === '0x';
122
- });
123
- }
124
- function getCode(ethQuery, address) {
125
- return __awaiter(this, void 0, void 0, function* () {
126
- return yield (0, controller_utils_1.query)(ethQuery, 'getCode', [address]);
127
- });
128
- }
129
- function getLatestBlock(ethQuery) {
130
- return __awaiter(this, void 0, void 0, function* () {
131
- return yield (0, controller_utils_1.query)(ethQuery, 'getBlockByNumber', ['latest', false]);
132
- });
133
- }
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+
6
+
7
+
8
+ var _chunkIUBAETUHjs = require('../chunk-IUBAETUH.js');
9
+ require('../chunk-RI6MVJJN.js');
10
+ require('../chunk-S6VGOPUY.js');
11
+ require('../chunk-Z4BLTVTB.js');
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+ exports.DEFAULT_GAS_MULTIPLIER = _chunkIUBAETUHjs.DEFAULT_GAS_MULTIPLIER; exports.FIXED_GAS = _chunkIUBAETUHjs.FIXED_GAS; exports.addGasBuffer = _chunkIUBAETUHjs.addGasBuffer; exports.estimateGas = _chunkIUBAETUHjs.estimateGas; exports.log = _chunkIUBAETUHjs.log; exports.updateGas = _chunkIUBAETUHjs.updateGas;
134
20
  //# sourceMappingURL=gas.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";AAAA,wCAAwC;;;;;;;;;;;;AAExC,iEAKoC;AAGpC,2CAA4D;AAE5D,4CAA0D;AAC1D,sCAA0C;AAU7B,QAAA,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,KAAK,CAAC,CAAC;AAE/C,QAAA,SAAS,GAAG,QAAQ,CAAC;AACrB,QAAA,sBAAsB,GAAG,GAAG,CAAC;AAE1C,SAAsB,SAAS,CAAC,OAAyB;;QACvD,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,MAAM,aAAa,qBAAQ,MAAM,CAAC,QAAQ,CAAE,CAAC;QAE7C,MAAM,CAAC,GAAG,EAAE,eAAe,CAAC,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;QAErD,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,GAAG,CAAC;QAC1B,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;QAEzC,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE;YACtB,MAAM,CAAC,mBAAmB,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC;SAClD;QAED,IAAI,CAAC,MAAM,CAAC,mBAAmB,EAAE;YAC/B,MAAM,CAAC,mBAAmB,GAAG,EAAE,CAAC;SACjC;QAED,MAAM,CAAC,mBAAmB,CAAC,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC;IACvD,CAAC;CAAA;AAlBD,8BAkBC;AAED,SAAsB,WAAW,CAC/B,QAA2B,EAC3B,QAAkB;;QAElB,MAAM,OAAO,qBAAQ,QAAQ,CAAE,CAAC;QAChC,MAAM,EAAE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nimport {\n BNToHex,\n fractionBN,\n hexToBN,\n query,\n} from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, createModuleLogger } from '@metamask/utils';\n\nimport { GAS_BUFFER_CHAIN_OVERRIDES } from '../constants';\nimport { projectLogger } from '../logger';\nimport type { TransactionMeta, TransactionParams } from '../types';\n\nexport type UpdateGasRequest = {\n ethQuery: EthQuery;\n isCustomNetwork: boolean;\n chainId: Hex;\n txMeta: TransactionMeta;\n};\n\nexport const log = createModuleLogger(projectLogger, 'gas');\n\nexport const FIXED_GAS = '0x5208';\nexport const DEFAULT_GAS_MULTIPLIER = 1.5;\n\nexport async function updateGas(request: UpdateGasRequest) {\n const { txMeta } = request;\n const initialParams = { ...txMeta.txParams };\n\n const [gas, simulationFails] = await getGas(request);\n\n txMeta.txParams.gas = gas;\n txMeta.simulationFails = simulationFails;\n\n if (!initialParams.gas) {\n txMeta.originalGasEstimate = txMeta.txParams.gas;\n }\n\n if (!txMeta.defaultGasEstimates) {\n txMeta.defaultGasEstimates = {};\n }\n\n txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;\n}\n\nexport async function estimateGas(\n txParams: TransactionParams,\n ethQuery: EthQuery,\n) {\n const request = { ...txParams };\n const { data, value } = request;\n\n const { gasLimit: gasLimitHex, number: blockNumber } = await getLatestBlock(\n ethQuery,\n );\n\n const gasLimitBN = hexToBN(gasLimitHex);\n\n request.data = data ? add0x(data) : data;\n request.gas = BNToHex(fractionBN(gasLimitBN, 19, 20));\n request.value = value || '0x0';\n\n let estimatedGas = request.gas;\n let simulationFails;\n\n try {\n estimatedGas = await query(ethQuery, 'estimateGas', [request]);\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (error: any) {\n simulationFails = {\n reason: error.message,\n errorKey: error.errorKey,\n debug: {\n blockNumber,\n blockGasLimit: gasLimitHex,\n },\n };\n\n log('Estimation failed', { ...simulationFails, fallback: estimateGas });\n }\n\n return {\n blockGasLimit: gasLimitHex,\n estimatedGas,\n simulationFails,\n };\n}\n\nexport function addGasBuffer(\n estimatedGas: string,\n blockGasLimit: string,\n multiplier: number,\n) {\n const estimatedGasBN = hexToBN(estimatedGas);\n const maxGasBN = hexToBN(blockGasLimit).muln(0.9);\n const paddedGasBN = estimatedGasBN.muln(multiplier);\n\n if (estimatedGasBN.gt(maxGasBN)) {\n const estimatedGasHex = add0x(estimatedGas);\n log('Using estimated value', estimatedGasHex);\n return estimatedGasHex;\n }\n\n if (paddedGasBN.lt(maxGasBN)) {\n const paddedHex = add0x(BNToHex(paddedGasBN));\n log('Using padded estimate', paddedHex, multiplier);\n return paddedHex;\n }\n\n const maxHex = add0x(BNToHex(maxGasBN));\n log('Using 90% of block gas limit', maxHex);\n return maxHex;\n}\n\nasync function getGas(\n request: UpdateGasRequest,\n): Promise<[string, TransactionMeta['simulationFails']?]> {\n const { isCustomNetwork, chainId, txMeta } = request;\n\n if (txMeta.txParams.gas) {\n log('Using value from request', txMeta.txParams.gas);\n return [txMeta.txParams.gas];\n }\n\n if (await requiresFixedGas(request)) {\n log('Using fixed value', FIXED_GAS);\n return [FIXED_GAS];\n }\n\n const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas(\n txMeta.txParams,\n request.ethQuery,\n );\n\n if (isCustomNetwork) {\n log('Using original estimate as custom network');\n return [estimatedGas, simulationFails];\n }\n\n const bufferMultiplier =\n GAS_BUFFER_CHAIN_OVERRIDES[\n chainId as keyof typeof GAS_BUFFER_CHAIN_OVERRIDES\n ] ?? DEFAULT_GAS_MULTIPLIER;\n\n const bufferedGas = addGasBuffer(\n estimatedGas,\n blockGasLimit,\n bufferMultiplier,\n );\n\n return [bufferedGas, simulationFails];\n}\n\nasync function requiresFixedGas({\n ethQuery,\n txMeta,\n isCustomNetwork,\n}: UpdateGasRequest): Promise<boolean> {\n const {\n txParams: { to, data },\n } = txMeta;\n\n if (isCustomNetwork || !to || data) {\n return false;\n }\n\n const code = await getCode(ethQuery, to);\n\n return !code || code === '0x';\n}\n\nasync function getCode(\n ethQuery: EthQuery,\n address: string,\n): Promise<string | undefined> {\n return await query(ethQuery, 'getCode', [address]);\n}\n\nasync function getLatestBlock(\n ethQuery: EthQuery,\n): Promise<{ gasLimit: string; number: string }> {\n return await query(ethQuery, 'getBlockByNumber', ['latest', false]);\n}\n"]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ import {
2
+ DEFAULT_GAS_MULTIPLIER,
3
+ FIXED_GAS,
4
+ addGasBuffer,
5
+ estimateGas,
6
+ log,
7
+ updateGas
8
+ } from "../chunk-UKYY2RVS.mjs";
9
+ import "../chunk-MHM5LRRF.mjs";
10
+ import "../chunk-UQQWZT6C.mjs";
11
+ import "../chunk-XUI43LEZ.mjs";
12
+ export {
13
+ DEFAULT_GAS_MULTIPLIER,
14
+ FIXED_GAS,
15
+ addGasBuffer,
16
+ estimateGas,
17
+ log,
18
+ updateGas
19
+ };
20
+ //# sourceMappingURL=gas.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}