@metamask/transaction-controller 24.0.0 → 25.1.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.
- package/CHANGELOG.md +129 -1
- package/dist/TransactionController.js +39 -1816
- package/dist/TransactionController.js.map +1 -1
- package/dist/TransactionController.mjs +40 -0
- package/dist/TransactionController.mjs.map +1 -0
- package/dist/chunk-2YXA3K67.mjs +62 -0
- package/dist/chunk-2YXA3K67.mjs.map +1 -0
- package/dist/chunk-3D3SA6PY.mjs +211 -0
- package/dist/chunk-3D3SA6PY.mjs.map +1 -0
- package/dist/chunk-4S25HUCL.js +46 -0
- package/dist/chunk-4S25HUCL.js.map +1 -0
- package/dist/chunk-5OQ373JS.js +211 -0
- package/dist/chunk-5OQ373JS.js.map +1 -0
- package/dist/chunk-5XBULBP2.js +399 -0
- package/dist/chunk-5XBULBP2.js.map +1 -0
- package/dist/chunk-72OTU5ZW.mjs +2332 -0
- package/dist/chunk-72OTU5ZW.mjs.map +1 -0
- package/dist/chunk-7LXE4KHV.js +40 -0
- package/dist/chunk-7LXE4KHV.js.map +1 -0
- package/dist/chunk-7XPI7KU5.mjs +46 -0
- package/dist/chunk-7XPI7KU5.mjs.map +1 -0
- package/dist/chunk-BILEZLAT.js +182 -0
- package/dist/chunk-BILEZLAT.js.map +1 -0
- package/dist/chunk-CQBXHAYR.js +121 -0
- package/dist/chunk-CQBXHAYR.js.map +1 -0
- package/dist/chunk-CXXGL43K.js +85 -0
- package/dist/chunk-CXXGL43K.js.map +1 -0
- package/dist/chunk-DTDTOMTB.js +238 -0
- package/dist/chunk-DTDTOMTB.js.map +1 -0
- package/dist/chunk-DTONMSFW.mjs +208 -0
- package/dist/chunk-DTONMSFW.mjs.map +1 -0
- package/dist/chunk-EEMJC7S7.mjs +182 -0
- package/dist/chunk-EEMJC7S7.mjs.map +1 -0
- package/dist/chunk-F3CMU2DM.js +170 -0
- package/dist/chunk-F3CMU2DM.js.map +1 -0
- package/dist/chunk-FRKQ3Z2L.mjs +40 -0
- package/dist/chunk-FRKQ3Z2L.mjs.map +1 -0
- package/dist/chunk-HS277C77.js +75 -0
- package/dist/chunk-HS277C77.js.map +1 -0
- package/dist/chunk-ITDY6AIZ.js +127 -0
- package/dist/chunk-ITDY6AIZ.js.map +1 -0
- package/dist/chunk-IZI7FQIN.mjs +170 -0
- package/dist/chunk-IZI7FQIN.mjs.map +1 -0
- package/dist/chunk-J56A7UCK.mjs +123 -0
- package/dist/chunk-J56A7UCK.mjs.map +1 -0
- package/dist/chunk-JRBREX22.mjs +75 -0
- package/dist/chunk-JRBREX22.mjs.map +1 -0
- package/dist/chunk-JRQHIBG5.mjs +399 -0
- package/dist/chunk-JRQHIBG5.mjs.map +1 -0
- package/dist/chunk-K26EBMGI.mjs +121 -0
- package/dist/chunk-K26EBMGI.mjs.map +1 -0
- package/dist/chunk-KFL2GGZC.mjs +48 -0
- package/dist/chunk-KFL2GGZC.mjs.map +1 -0
- package/dist/chunk-NHRBO3LU.mjs +50 -0
- package/dist/chunk-NHRBO3LU.mjs.map +1 -0
- package/dist/chunk-NM4LTWRU.mjs +76 -0
- package/dist/chunk-NM4LTWRU.mjs.map +1 -0
- package/dist/chunk-NM6OYEPP.mjs +182 -0
- package/dist/chunk-NM6OYEPP.mjs.map +1 -0
- package/dist/chunk-NRWEI43Q.js +320 -0
- package/dist/chunk-NRWEI43Q.js.map +1 -0
- package/dist/chunk-O7H2MC7R.js +62 -0
- package/dist/chunk-O7H2MC7R.js.map +1 -0
- package/dist/chunk-OF6NSLXF.mjs +90 -0
- package/dist/chunk-OF6NSLXF.mjs.map +1 -0
- package/dist/chunk-QDIYZX5V.js +2332 -0
- package/dist/chunk-QDIYZX5V.js.map +1 -0
- package/dist/chunk-QP75SWIQ.js +53 -0
- package/dist/chunk-QP75SWIQ.js.map +1 -0
- package/dist/chunk-QPNEFZB3.js +208 -0
- package/dist/chunk-QPNEFZB3.js.map +1 -0
- package/dist/chunk-QSBIXUMB.mjs +242 -0
- package/dist/chunk-QSBIXUMB.mjs.map +1 -0
- package/dist/chunk-R7NJVDWN.js +48 -0
- package/dist/chunk-R7NJVDWN.js.map +1 -0
- package/dist/chunk-RQKICZYP.js +137 -0
- package/dist/chunk-RQKICZYP.js.map +1 -0
- package/dist/chunk-S6VGOPUY.js +14 -0
- package/dist/chunk-S6VGOPUY.js.map +1 -0
- package/dist/chunk-TXVH44HM.js +90 -0
- package/dist/chunk-TXVH44HM.js.map +1 -0
- package/dist/chunk-UGFBA4GV.js +123 -0
- package/dist/chunk-UGFBA4GV.js.map +1 -0
- package/dist/chunk-UQQWZT6C.mjs +14 -0
- package/dist/chunk-UQQWZT6C.mjs.map +1 -0
- package/dist/chunk-VEREDMI2.mjs +85 -0
- package/dist/chunk-VEREDMI2.mjs.map +1 -0
- package/dist/chunk-VH47Q6TS.js +182 -0
- package/dist/chunk-VH47Q6TS.js.map +1 -0
- package/dist/chunk-W3GAOR7Y.js +76 -0
- package/dist/chunk-W3GAOR7Y.js.map +1 -0
- package/dist/chunk-WXQZIUNW.js +242 -0
- package/dist/chunk-WXQZIUNW.js.map +1 -0
- package/dist/chunk-XGRAHX6T.mjs +53 -0
- package/dist/chunk-XGRAHX6T.mjs.map +1 -0
- package/dist/chunk-XKNFL657.mjs +137 -0
- package/dist/chunk-XKNFL657.mjs.map +1 -0
- package/dist/chunk-XUI43LEZ.mjs +30 -0
- package/dist/chunk-XUI43LEZ.mjs.map +1 -0
- package/dist/chunk-Y7ENNK7L.mjs +238 -0
- package/dist/chunk-Y7ENNK7L.mjs.map +1 -0
- package/dist/chunk-Z3HHSD5I.mjs +127 -0
- package/dist/chunk-Z3HHSD5I.mjs.map +1 -0
- package/dist/chunk-Z4BLTVTB.js +30 -0
- package/dist/chunk-Z4BLTVTB.js.map +1 -0
- package/dist/chunk-ZNZEJDOE.js +50 -0
- package/dist/chunk-ZNZEJDOE.js.map +1 -0
- package/dist/chunk-ZQFMTLZJ.mjs +320 -0
- package/dist/chunk-ZQFMTLZJ.mjs.map +1 -0
- package/dist/constants.js +15 -110
- package/dist/constants.js.map +1 -1
- package/dist/constants.mjs +16 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/gas-flows/DefaultGasFeeFlow.js +14 -77
- package/dist/gas-flows/DefaultGasFeeFlow.js.map +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.mjs +15 -0
- package/dist/gas-flows/DefaultGasFeeFlow.mjs.map +1 -0
- package/dist/gas-flows/LineaGasFeeFlow.js +15 -110
- package/dist/gas-flows/LineaGasFeeFlow.js.map +1 -1
- package/dist/gas-flows/LineaGasFeeFlow.mjs +16 -0
- package/dist/gas-flows/LineaGasFeeFlow.mjs.map +1 -0
- package/dist/helpers/EtherscanRemoteTransactionSource.js +11 -145
- package/dist/helpers/EtherscanRemoteTransactionSource.js.map +1 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.mjs +12 -0
- package/dist/helpers/EtherscanRemoteTransactionSource.mjs.map +1 -0
- package/dist/helpers/GasFeePoller.js +11 -143
- package/dist/helpers/GasFeePoller.js.map +1 -1
- package/dist/helpers/GasFeePoller.mjs +12 -0
- package/dist/helpers/GasFeePoller.mjs.map +1 -0
- package/dist/helpers/IncomingTransactionHelper.js +8 -205
- package/dist/helpers/IncomingTransactionHelper.js.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.mjs +9 -0
- package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -0
- package/dist/helpers/MultichainTrackingHelper.js +12 -291
- package/dist/helpers/MultichainTrackingHelper.js.map +1 -1
- package/dist/helpers/MultichainTrackingHelper.mjs +13 -0
- package/dist/helpers/MultichainTrackingHelper.mjs.map +1 -0
- package/dist/helpers/PendingTransactionTracker.js +9 -360
- package/dist/helpers/PendingTransactionTracker.js.map +1 -1
- package/dist/helpers/PendingTransactionTracker.mjs +10 -0
- package/dist/helpers/PendingTransactionTracker.mjs.map +1 -0
- package/dist/index.js +57 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger.js +11 -8
- package/dist/logger.js.map +1 -1
- package/dist/logger.mjs +12 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/{TransactionController.d.ts → types/TransactionController.d.ts} +236 -46
- package/dist/types/TransactionController.d.ts.map +1 -0
- package/dist/{constants.d.ts → types/constants.d.ts} +10 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/gas-flows/DefaultGasFeeFlow.d.ts.map +1 -0
- package/dist/types/gas-flows/LineaGasFeeFlow.d.ts.map +1 -0
- package/dist/types/helpers/EtherscanRemoteTransactionSource.d.ts.map +1 -0
- package/dist/{helpers → types/helpers}/GasFeePoller.d.ts +5 -3
- package/dist/types/helpers/GasFeePoller.d.ts.map +1 -0
- package/dist/types/helpers/IncomingTransactionHelper.d.ts.map +1 -0
- package/dist/types/helpers/MultichainTrackingHelper.d.ts.map +1 -0
- package/dist/types/helpers/PendingTransactionTracker.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/{types.d.ts → types/types.d.ts} +94 -65
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils/etherscan.d.ts.map +1 -0
- package/dist/types/utils/external-transactions.d.ts.map +1 -0
- package/dist/types/utils/gas-fees.d.ts.map +1 -0
- package/dist/types/utils/gas-flow.d.ts.map +1 -0
- package/dist/types/utils/gas.d.ts.map +1 -0
- package/dist/types/utils/history.d.ts +20 -0
- package/dist/types/utils/history.d.ts.map +1 -0
- package/dist/types/utils/layer1-gas-fee-flow.d.ts +16 -0
- package/dist/types/utils/layer1-gas-fee-flow.d.ts.map +1 -0
- package/dist/types/utils/nonce.d.ts.map +1 -0
- package/dist/types/utils/simulation-api.d.ts +99 -0
- package/dist/types/utils/simulation-api.d.ts.map +1 -0
- package/dist/types/utils/simulation.d.ts +21 -0
- package/dist/types/utils/simulation.d.ts.map +1 -0
- package/dist/{utils → types/utils}/swaps.d.ts +8 -5
- package/dist/types/utils/swaps.d.ts.map +1 -0
- package/dist/types/utils/transaction-type.d.ts.map +1 -0
- package/dist/types/utils/utils.d.ts.map +1 -0
- package/dist/types/utils/validation.d.ts.map +1 -0
- package/dist/types.js +19 -170
- package/dist/types.js.map +1 -1
- package/dist/types.mjs +20 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils/etherscan.js +13 -118
- package/dist/utils/etherscan.js.map +1 -1
- package/dist/utils/etherscan.mjs +14 -0
- package/dist/utils/etherscan.mjs.map +1 -0
- package/dist/utils/external-transactions.js +8 -35
- package/dist/utils/external-transactions.js.map +1 -1
- package/dist/utils/external-transactions.mjs +9 -0
- package/dist/utils/external-transactions.mjs.map +1 -0
- package/dist/utils/gas-fees.js +15 -200
- package/dist/utils/gas-fees.js.map +1 -1
- package/dist/utils/gas-fees.mjs +16 -0
- package/dist/utils/gas-fees.mjs.map +1 -0
- package/dist/utils/gas-flow.js +10 -52
- package/dist/utils/gas-flow.js.map +1 -1
- package/dist/utils/gas-flow.mjs +11 -0
- package/dist/utils/gas-flow.mjs.map +1 -0
- package/dist/utils/gas.js +19 -133
- package/dist/utils/gas.js.map +1 -1
- package/dist/utils/gas.mjs +20 -0
- package/dist/utils/gas.mjs.map +1 -0
- package/dist/utils/history.js +9 -83
- package/dist/utils/history.js.map +1 -1
- package/dist/utils/history.mjs +10 -0
- package/dist/utils/history.mjs.map +1 -0
- package/dist/utils/layer1-gas-fee-flow.js +9 -0
- package/dist/utils/layer1-gas-fee-flow.js.map +1 -0
- package/dist/utils/layer1-gas-fee-flow.mjs +9 -0
- package/dist/utils/layer1-gas-fee-flow.mjs.map +1 -0
- package/dist/utils/nonce.js +10 -76
- package/dist/utils/nonce.js.map +1 -1
- package/dist/utils/nonce.mjs +11 -0
- package/dist/utils/nonce.mjs.map +1 -0
- package/dist/utils/simulation-api.js +10 -0
- package/dist/utils/simulation-api.js.map +1 -0
- package/dist/utils/simulation-api.mjs +10 -0
- package/dist/utils/simulation-api.mjs.map +1 -0
- package/dist/utils/simulation.js +12 -0
- package/dist/utils/simulation.js.map +1 -0
- package/dist/utils/simulation.mjs +12 -0
- package/dist/utils/simulation.mjs.map +1 -0
- package/dist/utils/swaps.js +23 -256
- package/dist/utils/swaps.js.map +1 -1
- package/dist/utils/swaps.mjs +24 -0
- package/dist/utils/swaps.mjs.map +1 -0
- package/dist/utils/transaction-type.js +10 -120
- package/dist/utils/transaction-type.js.map +1 -1
- package/dist/utils/transaction-type.mjs +11 -0
- package/dist/utils/transaction-type.mjs.map +1 -0
- package/dist/utils/utils.js +32 -160
- package/dist/utils/utils.js.map +1 -1
- package/dist/utils/utils.mjs +33 -0
- package/dist/utils/utils.mjs.map +1 -0
- package/dist/utils/validation.js +11 -258
- package/dist/utils/validation.js.map +1 -1
- package/dist/utils/validation.mjs +12 -0
- package/dist/utils/validation.mjs.map +1 -0
- package/package.json +22 -10
- package/dist/TransactionController.d.ts.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/gas-flows/DefaultGasFeeFlow.d.ts.map +0 -1
- package/dist/gas-flows/LineaGasFeeFlow.d.ts.map +0 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.d.ts.map +0 -1
- package/dist/helpers/GasFeePoller.d.ts.map +0 -1
- package/dist/helpers/IncomingTransactionHelper.d.ts.map +0 -1
- package/dist/helpers/MultichainTrackingHelper.d.ts.map +0 -1
- package/dist/helpers/PendingTransactionTracker.d.ts.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/logger.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/utils/etherscan.d.ts.map +0 -1
- package/dist/utils/external-transactions.d.ts.map +0 -1
- package/dist/utils/gas-fees.d.ts.map +0 -1
- package/dist/utils/gas-flow.d.ts.map +0 -1
- package/dist/utils/gas.d.ts.map +0 -1
- package/dist/utils/history.d.ts +0 -15
- package/dist/utils/history.d.ts.map +0 -1
- package/dist/utils/nonce.d.ts.map +0 -1
- package/dist/utils/swaps.d.ts.map +0 -1
- package/dist/utils/transaction-type.d.ts.map +0 -1
- package/dist/utils/utils.d.ts.map +0 -1
- package/dist/utils/validation.d.ts.map +0 -1
- /package/dist/{gas-flows → types/gas-flows}/DefaultGasFeeFlow.d.ts +0 -0
- /package/dist/{gas-flows → types/gas-flows}/LineaGasFeeFlow.d.ts +0 -0
- /package/dist/{helpers → types/helpers}/EtherscanRemoteTransactionSource.d.ts +0 -0
- /package/dist/{helpers → types/helpers}/IncomingTransactionHelper.d.ts +0 -0
- /package/dist/{helpers → types/helpers}/MultichainTrackingHelper.d.ts +0 -0
- /package/dist/{helpers → types/helpers}/PendingTransactionTracker.d.ts +0 -0
- /package/dist/{logger.d.ts → types/logger.d.ts} +0 -0
- /package/dist/{utils → types/utils}/etherscan.d.ts +0 -0
- /package/dist/{utils → types/utils}/external-transactions.d.ts +0 -0
- /package/dist/{utils → types/utils}/gas-fees.d.ts +0 -0
- /package/dist/{utils → types/utils}/gas-flow.d.ts +0 -0
- /package/dist/{utils → types/utils}/gas.d.ts +0 -0
- /package/dist/{utils → types/utils}/nonce.d.ts +0 -0
- /package/dist/{utils → types/utils}/transaction-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/utils.d.ts +0 -0
- /package/dist/{utils → types/utils}/validation.d.ts +0 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunk5OQ373JSjs = require('./chunk-5OQ373JS.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkO7H2MC7Rjs = require('./chunk-O7H2MC7R.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkS6VGOPUYjs = require('./chunk-S6VGOPUY.js');
|
|
10
|
+
|
|
11
|
+
// src/utils/gas-fees.ts
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var _controllerutils = require('@metamask/controller-utils');
|
|
18
|
+
var _utils = require('@metamask/utils');
|
|
19
|
+
var log = _utils.createModuleLogger.call(void 0, _chunkS6VGOPUYjs.projectLogger, "gas-fees");
|
|
20
|
+
async function updateGasFees(request) {
|
|
21
|
+
const { txMeta } = request;
|
|
22
|
+
const initialParams = { ...txMeta.txParams };
|
|
23
|
+
const isSwap = _chunk5OQ373JSjs.SWAP_TRANSACTION_TYPES.includes(
|
|
24
|
+
txMeta.type
|
|
25
|
+
);
|
|
26
|
+
const savedGasFees = isSwap ? void 0 : request.getSavedGasFees(txMeta.chainId);
|
|
27
|
+
const suggestedGasFees = await getSuggestedGasFees(request);
|
|
28
|
+
log("Suggested gas fees", suggestedGasFees);
|
|
29
|
+
const getGasFeeRequest = {
|
|
30
|
+
...request,
|
|
31
|
+
initialParams,
|
|
32
|
+
savedGasFees,
|
|
33
|
+
suggestedGasFees
|
|
34
|
+
};
|
|
35
|
+
txMeta.txParams.maxFeePerGas = getMaxFeePerGas(getGasFeeRequest);
|
|
36
|
+
txMeta.txParams.maxPriorityFeePerGas = 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
|
+
function gweiDecimalToWeiHex(value) {
|
|
54
|
+
return _controllerutils.toHex.call(void 0, _controllerutils.gweiDecToWEIBN.call(void 0, value));
|
|
55
|
+
}
|
|
56
|
+
function getMaxFeePerGas(request) {
|
|
57
|
+
const { savedGasFees, eip1559, initialParams, suggestedGasFees } = request;
|
|
58
|
+
if (!eip1559) {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
61
|
+
if (savedGasFees) {
|
|
62
|
+
const maxFeePerGas = gweiDecimalToWeiHex(savedGasFees.maxBaseFee);
|
|
63
|
+
log("Using maxFeePerGas from savedGasFees", maxFeePerGas);
|
|
64
|
+
return maxFeePerGas;
|
|
65
|
+
}
|
|
66
|
+
if (initialParams.maxFeePerGas) {
|
|
67
|
+
log("Using maxFeePerGas from request", initialParams.maxFeePerGas);
|
|
68
|
+
return initialParams.maxFeePerGas;
|
|
69
|
+
}
|
|
70
|
+
if (initialParams.gasPrice && !initialParams.maxPriorityFeePerGas) {
|
|
71
|
+
log(
|
|
72
|
+
"Setting maxFeePerGas to gasPrice from request",
|
|
73
|
+
initialParams.gasPrice
|
|
74
|
+
);
|
|
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(
|
|
83
|
+
"Setting maxFeePerGas to suggested gasPrice",
|
|
84
|
+
suggestedGasFees.gasPrice
|
|
85
|
+
);
|
|
86
|
+
return suggestedGasFees.gasPrice;
|
|
87
|
+
}
|
|
88
|
+
log("maxFeePerGas not set");
|
|
89
|
+
return void 0;
|
|
90
|
+
}
|
|
91
|
+
function getMaxPriorityFeePerGas(request) {
|
|
92
|
+
const { eip1559, initialParams, savedGasFees, suggestedGasFees, txMeta } = request;
|
|
93
|
+
if (!eip1559) {
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
if (savedGasFees) {
|
|
97
|
+
const maxPriorityFeePerGas = gweiDecimalToWeiHex(savedGasFees.priorityFee);
|
|
98
|
+
log(
|
|
99
|
+
"Using maxPriorityFeePerGas from savedGasFees.priorityFee",
|
|
100
|
+
maxPriorityFeePerGas
|
|
101
|
+
);
|
|
102
|
+
return maxPriorityFeePerGas;
|
|
103
|
+
}
|
|
104
|
+
if (initialParams.maxPriorityFeePerGas) {
|
|
105
|
+
log(
|
|
106
|
+
"Using maxPriorityFeePerGas from request",
|
|
107
|
+
initialParams.maxPriorityFeePerGas
|
|
108
|
+
);
|
|
109
|
+
return initialParams.maxPriorityFeePerGas;
|
|
110
|
+
}
|
|
111
|
+
if (initialParams.gasPrice && !initialParams.maxFeePerGas) {
|
|
112
|
+
log(
|
|
113
|
+
"Setting maxPriorityFeePerGas to gasPrice from request",
|
|
114
|
+
initialParams.gasPrice
|
|
115
|
+
);
|
|
116
|
+
return initialParams.gasPrice;
|
|
117
|
+
}
|
|
118
|
+
if (suggestedGasFees.maxPriorityFeePerGas) {
|
|
119
|
+
log(
|
|
120
|
+
"Using suggested maxPriorityFeePerGas",
|
|
121
|
+
suggestedGasFees.maxPriorityFeePerGas
|
|
122
|
+
);
|
|
123
|
+
return suggestedGasFees.maxPriorityFeePerGas;
|
|
124
|
+
}
|
|
125
|
+
if (txMeta.txParams.maxFeePerGas) {
|
|
126
|
+
log(
|
|
127
|
+
"Setting maxPriorityFeePerGas to maxFeePerGas",
|
|
128
|
+
txMeta.txParams.maxFeePerGas
|
|
129
|
+
);
|
|
130
|
+
return txMeta.txParams.maxFeePerGas;
|
|
131
|
+
}
|
|
132
|
+
log("maxPriorityFeePerGas not set");
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
function getGasPrice(request) {
|
|
136
|
+
const { eip1559, initialParams, suggestedGasFees } = request;
|
|
137
|
+
if (eip1559) {
|
|
138
|
+
return void 0;
|
|
139
|
+
}
|
|
140
|
+
if (initialParams.gasPrice) {
|
|
141
|
+
log("Using gasPrice from request", initialParams.gasPrice);
|
|
142
|
+
return initialParams.gasPrice;
|
|
143
|
+
}
|
|
144
|
+
if (suggestedGasFees.maxFeePerGas) {
|
|
145
|
+
log("Using suggested maxFeePerGas", suggestedGasFees.maxFeePerGas);
|
|
146
|
+
return suggestedGasFees.maxFeePerGas;
|
|
147
|
+
}
|
|
148
|
+
if (suggestedGasFees.gasPrice) {
|
|
149
|
+
log("Using suggested gasPrice", suggestedGasFees.gasPrice);
|
|
150
|
+
return suggestedGasFees.gasPrice;
|
|
151
|
+
}
|
|
152
|
+
log("gasPrice not set");
|
|
153
|
+
return void 0;
|
|
154
|
+
}
|
|
155
|
+
function getUserFeeLevel(request) {
|
|
156
|
+
const { eip1559, initialParams, savedGasFees, suggestedGasFees, txMeta } = request;
|
|
157
|
+
if (!eip1559) {
|
|
158
|
+
return void 0;
|
|
159
|
+
}
|
|
160
|
+
if (savedGasFees) {
|
|
161
|
+
return "custom" /* CUSTOM */;
|
|
162
|
+
}
|
|
163
|
+
if (!initialParams.maxFeePerGas && !initialParams.maxPriorityFeePerGas && initialParams.gasPrice) {
|
|
164
|
+
return txMeta.origin === _controllerutils.ORIGIN_METAMASK ? "custom" /* CUSTOM */ : "dappSuggested" /* DAPP_SUGGESTED */;
|
|
165
|
+
}
|
|
166
|
+
if (!initialParams.maxFeePerGas && !initialParams.maxPriorityFeePerGas && suggestedGasFees.maxFeePerGas && suggestedGasFees.maxPriorityFeePerGas) {
|
|
167
|
+
return "medium" /* MEDIUM */;
|
|
168
|
+
}
|
|
169
|
+
if (txMeta.origin === _controllerutils.ORIGIN_METAMASK) {
|
|
170
|
+
return "medium" /* MEDIUM */;
|
|
171
|
+
}
|
|
172
|
+
return "dappSuggested" /* DAPP_SUGGESTED */;
|
|
173
|
+
}
|
|
174
|
+
function updateDefaultGasEstimates(txMeta) {
|
|
175
|
+
if (!txMeta.defaultGasEstimates) {
|
|
176
|
+
txMeta.defaultGasEstimates = {};
|
|
177
|
+
}
|
|
178
|
+
txMeta.defaultGasEstimates.maxFeePerGas = txMeta.txParams.maxFeePerGas;
|
|
179
|
+
txMeta.defaultGasEstimates.maxPriorityFeePerGas = txMeta.txParams.maxPriorityFeePerGas;
|
|
180
|
+
txMeta.defaultGasEstimates.gasPrice = txMeta.txParams.gasPrice;
|
|
181
|
+
txMeta.defaultGasEstimates.estimateType = txMeta.userFeeLevel;
|
|
182
|
+
}
|
|
183
|
+
async function getSuggestedGasFees(request) {
|
|
184
|
+
const { eip1559, ethQuery, gasFeeFlows, getGasFeeEstimates, txMeta } = request;
|
|
185
|
+
if (!eip1559 && txMeta.txParams.gasPrice || eip1559 && txMeta.txParams.maxFeePerGas && txMeta.txParams.maxPriorityFeePerGas) {
|
|
186
|
+
return {};
|
|
187
|
+
}
|
|
188
|
+
const gasFeeFlow = _chunkO7H2MC7Rjs.getGasFeeFlow.call(void 0, txMeta, gasFeeFlows);
|
|
189
|
+
try {
|
|
190
|
+
const response = await gasFeeFlow.getGasFees({
|
|
191
|
+
ethQuery,
|
|
192
|
+
getGasFeeControllerEstimates: getGasFeeEstimates,
|
|
193
|
+
transactionMeta: txMeta
|
|
194
|
+
});
|
|
195
|
+
return response.estimates.medium;
|
|
196
|
+
} catch (error) {
|
|
197
|
+
log("Failed to get suggested gas fees", error);
|
|
198
|
+
}
|
|
199
|
+
const gasPriceDecimal = await _controllerutils.query.call(void 0, ethQuery, "gasPrice");
|
|
200
|
+
const gasPrice = gasPriceDecimal ? _utils.add0x.call(void 0, gasPriceDecimal.toString(16)) : void 0;
|
|
201
|
+
return { gasPrice };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
exports.updateGasFees = updateGasFees;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WAAW,OAAO,SAAS;AACtD,SAAO,oBAAoB,eAAe,OAAO;AACnD;AAEA,eAAe,oBACb,SAC2B;AAC3B,QAAM,EAAE,SAAS,UAAU,aAAa,oBAAoB,OAAO,IACjE;AAEF,MACG,CAAC,WAAW,OAAO,SAAS,YAC5B,WACC,OAAO,SAAS,gBAChB,OAAO,SAAS,sBAClB;AACA,WAAO,CAAC;AAAA,EACV;AAEA,QAAM,aAAa,cAAc,QAAQ,WAAW;AAEpD,MAAI;AACF,UAAM,WAAW,MAAM,WAAW,WAAW;AAAA,MAC3C;AAAA,MACA,8BAA8B;AAAA,MAC9B,iBAAiB;AAAA,IACnB,CAAC;AAED,WAAO,SAAS,UAAU;AAAA,EAC5B,SAAS,OAAO;AACd,QAAI,oCAAoC,KAAK;AAAA,EAC/C;AAEA,QAAM,kBAAmB,MAAM,MAAM,UAAU,UAAU;AAEzD,QAAM,WAAW,kBACb,MAAM,gBAAgB,SAAS,EAAE,CAAC,IAClC;AAEJ,SAAO,EAAE,SAAS;AACpB","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"]}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {
|
|
2
|
+
simulateTransactions
|
|
3
|
+
} from "./chunk-7XPI7KU5.mjs";
|
|
4
|
+
import {
|
|
5
|
+
projectLogger
|
|
6
|
+
} from "./chunk-UQQWZT6C.mjs";
|
|
7
|
+
|
|
8
|
+
// src/utils/simulation.ts
|
|
9
|
+
import { Interface } from "@ethersproject/abi";
|
|
10
|
+
import { hexToBN, toHex } from "@metamask/controller-utils";
|
|
11
|
+
import { abiERC20, abiERC721, abiERC1155 } from "@metamask/metamask-eth-abis";
|
|
12
|
+
import { createModuleLogger } from "@metamask/utils";
|
|
13
|
+
var log = createModuleLogger(projectLogger, "simulation");
|
|
14
|
+
async function getSimulationData(request) {
|
|
15
|
+
const { chainId, from, to, value, data } = request;
|
|
16
|
+
log("Getting simulation data", request);
|
|
17
|
+
try {
|
|
18
|
+
const response = await simulateTransactions(chainId, {
|
|
19
|
+
transactions: [{ from, to, value, data }],
|
|
20
|
+
withCallTrace: true,
|
|
21
|
+
withLogs: true
|
|
22
|
+
});
|
|
23
|
+
const nativeBalanceChange = getNativeBalanceChange(request.from, response);
|
|
24
|
+
const events = getEvents(response);
|
|
25
|
+
log("Parsed events", events);
|
|
26
|
+
const tokenBalanceChanges = await getTokenBalanceChanges(request, events);
|
|
27
|
+
return {
|
|
28
|
+
nativeBalanceChange,
|
|
29
|
+
tokenBalanceChanges
|
|
30
|
+
};
|
|
31
|
+
} catch (error) {
|
|
32
|
+
log("Failed to get simulation data", error, request);
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function getNativeBalanceChange(userAddress, response) {
|
|
37
|
+
const transactionResponse = response.transactions[0];
|
|
38
|
+
if (!transactionResponse) {
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
const { stateDiff } = transactionResponse;
|
|
42
|
+
const previousBalance = stateDiff.pre[userAddress]?.balance;
|
|
43
|
+
const newBalance = stateDiff.post[userAddress]?.balance;
|
|
44
|
+
if (!previousBalance || !newBalance) {
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
return getSimulationBalanceChange(previousBalance, newBalance);
|
|
48
|
+
}
|
|
49
|
+
function getEvents(response) {
|
|
50
|
+
const logs = extractLogs(response.transactions[0]?.callTrace ?? {});
|
|
51
|
+
log("Extracted logs", logs);
|
|
52
|
+
const erc20Interface = new Interface(abiERC20);
|
|
53
|
+
const erc721Interface = new Interface(abiERC721);
|
|
54
|
+
const erc1155Interface = new Interface(abiERC1155);
|
|
55
|
+
return logs.map((currentLog) => {
|
|
56
|
+
const event = parseLog(
|
|
57
|
+
currentLog,
|
|
58
|
+
erc20Interface,
|
|
59
|
+
erc721Interface,
|
|
60
|
+
erc1155Interface
|
|
61
|
+
);
|
|
62
|
+
if (!event) {
|
|
63
|
+
log("Failed to parse log", currentLog);
|
|
64
|
+
return void 0;
|
|
65
|
+
}
|
|
66
|
+
const inputs = event.abi.find((e) => e.name === event.name)?.inputs;
|
|
67
|
+
if (!inputs) {
|
|
68
|
+
log("Failed to find inputs for event", event);
|
|
69
|
+
return void 0;
|
|
70
|
+
}
|
|
71
|
+
const args = parseEventArgs(event.args, inputs);
|
|
72
|
+
return {
|
|
73
|
+
contractAddress: currentLog.address,
|
|
74
|
+
tokenStandard: event.standard,
|
|
75
|
+
name: event.name,
|
|
76
|
+
args,
|
|
77
|
+
abi: event.abi
|
|
78
|
+
};
|
|
79
|
+
}).filter((e) => e !== void 0);
|
|
80
|
+
}
|
|
81
|
+
function parseEventArgs(args, abiInputs) {
|
|
82
|
+
return args.reduce((result, arg, index) => {
|
|
83
|
+
const name = abiInputs[index].name.replace("_", "");
|
|
84
|
+
const value = parseEventArgValue(arg);
|
|
85
|
+
result[name] = value;
|
|
86
|
+
return result;
|
|
87
|
+
}, {});
|
|
88
|
+
}
|
|
89
|
+
function parseEventArgValue(value) {
|
|
90
|
+
if (Array.isArray(value)) {
|
|
91
|
+
return value.map(parseEventArgValue);
|
|
92
|
+
}
|
|
93
|
+
return (value.toHexString?.() ?? value).toLowerCase();
|
|
94
|
+
}
|
|
95
|
+
async function getTokenBalanceChanges(request, events) {
|
|
96
|
+
const balanceTransactionsByToken = getTokenBalanceTransactions(
|
|
97
|
+
request,
|
|
98
|
+
events
|
|
99
|
+
);
|
|
100
|
+
const balanceTransactions = [...balanceTransactionsByToken.values()];
|
|
101
|
+
log("Generated balance transactions", balanceTransactions);
|
|
102
|
+
if (!balanceTransactions.length) {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
const response = await simulateTransactions(request.chainId, {
|
|
106
|
+
transactions: [...balanceTransactions, request, ...balanceTransactions]
|
|
107
|
+
});
|
|
108
|
+
log("Balance simulation response", response);
|
|
109
|
+
if (response.transactions.length !== balanceTransactions.length * 2 + 1) {
|
|
110
|
+
throw new Error("Invalid response from simulation API");
|
|
111
|
+
}
|
|
112
|
+
return [...balanceTransactionsByToken.keys()].map((token, index) => {
|
|
113
|
+
const previousBalance = normalizeReturnValue(
|
|
114
|
+
response.transactions[index].return
|
|
115
|
+
);
|
|
116
|
+
const newBalance = normalizeReturnValue(
|
|
117
|
+
response.transactions[index + balanceTransactions.length + 1].return
|
|
118
|
+
);
|
|
119
|
+
const balanceChange = getSimulationBalanceChange(
|
|
120
|
+
previousBalance,
|
|
121
|
+
newBalance
|
|
122
|
+
);
|
|
123
|
+
if (!balanceChange) {
|
|
124
|
+
return void 0;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
...token,
|
|
128
|
+
...balanceChange
|
|
129
|
+
};
|
|
130
|
+
}).filter((change) => change !== void 0);
|
|
131
|
+
}
|
|
132
|
+
function getTokenBalanceTransactions(request, events) {
|
|
133
|
+
const tokenKeys = /* @__PURE__ */ new Set();
|
|
134
|
+
return events.reduce((result, event) => {
|
|
135
|
+
if (!["Transfer", "TransferSingle", "TransferBatch"].includes(event.name) || ![event.args.from, event.args.to].includes(request.from)) {
|
|
136
|
+
log("Ignoring event", event);
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
let tokenIds = [void 0];
|
|
140
|
+
if (event.tokenStandard === "erc721" /* erc721 */) {
|
|
141
|
+
tokenIds = [event.args.tokenId];
|
|
142
|
+
}
|
|
143
|
+
if (event.tokenStandard === "erc1155" /* erc1155 */ && event.name === "TransferSingle") {
|
|
144
|
+
tokenIds = [event.args.id];
|
|
145
|
+
}
|
|
146
|
+
if (event.tokenStandard === "erc1155" /* erc1155 */ && event.name === "TransferBatch") {
|
|
147
|
+
tokenIds = event.args.ids;
|
|
148
|
+
}
|
|
149
|
+
log("Extracted token ids", tokenIds);
|
|
150
|
+
for (const tokenId of tokenIds) {
|
|
151
|
+
const simulationToken = {
|
|
152
|
+
address: event.contractAddress,
|
|
153
|
+
standard: event.tokenStandard,
|
|
154
|
+
id: tokenId
|
|
155
|
+
};
|
|
156
|
+
const tokenKey = JSON.stringify(simulationToken);
|
|
157
|
+
if (tokenKeys.has(tokenKey)) {
|
|
158
|
+
log(
|
|
159
|
+
"Ignoring additional event with same contract and token ID",
|
|
160
|
+
simulationToken
|
|
161
|
+
);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
tokenKeys.add(tokenKey);
|
|
165
|
+
const parameters = [request.from];
|
|
166
|
+
if (event.tokenStandard === "erc1155" /* erc1155 */) {
|
|
167
|
+
parameters.push(tokenId);
|
|
168
|
+
}
|
|
169
|
+
result.set(simulationToken, {
|
|
170
|
+
from: request.from,
|
|
171
|
+
to: event.contractAddress,
|
|
172
|
+
data: new Interface(event.abi).encodeFunctionData(
|
|
173
|
+
"balanceOf",
|
|
174
|
+
parameters
|
|
175
|
+
)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}, /* @__PURE__ */ new Map());
|
|
180
|
+
}
|
|
181
|
+
function parseLog(eventLog, erc20, erc721, erc1155) {
|
|
182
|
+
const abisByStandard = [
|
|
183
|
+
{
|
|
184
|
+
abi: abiERC20,
|
|
185
|
+
contractInterface: erc20,
|
|
186
|
+
standard: "erc20" /* erc20 */
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
abi: abiERC721,
|
|
190
|
+
contractInterface: erc721,
|
|
191
|
+
standard: "erc721" /* erc721 */
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
abi: abiERC1155,
|
|
195
|
+
contractInterface: erc1155,
|
|
196
|
+
standard: "erc1155" /* erc1155 */
|
|
197
|
+
}
|
|
198
|
+
];
|
|
199
|
+
for (const { abi, contractInterface, standard } of abisByStandard) {
|
|
200
|
+
try {
|
|
201
|
+
return {
|
|
202
|
+
...contractInterface.parseLog(eventLog),
|
|
203
|
+
abi,
|
|
204
|
+
standard
|
|
205
|
+
};
|
|
206
|
+
} catch (e) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return void 0;
|
|
211
|
+
}
|
|
212
|
+
function extractLogs(call) {
|
|
213
|
+
const logs = call.logs ?? [];
|
|
214
|
+
const nestedCalls = call.calls ?? [];
|
|
215
|
+
return [
|
|
216
|
+
...logs,
|
|
217
|
+
...nestedCalls.map((nestedCall) => extractLogs(nestedCall)).flat()
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
function getSimulationBalanceChange(previousBalance, newBalance) {
|
|
221
|
+
const differenceBN = hexToBN(newBalance).sub(hexToBN(previousBalance));
|
|
222
|
+
const isDecrease = differenceBN.isNeg();
|
|
223
|
+
const difference = toHex(differenceBN.abs());
|
|
224
|
+
if (differenceBN.isZero()) {
|
|
225
|
+
log("Balance change is zero");
|
|
226
|
+
return void 0;
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
previousBalance,
|
|
230
|
+
newBalance,
|
|
231
|
+
difference,
|
|
232
|
+
isDecrease
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function normalizeReturnValue(value) {
|
|
236
|
+
return toHex(hexToBN(value));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export {
|
|
240
|
+
getSimulationData
|
|
241
|
+
};
|
|
242
|
+
//# sourceMappingURL=chunk-QSBIXUMB.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/simulation.ts"],"sourcesContent":["import type { Fragment, LogDescription, Result } from '@ethersproject/abi';\nimport { Interface } from '@ethersproject/abi';\nimport { hexToBN, toHex } from '@metamask/controller-utils';\nimport { abiERC20, abiERC721, abiERC1155 } from '@metamask/metamask-eth-abis';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type {\n SimulationBalanceChange,\n SimulationData,\n SimulationTokenBalanceChange,\n SimulationToken,\n} from '../types';\nimport { SimulationTokenStandard } from '../types';\nimport type {\n SimulationResponseLog,\n SimulationRequestTransaction,\n SimulationResponse,\n SimulationResponseCallTrace,\n} from './simulation-api';\nimport { simulateTransactions } from './simulation-api';\n\nconst log = createModuleLogger(projectLogger, 'simulation');\n\ntype ABI = Fragment[];\n\nexport type GetSimulationDataRequest = {\n chainId: Hex;\n from: Hex;\n to?: Hex;\n value?: Hex;\n data?: Hex;\n};\n\ntype ParsedEvent = {\n contractAddress: Hex;\n tokenStandard: SimulationTokenStandard;\n name: string;\n args: Record<string, Hex | Hex[]>;\n abi: ABI;\n};\n\n/**\n * Generate simulation data for a transaction.\n * @param request - The transaction to simulate.\n * @param request.chainId - The chain ID of the transaction.\n * @param request.from - The sender of the transaction.\n * @param request.to - The recipient of the transaction.\n * @param request.value - The value of the transaction.\n * @param request.data - The data of the transaction.\n * @returns The simulation data.\n */\nexport async function getSimulationData(\n request: GetSimulationDataRequest,\n): Promise<SimulationData | undefined> {\n const { chainId, from, to, value, data } = request;\n\n log('Getting simulation data', request);\n\n try {\n const response = await simulateTransactions(chainId, {\n transactions: [{ from, to, value, data }],\n withCallTrace: true,\n withLogs: true,\n });\n\n const nativeBalanceChange = getNativeBalanceChange(request.from, response);\n const events = getEvents(response);\n\n log('Parsed events', events);\n\n const tokenBalanceChanges = await getTokenBalanceChanges(request, events);\n\n return {\n nativeBalanceChange,\n tokenBalanceChanges,\n };\n } catch (error) {\n log('Failed to get simulation data', error, request);\n return undefined;\n }\n}\n\n/**\n * Extract the native balance change from a simulation response.\n * @param userAddress - The user's account address.\n * @param response - The simulation response.\n * @returns The native balance change or undefined if unchanged.\n */\nfunction getNativeBalanceChange(\n userAddress: Hex,\n response: SimulationResponse,\n): SimulationBalanceChange | undefined {\n const transactionResponse = response.transactions[0];\n\n /* istanbul ignore next */\n if (!transactionResponse) {\n return undefined;\n }\n\n const { stateDiff } = transactionResponse;\n const previousBalance = stateDiff.pre[userAddress]?.balance;\n const newBalance = stateDiff.post[userAddress]?.balance;\n\n if (!previousBalance || !newBalance) {\n return undefined;\n }\n\n return getSimulationBalanceChange(previousBalance, newBalance);\n}\n\n/**\n * Extract events from a simulation response.\n * @param response - The simulation response.\n * @returns The parsed events.\n */\nfunction getEvents(response: SimulationResponse): ParsedEvent[] {\n /* istanbul ignore next */\n const logs = extractLogs(response.transactions[0]?.callTrace ?? {});\n\n log('Extracted logs', logs);\n\n const erc20Interface = new Interface(abiERC20);\n const erc721Interface = new Interface(abiERC721);\n const erc1155Interface = new Interface(abiERC1155);\n\n return logs\n .map((currentLog) => {\n const event = parseLog(\n currentLog,\n erc20Interface,\n erc721Interface,\n erc1155Interface,\n );\n\n if (!event) {\n log('Failed to parse log', currentLog);\n return undefined;\n }\n\n /* istanbul ignore next */\n const inputs = event.abi.find((e) => e.name === event.name)?.inputs;\n\n /* istanbul ignore if */\n if (!inputs) {\n log('Failed to find inputs for event', event);\n return undefined;\n }\n\n const args = parseEventArgs(event.args, inputs);\n\n return {\n contractAddress: currentLog.address,\n tokenStandard: event.standard,\n name: event.name,\n args,\n abi: event.abi,\n };\n })\n .filter((e) => e !== undefined) as ParsedEvent[];\n}\n\n/**\n * Parse event arguments using ABI input definitions.\n * @param args - The raw event arguments.\n * @param abiInputs - The ABI input definitions.\n * @returns The parsed event arguments.\n */\nfunction parseEventArgs(\n args: Result,\n abiInputs: { name: string }[],\n): Record<string, Hex | Hex[]> {\n return args.reduce((result, arg, index) => {\n const name = abiInputs[index].name.replace('_', '');\n const value = parseEventArgValue(arg);\n\n result[name] = value;\n\n return result;\n }, {});\n}\n\n/**\n * Parse an event argument value.\n * @param value - The event argument value.\n * @returns The parsed event argument value.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction parseEventArgValue(value: any): Hex | Hex[] {\n if (Array.isArray(value)) {\n return value.map(parseEventArgValue) as Hex[];\n }\n\n return (value.toHexString?.() ?? value).toLowerCase();\n}\n\n/**\n * Generate token balance changes from parsed events.\n * @param request - The transaction that was simulated.\n * @param events - The parsed events.\n * @returns An array of token balance changes.\n */\nasync function getTokenBalanceChanges(\n request: GetSimulationDataRequest,\n events: ParsedEvent[],\n): Promise<SimulationTokenBalanceChange[]> {\n const balanceTransactionsByToken = getTokenBalanceTransactions(\n request,\n events,\n );\n\n const balanceTransactions = [...balanceTransactionsByToken.values()];\n\n log('Generated balance transactions', balanceTransactions);\n\n if (!balanceTransactions.length) {\n return [];\n }\n\n const response = await simulateTransactions(request.chainId as Hex, {\n transactions: [...balanceTransactions, request, ...balanceTransactions],\n });\n\n log('Balance simulation response', response);\n\n if (response.transactions.length !== balanceTransactions.length * 2 + 1) {\n throw new Error('Invalid response from simulation API');\n }\n\n return [...balanceTransactionsByToken.keys()]\n .map((token, index) => {\n const previousBalance = normalizeReturnValue(\n response.transactions[index].return,\n );\n\n const newBalance = normalizeReturnValue(\n response.transactions[index + balanceTransactions.length + 1].return,\n );\n\n const balanceChange = getSimulationBalanceChange(\n previousBalance,\n newBalance,\n );\n\n if (!balanceChange) {\n return undefined;\n }\n\n return {\n ...token,\n ...balanceChange,\n };\n })\n .filter((change) => change !== undefined) as SimulationTokenBalanceChange[];\n}\n\n/**\n * Generate transactions to check token balances.\n * @param request - The transaction that was simulated.\n * @param events - The parsed events.\n * @returns A map of token balance transactions keyed by token.\n */\nfunction getTokenBalanceTransactions(\n request: GetSimulationDataRequest,\n events: ParsedEvent[],\n): Map<SimulationToken, SimulationRequestTransaction> {\n const tokenKeys = new Set();\n\n return events.reduce((result, event) => {\n if (\n !['Transfer', 'TransferSingle', 'TransferBatch'].includes(event.name) ||\n ![event.args.from, event.args.to].includes(request.from)\n ) {\n log('Ignoring event', event);\n return result;\n }\n\n // ERC-20 does not have a token ID so default to undefined.\n let tokenIds: (Hex | undefined)[] = [undefined];\n\n if (event.tokenStandard === SimulationTokenStandard.erc721) {\n tokenIds = [event.args.tokenId as Hex];\n }\n\n if (\n event.tokenStandard === SimulationTokenStandard.erc1155 &&\n event.name === 'TransferSingle'\n ) {\n tokenIds = [event.args.id as Hex];\n }\n\n if (\n event.tokenStandard === SimulationTokenStandard.erc1155 &&\n event.name === 'TransferBatch'\n ) {\n tokenIds = event.args.ids as Hex[];\n }\n\n log('Extracted token ids', tokenIds);\n\n for (const tokenId of tokenIds) {\n const simulationToken: SimulationToken = {\n address: event.contractAddress,\n standard: event.tokenStandard,\n id: tokenId,\n };\n\n const tokenKey = JSON.stringify(simulationToken);\n\n if (tokenKeys.has(tokenKey)) {\n log(\n 'Ignoring additional event with same contract and token ID',\n simulationToken,\n );\n continue;\n }\n\n tokenKeys.add(tokenKey);\n\n const parameters = [request.from];\n\n if (event.tokenStandard === SimulationTokenStandard.erc1155) {\n parameters.push(tokenId as Hex);\n }\n\n result.set(simulationToken, {\n from: request.from,\n to: event.contractAddress,\n data: new Interface(event.abi).encodeFunctionData(\n 'balanceOf',\n parameters,\n ) as Hex,\n });\n }\n\n return result;\n }, new Map<SimulationToken, SimulationRequestTransaction>());\n}\n\n/**\n * Parse a raw event log using known ABIs.\n * @param eventLog - The raw event log.\n * @param erc20 - The ERC-20 ABI interface.\n * @param erc721 - The ERC-721 ABI interface.\n * @param erc1155 - The ERC-1155 ABI interface.\n * @returns The parsed event log or undefined if it could not be parsed.\n */\nfunction parseLog(\n eventLog: SimulationResponseLog,\n erc20: Interface,\n erc721: Interface,\n erc1155: Interface,\n):\n | (LogDescription & { abi: ABI; standard: SimulationTokenStandard })\n | undefined {\n const abisByStandard = [\n {\n abi: abiERC20,\n contractInterface: erc20,\n standard: SimulationTokenStandard.erc20,\n },\n {\n abi: abiERC721,\n contractInterface: erc721,\n standard: SimulationTokenStandard.erc721,\n },\n {\n abi: abiERC1155,\n contractInterface: erc1155,\n standard: SimulationTokenStandard.erc1155,\n },\n ];\n\n for (const { abi, contractInterface, standard } of abisByStandard) {\n try {\n return {\n ...contractInterface.parseLog(eventLog),\n abi,\n standard,\n };\n } catch (e) {\n continue;\n }\n }\n\n return undefined;\n}\n\n/**\n * Extract all logs from a call trace tree.\n * @param call - The root call trace.\n * @returns An array of logs.\n */\nfunction extractLogs(\n call: SimulationResponseCallTrace,\n): SimulationResponseLog[] {\n /* istanbul ignore next */\n const logs = call.logs ?? [];\n\n /* istanbul ignore next */\n const nestedCalls = call.calls ?? [];\n\n return [\n ...logs,\n ...nestedCalls.map((nestedCall) => extractLogs(nestedCall)).flat(),\n ];\n}\n\n/**\n * Generate balance change data from previous and new balances.\n * @param previousBalance - The previous balance.\n * @param newBalance - The new balance.\n * @returns The balance change data or undefined if unchanged.\n */\nfunction getSimulationBalanceChange(\n previousBalance: Hex,\n newBalance: Hex,\n): SimulationBalanceChange | undefined {\n const differenceBN = hexToBN(newBalance).sub(hexToBN(previousBalance));\n const isDecrease = differenceBN.isNeg();\n const difference = toHex(differenceBN.abs());\n\n if (differenceBN.isZero()) {\n log('Balance change is zero');\n return undefined;\n }\n\n return {\n previousBalance,\n newBalance,\n difference,\n isDecrease,\n };\n}\n\n/**\n * Normalize a return value.\n * @param value - The return value to normalize.\n * @returns The normalized return value.\n */\nfunction normalizeReturnValue(value: Hex): Hex {\n return toHex(hexToBN(value));\n}\n"],"mappings":";;;;;;;;AACA,SAAS,iBAAiB;AAC1B,SAAS,SAAS,aAAa;AAC/B,SAAS,UAAU,WAAW,kBAAkB;AAChD,SAAS,0BAAoC;AAkB7C,IAAM,MAAM,mBAAmB,eAAe,YAAY;AA8B1D,eAAsB,kBACpB,SACqC;AACrC,QAAM,EAAE,SAAS,MAAM,IAAI,OAAO,KAAK,IAAI;AAE3C,MAAI,2BAA2B,OAAO;AAEtC,MAAI;AACF,UAAM,WAAW,MAAM,qBAAqB,SAAS;AAAA,MACnD,cAAc,CAAC,EAAE,MAAM,IAAI,OAAO,KAAK,CAAC;AAAA,MACxC,eAAe;AAAA,MACf,UAAU;AAAA,IACZ,CAAC;AAED,UAAM,sBAAsB,uBAAuB,QAAQ,MAAM,QAAQ;AACzE,UAAM,S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AAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAOA,SAAS,qBAAqB,OAAiB;AAC7C,SAAO,MAAM,QAAQ,KAAK,CAAC;AAC7B;","names":[]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkS6VGOPUYjs = require('./chunk-S6VGOPUY.js');
|
|
4
|
+
|
|
5
|
+
// src/utils/layer1-gas-fee-flow.ts
|
|
6
|
+
var _utils = require('@metamask/utils');
|
|
7
|
+
var log = _utils.createModuleLogger.call(void 0, _chunkS6VGOPUYjs.projectLogger, "layer-1-gas-fee-flow");
|
|
8
|
+
async function updateTransactionLayer1GasFee(request) {
|
|
9
|
+
const layer1GasFee = await getTransactionLayer1GasFee(request);
|
|
10
|
+
if (layer1GasFee) {
|
|
11
|
+
const { transactionMeta } = request;
|
|
12
|
+
transactionMeta.layer1GasFee = layer1GasFee;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function getLayer1GasFeeFlow(transactionMeta, layer1GasFeeFlows) {
|
|
16
|
+
return layer1GasFeeFlows.find(
|
|
17
|
+
(layer1GasFeeFlow) => layer1GasFeeFlow.matchesTransaction(transactionMeta)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
async function getTransactionLayer1GasFee({
|
|
21
|
+
ethQuery,
|
|
22
|
+
transactionMeta,
|
|
23
|
+
layer1GasFeeFlows
|
|
24
|
+
}) {
|
|
25
|
+
const layer1GasFeeFlow = getLayer1GasFeeFlow(
|
|
26
|
+
transactionMeta,
|
|
27
|
+
layer1GasFeeFlows
|
|
28
|
+
);
|
|
29
|
+
if (!layer1GasFeeFlow) {
|
|
30
|
+
log("Layer 1 gas fee flow not found", transactionMeta.id);
|
|
31
|
+
return void 0;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const { layer1Fee } = await layer1GasFeeFlow.getLayer1Fee({
|
|
35
|
+
ethQuery,
|
|
36
|
+
transactionMeta
|
|
37
|
+
});
|
|
38
|
+
return layer1Fee;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
log("Failed to get layer 1 gas fee", transactionMeta.id, error);
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
exports.updateTransactionLayer1GasFee = updateTransactionLayer1GasFee;
|
|
48
|
+
//# sourceMappingURL=chunk-R7NJVDWN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/layer1-gas-fee-flow.ts"],"names":[],"mappings":";;;;;AACA,SAAS,0BAAoC;AAK7C,IAAM,MAAM,mBAAmB,eAAe,sBAAsB;AAepE,eAAsB,8BACpB,SACA;AACA,QAAM,eAAe,MAAM,2BAA2B,OAAO;AAE7D,MAAI,cAAc;AAChB,UAAM,EAAE,gBAAgB,IAAI;AAC5B,oBAAgB,eAAe;AAAA,EACjC;AACF;AAQA,SAAS,oBACP,iBACA,mBAC8B;AAC9B,SAAO,kBAAkB;AAAA,IAAK,CAAC,qBAC7B,iBAAiB,mBAAmB,eAAe;AAAA,EACrD;AACF;AASA,eAAe,2BAA2B;AAAA,EACxC;AAAA,EACA;AAAA,EACA;AACF,GAAwD;AACtD,QAAM,mBAAmB;AAAA,IACvB;AAAA,IACA;AAAA,EACF;AACA,MAAI,CAAC,kBAAkB;AACrB,QAAI,kCAAkC,gBAAgB,EAAE;AACxD,WAAO;AAAA,EACT;AAEA,MAAI;AACF,UAAM,EAAE,UAAU,IAAI,MAAM,iBAAiB,aAAa;AAAA,MACxD;AAAA,MACA;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT,SAAS,OAAO;AACd,QAAI,iCAAiC,gBAAgB,IAAI,KAAK;AAC9D,WAAO;AAAA,EACT;AACF","sourcesContent":["import type EthQuery from '@metamask/eth-query';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type { Layer1GasFeeFlow, TransactionMeta } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'layer-1-gas-fee-flow');\n\nexport type UpdateLayer1GasFeeRequest = {\n ethQuery: EthQuery;\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n transactionMeta: TransactionMeta;\n};\n\n/**\n * Updates the given transactionMeta with the layer 1 gas fee.\n * @param request - The request to use when getting the layer 1 gas fee.\n * @param request.provider - Provider used to create a new underlying EthQuery instance\n * @param request.transactionMeta - The transaction to get the layer 1 gas fee for.\n * @param request.layer1GasFeeFlows - The layer 1 gas fee flows to search.\n */\nexport async function updateTransactionLayer1GasFee(\n request: UpdateLayer1GasFeeRequest,\n) {\n const layer1GasFee = await getTransactionLayer1GasFee(request);\n\n if (layer1GasFee) {\n const { transactionMeta } = request;\n transactionMeta.layer1GasFee = layer1GasFee;\n }\n}\n\n/**\n * Get the layer 1 gas fee flow for a transaction.\n * @param transactionMeta - The transaction to get the layer 1 gas fee flow for.\n * @param layer1GasFeeFlows - The layer 1 gas fee flows to search.\n * @returns The layer 1 gas fee flow for the transaction, or undefined if none match.\n */\nfunction getLayer1GasFeeFlow(\n transactionMeta: TransactionMeta,\n layer1GasFeeFlows: Layer1GasFeeFlow[],\n): Layer1GasFeeFlow | undefined {\n return layer1GasFeeFlows.find((layer1GasFeeFlow) =>\n layer1GasFeeFlow.matchesTransaction(transactionMeta),\n );\n}\n\n/**\n * Get the layer 1 gas fee for a transaction and return the layer1Fee.\n * @param request - The request to use when getting the layer 1 gas fee.\n * @param request.ethQuery - The EthQuery instance to use to get the layer 1 gas fee.\n * @param request.layer1GasFeeFlows - The layer 1 gas fee flows to search.\n * @param request.transactionMeta - The transaction to get the layer 1 gas fee for.\n */\nasync function getTransactionLayer1GasFee({\n ethQuery,\n transactionMeta,\n layer1GasFeeFlows,\n}: UpdateLayer1GasFeeRequest): Promise<Hex | undefined> {\n const layer1GasFeeFlow = getLayer1GasFeeFlow(\n transactionMeta,\n layer1GasFeeFlows,\n );\n if (!layer1GasFeeFlow) {\n log('Layer 1 gas fee flow not found', transactionMeta.id);\n return undefined;\n }\n\n try {\n const { layer1Fee } = await layer1GasFeeFlow.getLayer1Fee({\n ethQuery,\n transactionMeta,\n });\n return layer1Fee;\n } catch (error) {\n log('Failed to get layer 1 gas fee', transactionMeta.id, error);\n return undefined;\n }\n}\n"]}
|