@metamask-previews/gas-fee-controller 20.0.0-preview-9edcf0e0 → 20.0.0-preview-cf09c0a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/GasFeeController.cjs +298 -0
  2. package/dist/GasFeeController.cjs.map +1 -0
  3. package/dist/{types/GasFeeController.d.ts → GasFeeController.d.cts} +5 -5
  4. package/dist/GasFeeController.d.cts.map +1 -0
  5. package/dist/GasFeeController.d.mts +244 -0
  6. package/dist/GasFeeController.d.mts.map +1 -0
  7. package/dist/GasFeeController.mjs +296 -12
  8. package/dist/GasFeeController.mjs.map +1 -1
  9. package/dist/determineGasFeeCalculations.cjs +105 -0
  10. package/dist/determineGasFeeCalculations.cjs.map +1 -0
  11. package/dist/{types/determineGasFeeCalculations.d.ts → determineGasFeeCalculations.d.cts} +2 -2
  12. package/dist/determineGasFeeCalculations.d.cts.map +1 -0
  13. package/dist/determineGasFeeCalculations.d.mts +42 -0
  14. package/dist/determineGasFeeCalculations.d.mts.map +1 -0
  15. package/dist/determineGasFeeCalculations.mjs +101 -7
  16. package/dist/determineGasFeeCalculations.mjs.map +1 -1
  17. package/dist/gas-util.cjs +143 -0
  18. package/dist/gas-util.cjs.map +1 -0
  19. package/dist/{types/gas-util.d.ts → gas-util.d.cts} +3 -3
  20. package/dist/gas-util.d.cts.map +1 -0
  21. package/dist/gas-util.d.mts +43 -0
  22. package/dist/gas-util.d.mts.map +1 -0
  23. package/dist/gas-util.mjs +138 -14
  24. package/dist/gas-util.mjs.map +1 -1
  25. package/dist/index.cjs +18 -0
  26. package/dist/index.cjs.map +1 -0
  27. package/dist/index.d.cts +2 -0
  28. package/dist/index.d.cts.map +1 -0
  29. package/dist/index.d.mts +2 -0
  30. package/dist/index.d.mts.map +1 -0
  31. package/dist/index.mjs +1 -11
  32. package/dist/index.mjs.map +1 -1
  33. package/package.json +12 -7
  34. package/dist/GasFeeController.js +0 -14
  35. package/dist/GasFeeController.js.map +0 -1
  36. package/dist/chunk-2MFVV2BX.js +0 -156
  37. package/dist/chunk-2MFVV2BX.js.map +0 -1
  38. package/dist/chunk-R3IOI7AK.mjs +0 -156
  39. package/dist/chunk-R3IOI7AK.mjs.map +0 -1
  40. package/dist/chunk-S5H5NUEH.js +0 -401
  41. package/dist/chunk-S5H5NUEH.js.map +0 -1
  42. package/dist/chunk-WXXOCPNT.mjs +0 -401
  43. package/dist/chunk-WXXOCPNT.mjs.map +0 -1
  44. package/dist/determineGasFeeCalculations.js +0 -8
  45. package/dist/determineGasFeeCalculations.js.map +0 -1
  46. package/dist/gas-util.js +0 -15
  47. package/dist/gas-util.js.map +0 -1
  48. package/dist/index.js +0 -12
  49. package/dist/index.js.map +0 -1
  50. package/dist/tsconfig.build.tsbuildinfo +0 -1
  51. package/dist/types/GasFeeController.d.ts.map +0 -1
  52. package/dist/types/determineGasFeeCalculations.d.ts.map +0 -1
  53. package/dist/types/gas-util.d.ts.map +0 -1
  54. package/dist/types/index.d.ts +0 -2
  55. package/dist/types/index.d.ts.map +0 -1
