@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,137 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkITDY6AIZjs = require('./chunk-ITDY6AIZ.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkS6VGOPUYjs = require('./chunk-S6VGOPUY.js');
|
|
7
|
+
|
|
8
|
+
// src/utils/gas.ts
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
var _controllerutils = require('@metamask/controller-utils');
|
|
15
|
+
var _utils = require('@metamask/utils');
|
|
16
|
+
var log = _utils.createModuleLogger.call(void 0, _chunkS6VGOPUYjs.projectLogger, "gas");
|
|
17
|
+
var FIXED_GAS = "0x5208";
|
|
18
|
+
var DEFAULT_GAS_MULTIPLIER = 1.5;
|
|
19
|
+
async function updateGas(request) {
|
|
20
|
+
const { txMeta } = request;
|
|
21
|
+
const initialParams = { ...txMeta.txParams };
|
|
22
|
+
const [gas, simulationFails] = await getGas(request);
|
|
23
|
+
txMeta.txParams.gas = gas;
|
|
24
|
+
txMeta.simulationFails = simulationFails;
|
|
25
|
+
if (!initialParams.gas) {
|
|
26
|
+
txMeta.originalGasEstimate = txMeta.txParams.gas;
|
|
27
|
+
}
|
|
28
|
+
if (!txMeta.defaultGasEstimates) {
|
|
29
|
+
txMeta.defaultGasEstimates = {};
|
|
30
|
+
}
|
|
31
|
+
txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;
|
|
32
|
+
}
|
|
33
|
+
async function estimateGas(txParams, ethQuery) {
|
|
34
|
+
const request = { ...txParams };
|
|
35
|
+
const { data, value } = request;
|
|
36
|
+
const { gasLimit: gasLimitHex, number: blockNumber } = await getLatestBlock(
|
|
37
|
+
ethQuery
|
|
38
|
+
);
|
|
39
|
+
const gasLimitBN = _controllerutils.hexToBN.call(void 0, gasLimitHex);
|
|
40
|
+
request.data = data ? _utils.add0x.call(void 0, data) : data;
|
|
41
|
+
request.gas = _controllerutils.BNToHex.call(void 0, _controllerutils.fractionBN.call(void 0, gasLimitBN, 19, 20));
|
|
42
|
+
request.value = value || "0x0";
|
|
43
|
+
let estimatedGas = request.gas;
|
|
44
|
+
let simulationFails;
|
|
45
|
+
try {
|
|
46
|
+
estimatedGas = await _controllerutils.query.call(void 0, ethQuery, "estimateGas", [request]);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
simulationFails = {
|
|
49
|
+
reason: error.message,
|
|
50
|
+
errorKey: error.errorKey,
|
|
51
|
+
debug: {
|
|
52
|
+
blockNumber,
|
|
53
|
+
blockGasLimit: gasLimitHex
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
log("Estimation failed", { ...simulationFails, fallback: estimateGas });
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
blockGasLimit: gasLimitHex,
|
|
60
|
+
estimatedGas,
|
|
61
|
+
simulationFails
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function addGasBuffer(estimatedGas, blockGasLimit, multiplier) {
|
|
65
|
+
const estimatedGasBN = _controllerutils.hexToBN.call(void 0, estimatedGas);
|
|
66
|
+
const maxGasBN = _controllerutils.hexToBN.call(void 0, blockGasLimit).muln(0.9);
|
|
67
|
+
const paddedGasBN = estimatedGasBN.muln(multiplier);
|
|
68
|
+
if (estimatedGasBN.gt(maxGasBN)) {
|
|
69
|
+
const estimatedGasHex = _utils.add0x.call(void 0, estimatedGas);
|
|
70
|
+
log("Using estimated value", estimatedGasHex);
|
|
71
|
+
return estimatedGasHex;
|
|
72
|
+
}
|
|
73
|
+
if (paddedGasBN.lt(maxGasBN)) {
|
|
74
|
+
const paddedHex = _utils.add0x.call(void 0, _controllerutils.BNToHex.call(void 0, paddedGasBN));
|
|
75
|
+
log("Using padded estimate", paddedHex, multiplier);
|
|
76
|
+
return paddedHex;
|
|
77
|
+
}
|
|
78
|
+
const maxHex = _utils.add0x.call(void 0, _controllerutils.BNToHex.call(void 0, maxGasBN));
|
|
79
|
+
log("Using 90% of block gas limit", maxHex);
|
|
80
|
+
return maxHex;
|
|
81
|
+
}
|
|
82
|
+
async function getGas(request) {
|
|
83
|
+
const { isCustomNetwork, chainId, txMeta } = request;
|
|
84
|
+
if (txMeta.txParams.gas) {
|
|
85
|
+
log("Using value from request", txMeta.txParams.gas);
|
|
86
|
+
return [txMeta.txParams.gas];
|
|
87
|
+
}
|
|
88
|
+
if (await requiresFixedGas(request)) {
|
|
89
|
+
log("Using fixed value", FIXED_GAS);
|
|
90
|
+
return [FIXED_GAS];
|
|
91
|
+
}
|
|
92
|
+
const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas(
|
|
93
|
+
txMeta.txParams,
|
|
94
|
+
request.ethQuery
|
|
95
|
+
);
|
|
96
|
+
if (isCustomNetwork) {
|
|
97
|
+
log("Using original estimate as custom network");
|
|
98
|
+
return [estimatedGas, simulationFails];
|
|
99
|
+
}
|
|
100
|
+
const bufferMultiplier = _chunkITDY6AIZjs.GAS_BUFFER_CHAIN_OVERRIDES[chainId] ?? DEFAULT_GAS_MULTIPLIER;
|
|
101
|
+
const bufferedGas = addGasBuffer(
|
|
102
|
+
estimatedGas,
|
|
103
|
+
blockGasLimit,
|
|
104
|
+
bufferMultiplier
|
|
105
|
+
);
|
|
106
|
+
return [bufferedGas, simulationFails];
|
|
107
|
+
}
|
|
108
|
+
async function requiresFixedGas({
|
|
109
|
+
ethQuery,
|
|
110
|
+
txMeta,
|
|
111
|
+
isCustomNetwork
|
|
112
|
+
}) {
|
|
113
|
+
const {
|
|
114
|
+
txParams: { to, data }
|
|
115
|
+
} = txMeta;
|
|
116
|
+
if (isCustomNetwork || !to || data) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const code = await getCode(ethQuery, to);
|
|
120
|
+
return !code || code === "0x";
|
|
121
|
+
}
|
|
122
|
+
async function getCode(ethQuery, address) {
|
|
123
|
+
return await _controllerutils.query.call(void 0, ethQuery, "getCode", [address]);
|
|
124
|
+
}
|
|
125
|
+
async function getLatestBlock(ethQuery) {
|
|
126
|
+
return await _controllerutils.query.call(void 0, ethQuery, "getBlockByNumber", ["latest", false]);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
exports.log = log; exports.FIXED_GAS = FIXED_GAS; exports.DEFAULT_GAS_MULTIPLIER = DEFAULT_GAS_MULTIPLIER; exports.updateGas = updateGas; exports.estimateGas = estimateGas; exports.addGasBuffer = addGasBuffer;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,IAClB;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,SAAO,CAAC,aAAa,eAAe;AACtC;AAEA,eAAe,iBAAiB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACF,GAAuC;AACrC,QAAM;AAAA,IACJ,UAAU,EAAE,IAAI,KAAK;AAAA,EACvB,IAAI;AAEJ,MAAI,mBAAmB,CAAC,MAAM,MAAM;AAClC,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,MAAM,QAAQ,UAAU,EAAE;AAEvC,SAAO,CAAC,QAAQ,SAAS;AAC3B;AAEA,eAAe,QACb,UACA,SAC6B;AAC7B,SAAO,MAAM,MAAM,UAAU,WAAW,CAAC,OAAO,CAAC;AACnD;AAEA,eAAe,eACb,UAC+C;AAC/C,SAAO,MAAM,MAAM,UAAU,oBAAoB,CAAC,UAAU,KAAK,CAAC;AACpE","sourcesContent":["/* eslint-disable jsdoc/require-jsdoc */\n\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"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/logger.ts
|
|
2
|
+
var _utils = require('@metamask/utils');
|
|
3
|
+
var projectLogger = _utils.createProjectLogger.call(void 0, "transaction-controller");
|
|
4
|
+
var incomingTransactionsLogger = _utils.createModuleLogger.call(void 0,
|
|
5
|
+
projectLogger,
|
|
6
|
+
"incoming-transactions"
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.createModuleLogger = _utils.createModuleLogger; exports.projectLogger = projectLogger; exports.incomingTransactionsLogger = incomingTransactionsLogger;
|
|
14
|
+
//# sourceMappingURL=chunk-S6VGOPUY.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/logger.ts"],"names":[],"mappings":";AAEA,SAAS,qBAAqB,0BAA0B;AAEjD,IAAM,gBAAgB,oBAAoB,wBAAwB;AAElE,IAAM,6BAA6B;AAAA,EACxC;AAAA,EACA;AACF","sourcesContent":["/* istanbul ignore file */\n\nimport { createProjectLogger, createModuleLogger } from '@metamask/utils';\n\nexport const projectLogger = createProjectLogger('transaction-controller');\n\nexport const incomingTransactionsLogger = createModuleLogger(\n projectLogger,\n 'incoming-transactions',\n);\n\nexport { createModuleLogger };\n"]}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkQPNEFZB3js = require('./chunk-QPNEFZB3.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkS6VGOPUYjs = require('./chunk-S6VGOPUY.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkW3GAOR7Yjs = require('./chunk-W3GAOR7Y.js');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var _chunkZ4BLTVTBjs = require('./chunk-Z4BLTVTB.js');
|
|
14
|
+
|
|
15
|
+
// src/gas-flows/DefaultGasFeeFlow.ts
|
|
16
|
+
var _gasfeecontroller = require('@metamask/gas-fee-controller');
|
|
17
|
+
var _utils = require('@metamask/utils');
|
|
18
|
+
var log = _utils.createModuleLogger.call(void 0, _chunkS6VGOPUYjs.projectLogger, "default-gas-fee-flow");
|
|
19
|
+
var _getEstimateLevel, getEstimateLevel_fn, _getFeeMarketLevel, getFeeMarketLevel_fn, _getLegacyLevel, getLegacyLevel_fn;
|
|
20
|
+
var DefaultGasFeeFlow = class {
|
|
21
|
+
constructor() {
|
|
22
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getEstimateLevel);
|
|
23
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getFeeMarketLevel);
|
|
24
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getLegacyLevel);
|
|
25
|
+
}
|
|
26
|
+
matchesTransaction(_transactionMeta) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
async getGasFees(request) {
|
|
30
|
+
const { getGasFeeControllerEstimates, transactionMeta } = request;
|
|
31
|
+
const { networkClientId } = transactionMeta;
|
|
32
|
+
const { gasEstimateType, gasFeeEstimates } = await getGasFeeControllerEstimates({ networkClientId });
|
|
33
|
+
if (gasEstimateType === _gasfeecontroller.GAS_ESTIMATE_TYPES.FEE_MARKET) {
|
|
34
|
+
log("Using fee market estimates", gasFeeEstimates);
|
|
35
|
+
} else if (gasEstimateType === _gasfeecontroller.GAS_ESTIMATE_TYPES.LEGACY) {
|
|
36
|
+
log("Using legacy estimates", gasFeeEstimates);
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error(`'No gas fee estimates available`);
|
|
39
|
+
}
|
|
40
|
+
const estimates = Object.values(_chunkW3GAOR7Yjs.GasFeeEstimateLevel).reduce(
|
|
41
|
+
(result, level) => ({
|
|
42
|
+
...result,
|
|
43
|
+
[level]: _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getEstimateLevel, getEstimateLevel_fn).call(this, {
|
|
44
|
+
gasEstimateType,
|
|
45
|
+
gasFeeEstimates,
|
|
46
|
+
level
|
|
47
|
+
})
|
|
48
|
+
}),
|
|
49
|
+
{}
|
|
50
|
+
);
|
|
51
|
+
return { estimates };
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
_getEstimateLevel = new WeakSet();
|
|
55
|
+
getEstimateLevel_fn = function({
|
|
56
|
+
gasEstimateType,
|
|
57
|
+
gasFeeEstimates,
|
|
58
|
+
level
|
|
59
|
+
}) {
|
|
60
|
+
if (gasEstimateType === _gasfeecontroller.GAS_ESTIMATE_TYPES.FEE_MARKET) {
|
|
61
|
+
return _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getFeeMarketLevel, getFeeMarketLevel_fn).call(this, gasFeeEstimates, level);
|
|
62
|
+
}
|
|
63
|
+
return _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getLegacyLevel, getLegacyLevel_fn).call(this, gasFeeEstimates, level);
|
|
64
|
+
};
|
|
65
|
+
_getFeeMarketLevel = new WeakSet();
|
|
66
|
+
getFeeMarketLevel_fn = function(gasFeeEstimates, level) {
|
|
67
|
+
const maxFeePerGas = _chunkQPNEFZB3js.gweiDecimalToWeiHex.call(void 0,
|
|
68
|
+
gasFeeEstimates[level].suggestedMaxFeePerGas
|
|
69
|
+
);
|
|
70
|
+
const maxPriorityFeePerGas = _chunkQPNEFZB3js.gweiDecimalToWeiHex.call(void 0,
|
|
71
|
+
gasFeeEstimates[level].suggestedMaxPriorityFeePerGas
|
|
72
|
+
);
|
|
73
|
+
return {
|
|
74
|
+
maxFeePerGas,
|
|
75
|
+
maxPriorityFeePerGas
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
_getLegacyLevel = new WeakSet();
|
|
79
|
+
getLegacyLevel_fn = function(gasFeeEstimates, level) {
|
|
80
|
+
const gasPrice = _chunkQPNEFZB3js.gweiDecimalToWeiHex.call(void 0, gasFeeEstimates[level]);
|
|
81
|
+
return {
|
|
82
|
+
maxFeePerGas: gasPrice,
|
|
83
|
+
maxPriorityFeePerGas: gasPrice
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
exports.DefaultGasFeeFlow = DefaultGasFeeFlow;
|
|
90
|
+
//# sourceMappingURL=chunk-TXVH44HM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/gas-flows/DefaultGasFeeFlow.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,SAAS,0BAA0B;AACnC,SAAS,0BAA0B;AAcnC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,EACF;AACF;AAEA;AAAA,oBAAe,SACb,iBACA,OACyB;AACzB,QAAM,WAAW,oBAAoB,gBAAgB,KAAK,CAAC;AAE3D,SAAO;AAAA,IACL,cAAc;AAAA,IACd,sBAAsB;AAAA,EACxB;AACF","sourcesContent":["import type {\n LegacyGasPriceEstimate,\n GasFeeEstimates as FeeMarketGasPriceEstimate,\n} from '@metamask/gas-fee-controller';\nimport { GAS_ESTIMATE_TYPES } from '@metamask/gas-fee-controller';\nimport { createModuleLogger } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type {\n GasFeeEstimates,\n GasFeeEstimatesForLevel,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasFeeFlowResponse,\n TransactionMeta,\n} from '../types';\nimport { GasFeeEstimateLevel } from '../types';\nimport { gweiDecimalToWeiHex } from '../utils/gas-fees';\n\nconst log = createModuleLogger(projectLogger, 'default-gas-fee-flow');\n\ntype FeeMarketGetEstimateLevelRequest = {\n gasEstimateType: 'fee-market';\n gasFeeEstimates: FeeMarketGasPriceEstimate;\n level: GasFeeEstimateLevel;\n};\n\ntype LegacyGetEstimateLevelRequest = {\n gasEstimateType: 'legacy';\n gasFeeEstimates: LegacyGasPriceEstimate;\n level: GasFeeEstimateLevel;\n};\n\n/**\n * The standard implementation of a gas fee flow that obtains gas fee estimates using only the GasFeeController.\n */\nexport class DefaultGasFeeFlow implements GasFeeFlow {\n matchesTransaction(_transactionMeta: TransactionMeta): boolean {\n return true;\n }\n\n async getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse> {\n const { getGasFeeControllerEstimates, transactionMeta } = request;\n const { networkClientId } = transactionMeta;\n\n const { gasEstimateType, gasFeeEstimates } =\n await getGasFeeControllerEstimates({ networkClientId });\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n log('Using fee market estimates', gasFeeEstimates);\n } else if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {\n log('Using legacy estimates', gasFeeEstimates);\n } else {\n throw new Error(`'No gas fee estimates available`);\n }\n\n const estimates = Object.values(GasFeeEstimateLevel).reduce(\n (result, level) => ({\n ...result,\n [level]: this.#getEstimateLevel({\n gasEstimateType,\n gasFeeEstimates,\n level,\n } as FeeMarketGetEstimateLevelRequest | LegacyGetEstimateLevelRequest),\n }),\n {} as GasFeeEstimates,\n );\n\n return { estimates };\n }\n\n #getEstimateLevel({\n gasEstimateType,\n gasFeeEstimates,\n level,\n }:\n | FeeMarketGetEstimateLevelRequest\n | LegacyGetEstimateLevelRequest): GasFeeEstimatesForLevel {\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n return this.#getFeeMarketLevel(gasFeeEstimates, level);\n }\n\n return this.#getLegacyLevel(gasFeeEstimates, level);\n }\n\n #getFeeMarketLevel(\n gasFeeEstimates: FeeMarketGasPriceEstimate,\n level: GasFeeEstimateLevel,\n ): GasFeeEstimatesForLevel {\n const maxFeePerGas = gweiDecimalToWeiHex(\n gasFeeEstimates[level].suggestedMaxFeePerGas,\n );\n\n const maxPriorityFeePerGas = gweiDecimalToWeiHex(\n gasFeeEstimates[level].suggestedMaxPriorityFeePerGas,\n );\n\n return {\n maxFeePerGas,\n maxPriorityFeePerGas,\n };\n }\n\n #getLegacyLevel(\n gasFeeEstimates: LegacyGasPriceEstimate,\n level: GasFeeEstimateLevel,\n ): GasFeeEstimatesForLevel {\n const gasPrice = gweiDecimalToWeiHex(gasFeeEstimates[level]);\n\n return {\n maxFeePerGas: gasPrice,\n maxPriorityFeePerGas: gasPrice,\n };\n }\n}\n"]}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/utils/utils.ts
|
|
2
|
+
var _controllerutils = require('@metamask/controller-utils');
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _utils = require('@metamask/utils');
|
|
8
|
+
var ESTIMATE_GAS_ERROR = "eth_estimateGas rpc method error";
|
|
9
|
+
var NORMALIZERS = {
|
|
10
|
+
data: (data) => _utils.add0x.call(void 0, padHexToEvenLength(data)),
|
|
11
|
+
from: (from) => _utils.add0x.call(void 0, from).toLowerCase(),
|
|
12
|
+
gas: (gas) => _utils.add0x.call(void 0, gas),
|
|
13
|
+
gasLimit: (gas) => _utils.add0x.call(void 0, gas),
|
|
14
|
+
gasPrice: (gasPrice) => _utils.add0x.call(void 0, gasPrice),
|
|
15
|
+
nonce: (nonce) => _utils.add0x.call(void 0, nonce),
|
|
16
|
+
to: (to) => _utils.add0x.call(void 0, to).toLowerCase(),
|
|
17
|
+
value: (value) => _utils.add0x.call(void 0, value),
|
|
18
|
+
maxFeePerGas: (maxFeePerGas) => _utils.add0x.call(void 0, maxFeePerGas),
|
|
19
|
+
maxPriorityFeePerGas: (maxPriorityFeePerGas) => _utils.add0x.call(void 0, maxPriorityFeePerGas),
|
|
20
|
+
estimatedBaseFee: (maxPriorityFeePerGas) => _utils.add0x.call(void 0, maxPriorityFeePerGas),
|
|
21
|
+
type: (type) => _utils.add0x.call(void 0, type)
|
|
22
|
+
};
|
|
23
|
+
function normalizeTransactionParams(txParams) {
|
|
24
|
+
const normalizedTxParams = { from: "" };
|
|
25
|
+
for (const key of _utils.getKnownPropertyNames.call(void 0, NORMALIZERS)) {
|
|
26
|
+
if (txParams[key]) {
|
|
27
|
+
normalizedTxParams[key] = NORMALIZERS[key](txParams[key]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!normalizedTxParams.value) {
|
|
31
|
+
normalizedTxParams.value = "0x0";
|
|
32
|
+
}
|
|
33
|
+
return normalizedTxParams;
|
|
34
|
+
}
|
|
35
|
+
function isEIP1559Transaction(txParams) {
|
|
36
|
+
const hasOwnProp = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
37
|
+
return hasOwnProp(txParams, "maxFeePerGas") && hasOwnProp(txParams, "maxPriorityFeePerGas");
|
|
38
|
+
}
|
|
39
|
+
var validateGasValues = (gasValues) => {
|
|
40
|
+
Object.keys(gasValues).forEach((key) => {
|
|
41
|
+
const value = gasValues[key];
|
|
42
|
+
if (typeof value !== "string" || !_utils.isStrictHexString.call(void 0, value)) {
|
|
43
|
+
throw new TypeError(
|
|
44
|
+
`expected hex string for ${key} but received: ${value}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
var isFeeMarketEIP1559Values = (gasValues) => gasValues?.maxFeePerGas !== void 0 || gasValues?.maxPriorityFeePerGas !== void 0;
|
|
50
|
+
var isGasPriceValue = (gasValues) => gasValues?.gasPrice !== void 0;
|
|
51
|
+
var getIncreasedPriceHex = (value, rate) => _utils.add0x.call(void 0, `${parseInt(`${value * rate}`, 10).toString(16)}`);
|
|
52
|
+
var getIncreasedPriceFromExisting = (value, rate) => {
|
|
53
|
+
return getIncreasedPriceHex(_controllerutils.convertHexToDecimal.call(void 0, value), rate);
|
|
54
|
+
};
|
|
55
|
+
function validateMinimumIncrease(proposed, min) {
|
|
56
|
+
const proposedDecimal = _controllerutils.convertHexToDecimal.call(void 0, proposed);
|
|
57
|
+
const minDecimal = _controllerutils.convertHexToDecimal.call(void 0, min);
|
|
58
|
+
if (proposedDecimal >= minDecimal) {
|
|
59
|
+
return proposed;
|
|
60
|
+
}
|
|
61
|
+
const errorMsg = `The proposed value: ${proposedDecimal} should meet or exceed the minimum value: ${minDecimal}`;
|
|
62
|
+
throw new Error(errorMsg);
|
|
63
|
+
}
|
|
64
|
+
function validateIfTransactionUnapproved(transactionMeta, fnName) {
|
|
65
|
+
if (transactionMeta?.status !== "unapproved" /* unapproved */) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`TransactionsController: Can only call ${fnName} on an unapproved transaction.
|
|
68
|
+
Current tx status: ${transactionMeta?.status}`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function normalizeTxError(error) {
|
|
73
|
+
return {
|
|
74
|
+
name: error.name,
|
|
75
|
+
message: error.message,
|
|
76
|
+
stack: error.stack,
|
|
77
|
+
code: error.code,
|
|
78
|
+
rpc: isJsonCompatible(error.value) ? error.value : void 0
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function normalizeGasFeeValues(gasFeeValues) {
|
|
82
|
+
const normalize = (value) => typeof value === "string" ? _utils.add0x.call(void 0, value) : value;
|
|
83
|
+
if ("gasPrice" in gasFeeValues) {
|
|
84
|
+
return {
|
|
85
|
+
gasPrice: normalize(gasFeeValues.gasPrice)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
maxFeePerGas: normalize(gasFeeValues.maxFeePerGas),
|
|
90
|
+
maxPriorityFeePerGas: normalize(gasFeeValues.maxPriorityFeePerGas)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function isJsonCompatible(value) {
|
|
94
|
+
try {
|
|
95
|
+
JSON.parse(JSON.stringify(value));
|
|
96
|
+
return true;
|
|
97
|
+
} catch {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function padHexToEvenLength(hex) {
|
|
102
|
+
const prefix = hex.toLowerCase().startsWith("0x") ? hex.slice(0, 2) : "";
|
|
103
|
+
const data = prefix ? hex.slice(2) : hex;
|
|
104
|
+
const evenData = data.length % 2 === 0 ? data : `0${data}`;
|
|
105
|
+
return prefix + evenData;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
exports.ESTIMATE_GAS_ERROR = ESTIMATE_GAS_ERROR; exports.normalizeTransactionParams = normalizeTransactionParams; exports.isEIP1559Transaction = isEIP1559Transaction; exports.validateGasValues = validateGasValues; exports.isFeeMarketEIP1559Values = isFeeMarketEIP1559Values; exports.isGasPriceValue = isGasPriceValue; exports.getIncreasedPriceHex = getIncreasedPriceHex; exports.getIncreasedPriceFromExisting = getIncreasedPriceFromExisting; exports.validateMinimumIncrease = validateMinimumIncrease; exports.validateIfTransactionUnapproved = validateIfTransactionUnapproved; exports.normalizeTxError = normalizeTxError; exports.normalizeGasFeeValues = normalizeGasFeeValues; exports.padHexToEvenLength = padHexToEvenLength;
|
|
123
|
+
//# sourceMappingURL=chunk-UGFBA4GV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/utils.ts"],"names":[],"mappings":";AAAA,SAAS,2BAA2B;AACpC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAcA,IAAM,qBAAqB;AAIlC,IAAM,cAA2D;AAAA,EAC/D,MAAM,CAAC,SAAiB,MAAM,mBAAmB,IAAI,CAAC;AAAA,EACtD,MAAM,CAAC,SAAiB,MAAM,IAAI,EAAE,YAAY;AAAA,EAChD,KAAK,CAAC,QAAgB,MAAM,GAAG;AAAA,EAC/B,UAAU,CAAC,QAAgB,MAAM,GAAG;AAAA,EACpC,UAAU,CAAC,aAAqB,MAAM,QAAQ;AAAA,EAC9C,OAAO,CAAC,UAAkB,MAAM,KAAK;AAAA,EACrC,IAAI,CAAC,OAAe,MAAM,EAAE,EAAE,YAAY;AAAA,EAC1C,OAAO,CAAC,UAAkB,MAAM,KAAK;AAAA,EACrC,cAAc,CAAC,iBAAyB,MAAM,YAAY;AAAA,EAC1D,sBAAsB,CAAC,yBACrB,MAAM,oBAAoB;AAAA,EAC5B,kBAAkB,CAAC,yBACjB,MAAM,oBAAoB;AAAA,EAC5B,MAAM,CAAC,SAAiB,MAAM,IAAI;AACpC;AAQO,SAAS,2BAA2B,UAA6B;AACtE,QAAM,qBAAwC,EAAE,MAAM,GAAG;AAEzD,aAAW,OAAO,sBAAsB,WAAW,GAAG;AACpD,QAAI,SAAS,GAAG,GAAG;AACjB,yBAAmB,GAAG,IAAI,YA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,yCAAyC,MAAM;AAAA,2BAC1B,iBAAiB,MAAM;AAAA,IAC9C;AAAA,EACF;AACF;AAQO,SAAS,iBACd,OACkB;AAClB,SAAO;AAAA,IACL,MAAM,MAAM;AAAA,IACZ,SAAS,MAAM;AAAA,IACf,OAAO,MAAM;AAAA,IACb,MAAM,MAAM;AAAA,IACZ,KAAK,iBAAiB,MAAM,KAAK,IAAI,MAAM,QAAQ;AAAA,EACrD;AACF;AAQO,SAAS,sBACd,cACwC;AAGxC,QAAM,YAAY,CAAC,UACjB,OAAO,UAAU,WAAW,MAAM,KAAK,IAAI;AAE7C,MAAI,cAAc,cAAc;AAC9B,WAAO;AAAA,MACL,UAAU,UAAU,aAAa,QAAQ;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,cAAc,UAAU,aAAa,YAAY;AAAA,IACjD,sBAAsB,UAAU,aAAa,oBAAoB;AAAA,EACnE;AACF;AAQA,SAAS,iBAAiB,OAA+B;AACvD,MAAI;AACF,SAAK,MAAM,KAAK,UAAU,KAAK,CAAC;AAChC,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AASO,SAAS,mBAAmB,KAAa;AAC9C,QAAM,SAAS,IAAI,YAAY,EAAE,WAAW,IAAI,IAAI,IAAI,MAAM,GAAG,CAAC,IAAI;AACtE,QAAM,OAAO,SAAS,IAAI,MAAM,CAAC,IAAI;AACrC,QAAM,WAAW,KAAK,SAAS,MAAM,IAAI,OAAO,IAAI,IAAI;AAExD,SAAO,SAAS;AAClB","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport {\n add0x,\n getKnownPropertyNames,\n isStrictHexString,\n} from '@metamask/utils';\nimport type { Json } from '@metamask/utils';\n\nimport type {\n GasPriceValue,\n FeeMarketEIP1559Values,\n} from '../TransactionController';\nimport { TransactionStatus } from '../types';\nimport type {\n TransactionParams,\n TransactionMeta,\n TransactionError,\n} from '../types';\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst NORMALIZERS: { [param in keyof TransactionParams]: any } = {\n data: (data: string) => add0x(padHexToEvenLength(data)),\n from: (from: string) => add0x(from).toLowerCase(),\n gas: (gas: string) => add0x(gas),\n gasLimit: (gas: string) => add0x(gas),\n gasPrice: (gasPrice: string) => add0x(gasPrice),\n nonce: (nonce: string) => add0x(nonce),\n to: (to: string) => add0x(to).toLowerCase(),\n value: (value: string) => add0x(value),\n maxFeePerGas: (maxFeePerGas: string) => add0x(maxFeePerGas),\n maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n estimatedBaseFee: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n type: (type: string) => add0x(type),\n};\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param txParams - The transaction params to normalize.\n * @returns Normalized transaction params.\n */\nexport function normalizeTransactionParams(txParams: TransactionParams) {\n const normalizedTxParams: TransactionParams = { from: '' };\n\n for (const key of getKnownPropertyNames(NORMALIZERS)) {\n if (txParams[key]) {\n normalizedTxParams[key] = NORMALIZERS[key](txParams[key]);\n }\n }\n\n if (!normalizedTxParams.value) {\n normalizedTxParams.value = '0x0';\n }\n\n return normalizedTxParams;\n}\n\n/**\n * Checks if a transaction is EIP-1559 by checking for the existence of\n * maxFeePerGas and maxPriorityFeePerGas within its parameters.\n *\n * @param txParams - Transaction params object to add.\n * @returns Boolean that is true if the transaction is EIP-1559 (has maxFeePerGas and maxPriorityFeePerGas), otherwise returns false.\n */\nexport function isEIP1559Transaction(txParams: TransactionParams): boolean {\n const hasOwnProp = (obj: TransactionParams, key: string) =>\n Object.prototype.hasOwnProperty.call(obj, key);\n return (\n hasOwnProp(txParams, 'maxFeePerGas') &&\n hasOwnProp(txParams, 'maxPriorityFeePerGas')\n );\n}\n\nexport const validateGasValues = (\n gasValues: GasPriceValue | FeeMarketEIP1559Values,\n) => {\n Object.keys(gasValues).forEach((key) => {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const value = (gasValues as any)[key];\n if (typeof value !== 'string' || !isStrictHexString(value)) {\n throw new TypeError(\n `expected hex string for ${key} but received: ${value}`,\n );\n }\n });\n};\n\nexport const isFeeMarketEIP1559Values = (\n gasValues?: GasPriceValue | FeeMarketEIP1559Values,\n): gasValues is FeeMarketEIP1559Values =>\n (gasValues as FeeMarketEIP1559Values)?.maxFeePerGas !== undefined ||\n (gasValues as FeeMarketEIP1559Values)?.maxPriorityFeePerGas !== undefined;\n\nexport const isGasPriceValue = (\n gasValues?: GasPriceValue | FeeMarketEIP1559Values,\n): gasValues is GasPriceValue =>\n (gasValues as GasPriceValue)?.gasPrice !== undefined;\n\nexport const getIncreasedPriceHex = (value: number, rate: number): string =>\n add0x(`${parseInt(`${value * rate}`, 10).toString(16)}`);\n\nexport const getIncreasedPriceFromExisting = (\n value: string | undefined,\n rate: number,\n): string => {\n return getIncreasedPriceHex(convertHexToDecimal(value), rate);\n};\n\n/**\n * Validates that the proposed value is greater than or equal to the minimum value.\n *\n * @param proposed - The proposed value.\n * @param min - The minimum value.\n * @returns The proposed value.\n * @throws Will throw if the proposed value is too low.\n */\nexport function validateMinimumIncrease(proposed: string, min: string) {\n const proposedDecimal = convertHexToDecimal(proposed);\n const minDecimal = convertHexToDecimal(min);\n if (proposedDecimal >= minDecimal) {\n return proposed;\n }\n const errorMsg = `The proposed value: ${proposedDecimal} should meet or exceed the minimum value: ${minDecimal}`;\n throw new Error(errorMsg);\n}\n\n/**\n * Validates that a transaction is unapproved.\n * Throws if the transaction is not unapproved.\n *\n * @param transactionMeta - The transaction metadata to check.\n * @param fnName - The name of the function calling this helper.\n */\nexport function validateIfTransactionUnapproved(\n transactionMeta: TransactionMeta | undefined,\n fnName: string,\n) {\n if (transactionMeta?.status !== TransactionStatus.unapproved) {\n throw new Error(\n `TransactionsController: Can only call ${fnName} on an unapproved transaction.\n Current tx status: ${transactionMeta?.status}`,\n );\n }\n}\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param error - The error to be normalize.\n * @returns Normalized transaction error.\n */\nexport function normalizeTxError(\n error: Error & { code?: string; value?: unknown },\n): TransactionError {\n return {\n name: error.name,\n message: error.message,\n stack: error.stack,\n code: error.code,\n rpc: isJsonCompatible(error.value) ? error.value : undefined,\n };\n}\n\n/**\n * Normalize an object containing gas fee values.\n *\n * @param gasFeeValues - An object containing gas fee values.\n * @returns An object containing normalized gas fee values.\n */\nexport function normalizeGasFeeValues(\n gasFeeValues: GasPriceValue | FeeMarketEIP1559Values,\n): GasPriceValue | FeeMarketEIP1559Values {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const normalize = (value: any) =>\n typeof value === 'string' ? add0x(value) : value;\n\n if ('gasPrice' in gasFeeValues) {\n return {\n gasPrice: normalize(gasFeeValues.gasPrice),\n };\n }\n\n return {\n maxFeePerGas: normalize(gasFeeValues.maxFeePerGas),\n maxPriorityFeePerGas: normalize(gasFeeValues.maxPriorityFeePerGas),\n };\n}\n\n/**\n * Determines whether the given value can be encoded as JSON.\n *\n * @param value - The value.\n * @returns True if the value is JSON-encodable, false if not.\n */\nfunction isJsonCompatible(value: unknown): value is Json {\n try {\n JSON.parse(JSON.stringify(value));\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Ensure a hex string is of even length by adding a leading 0 if necessary.\n * Any existing `0x` prefix is preserved but is not added if missing.\n *\n * @param hex - The hex string to ensure is even.\n * @returns The hex string with an even length.\n */\nexport function padHexToEvenLength(hex: string) {\n const prefix = hex.toLowerCase().startsWith('0x') ? hex.slice(0, 2) : '';\n const data = prefix ? hex.slice(2) : hex;\n const evenData = data.length % 2 === 0 ? data : `0${data}`;\n\n return prefix + evenData;\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/logger.ts
|
|
2
|
+
import { createProjectLogger, createModuleLogger } from "@metamask/utils";
|
|
3
|
+
var projectLogger = createProjectLogger("transaction-controller");
|
|
4
|
+
var incomingTransactionsLogger = createModuleLogger(
|
|
5
|
+
projectLogger,
|
|
6
|
+
"incoming-transactions"
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
createModuleLogger,
|
|
11
|
+
projectLogger,
|
|
12
|
+
incomingTransactionsLogger
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=chunk-UQQWZT6C.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/logger.ts"],"sourcesContent":["/* istanbul ignore file */\n\nimport { createProjectLogger, createModuleLogger } from '@metamask/utils';\n\nexport const projectLogger = createProjectLogger('transaction-controller');\n\nexport const incomingTransactionsLogger = createModuleLogger(\n projectLogger,\n 'incoming-transactions',\n);\n\nexport { createModuleLogger };\n"],"mappings":";AAEA,SAAS,qBAAqB,0BAA0B;AAEjD,IAAM,gBAAgB,oBAAoB,wBAAwB;AAElE,IAAM,6BAA6B;AAAA,EACxC;AAAA,EACA;AACF;","names":[]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ETHERSCAN_SUPPORTED_NETWORKS
|
|
3
|
+
} from "./chunk-Z3HHSD5I.mjs";
|
|
4
|
+
import {
|
|
5
|
+
incomingTransactionsLogger
|
|
6
|
+
} from "./chunk-UQQWZT6C.mjs";
|
|
7
|
+
|
|
8
|
+
// src/utils/etherscan.ts
|
|
9
|
+
import { handleFetch } from "@metamask/controller-utils";
|
|
10
|
+
async function fetchEtherscanTransactions({
|
|
11
|
+
address,
|
|
12
|
+
chainId,
|
|
13
|
+
fromBlock,
|
|
14
|
+
limit
|
|
15
|
+
}) {
|
|
16
|
+
return await fetchTransactions("txlist", {
|
|
17
|
+
address,
|
|
18
|
+
chainId,
|
|
19
|
+
fromBlock,
|
|
20
|
+
limit
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
async function fetchEtherscanTokenTransactions({
|
|
24
|
+
address,
|
|
25
|
+
chainId,
|
|
26
|
+
fromBlock,
|
|
27
|
+
limit
|
|
28
|
+
}) {
|
|
29
|
+
return await fetchTransactions("tokentx", {
|
|
30
|
+
address,
|
|
31
|
+
chainId,
|
|
32
|
+
fromBlock,
|
|
33
|
+
limit
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async function fetchTransactions(action, {
|
|
37
|
+
address,
|
|
38
|
+
chainId,
|
|
39
|
+
fromBlock,
|
|
40
|
+
limit
|
|
41
|
+
}) {
|
|
42
|
+
const urlParams = {
|
|
43
|
+
module: "account",
|
|
44
|
+
address,
|
|
45
|
+
startBlock: fromBlock?.toString(),
|
|
46
|
+
offset: limit?.toString(),
|
|
47
|
+
sort: "desc"
|
|
48
|
+
};
|
|
49
|
+
const etherscanTxUrl = getEtherscanApiUrl(chainId, {
|
|
50
|
+
...urlParams,
|
|
51
|
+
action
|
|
52
|
+
});
|
|
53
|
+
incomingTransactionsLogger("Sending Etherscan request", etherscanTxUrl);
|
|
54
|
+
const response = await handleFetch(
|
|
55
|
+
etherscanTxUrl
|
|
56
|
+
);
|
|
57
|
+
return response;
|
|
58
|
+
}
|
|
59
|
+
function getEtherscanApiUrl(chainId, urlParams) {
|
|
60
|
+
const apiUrl = getEtherscanApiHost(chainId);
|
|
61
|
+
let url = `${apiUrl}/api?`;
|
|
62
|
+
for (const paramKey of Object.keys(urlParams)) {
|
|
63
|
+
const value = urlParams[paramKey];
|
|
64
|
+
if (!value) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
url += `${paramKey}=${value}&`;
|
|
68
|
+
}
|
|
69
|
+
url += "tag=latest&page=1";
|
|
70
|
+
return url;
|
|
71
|
+
}
|
|
72
|
+
function getEtherscanApiHost(chainId) {
|
|
73
|
+
const networkInfo = ETHERSCAN_SUPPORTED_NETWORKS[chainId];
|
|
74
|
+
if (!networkInfo) {
|
|
75
|
+
throw new Error(`Etherscan does not support chain with ID: ${chainId}`);
|
|
76
|
+
}
|
|
77
|
+
return `https://${networkInfo.subdomain}.${networkInfo.domain}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export {
|
|
81
|
+
fetchEtherscanTransactions,
|
|
82
|
+
fetchEtherscanTokenTransactions,
|
|
83
|
+
getEtherscanApiHost
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=chunk-VEREDMI2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/etherscan.ts"],"sourcesContent":["import { handleFetch } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { ETHERSCAN_SUPPORTED_NETWORKS } from '../constants';\nimport { incomingTransactionsLogger as log } from '../logger';\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionMetaBase {\n blockNumber: string;\n blockHash: string;\n confirmations: string;\n contractAddress: string;\n cumulativeGasUsed: string;\n from: string;\n gas: string;\n gasPrice: string;\n gasUsed: string;\n hash: string;\n nonce: string;\n timeStamp: string;\n to: string;\n transactionIndex: string;\n value: string;\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionMeta extends EtherscanTransactionMetaBase {\n functionName: string;\n input: string;\n isError: string;\n methodId: string;\n txreceipt_status: string;\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTokenTransactionMeta\n extends EtherscanTransactionMetaBase {\n tokenDecimal: string;\n tokenName: string;\n tokenSymbol: string;\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionResponse<\n T extends EtherscanTransactionMetaBase,\n> {\n status: '0' | '1';\n message?: string;\n result: string | T[];\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionRequest {\n address: string;\n chainId: Hex;\n fromBlock?: number;\n limit?: number;\n}\n\n/**\n * Retrieves transaction data from Etherscan.\n *\n * @param request - Configuration required to fetch transactions.\n * @param request.address - Address to retrieve transactions for.\n * @param request.chainId - Current chain ID used to determine subdomain and domain.\n * @param request.fromBlock - Block number to start fetching transactions from.\n * @param request.limit - Number of transactions to retrieve.\n * @returns An Etherscan response object containing the request status and an array of token transaction data.\n */\nexport async function fetchEtherscanTransactions({\n address,\n chainId,\n fromBlock,\n limit,\n}: EtherscanTransactionRequest): Promise<\n EtherscanTransactionResponse<EtherscanTransactionMeta>\n> {\n return await fetchTransactions('txlist', {\n address,\n chainId,\n fromBlock,\n limit,\n });\n}\n\n/**\n * Retrieves token transaction data from Etherscan.\n *\n * @param request - Configuration required to fetch token transactions.\n * @param request.address - Address to retrieve token transactions for.\n * @param request.chainId - Current chain ID used to determine subdomain and domain.\n * @param request.fromBlock - Block number to start fetching token transactions from.\n * @param request.limit - Number of token transactions to retrieve.\n * @returns An Etherscan response object containing the request status and an array of token transaction data.\n */\nexport async function fetchEtherscanTokenTransactions({\n address,\n chainId,\n fromBlock,\n limit,\n}: EtherscanTransactionRequest): Promise<\n EtherscanTransactionResponse<EtherscanTokenTransactionMeta>\n> {\n return await fetchTransactions('tokentx', {\n address,\n chainId,\n fromBlock,\n limit,\n });\n}\n\n/**\n * Retrieves transaction data from Etherscan from a specific endpoint.\n *\n * @param action - The Etherscan endpoint to use.\n * @param options - Options bag.\n * @param options.address - Address to retrieve transactions for.\n * @param options.chainId - Current chain ID used to determine subdomain and domain.\n * @param options.fromBlock - Block number to start fetching transactions from.\n * @param options.limit - Number of transactions to retrieve.\n * @returns An object containing the request status and an array of transaction data.\n */\nasync function fetchTransactions<T extends EtherscanTransactionMetaBase>(\n action: string,\n {\n address,\n chainId,\n fromBlock,\n limit,\n }: {\n address: string;\n chainId: Hex;\n fromBlock?: number;\n limit?: number;\n },\n): Promise<EtherscanTransactionResponse<T>> {\n const urlParams = {\n module: 'account',\n address,\n startBlock: fromBlock?.toString(),\n offset: limit?.toString(),\n sort: 'desc',\n };\n\n const etherscanTxUrl = getEtherscanApiUrl(chainId, {\n ...urlParams,\n action,\n });\n\n log('Sending Etherscan request', etherscanTxUrl);\n\n const response = (await handleFetch(\n etherscanTxUrl,\n )) as EtherscanTransactionResponse<T>;\n\n return response;\n}\n\n/**\n * Return a URL that can be used to fetch data from Etherscan.\n *\n * @param chainId - Current chain ID used to determine subdomain and domain.\n * @param urlParams - The parameters used to construct the URL.\n * @returns URL to access Etherscan data.\n */\nfunction getEtherscanApiUrl(\n chainId: Hex,\n urlParams: Record<string, string | undefined>,\n): string {\n const apiUrl = getEtherscanApiHost(chainId);\n let url = `${apiUrl}/api?`;\n\n for (const paramKey of Object.keys(urlParams)) {\n const value = urlParams[paramKey];\n\n if (!value) {\n continue;\n }\n\n url += `${paramKey}=${value}&`;\n }\n\n url += 'tag=latest&page=1';\n\n return url;\n}\n\n/**\n * Return the host url used to fetch data from Etherscan.\n *\n * @param chainId - Current chain ID used to determine subdomain and domain.\n * @returns host URL to access Etherscan data.\n */\nexport function getEtherscanApiHost(chainId: Hex) {\n // @ts-expect-error We account for `chainId` not being a property below\n const networkInfo = ETHERSCAN_SUPPORTED_NETWORKS[chainId];\n\n if (!networkInfo) {\n throw new Error(`Etherscan does not support chain with ID: ${chainId}`);\n }\n\n return `https://${networkInfo.subdomain}.${networkInfo.domain}`;\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,mBAAmB;AA+E5B,eAAsB,2BAA2B;AAAA,EAC/C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAEE;AACA,SAAO,MAAM,kBAAkB,UAAU;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAY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