@metamask/transaction-controller 28.1.1 → 29.0.1
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 +40 -1
- package/dist/TransactionController.js +11 -10
- package/dist/TransactionController.mjs +10 -9
- package/dist/{chunk-HQAADNXH.mjs → chunk-26BABWPE.mjs} +201 -113
- package/dist/chunk-26BABWPE.mjs.map +1 -0
- package/dist/{chunk-LDXTSESK.mjs → chunk-4M7FHI4S.mjs} +20 -4
- package/dist/chunk-4M7FHI4S.mjs.map +1 -0
- package/dist/{chunk-FK7AP6SH.js → chunk-5C3CSQAD.js} +12 -5
- package/dist/{chunk-YEJJKWT2.mjs.map → chunk-5C3CSQAD.js.map} +1 -1
- package/dist/{chunk-V6UIRCOF.js → chunk-7DJO6UZA.js} +48 -13
- package/dist/chunk-7DJO6UZA.js.map +1 -0
- package/dist/{chunk-TWND5SFD.mjs → chunk-AGJNOOBN.mjs} +17 -12
- package/dist/chunk-AGJNOOBN.mjs.map +1 -0
- package/dist/{chunk-LNX4JTOL.js → chunk-DUFOYA4D.js} +7 -7
- package/dist/chunk-DUFOYA4D.js.map +1 -0
- package/dist/{chunk-UH7FNVKS.js → chunk-EMDU4XLM.js} +19 -14
- package/dist/chunk-EMDU4XLM.js.map +1 -0
- package/dist/chunk-FMRLPVFZ.mjs +66 -0
- package/dist/chunk-FMRLPVFZ.mjs.map +1 -0
- package/dist/{chunk-EQNKFFTM.js → chunk-KNYSD4JP.js} +21 -5
- package/dist/chunk-KNYSD4JP.js.map +1 -0
- package/dist/{chunk-FMCTVRU2.js → chunk-KT3F6CZP.js} +204 -116
- package/dist/chunk-KT3F6CZP.js.map +1 -0
- package/dist/{chunk-YEJJKWT2.mjs → chunk-N6JXPWLY.mjs} +11 -4
- package/dist/chunk-N6JXPWLY.mjs.map +1 -0
- package/dist/chunk-REOG6YD4.mjs +112 -0
- package/dist/chunk-REOG6YD4.mjs.map +1 -0
- package/dist/{chunk-HHK4QSNF.mjs → chunk-SL3GHHKG.mjs} +47 -12
- package/dist/chunk-SL3GHHKG.mjs.map +1 -0
- package/dist/chunk-TJMQEH57.js +66 -0
- package/dist/chunk-TJMQEH57.js.map +1 -0
- package/dist/chunk-VJYFB7BM.js +112 -0
- package/dist/chunk-VJYFB7BM.js.map +1 -0
- package/dist/{chunk-NCRWKI6Y.mjs → chunk-XL3HBKDE.mjs} +4 -4
- package/dist/chunk-XL3HBKDE.mjs.map +1 -0
- package/dist/errors.js +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.js +5 -5
- package/dist/gas-flows/DefaultGasFeeFlow.mjs +4 -4
- package/dist/gas-flows/LineaGasFeeFlow.js +6 -6
- package/dist/gas-flows/LineaGasFeeFlow.mjs +5 -5
- package/dist/gas-flows/TestGasFeeFlow.js +9 -0
- package/dist/gas-flows/TestGasFeeFlow.js.map +1 -0
- package/dist/gas-flows/TestGasFeeFlow.mjs +9 -0
- package/dist/gas-flows/TestGasFeeFlow.mjs.map +1 -0
- package/dist/helpers/EtherscanRemoteTransactionSource.js +1 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.mjs +1 -1
- package/dist/helpers/GasFeePoller.js +4 -4
- package/dist/helpers/GasFeePoller.mjs +3 -3
- package/dist/helpers/MultichainTrackingHelper.js +1 -1
- package/dist/helpers/MultichainTrackingHelper.mjs +1 -1
- package/dist/helpers/PendingTransactionTracker.js +1 -1
- package/dist/helpers/PendingTransactionTracker.mjs +1 -1
- package/dist/index.js +15 -10
- package/dist/index.mjs +14 -9
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/TransactionController.d.ts +16 -7
- package/dist/types/TransactionController.d.ts.map +1 -1
- package/dist/types/gas-flows/DefaultGasFeeFlow.d.ts.map +1 -1
- package/dist/types/gas-flows/TestGasFeeFlow.d.ts +12 -0
- package/dist/types/gas-flows/TestGasFeeFlow.d.ts.map +1 -0
- package/dist/types/helpers/GasFeePoller.d.ts +5 -3
- package/dist/types/helpers/GasFeePoller.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +36 -18
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/gas-flow.d.ts +8 -14
- package/dist/types/utils/gas-flow.d.ts.map +1 -1
- package/dist/types.js +4 -2
- package/dist/types.mjs +3 -1
- package/dist/utils/external-transactions.js +1 -1
- package/dist/utils/external-transactions.mjs +1 -1
- package/dist/utils/gas-fees.js +4 -4
- package/dist/utils/gas-fees.mjs +3 -3
- package/dist/utils/gas-flow.js +3 -3
- package/dist/utils/gas-flow.mjs +2 -2
- package/dist/utils/simulation-api.js +1 -1
- package/dist/utils/simulation-api.mjs +1 -1
- package/dist/utils/simulation.js +1 -1
- package/dist/utils/simulation.mjs +1 -1
- package/dist/utils/swaps.js +1 -1
- package/dist/utils/swaps.mjs +1 -1
- package/dist/utils/transaction-type.js +1 -1
- package/dist/utils/transaction-type.mjs +1 -1
- package/dist/utils/utils.js +1 -1
- package/dist/utils/utils.mjs +1 -1
- package/dist/utils/validation.js +1 -1
- package/dist/utils/validation.mjs +1 -1
- package/package.json +5 -5
- package/dist/chunk-4RJXBKSR.js +0 -90
- package/dist/chunk-4RJXBKSR.js.map +0 -1
- package/dist/chunk-EQNKFFTM.js.map +0 -1
- package/dist/chunk-FK7AP6SH.js.map +0 -1
- package/dist/chunk-FMCTVRU2.js.map +0 -1
- package/dist/chunk-HHK4QSNF.mjs.map +0 -1
- package/dist/chunk-HQAADNXH.mjs.map +0 -1
- package/dist/chunk-LDXTSESK.mjs.map +0 -1
- package/dist/chunk-LNX4JTOL.js.map +0 -1
- package/dist/chunk-NCRWKI6Y.mjs.map +0 -1
- package/dist/chunk-NPEXNXSY.mjs +0 -90
- package/dist/chunk-NPEXNXSY.mjs.map +0 -1
- package/dist/chunk-TWND5SFD.mjs.map +0 -1
- package/dist/chunk-UH7FNVKS.js.map +0 -1
- package/dist/chunk-V6UIRCOF.js.map +0 -1
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__privateAdd,
|
|
3
|
+
__privateGet,
|
|
4
|
+
__privateMethod,
|
|
5
|
+
__privateSet
|
|
6
|
+
} from "./chunk-XUI43LEZ.mjs";
|
|
7
|
+
|
|
8
|
+
// src/gas-flows/TestGasFeeFlow.ts
|
|
9
|
+
import { toHex } from "@metamask/controller-utils";
|
|
10
|
+
var INCREMENT = 1e15;
|
|
11
|
+
var LEVEL_DIFFERENCE = 0.5;
|
|
12
|
+
var _counter, _getValueForTotalFee, getValueForTotalFee_fn;
|
|
13
|
+
var TestGasFeeFlow = class {
|
|
14
|
+
constructor() {
|
|
15
|
+
__privateAdd(this, _getValueForTotalFee);
|
|
16
|
+
__privateAdd(this, _counter, 1);
|
|
17
|
+
}
|
|
18
|
+
matchesTransaction(_transactionMeta) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
async getGasFees(request) {
|
|
22
|
+
const { transactionMeta } = request;
|
|
23
|
+
const { txParams } = transactionMeta;
|
|
24
|
+
const { gas: gasHex } = txParams;
|
|
25
|
+
if (!gasHex) {
|
|
26
|
+
throw new Error("Cannot estimate fee without gas value");
|
|
27
|
+
}
|
|
28
|
+
const gasDecimal = parseInt(gasHex, 16);
|
|
29
|
+
const difference = INCREMENT * LEVEL_DIFFERENCE;
|
|
30
|
+
const mediumMaxTarget = (__privateGet(this, _counter) + 1) * INCREMENT;
|
|
31
|
+
const mediumPriorityTarget = __privateGet(this, _counter) * INCREMENT;
|
|
32
|
+
const lowMaxTarget = mediumMaxTarget - difference;
|
|
33
|
+
const lowPriorityTarget = mediumPriorityTarget - difference;
|
|
34
|
+
const highMaxTarget = mediumMaxTarget + difference;
|
|
35
|
+
const highPriorityTarget = mediumPriorityTarget + difference;
|
|
36
|
+
__privateSet(this, _counter, __privateGet(this, _counter) + 1);
|
|
37
|
+
return {
|
|
38
|
+
estimates: {
|
|
39
|
+
type: "fee-market" /* FeeMarket */,
|
|
40
|
+
low: {
|
|
41
|
+
maxFeePerGas: __privateMethod(this, _getValueForTotalFee, getValueForTotalFee_fn).call(this, lowMaxTarget, gasDecimal),
|
|
42
|
+
maxPriorityFeePerGas: __privateMethod(this, _getValueForTotalFee, getValueForTotalFee_fn).call(this, lowPriorityTarget, gasDecimal)
|
|
43
|
+
},
|
|
44
|
+
medium: {
|
|
45
|
+
maxFeePerGas: __privateMethod(this, _getValueForTotalFee, getValueForTotalFee_fn).call(this, mediumMaxTarget, gasDecimal),
|
|
46
|
+
maxPriorityFeePerGas: __privateMethod(this, _getValueForTotalFee, getValueForTotalFee_fn).call(this, mediumPriorityTarget, gasDecimal)
|
|
47
|
+
},
|
|
48
|
+
high: {
|
|
49
|
+
maxFeePerGas: __privateMethod(this, _getValueForTotalFee, getValueForTotalFee_fn).call(this, highMaxTarget, gasDecimal),
|
|
50
|
+
maxPriorityFeePerGas: __privateMethod(this, _getValueForTotalFee, getValueForTotalFee_fn).call(this, highPriorityTarget, gasDecimal)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
_counter = new WeakMap();
|
|
57
|
+
_getValueForTotalFee = new WeakSet();
|
|
58
|
+
getValueForTotalFee_fn = function(totalFee, gas) {
|
|
59
|
+
const feeDecimal = Math.ceil(totalFee / gas);
|
|
60
|
+
return toHex(feeDecimal);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
TestGasFeeFlow
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=chunk-FMRLPVFZ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/gas-flows/TestGasFeeFlow.ts"],"sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n GasFeeEstimateType,\n type GasFeeFlow,\n type GasFeeFlowRequest,\n type GasFeeFlowResponse,\n type TransactionMeta,\n} from '../types';\n\nconst INCREMENT = 1e15; // 0.001 ETH\nconst LEVEL_DIFFERENCE = 0.5;\n\n/**\n * A gas fee flow to facilitate testing in the clients.\n * Increments the total gas fee by a fixed amount each time it is called.\n * Relies on the transaction's gas value to generate a distinct total fee in the UI.\n */\nexport class TestGasFeeFlow implements GasFeeFlow {\n #counter = 1;\n\n matchesTransaction(_transactionMeta: TransactionMeta): boolean {\n return true;\n }\n\n async getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse> {\n const { transactionMeta } = request;\n const { txParams } = transactionMeta;\n const { gas: gasHex } = txParams;\n\n if (!gasHex) {\n throw new Error('Cannot estimate fee without gas value');\n }\n\n const gasDecimal = parseInt(gasHex, 16);\n const difference = INCREMENT * LEVEL_DIFFERENCE;\n\n const mediumMaxTarget = (this.#counter + 1) * INCREMENT;\n const mediumPriorityTarget = this.#counter * INCREMENT;\n\n const lowMaxTarget = mediumMaxTarget - difference;\n const lowPriorityTarget = mediumPriorityTarget - difference;\n\n const highMaxTarget = mediumMaxTarget + difference;\n const highPriorityTarget = mediumPriorityTarget + difference;\n\n this.#counter += 1;\n\n return {\n estimates: {\n type: GasFeeEstimateType.FeeMarket,\n low: {\n maxFeePerGas: this.#getValueForTotalFee(lowMaxTarget, gasDecimal),\n maxPriorityFeePerGas: this.#getValueForTotalFee(\n lowPriorityTarget,\n gasDecimal,\n ),\n },\n medium: {\n maxFeePerGas: this.#getValueForTotalFee(mediumMaxTarget, gasDecimal),\n maxPriorityFeePerGas: this.#getValueForTotalFee(\n mediumPriorityTarget,\n gasDecimal,\n ),\n },\n high: {\n maxFeePerGas: this.#getValueForTotalFee(highMaxTarget, gasDecimal),\n maxPriorityFeePerGas: this.#getValueForTotalFee(\n highPriorityTarget,\n gasDecimal,\n ),\n },\n },\n };\n }\n\n #getValueForTotalFee(totalFee: number, gas: number): Hex {\n const feeDecimal = Math.ceil(totalFee / gas);\n return toHex(feeDecimal);\n }\n}\n"],"m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SAAO,MAAM,UAAU;AACzB;","names":[]}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var _chunkC2LNDHP4js = require('./chunk-C2LNDHP4.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var _chunkEMDU4XLMjs = require('./chunk-EMDU4XLM.js');
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
var _chunkS6VGOPUYjs = require('./chunk-S6VGOPUY.js');
|
|
@@ -182,17 +182,33 @@ function updateDefaultGasEstimates(txMeta) {
|
|
|
182
182
|
}
|
|
183
183
|
async function getSuggestedGasFees(request) {
|
|
184
184
|
const { eip1559, ethQuery, gasFeeFlows, getGasFeeEstimates, txMeta } = request;
|
|
185
|
+
const { networkClientId } = txMeta;
|
|
185
186
|
if (!eip1559 && txMeta.txParams.gasPrice || eip1559 && txMeta.txParams.maxFeePerGas && txMeta.txParams.maxPriorityFeePerGas) {
|
|
186
187
|
return {};
|
|
187
188
|
}
|
|
188
|
-
const gasFeeFlow =
|
|
189
|
+
const gasFeeFlow = _chunkEMDU4XLMjs.getGasFeeFlow.call(void 0, txMeta, gasFeeFlows);
|
|
189
190
|
try {
|
|
191
|
+
const gasFeeControllerData = await getGasFeeEstimates({ networkClientId });
|
|
190
192
|
const response = await gasFeeFlow.getGasFees({
|
|
191
193
|
ethQuery,
|
|
192
|
-
|
|
194
|
+
gasFeeControllerData,
|
|
193
195
|
transactionMeta: txMeta
|
|
194
196
|
});
|
|
195
|
-
|
|
197
|
+
const gasFeeEstimateType = response.estimates?.type;
|
|
198
|
+
switch (gasFeeEstimateType) {
|
|
199
|
+
case "fee-market" /* FeeMarket */:
|
|
200
|
+
return response.estimates.medium;
|
|
201
|
+
case "legacy" /* Legacy */:
|
|
202
|
+
return {
|
|
203
|
+
gasPrice: response.estimates.medium
|
|
204
|
+
};
|
|
205
|
+
case "eth_gasPrice" /* GasPrice */:
|
|
206
|
+
return { gasPrice: response.estimates.gasPrice };
|
|
207
|
+
default:
|
|
208
|
+
throw new Error(
|
|
209
|
+
`Unsupported gas fee estimate type returned from flow: ${gasFeeEstimateType}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
196
212
|
} catch (error) {
|
|
197
213
|
log("Failed to get suggested gas fees", error);
|
|
198
214
|
}
|
|
@@ -205,4 +221,4 @@ async function getSuggestedGasFees(request) {
|
|
|
205
221
|
|
|
206
222
|
|
|
207
223
|
exports.updateGasFees = updateGasFees; exports.gweiDecimalToWeiHex = gweiDecimalToWeiHex;
|
|
208
|
-
//# sourceMappingURL=chunk-
|
|
224
|
+
//# sourceMappingURL=chunk-KNYSD4JP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/gas-fees.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAOP,SAAS,OAAO,0BAA0B;AAqC1C,IAAM,MAAM,mBAAmB,eAAe,UAAU;AAExD,eAAsB,cAAc,SAA+B;AACjE,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,gBAAgB,EAAE,GAAG,OAAO,SAAS;AAE3C,QAAM,SAAS,uBAAuB;AAAA,IACpC,OAAO;AAAA,EACT;AACA,QAAM,eAAe,SACjB,SACA,QAAQ,gBAAgB,OAAO,OAAO;AAE1C,QAAM,mBAAmB,MAAM,oBAAoB,OAAO;AAE1D,MAAI,sBAAsB,gBAAgB;AAE1C,QAAM,mBAAqC;AAAA,IACzC,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,SAAO,SAAS,eAAe,gBAAgB,gBAAgB;AAE/D,SAAO,SAAS,uBACd,wBAAwB,gBAAgB;AAE1C,SAAO,SAAS,WAAW,YAAY,gBAAgB;AACvD,SAAO,eAAe,gBAAgB,gBAAgB;AAEtD,MAAI,8BAA8B;AAAA,IAChC,cAAc,OAAO,SAAS;AAAA,IAC9B,sBAAsB,OAAO,SAAS;AAAA,IACt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AAS,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 { GasFeeEstimateType, 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 const { networkClientId } = txMeta;\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 gasFeeControllerData = await getGasFeeEstimates({ networkClientId });\n\n const response = await gasFeeFlow.getGasFees({\n ethQuery,\n gasFeeControllerData,\n transactionMeta: txMeta,\n });\n\n const gasFeeEstimateType = response.estimates?.type;\n\n switch (gasFeeEstimateType) {\n case GasFeeEstimateType.FeeMarket:\n return response.estimates.medium;\n case GasFeeEstimateType.Legacy:\n return {\n gasPrice: response.estimates.medium,\n };\n case GasFeeEstimateType.GasPrice:\n return { gasPrice: response.estimates.gasPrice };\n default:\n throw new Error(\n `Unsupported gas fee estimate type returned from flow: ${gasFeeEstimateType}`,\n );\n }\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"]}
|