@@ -1,14 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
-
6
- var _chunkS5H5NUEHjs = require('./chunk-S5H5NUEH.js');
7
- require('./chunk-2MFVV2BX.js');
8
-
9
-
10
-
11
-
12
-
13
- exports.GAS_ESTIMATE_TYPES = _chunkS5H5NUEHjs.GAS_ESTIMATE_TYPES; exports.GasFeeController = _chunkS5H5NUEHjs.GasFeeController; exports.LEGACY_GAS_PRICES_API_URL = _chunkS5H5NUEHjs.LEGACY_GAS_PRICES_API_URL; exports.default = _chunkS5H5NUEHjs.GasFeeController_default;
14
- //# sourceMappingURL=GasFeeController.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,156 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __accessCheck = (obj, member, msg) => {
2
- if (!member.has(obj))
3
- throw TypeError("Cannot " + msg);
4
- };
5
- var __privateGet = (obj, member, getter) => {
6
- __accessCheck(obj, member, "read from private field");
7
- return getter ? getter.call(obj) : member.get(obj);
8
- };
9
- var __privateAdd = (obj, member, value) => {
10
- if (member.has(obj))
11
- throw TypeError("Cannot add the same private member more than once");
12
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
- };
14
- var __privateSet = (obj, member, value, setter) => {
15
- __accessCheck(obj, member, "write to private field");
16
- setter ? setter.call(obj, value) : member.set(obj, value);
17
- return value;
18
- };
19
- var __privateMethod = (obj, member, method) => {
20
- __accessCheck(obj, member, "access private method");
21
- return method;
22
- };
23
-
24
- // src/gas-util.ts
25
-
26
-
27
-
28
-
29
-
30
- var _controllerutils = require('@metamask/controller-utils');
31
- var _bnjs = require('bn.js'); var _bnjs2 = _interopRequireDefault(_bnjs);
32
- var makeClientIdHeader = (clientId) => ({ "X-Client-Id": clientId });
33
- function normalizeGWEIDecimalNumbers(n) {
34
- const numberAsWEIHex = _controllerutils.gweiDecToWEIBN.call(void 0, n).toString(16);
35
- const numberAsGWEI = _controllerutils.weiHexToGweiDec.call(void 0, numberAsWEIHex);
36
- return numberAsGWEI;
37
- }
38
- async function fetchGasEstimates(url, clientId) {
39
- const estimates = await _controllerutils.handleFetch.call(void 0,
40
- url,
41
- clientId ? { headers: makeClientIdHeader(clientId) } : void 0
42
- );
43
- return {
44
- low: {
45
- ...estimates.low,
46
- suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(
47
- estimates.low.suggestedMaxPriorityFeePerGas
48
- ),
49
- suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(
50
- estimates.low.suggestedMaxFeePerGas
51
- )
52
- },
53
- medium: {
54
- ...estimates.medium,
55
- suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(
56
- estimates.medium.suggestedMaxPriorityFeePerGas
57
- ),
58
- suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(
59
- estimates.medium.suggestedMaxFeePerGas
60
- )
61
- },
62
- high: {
63
- ...estimates.high,
64
- suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(
65
- estimates.high.suggestedMaxPriorityFeePerGas
66
- ),
67
- suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(
68
- estimates.high.suggestedMaxFeePerGas
69
- )
70
- },
71
- estimatedBaseFee: normalizeGWEIDecimalNumbers(estimates.estimatedBaseFee),
72
- historicalBaseFeeRange: estimates.historicalBaseFeeRange,
73
- baseFeeTrend: estimates.baseFeeTrend,
74
- latestPriorityFeeRange: estimates.latestPriorityFeeRange,
75
- historicalPriorityFeeRange: estimates.historicalPriorityFeeRange,
76
- priorityFeeTrend: estimates.priorityFeeTrend,
77
- networkCongestion: estimates.networkCongestion
78
- };
79
- }
80
- async function fetchLegacyGasPriceEstimates(url, clientId) {
81
- const result = await _controllerutils.handleFetch.call(void 0, url, {
82
- referrer: url,
83
- referrerPolicy: "no-referrer-when-downgrade",
84
- method: "GET",
85
- mode: "cors",
86
- headers: {
87
- "Content-Type": "application/json",
88
- ...clientId && makeClientIdHeader(clientId)
89
- }
90
- });
91
- return {
92
- low: result.SafeGasPrice,
93
- medium: result.ProposeGasPrice,
94
- high: result.FastGasPrice
95
- };
96
- }
97
- async function fetchEthGasPriceEstimate(ethQuery) {
98
- const gasPrice = await _controllerutils.query.call(void 0, ethQuery, "gasPrice");
99
- return {
100
- gasPrice: _controllerutils.weiHexToGweiDec.call(void 0, gasPrice).toString()
101
- };
102
- }
103
- function calculateTimeEstimate(maxPriorityFeePerGas, maxFeePerGas, gasFeeEstimates) {
104
- const { low, medium, high, estimatedBaseFee } = gasFeeEstimates;
105
- const maxPriorityFeePerGasInWEI = _controllerutils.gweiDecToWEIBN.call(void 0, maxPriorityFeePerGas);
106
- const maxFeePerGasInWEI = _controllerutils.gweiDecToWEIBN.call(void 0, maxFeePerGas);
107
- const estimatedBaseFeeInWEI = _controllerutils.gweiDecToWEIBN.call(void 0, estimatedBaseFee);
108
- const effectiveMaxPriorityFee = _bnjs2.default.min(
109
- maxPriorityFeePerGasInWEI,
110
- maxFeePerGasInWEI.sub(estimatedBaseFeeInWEI)
111
- );
112
- const lowMaxPriorityFeeInWEI = _controllerutils.gweiDecToWEIBN.call(void 0,
113
- low.suggestedMaxPriorityFeePerGas
114
- );
115
- const mediumMaxPriorityFeeInWEI = _controllerutils.gweiDecToWEIBN.call(void 0,
116
- medium.suggestedMaxPriorityFeePerGas
117
- );
118
- const highMaxPriorityFeeInWEI = _controllerutils.gweiDecToWEIBN.call(void 0,
119
- high.suggestedMaxPriorityFeePerGas
120
- );
121
- let lowerTimeBound;
122
- let upperTimeBound;
123
- if (effectiveMaxPriorityFee.lt(lowMaxPriorityFeeInWEI)) {
124
- lowerTimeBound = null;
125
- upperTimeBound = "unknown";
126
- } else if (effectiveMaxPriorityFee.gte(lowMaxPriorityFeeInWEI) && effectiveMaxPriorityFee.lt(mediumMaxPriorityFeeInWEI)) {
127
- lowerTimeBound = low.minWaitTimeEstimate;
128
- upperTimeBound = low.maxWaitTimeEstimate;
129
- } else if (effectiveMaxPriorityFee.gte(mediumMaxPriorityFeeInWEI) && effectiveMaxPriorityFee.lt(highMaxPriorityFeeInWEI)) {
130
- lowerTimeBound = medium.minWaitTimeEstimate;
131
- upperTimeBound = medium.maxWaitTimeEstimate;
132
- } else if (effectiveMaxPriorityFee.eq(highMaxPriorityFeeInWEI)) {
133
- lowerTimeBound = high.minWaitTimeEstimate;
134
- upperTimeBound = high.maxWaitTimeEstimate;
135
- } else {
136
- lowerTimeBound = 0;
137
- upperTimeBound = high.maxWaitTimeEstimate;
138
- }
139
- return {
140
- lowerTimeBound,
141
- upperTimeBound
142
- };
143
- }
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
- exports.__privateGet = __privateGet; exports.__privateAdd = __privateAdd; exports.__privateSet = __privateSet; exports.__privateMethod = __privateMethod; exports.normalizeGWEIDecimalNumbers = normalizeGWEIDecimalNumbers; exports.fetchGasEstimates = fetchGasEstimates; exports.fetchLegacyGasPriceEstimates = fetchLegacyGasPriceEstimates; exports.fetchEthGasPriceEstimate = fetchEthGasPriceEstimate; exports.calculateTimeEstimate = calculateTimeEstimate;
156
- //# sourceMappingURL=chunk-2MFVV2BX.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/gas-util.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,OAAO,QAAQ;AAUf,IAAM,qBAAqB,CAAC,cAAsB,EAAE,eAAe,SAAS;AAQrE,SAAS,4BAA4B,GAAoB;AAC9D,QAAM,iBAAiB,eAAe,CAAC,EAAE,SAAS,EAAE;AACpD,QAAM,eAAe,gBAAgB,cAAc;AACnD,SAAO;AACT;AASA,eAAsB,kBACpB,KACA,UAC0B;AAC1B,QAAM,YAAY,MAAM;AAAA,IACtB;AAAA,IACA,WAAW,EAAE,SAAS,mBAAmB,QAAQ,EAAE,IAAI;AAAA,EACzD;AACA,SAAO;AAAA,IACL,KAAK;AAAA,MACH,GAAG,UAAU;AAAA,MACb,+BAA+B;AAAA,QAC7B,UAAU,IAAI;AAAA,MAChB;AAAA,MACA,uBAAuB;AAAA,QACrB,UAAU,IAAI;AAAA,MAChB;AAAA,IACF;AAAA,IACA,QAAQ;AAAA,MACN,GAAG,UAAU;AAAA,MACb,+BAA+B;AAAA,QAC7B,UAAU,OAAO;AAAA,MACnB;AAAA,MACA,uBAAuB;AAAA,QACrB,UAAU,OAAO;AAAA,MACnB;AAAA,IACF;AAAA,IACA,MAAM;AAAA,MACJ,GAAG,UAAU;AAAA,MACb,+BAA+B;AAAA,QAC7B,UAAU,KAAK;AAAA,MACjB;AAAA,MACA,uBAAuB;AAAA,QACrB,UAAU,KAAK;AAAA,MACjB;AAAA,IACF;AAAA,IAC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B,WACE,wBAAwB,IAAI,sBAAsB,KAClD,wBAAwB,GAAG,yBAAyB,GACpD;AACA,qBAAiB,IAAI;AACrB,qBAAiB,IAAI;AAAA,EACvB,WACE,wBAAwB,IAAI,yBAAyB,KACrD,wBAAwB,GAAG,uBAAuB,GAClD;AACA,qBAAiB,OAAO;AACxB,qBAAiB,OAAO;AAAA,EAC1B,WAAW,wBAAwB,GAAG,uBAAuB,GAAG;AAC9D,qBAAiB,KAAK;AACtB,qBAAiB,KAAK;AAAA,EACxB,OAAO;AACL,qBAAiB;AACjB,qBAAiB,KAAK;AAAA,EACxB;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF","sourcesContent":["import {\n query,\n handleFetch,\n gweiDecToWEIBN,\n weiHexToGweiDec,\n} from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport BN from 'bn.js';\n\nimport type {\n GasFeeEstimates,\n EthGasPriceEstimate,\n EstimatedGasFeeTimeBounds,\n unknownString,\n LegacyGasPriceEstimate,\n} from './GasFeeController';\n\nconst makeClientIdHeader = (clientId: string) => ({ 'X-Client-Id': clientId });\n\n/**\n * Convert a decimal GWEI value to a decimal string rounded to the nearest WEI.\n *\n * @param n - The input GWEI amount, as a decimal string or a number.\n * @returns The decimal string GWEI amount.\n */\nexport function normalizeGWEIDecimalNumbers(n: string | number) {\n const numberAsWEIHex = gweiDecToWEIBN(n).toString(16);\n const numberAsGWEI = weiHexToGweiDec(numberAsWEIHex);\n return numberAsGWEI;\n}\n\n/**\n * Fetch gas estimates from the given URL.\n *\n * @param url - The gas estimate URL.\n * @param clientId - The client ID used to identify to the API who is asking for estimates.\n * @returns The gas estimates.\n */\nexport async function fetchGasEstimates(\n url: string,\n clientId?: string,\n): Promise<GasFeeEstimates> {\n const estimates = await handleFetch(\n url,\n clientId ? { headers: makeClientIdHeader(clientId) } : undefined,\n );\n return {\n low: {\n ...estimates.low,\n suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.low.suggestedMaxPriorityFeePerGas,\n ),\n suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.low.suggestedMaxFeePerGas,\n ),\n },\n medium: {\n ...estimates.medium,\n suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.medium.suggestedMaxPriorityFeePerGas,\n ),\n suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.medium.suggestedMaxFeePerGas,\n ),\n },\n high: {\n ...estimates.high,\n suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.high.suggestedMaxPriorityFeePerGas,\n ),\n suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.high.suggestedMaxFeePerGas,\n ),\n },\n estimatedBaseFee: normalizeGWEIDecimalNumbers(estimates.estimatedBaseFee),\n historicalBaseFeeRange: estimates.historicalBaseFeeRange,\n baseFeeTrend: estimates.baseFeeTrend,\n latestPriorityFeeRange: estimates.latestPriorityFeeRange,\n historicalPriorityFeeRange: estimates.historicalPriorityFeeRange,\n priorityFeeTrend: estimates.priorityFeeTrend,\n networkCongestion: estimates.networkCongestion,\n };\n}\n\n/**\n * Hit the legacy MetaSwaps gasPrices estimate api and return the low, medium\n * high values from that API.\n *\n * @param url - The URL to fetch gas price estimates from.\n * @param clientId - The client ID used to identify to the API who is asking for estimates.\n * @returns The gas price estimates.\n */\nexport async function fetchLegacyGasPriceEstimates(\n url: string,\n clientId?: string,\n): Promise<LegacyGasPriceEstimate> {\n const result = await handleFetch(url, {\n referrer: url,\n referrerPolicy: 'no-referrer-when-downgrade',\n method: 'GET',\n mode: 'cors',\n headers: {\n 'Content-Type': 'application/json',\n ...(clientId && makeClientIdHeader(clientId)),\n },\n });\n return {\n low: result.SafeGasPrice,\n medium: result.ProposeGasPrice,\n high: result.FastGasPrice,\n };\n}\n\n/**\n * Get a gas price estimate from the network using the `eth_gasPrice` method.\n *\n * @param ethQuery - The EthQuery instance to call the network with.\n * @returns A gas price estimate.\n */\nexport async function fetchEthGasPriceEstimate(\n ethQuery: EthQuery,\n): Promise<EthGasPriceEstimate> {\n const gasPrice = await query(ethQuery, 'gasPrice');\n return {\n gasPrice: weiHexToGweiDec(gasPrice).toString(),\n };\n}\n\n/**\n * Estimate the time it will take for a transaction to be confirmed.\n *\n * @param maxPriorityFeePerGas - The max priority fee per gas.\n * @param maxFeePerGas - The max fee per gas.\n * @param gasFeeEstimates - The gas fee estimates.\n * @returns The estimated lower and upper bounds for when this transaction will be confirmed.\n */\nexport function calculateTimeEstimate(\n maxPriorityFeePerGas: string,\n maxFeePerGas: string,\n gasFeeEstimates: GasFeeEstimates,\n): EstimatedGasFeeTimeBounds {\n const { low, medium, high, estimatedBaseFee } = gasFeeEstimates;\n\n const maxPriorityFeePerGasInWEI = gweiDecToWEIBN(maxPriorityFeePerGas);\n const maxFeePerGasInWEI = gweiDecToWEIBN(maxFeePerGas);\n const estimatedBaseFeeInWEI = gweiDecToWEIBN(estimatedBaseFee);\n\n const effectiveMaxPriorityFee = BN.min(\n maxPriorityFeePerGasInWEI,\n maxFeePerGasInWEI.sub(estimatedBaseFeeInWEI),\n );\n\n const lowMaxPriorityFeeInWEI = gweiDecToWEIBN(\n low.suggestedMaxPriorityFeePerGas,\n );\n const mediumMaxPriorityFeeInWEI = gweiDecToWEIBN(\n medium.suggestedMaxPriorityFeePerGas,\n );\n const highMaxPriorityFeeInWEI = gweiDecToWEIBN(\n high.suggestedMaxPriorityFeePerGas,\n );\n\n let lowerTimeBound;\n let upperTimeBound;\n\n if (effectiveMaxPriorityFee.lt(lowMaxPriorityFeeInWEI)) {\n lowerTimeBound = null;\n upperTimeBound = 'unknown' as unknownString;\n } else if (\n effectiveMaxPriorityFee.gte(lowMaxPriorityFeeInWEI) &&\n effectiveMaxPriorityFee.lt(mediumMaxPriorityFeeInWEI)\n ) {\n lowerTimeBound = low.minWaitTimeEstimate;\n upperTimeBound = low.maxWaitTimeEstimate;\n } else if (\n effectiveMaxPriorityFee.gte(mediumMaxPriorityFeeInWEI) &&\n effectiveMaxPriorityFee.lt(highMaxPriorityFeeInWEI)\n ) {\n lowerTimeBound = medium.minWaitTimeEstimate;\n upperTimeBound = medium.maxWaitTimeEstimate;\n } else if (effectiveMaxPriorityFee.eq(highMaxPriorityFeeInWEI)) {\n lowerTimeBound = high.minWaitTimeEstimate;\n upperTimeBound = high.maxWaitTimeEstimate;\n } else {\n lowerTimeBound = 0;\n upperTimeBound = high.maxWaitTimeEstimate;\n }\n\n return {\n lowerTimeBound,\n upperTimeBound,\n };\n}\n"]}
@@ -1,156 +0,0 @@
1
- var __accessCheck = (obj, member, msg) => {
2
- if (!member.has(obj))
3
- throw TypeError("Cannot " + msg);
4
- };
5
- var __privateGet = (obj, member, getter) => {
6
- __accessCheck(obj, member, "read from private field");
7
- return getter ? getter.call(obj) : member.get(obj);
8
- };
9
- var __privateAdd = (obj, member, value) => {
10
- if (member.has(obj))
11
- throw TypeError("Cannot add the same private member more than once");
12
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
- };
14
- var __privateSet = (obj, member, value, setter) => {
15
- __accessCheck(obj, member, "write to private field");
16
- setter ? setter.call(obj, value) : member.set(obj, value);
17
- return value;
18
- };
19
- var __privateMethod = (obj, member, method) => {
20
- __accessCheck(obj, member, "access private method");
21
- return method;
22
- };
23
-
24
- // src/gas-util.ts
25
- import {
26
- query,
27
- handleFetch,
28
- gweiDecToWEIBN,
29
- weiHexToGweiDec
30
- } from "@metamask/controller-utils";
31
- import BN from "bn.js";
32
- var makeClientIdHeader = (clientId) => ({ "X-Client-Id": clientId });
33
- function normalizeGWEIDecimalNumbers(n) {
34
- const numberAsWEIHex = gweiDecToWEIBN(n).toString(16);
35
- const numberAsGWEI = weiHexToGweiDec(numberAsWEIHex);
36
- return numberAsGWEI;
37
- }
38
- async function fetchGasEstimates(url, clientId) {
39
- const estimates = await handleFetch(
40
- url,
41
- clientId ? { headers: makeClientIdHeader(clientId) } : void 0
42
- );
43
- return {
44
- low: {
45
- ...estimates.low,
46
- suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(
47
- estimates.low.suggestedMaxPriorityFeePerGas
48
- ),
49
- suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(
50
- estimates.low.suggestedMaxFeePerGas
51
- )
52
- },
53
- medium: {
54
- ...estimates.medium,
55
- suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(
56
- estimates.medium.suggestedMaxPriorityFeePerGas
57
- ),
58
- suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(
59
- estimates.medium.suggestedMaxFeePerGas
60
- )
61
- },
62
- high: {
63
- ...estimates.high,
64
- suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(
65
- estimates.high.suggestedMaxPriorityFeePerGas
66
- ),
67
- suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(
68
- estimates.high.suggestedMaxFeePerGas
69
- )
70
- },
71
- estimatedBaseFee: normalizeGWEIDecimalNumbers(estimates.estimatedBaseFee),
72
- historicalBaseFeeRange: estimates.historicalBaseFeeRange,
73
- baseFeeTrend: estimates.baseFeeTrend,
74
- latestPriorityFeeRange: estimates.latestPriorityFeeRange,
75
- historicalPriorityFeeRange: estimates.historicalPriorityFeeRange,
76
- priorityFeeTrend: estimates.priorityFeeTrend,
77
- networkCongestion: estimates.networkCongestion
78
- };
79
- }
80
- async function fetchLegacyGasPriceEstimates(url, clientId) {
81
- const result = await handleFetch(url, {
82
- referrer: url,
83
- referrerPolicy: "no-referrer-when-downgrade",
84
- method: "GET",
85
- mode: "cors",
86
- headers: {
87
- "Content-Type": "application/json",
88
- ...clientId && makeClientIdHeader(clientId)
89
- }
90
- });
91
- return {
92
- low: result.SafeGasPrice,
93
- medium: result.ProposeGasPrice,
94
- high: result.FastGasPrice
95
- };
96
- }
97
- async function fetchEthGasPriceEstimate(ethQuery) {
98
- const gasPrice = await query(ethQuery, "gasPrice");
99
- return {
100
- gasPrice: weiHexToGweiDec(gasPrice).toString()
101
- };
102
- }
103
- function calculateTimeEstimate(maxPriorityFeePerGas, maxFeePerGas, gasFeeEstimates) {
104
- const { low, medium, high, estimatedBaseFee } = gasFeeEstimates;
105
- const maxPriorityFeePerGasInWEI = gweiDecToWEIBN(maxPriorityFeePerGas);
106
- const maxFeePerGasInWEI = gweiDecToWEIBN(maxFeePerGas);
107
- const estimatedBaseFeeInWEI = gweiDecToWEIBN(estimatedBaseFee);
108
- const effectiveMaxPriorityFee = BN.min(
109
- maxPriorityFeePerGasInWEI,
110
- maxFeePerGasInWEI.sub(estimatedBaseFeeInWEI)
111
- );
112
- const lowMaxPriorityFeeInWEI = gweiDecToWEIBN(
113
- low.suggestedMaxPriorityFeePerGas
114
- );
115
- const mediumMaxPriorityFeeInWEI = gweiDecToWEIBN(
116
- medium.suggestedMaxPriorityFeePerGas
117
- );
118
- const highMaxPriorityFeeInWEI = gweiDecToWEIBN(
119
- high.suggestedMaxPriorityFeePerGas
120
- );
121
- let lowerTimeBound;
122
- let upperTimeBound;
123
- if (effectiveMaxPriorityFee.lt(lowMaxPriorityFeeInWEI)) {
124
- lowerTimeBound = null;
125
- upperTimeBound = "unknown";
126
- } else if (effectiveMaxPriorityFee.gte(lowMaxPriorityFeeInWEI) && effectiveMaxPriorityFee.lt(mediumMaxPriorityFeeInWEI)) {
127
- lowerTimeBound = low.minWaitTimeEstimate;
128
- upperTimeBound = low.maxWaitTimeEstimate;
129
- } else if (effectiveMaxPriorityFee.gte(mediumMaxPriorityFeeInWEI) && effectiveMaxPriorityFee.lt(highMaxPriorityFeeInWEI)) {
130
- lowerTimeBound = medium.minWaitTimeEstimate;
131
- upperTimeBound = medium.maxWaitTimeEstimate;
132
- } else if (effectiveMaxPriorityFee.eq(highMaxPriorityFeeInWEI)) {
133
- lowerTimeBound = high.minWaitTimeEstimate;
134
- upperTimeBound = high.maxWaitTimeEstimate;
135
- } else {
136
- lowerTimeBound = 0;
137
- upperTimeBound = high.maxWaitTimeEstimate;
138
- }
139
- return {
140
- lowerTimeBound,
141
- upperTimeBound
142
- };
143
- }
144
-
145
- export {
146
- __privateGet,
147
- __privateAdd,
148
- __privateSet,
149
- __privateMethod,
150
- normalizeGWEIDecimalNumbers,
151
- fetchGasEstimates,
152
- fetchLegacyGasPriceEstimates,
153
- fetchEthGasPriceEstimate,
154
- calculateTimeEstimate
155
- };
156
- //# sourceMappingURL=chunk-R3IOI7AK.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/gas-util.ts"],"sourcesContent":["import {\n query,\n handleFetch,\n gweiDecToWEIBN,\n weiHexToGweiDec,\n} from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport BN from 'bn.js';\n\nimport type {\n GasFeeEstimates,\n EthGasPriceEstimate,\n EstimatedGasFeeTimeBounds,\n unknownString,\n LegacyGasPriceEstimate,\n} from './GasFeeController';\n\nconst makeClientIdHeader = (clientId: string) => ({ 'X-Client-Id': clientId });\n\n/**\n * Convert a decimal GWEI value to a decimal string rounded to the nearest WEI.\n *\n * @param n - The input GWEI amount, as a decimal string or a number.\n * @returns The decimal string GWEI amount.\n */\nexport function normalizeGWEIDecimalNumbers(n: string | number) {\n const numberAsWEIHex = gweiDecToWEIBN(n).toString(16);\n const numberAsGWEI = weiHexToGweiDec(numberAsWEIHex);\n return numberAsGWEI;\n}\n\n/**\n * Fetch gas estimates from the given URL.\n *\n * @param url - The gas estimate URL.\n * @param clientId - The client ID used to identify to the API who is asking for estimates.\n * @returns The gas estimates.\n */\nexport async function fetchGasEstimates(\n url: string,\n clientId?: string,\n): Promise<GasFeeEstimates> {\n const estimates = await handleFetch(\n url,\n clientId ? { headers: makeClientIdHeader(clientId) } : undefined,\n );\n return {\n low: {\n ...estimates.low,\n suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.low.suggestedMaxPriorityFeePerGas,\n ),\n suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.low.suggestedMaxFeePerGas,\n ),\n },\n medium: {\n ...estimates.medium,\n suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.medium.suggestedMaxPriorityFeePerGas,\n ),\n suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.medium.suggestedMaxFeePerGas,\n ),\n },\n high: {\n ...estimates.high,\n suggestedMaxPriorityFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.high.suggestedMaxPriorityFeePerGas,\n ),\n suggestedMaxFeePerGas: normalizeGWEIDecimalNumbers(\n estimates.high.suggestedMaxFeePerGas,\n ),\n },\n estimatedBaseFee: normalizeGWEIDecimalNumbers(estimates.estimatedBaseFee),\n historicalBaseFeeRange: estimates.historicalBaseFeeRange,\n baseFeeTrend: estimates.baseFeeTrend,\n latestPriorityFeeRange: estimates.latestPriorityFeeRange,\n historicalPriorityFeeRange: estimates.historicalPriorityFeeRange,\n priorityFeeTrend: estimates.priorityFeeTrend,\n networkCongestion: estimates.networkCongestion,\n };\n}\n\n/**\n * Hit the legacy MetaSwaps gasPrices estimate api and return the low, medium\n * high values from that API.\n *\n * @param url - The URL to fetch gas price estimates from.\n * @param clientId - The client ID used to identify to the API who is asking for estimates.\n * @returns The gas price estimates.\n */\nexport async function fetchLegacyGasPriceEstimates(\n url: string,\n clientId?: string,\n): Promise<LegacyGasPriceEstimate> {\n const result = await handleFetch(url, {\n referrer: url,\n referrerPolicy: 'no-referrer-when-downgrade',\n method: 'GET',\n mode: 'cors',\n headers: {\n 'Content-Type': 'application/json',\n ...(clientId && makeClientIdHeader(clientId)),\n },\n });\n return {\n low: result.SafeGasPrice,\n medium: result.ProposeGasPrice,\n high: result.FastGasPrice,\n };\n}\n\n/**\n * Get a gas price estimate from the network using the `eth_gasPrice` method.\n *\n * @param ethQuery - The EthQuery instance to call the network with.\n * @returns A gas price estimate.\n */\nexport async function fetchEthGasPriceEstimate(\n ethQuery: EthQuery,\n): Promise<EthGasPriceEstimate> {\n const gasPrice = await query(ethQuery, 'gasPrice');\n return {\n gasPrice: weiHexToGweiDec(gasPrice).toString(),\n };\n}\n\n/**\n * Estimate the time it will take for a transaction to be confirmed.\n *\n * @param maxPriorityFeePerGas - The max priority fee per gas.\n * @param maxFeePerGas - The max fee per gas.\n * @param gasFeeEstimates - The gas fee estimates.\n * @returns The estimated lower and upper bounds for when this transaction will be confirmed.\n */\nexport function calculateTimeEstimate(\n maxPriorityFeePerGas: string,\n maxFeePerGas: string,\n gasFeeEstimates: GasFeeEstimates,\n): EstimatedGasFeeTimeBounds {\n const { low, medium, high, estimatedBaseFee } = gasFeeEstimates;\n\n const maxPriorityFeePerGasInWEI = gweiDecToWEIBN(maxPriorityFeePerGas);\n const maxFeePerGasInWEI = gweiDecToWEIBN(maxFeePerGas);\n const estimatedBaseFeeInWEI = gweiDecToWEIBN(estimatedBaseFee);\n\n const effectiveMaxPriorityFee = BN.min(\n maxPriorityFeePerGasInWEI,\n maxFeePerGasInWEI.sub(estimatedBaseFeeInWEI),\n );\n\n const lowMaxPriorityFeeInWEI = gweiDecToWEIBN(\n low.suggestedMaxPriorityFeePerGas,\n );\n const mediumMaxPriorityFeeInWEI = gweiDecToWEIBN(\n medium.suggestedMaxPriorityFeePerGas,\n );\n const highMaxPriorityFeeInWEI = gweiDecToWEIBN(\n high.suggestedMaxPriorityFeePerGas,\n );\n\n let lowerTimeBound;\n let upperTimeBound;\n\n if (effectiveMaxPriorityFee.lt(lowMaxPriorityFeeInWEI)) {\n lowerTimeBound = null;\n upperTimeBound = 'unknown' as unknownString;\n } else if (\n effectiveMaxPriorityFee.gte(lowMaxPriorityFeeInWEI) &&\n effectiveMaxPriorityFee.lt(mediumMaxPriorityFeeInWEI)\n ) {\n lowerTimeBound = low.minWaitTimeEstimate;\n upperTimeBound = low.maxWaitTimeEstimate;\n } else if (\n effectiveMaxPriorityFee.gte(mediumMaxPriorityFeeInWEI) &&\n effectiveMaxPriorityFee.lt(highMaxPriorityFeeInWEI)\n ) {\n lowerTimeBound = medium.minWaitTimeEstimate;\n upperTimeBound = medium.maxWaitTimeEstimate;\n } else if (effectiveMaxPriorityFee.eq(highMaxPriorityFeeInWEI)) {\n lowerTimeBound = high.minWaitTimeEstimate;\n upperTimeBound = high.maxWaitTimeEstimate;\n } else {\n lowerTimeBound = 0;\n upperTimeBound = high.maxWaitTimeEstimate;\n }\n\n return {\n lowerTimeBound,\n upperTimeBound,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,OAAO,QAAQ;AAUf,IAAM,qBAAqB,CAAC,cAAsB,EAAE,eAAe,SAAS;AAQrE,SAAS,4BAA4B,GAAoB;AAC9D,QAAM,iBAAiB,eAAe,CAAC,EAAE,SAAS,EAAE;AACpD,QAAM,eAAe,gBAAgB,cAAc;AACnD,SAAO;AACT;AASA,eAAsB,kBACpB,KACA,UAC0B;AAC1B,QAAM,YAAY,MAAM;AAAA,IACtB;AAAA,IACA,WAAW,EAAE,SAAS,mBAAmB,QAAQ,EAAE,IAAI;AAAA,EACzD;AACA,SAAO;AAAA,IACL,KAAK;AAAA,MACH,GAAG,UAAU;AAAA,MACb,+BAA+B;AAAA,QAC7B,UAAU,IAAI;AAAA,MAChB;AAAA,MACA,uBAAuB;AAAA,QACrB,UAAU,IAAI;AAAA,MAChB;AAAA,IACF;AAAA,IACA,QAAQ;AAAA,MACN,GAAG,UAAU;AAAA,MACb,+BAA+B;AAAA,QAC7B,UAAU,OAAO;AAAA,MACnB;AAAA,MACA,uBAAuB;AAAA,QACrB,UAAU,OAAO;AAAA,MACnB;AAAA,IACF;AAAA,IACA,MAAM;AAAA,MACJ,GAAG,UAAU;AAAA,MACb,+BAA+B;AAAA,QAC7B,UAAU,KAAK;AAAA,MACjB;AAAA,MACA,uBAAuB;AAAA,QACrB,UAAU,KAAK;AAAA,MACjB;AAAA,IACF;AAAA,IACA,kBAAkB,4BAA4B,UAAU,gBAAgB;AAAA,IACxE,wBAAwB,UAAU;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