@metamask/transaction-controller 22.0.0 → 23.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 +51 -1
- package/dist/TransactionController.d.ts +106 -107
- package/dist/TransactionController.d.ts.map +1 -1
- package/dist/TransactionController.js +355 -219
- package/dist/TransactionController.js.map +1 -1
- package/dist/gas-flows/DefaultGasFeeFlow.d.ts +10 -0
- package/dist/gas-flows/DefaultGasFeeFlow.d.ts.map +1 -0
- package/dist/gas-flows/DefaultGasFeeFlow.js +78 -0
- package/dist/gas-flows/DefaultGasFeeFlow.js.map +1 -0
- package/dist/gas-flows/LineaGasFeeFlow.d.ts +12 -0
- package/dist/gas-flows/LineaGasFeeFlow.d.ts.map +1 -0
- package/dist/gas-flows/LineaGasFeeFlow.js +111 -0
- package/dist/gas-flows/LineaGasFeeFlow.js.map +1 -0
- package/dist/helpers/EtherscanRemoteTransactionSource.d.ts.map +1 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.js +37 -14
- package/dist/helpers/EtherscanRemoteTransactionSource.js.map +1 -1
- package/dist/helpers/GasFeePoller.d.ts +32 -0
- package/dist/helpers/GasFeePoller.d.ts.map +1 -0
- package/dist/helpers/GasFeePoller.js +144 -0
- package/dist/helpers/GasFeePoller.js.map +1 -0
- package/dist/helpers/IncomingTransactionHelper.d.ts +17 -3
- package/dist/helpers/IncomingTransactionHelper.d.ts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.js +17 -14
- package/dist/helpers/IncomingTransactionHelper.js.map +1 -1
- package/dist/helpers/MultichainTrackingHelper.d.ts +72 -0
- package/dist/helpers/MultichainTrackingHelper.d.ts.map +1 -0
- package/dist/helpers/MultichainTrackingHelper.js +292 -0
- package/dist/helpers/MultichainTrackingHelper.js.map +1 -0
- package/dist/helpers/PendingTransactionTracker.d.ts +7 -7
- package/dist/helpers/PendingTransactionTracker.d.ts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.js +28 -28
- package/dist/helpers/PendingTransactionTracker.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +62 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +8 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/etherscan.d.ts +7 -0
- package/dist/utils/etherscan.d.ts.map +1 -1
- package/dist/utils/etherscan.js +17 -6
- package/dist/utils/etherscan.js.map +1 -1
- package/dist/utils/gas-fees.d.ts +14 -19
- package/dist/utils/gas-fees.d.ts.map +1 -1
- package/dist/utils/gas-fees.js +24 -35
- package/dist/utils/gas-fees.js.map +1 -1
- package/dist/utils/gas-flow.d.ts +32 -0
- package/dist/utils/gas-flow.d.ts.map +1 -0
- package/dist/utils/gas-flow.js +53 -0
- package/dist/utils/gas-flow.js.map +1 -0
- package/dist/utils/gas.d.ts +4 -3
- package/dist/utils/gas.d.ts.map +1 -1
- package/dist/utils/gas.js +8 -10
- package/dist/utils/gas.js.map +1 -1
- package/dist/utils/nonce.d.ts +3 -3
- package/dist/utils/nonce.d.ts.map +1 -1
- package/dist/utils/nonce.js +3 -3
- package/dist/utils/nonce.js.map +1 -1
- package/dist/utils/utils.d.ts.map +1 -1
- package/dist/utils/utils.js +15 -16
- package/dist/utils/utils.js.map +1 -1
- package/package.json +6 -3
package/dist/utils/gas.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gas.d.ts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"gas.d.ts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";AAQA,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAChD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAK3C,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAEnE,oBAAY,gBAAgB,GAAG;IAC7B,QAAQ,EAAE,QAAQ,CAAC;IACnB,eAAe,EAAE,OAAO,CAAC;IACzB,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,eAAe,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,GAAG,0BAA2C,CAAC;AAE5D,eAAO,MAAM,SAAS,WAAW,CAAC;AAClC,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAE1C,wBAAsB,SAAS,CAAC,OAAO,EAAE,gBAAgB,iBAkBxD;AAED,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,iBAAiB,EAC3B,QAAQ,EAAE,QAAQ;;;;;;;;;;;GAwCnB;AAED,wBAAgB,YAAY,CAC1B,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE,MAAM,iBAqBnB"}
|
package/dist/utils/gas.js
CHANGED
|
@@ -13,7 +13,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
13
13
|
exports.addGasBuffer = exports.estimateGas = exports.updateGas = exports.DEFAULT_GAS_MULTIPLIER = exports.FIXED_GAS = exports.log = void 0;
|
|
14
14
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
15
15
|
const utils_1 = require("@metamask/utils");
|
|
16
|
-
const ethereumjs_util_1 = require("ethereumjs-util");
|
|
17
16
|
const constants_1 = require("../constants");
|
|
18
17
|
const logger_1 = require("../logger");
|
|
19
18
|
exports.log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'gas');
|
|
@@ -42,7 +41,7 @@ function estimateGas(txParams, ethQuery) {
|
|
|
42
41
|
const { data, value } = request;
|
|
43
42
|
const { gasLimit: gasLimitHex, number: blockNumber } = yield getLatestBlock(ethQuery);
|
|
44
43
|
const gasLimitBN = (0, controller_utils_1.hexToBN)(gasLimitHex);
|
|
45
|
-
request.data = data ? (0,
|
|
44
|
+
request.data = data ? (0, utils_1.add0x)(data) : data;
|
|
46
45
|
request.gas = (0, controller_utils_1.BNToHex)((0, controller_utils_1.fractionBN)(gasLimitBN, 19, 20));
|
|
47
46
|
request.value = value || '0x0';
|
|
48
47
|
let estimatedGas = request.gas;
|
|
@@ -76,16 +75,16 @@ function addGasBuffer(estimatedGas, blockGasLimit, multiplier) {
|
|
|
76
75
|
const maxGasBN = (0, controller_utils_1.hexToBN)(blockGasLimit).muln(0.9);
|
|
77
76
|
const paddedGasBN = estimatedGasBN.muln(multiplier);
|
|
78
77
|
if (estimatedGasBN.gt(maxGasBN)) {
|
|
79
|
-
const estimatedGasHex = (0,
|
|
78
|
+
const estimatedGasHex = (0, utils_1.add0x)(estimatedGas);
|
|
80
79
|
(0, exports.log)('Using estimated value', estimatedGasHex);
|
|
81
80
|
return estimatedGasHex;
|
|
82
81
|
}
|
|
83
82
|
if (paddedGasBN.lt(maxGasBN)) {
|
|
84
|
-
const paddedHex = (0,
|
|
83
|
+
const paddedHex = (0, utils_1.add0x)((0, controller_utils_1.BNToHex)(paddedGasBN));
|
|
85
84
|
(0, exports.log)('Using padded estimate', paddedHex, multiplier);
|
|
86
85
|
return paddedHex;
|
|
87
86
|
}
|
|
88
|
-
const maxHex = (0,
|
|
87
|
+
const maxHex = (0, utils_1.add0x)((0, controller_utils_1.BNToHex)(maxGasBN));
|
|
89
88
|
(0, exports.log)('Using 90% of block gas limit', maxHex);
|
|
90
89
|
return maxHex;
|
|
91
90
|
}
|
|
@@ -93,7 +92,7 @@ exports.addGasBuffer = addGasBuffer;
|
|
|
93
92
|
function getGas(request) {
|
|
94
93
|
var _a;
|
|
95
94
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
-
const {
|
|
95
|
+
const { isCustomNetwork, chainId, txMeta } = request;
|
|
97
96
|
if (txMeta.txParams.gas) {
|
|
98
97
|
(0, exports.log)('Using value from request', txMeta.txParams.gas);
|
|
99
98
|
return [txMeta.txParams.gas];
|
|
@@ -103,18 +102,17 @@ function getGas(request) {
|
|
|
103
102
|
return [exports.FIXED_GAS];
|
|
104
103
|
}
|
|
105
104
|
const { blockGasLimit, estimatedGas, simulationFails } = yield estimateGas(txMeta.txParams, request.ethQuery);
|
|
106
|
-
if (
|
|
105
|
+
if (isCustomNetwork) {
|
|
107
106
|
(0, exports.log)('Using original estimate as custom network');
|
|
108
107
|
return [estimatedGas, simulationFails];
|
|
109
108
|
}
|
|
110
|
-
const bufferMultiplier = (_a = constants_1.GAS_BUFFER_CHAIN_OVERRIDES[
|
|
109
|
+
const bufferMultiplier = (_a = constants_1.GAS_BUFFER_CHAIN_OVERRIDES[chainId]) !== null && _a !== void 0 ? _a : exports.DEFAULT_GAS_MULTIPLIER;
|
|
111
110
|
const bufferedGas = addGasBuffer(estimatedGas, blockGasLimit, bufferMultiplier);
|
|
112
111
|
return [bufferedGas, simulationFails];
|
|
113
112
|
});
|
|
114
113
|
}
|
|
115
|
-
function requiresFixedGas({ ethQuery, txMeta,
|
|
114
|
+
function requiresFixedGas({ ethQuery, txMeta, isCustomNetwork, }) {
|
|
116
115
|
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
const isCustomNetwork = providerConfig.type === controller_utils_1.NetworkType.rpc;
|
|
118
116
|
const { txParams: { to, data }, } = txMeta;
|
|
119
117
|
if (isCustomNetwork || !to || data) {
|
|
120
118
|
return false;
|
package/dist/utils/gas.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";AAAA,wCAAwC;;;;;;;;;;;;AAExC,
|
|
1
|
+
{"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";AAAA,wCAAwC;;;;;;;;;;;;AAExC,iEAKoC;AAGpC,2CAA4D;AAE5D,4CAA0D;AAC1D,sCAA0C;AAU7B,QAAA,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,KAAK,CAAC,CAAC;AAE/C,QAAA,SAAS,GAAG,QAAQ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b,gBAAgB,CACjB,CAAC;QAEF,OAAO,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC;;CACvC;AAED,SAAe,gBAAgB,CAAC,EAC9B,QAAQ,EACR,MAAM,EACN,eAAe,GACE;;QACjB,MAAM,EACJ,QAAQ,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,GACvB,GAAG,MAAM,CAAC;QAEX,IAAI,eAAe,IAAI,CAAC,EAAE,IAAI,IAAI,EAAE;YAClC,OAAO,KAAK,CAAC;SACd;QAED,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAEzC,OAAO,CAAC,IAAI,IAAI,IAAI,KAAK,IAAI,CAAC;IAChC,CAAC;CAAA;AAED,SAAe,OAAO,CACpB,QAAkB,EAClB,OAAe;;QAEf,OAAO,MAAM,IAAA,wBAAK,EAAC,QAAQ,EAAE,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IACrD,CAAC;CAAA;AAED,SAAe,cAAc,CAC3B,QAAkB;;QAElB,OAAO,MAAM,IAAA,wBAAK,EAAC,QAAQ,EAAE,kBAAkB,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;IACtE,CAAC;CAAA","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"]}
|
package/dist/utils/nonce.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NonceLock, Transaction as NonceTrackerTransaction } from 'nonce-tracker';
|
|
2
2
|
import type { TransactionMeta, TransactionStatus } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* Determine the next nonce to be used for a transaction.
|
|
5
5
|
*
|
|
6
6
|
* @param txMeta - The transaction metadata.
|
|
7
|
-
* @param
|
|
7
|
+
* @param getNonceLock - An anonymous function that acquires the nonce lock for an address
|
|
8
8
|
* @returns The next hexadecimal nonce to be used for the given transaction, and optionally a function to release the nonce lock.
|
|
9
9
|
*/
|
|
10
|
-
export declare function getNextNonce(txMeta: TransactionMeta,
|
|
10
|
+
export declare function getNextNonce(txMeta: TransactionMeta, getNonceLock: (address: string) => Promise<NonceLock>): Promise<[string, (() => void) | undefined]>;
|
|
11
11
|
/**
|
|
12
12
|
* Filter and format transactions for the nonce tracker.
|
|
13
13
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nonce.d.ts","sourceRoot":"","sources":["../../src/utils/nonce.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,
|
|
1
|
+
{"version":3,"file":"nonce.d.ts","sourceRoot":"","sources":["../../src/utils/nonce.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,WAAW,IAAI,uBAAuB,EACvC,MAAM,eAAe,CAAC;AAGvB,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAInE;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,eAAe,EACvB,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,CAAC,GACpD,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,CAyB7C;AAED;;;;;;;;GAQG;AACH,wBAAgB,uCAAuC,CACrD,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,MAAM,EACnB,iBAAiB,EAAE,iBAAiB,EACpC,YAAY,EAAE,eAAe,EAAE,GAC9B,uBAAuB,EAAE,CA0B3B"}
|
package/dist/utils/nonce.js
CHANGED
|
@@ -17,10 +17,10 @@ const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'nonce');
|
|
|
17
17
|
* Determine the next nonce to be used for a transaction.
|
|
18
18
|
*
|
|
19
19
|
* @param txMeta - The transaction metadata.
|
|
20
|
-
* @param
|
|
20
|
+
* @param getNonceLock - An anonymous function that acquires the nonce lock for an address
|
|
21
21
|
* @returns The next hexadecimal nonce to be used for the given transaction, and optionally a function to release the nonce lock.
|
|
22
22
|
*/
|
|
23
|
-
function getNextNonce(txMeta,
|
|
23
|
+
function getNextNonce(txMeta, getNonceLock) {
|
|
24
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
25
|
const { customNonceValue, txParams: { from, nonce: existingNonce }, } = txMeta;
|
|
26
26
|
const customNonce = customNonceValue ? (0, controller_utils_1.toHex)(customNonceValue) : undefined;
|
|
@@ -32,7 +32,7 @@ function getNextNonce(txMeta, nonceTracker) {
|
|
|
32
32
|
log('Using existing nonce', existingNonce);
|
|
33
33
|
return [existingNonce, undefined];
|
|
34
34
|
}
|
|
35
|
-
const nonceLock = yield
|
|
35
|
+
const nonceLock = yield getNonceLock(from);
|
|
36
36
|
const nonce = (0, controller_utils_1.toHex)(nonceLock.nextNonce);
|
|
37
37
|
const releaseLock = nonceLock.releaseLock.bind(nonceLock);
|
|
38
38
|
log('Using nonce from nonce tracker', nonce, nonceLock.nonceDetails);
|
package/dist/utils/nonce.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nonce.js","sourceRoot":"","sources":["../../src/utils/nonce.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iEAAmD;AAMnD,sCAA8D;AAG9D,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,OAAO,CAAC,CAAC;AAEvD;;;;;;GAMG;AACH,SAAsB,YAAY,CAChC,MAAuB,EACvB,
|
|
1
|
+
{"version":3,"file":"nonce.js","sourceRoot":"","sources":["../../src/utils/nonce.ts"],"names":[],"mappings":";;;;;;;;;;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EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,CACvE,CAAC,UAAU;QACX,CAAC,eAAe;QAChB,OAAO,KAAK,cAAc;QAC1B,MAAM,KAAK,iBAAiB;QAC5B,IAAI,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CACnD;SACA,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;QACzD,4CAA4C;QAC5C,6DAA6D;QAC7D,kDAAkD;QAClD,0BAA0B;QAC1B,OAAO;YACL,MAAM;YACN,OAAO,EAAE,CAAC,EAAE,CAAC;YACb,QAAQ,EAAE;gBACR,IAAI,EAAE,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE;gBAChB,GAAG,EAAE,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,EAAE;gBACd,KAAK,EAAE,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE;gBAClB,KAAK,EAAE,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE;aACnB;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACP,CAAC;AA/BD,0FA+BC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type {\n NonceLock,\n Transaction as NonceTrackerTransaction,\n} from 'nonce-tracker';\n\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { TransactionMeta, TransactionStatus } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'nonce');\n\n/**\n * Determine the next nonce to be used for a transaction.\n *\n * @param txMeta - The transaction metadata.\n * @param getNonceLock - An anonymous function that acquires the nonce lock for an address\n * @returns The next hexadecimal nonce to be used for the given transaction, and optionally a function to release the nonce lock.\n */\nexport async function getNextNonce(\n txMeta: TransactionMeta,\n getNonceLock: (address: string) => Promise<NonceLock>,\n): Promise<[string, (() => void) | undefined]> {\n const {\n customNonceValue,\n txParams: { from, nonce: existingNonce },\n } = txMeta;\n\n const customNonce = customNonceValue ? toHex(customNonceValue) : undefined;\n\n if (customNonce) {\n log('Using custom nonce', customNonce);\n return [customNonce, undefined];\n }\n\n if (existingNonce) {\n log('Using existing nonce', existingNonce);\n return [existingNonce, undefined];\n }\n\n const nonceLock = await getNonceLock(from);\n const nonce = toHex(nonceLock.nextNonce);\n const releaseLock = nonceLock.releaseLock.bind(nonceLock);\n\n log('Using nonce from nonce tracker', nonce, nonceLock.nonceDetails);\n\n return [nonce, releaseLock];\n}\n\n/**\n * Filter and format transactions for the nonce tracker.\n *\n * @param currentChainId - Chain ID of the current network.\n * @param fromAddress - Address of the account from which the transactions to filter from are sent.\n * @param transactionStatus - Status of the transactions for which to filter.\n * @param transactions - Array of transactionMeta objects that have been prefiltered.\n * @returns Array of transactions formatted for the nonce tracker.\n */\nexport function getAndFormatTransactionsForNonceTracker(\n currentChainId: string,\n fromAddress: string,\n transactionStatus: TransactionStatus,\n transactions: TransactionMeta[],\n): NonceTrackerTransaction[] {\n return transactions\n .filter(\n ({ chainId, isTransfer, isUserOperation, status, txParams: { from } }) =>\n !isTransfer &&\n !isUserOperation &&\n chainId === currentChainId &&\n status === transactionStatus &&\n from.toLowerCase() === fromAddress.toLowerCase(),\n )\n .map(({ status, txParams: { from, gas, value, nonce } }) => {\n // the only value we care about is the nonce\n // but we need to return the other values to satisfy the type\n // TODO: refactor nonceTracker to not require this\n /* istanbul ignore next */\n return {\n status,\n history: [{}],\n txParams: {\n from: from ?? '',\n gas: gas ?? '',\n value: value ?? '',\n nonce: nonce ?? '',\n },\n };\n });\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,aAAa,EACb,sBAAsB,EACvB,MAAM,0BAA0B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exports.normalizeTxError = exports.validateIfTransactionUnapproved = exports.validateMinimumIncrease = exports.getIncreasedPriceFromExisting = exports.getIncreasedPriceHex = exports.isGasPriceValue = exports.isFeeMarketEIP1559Values = exports.validateGasValues = exports.isEIP1559Transaction = exports.normalizeTxParams = exports.ESTIMATE_GAS_ERROR = void 0;
|
|
4
4
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
5
5
|
const utils_1 = require("@metamask/utils");
|
|
6
|
-
const ethereumjs_util_1 = require("ethereumjs-util");
|
|
7
6
|
const types_1 = require("../types");
|
|
8
7
|
exports.ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';
|
|
9
8
|
// TODO: Replace `any` with type
|
|
10
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
10
|
const NORMALIZERS = {
|
|
12
|
-
data: (data) => (0,
|
|
13
|
-
from: (from) => (0,
|
|
14
|
-
gas: (gas) => (0,
|
|
15
|
-
gasLimit: (gas) => (0,
|
|
16
|
-
gasPrice: (gasPrice) => (0,
|
|
17
|
-
nonce: (nonce) => (0,
|
|
18
|
-
to: (to) => (0,
|
|
19
|
-
value: (value) => (0,
|
|
20
|
-
maxFeePerGas: (maxFeePerGas) => (0,
|
|
21
|
-
maxPriorityFeePerGas: (maxPriorityFeePerGas) => (0,
|
|
22
|
-
estimatedBaseFee: (maxPriorityFeePerGas) => (0,
|
|
23
|
-
type: (type) => (0,
|
|
11
|
+
data: (data) => (0, utils_1.add0x)(data),
|
|
12
|
+
from: (from) => (0, utils_1.add0x)(from).toLowerCase(),
|
|
13
|
+
gas: (gas) => (0, utils_1.add0x)(gas),
|
|
14
|
+
gasLimit: (gas) => (0, utils_1.add0x)(gas),
|
|
15
|
+
gasPrice: (gasPrice) => (0, utils_1.add0x)(gasPrice),
|
|
16
|
+
nonce: (nonce) => (0, utils_1.add0x)(nonce),
|
|
17
|
+
to: (to) => (0, utils_1.add0x)(to).toLowerCase(),
|
|
18
|
+
value: (value) => (0, utils_1.add0x)(value),
|
|
19
|
+
maxFeePerGas: (maxFeePerGas) => (0, utils_1.add0x)(maxFeePerGas),
|
|
20
|
+
maxPriorityFeePerGas: (maxPriorityFeePerGas) => (0, utils_1.add0x)(maxPriorityFeePerGas),
|
|
21
|
+
estimatedBaseFee: (maxPriorityFeePerGas) => (0, utils_1.add0x)(maxPriorityFeePerGas),
|
|
22
|
+
type: (type) => (0, utils_1.add0x)(type),
|
|
24
23
|
};
|
|
25
24
|
/**
|
|
26
25
|
* Normalizes properties on transaction params.
|
|
@@ -59,7 +58,7 @@ const validateGasValues = (gasValues) => {
|
|
|
59
58
|
// TODO: Replace `any` with type
|
|
60
59
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
60
|
const value = gasValues[key];
|
|
62
|
-
if (typeof value !== 'string' || !(0,
|
|
61
|
+
if (typeof value !== 'string' || !(0, utils_1.isStrictHexString)(value)) {
|
|
63
62
|
throw new TypeError(`expected hex string for ${key} but received: ${value}`);
|
|
64
63
|
}
|
|
65
64
|
});
|
|
@@ -70,7 +69,7 @@ const isFeeMarketEIP1559Values = (gasValues) => (gasValues === null || gasValues
|
|
|
70
69
|
exports.isFeeMarketEIP1559Values = isFeeMarketEIP1559Values;
|
|
71
70
|
const isGasPriceValue = (gasValues) => (gasValues === null || gasValues === void 0 ? void 0 : gasValues.gasPrice) !== undefined;
|
|
72
71
|
exports.isGasPriceValue = isGasPriceValue;
|
|
73
|
-
const getIncreasedPriceHex = (value, rate) => (0,
|
|
72
|
+
const getIncreasedPriceHex = (value, rate) => (0, utils_1.add0x)(`${parseInt(`${value * rate}`, 10).toString(16)}`);
|
|
74
73
|
exports.getIncreasedPriceHex = getIncreasedPriceHex;
|
|
75
74
|
const getIncreasedPriceFromExisting = (value, rate) => {
|
|
76
75
|
return (0, exports.getIncreasedPriceHex)((0, controller_utils_1.convertHexToDecimal)(value), rate);
|
|
@@ -133,7 +132,7 @@ exports.normalizeTxError = normalizeTxError;
|
|
|
133
132
|
function normalizeGasFeeValues(gasFeeValues) {
|
|
134
133
|
// TODO: Replace `any` with type
|
|
135
134
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
136
|
-
const normalize = (value) => typeof value === 'string' ? (0,
|
|
135
|
+
const normalize = (value) => typeof value === 'string' ? (0, utils_1.add0x)(value) : value;
|
|
137
136
|
if ('gasPrice' in gasFeeValues) {
|
|
138
137
|
return {
|
|
139
138
|
gasPrice: normalize(gasFeeValues.gasPrice),
|
package/dist/utils/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;AAAA,iEAAiE;AACjE,2CAAwD;AACxD,qDAA4D;AAM5D,oCAA6C;AAOhC,QAAA,kBAAkB,GAAG,kCAAkC,CAAC;AAErE,gCAAgC;AAChC,8DAA8D;AAC9D,MAAM,WAAW,GAAgD;IAC/D,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,IAAI,CAAC;IAC1C,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,IAAI,CAAC,CAAC,WAAW,EAAE;IACxD,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,GAAG,CAAC;IACvC,QAAQ,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,GAAG,CAAC;IAC5C,QAAQ,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,QAAQ,CAAC;IACtD,KAAK,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,KAAK,CAAC;IAC7C,EAAE,EAAE,CAAC,EAAU,EAAE,EAAE,CAAC,IAAA,8BAAY,EAAC,EAAE,CAAC,CAAC,WAAW,EAAE;IAClD,KAAK,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAA,8BAAY,E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AY,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAE1D,IAAI,UAAU,IAAI,YAAY,EAAE;QAC9B,OAAO;YACL,QAAQ,EAAE,SAAS,CAAC,YAAY,CAAC,QAAQ,CAAC;SAC3C,CAAC;KACH;IAED,OAAO;QACL,YAAY,EAAE,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC;QAClD,oBAAoB,EAAE,SAAS,CAAC,YAAY,CAAC,oBAAoB,CAAC;KACnE,CAAC;AACJ,CAAC;AAlBD,sDAkBC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { getKnownPropertyNames } from '@metamask/utils';\nimport { addHexPrefix, isHexString } from 'ethereumjs-util';\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) => addHexPrefix(data),\n from: (from: string) => addHexPrefix(from).toLowerCase(),\n gas: (gas: string) => addHexPrefix(gas),\n gasLimit: (gas: string) => addHexPrefix(gas),\n gasPrice: (gasPrice: string) => addHexPrefix(gasPrice),\n nonce: (nonce: string) => addHexPrefix(nonce),\n to: (to: string) => addHexPrefix(to).toLowerCase(),\n value: (value: string) => addHexPrefix(value),\n maxFeePerGas: (maxFeePerGas: string) => addHexPrefix(maxFeePerGas),\n maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>\n addHexPrefix(maxPriorityFeePerGas),\n estimatedBaseFee: (maxPriorityFeePerGas: string) =>\n addHexPrefix(maxPriorityFeePerGas),\n type: (type: string) => addHexPrefix(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 normalizeTxParams(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' || !isHexString(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 addHexPrefix(`${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: error?.value,\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' ? addHexPrefix(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"]}
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;AAAA,iEAAiE;AACjE,2CAIyB;AAMzB,oCAA6C;AAOhC,QAAA,kBAAkB,GAAG,kCAAkC,CAAC;AAErE,gCAAgC;AAChC,8DAA8D;AAC9D,MAAM,WAAW,GAAgD;IAC/D,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,IAAI,CAAC;IACnC,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,IAAI,CAAC,CAAC,WAAW,EAAE;IACjD,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,GAAG,CAAC;IAChC,QAAQ,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,GAAG,CAAC;IACrC,QAAQ,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,QAAQ,CAAC;IAC/C,KAAK,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,KAAK,CAAC;IACtC,EAAE,EAAE,CAAC,EAAU,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,EAAE,CAAC,CAAC,WAAW,EAAE;IAC3C,KAAK,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,KAAK,CAAC;IACtC,YAAY,EAAE,CAAC,YAAoB,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,YAAY,CAAC;IAC3D,oBAAoB,EAAE,CAAC,oBAA4B,EAAE,EAAE,CACrD,IAAA,aAAK,EAAC,oBAAoB,CAAC;IAC7B,gBAAgB,EAAE,CAAC,oBAA4B,EAAE,EAAE,CACjD,IAAA,aAAK,EAAC,oBAAoB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S,CAAC,YAAY,CAAC,oBAAoB,CAAC;KACnE,CAAC;AACJ,CAAC;AAlBD,sDAkBC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport {\n add0x,\n getKnownPropertyNames,\n isStrictHexString,\n} 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(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 normalizeTxParams(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: error?.value,\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/transaction-controller",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "23.1.0",
|
|
4
4
|
"description": "Stores transactions alongside their periodically updated statuses and manages interactions such as approval and cancellation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@ethereumjs/common": "^3.2.0",
|
|
35
35
|
"@ethereumjs/tx": "^4.2.0",
|
|
36
|
+
"@ethereumjs/util": "^8.1.0",
|
|
36
37
|
"@ethersproject/abi": "^5.7.0",
|
|
37
38
|
"@metamask/approval-controller": "^5.1.2",
|
|
38
39
|
"@metamask/base-controller": "^4.1.1",
|
|
@@ -41,11 +42,11 @@
|
|
|
41
42
|
"@metamask/gas-fee-controller": "^13.0.1",
|
|
42
43
|
"@metamask/metamask-eth-abis": "^3.0.0",
|
|
43
44
|
"@metamask/network-controller": "^17.2.0",
|
|
44
|
-
"@metamask/rpc-errors": "^6.1
|
|
45
|
+
"@metamask/rpc-errors": "^6.2.1",
|
|
45
46
|
"@metamask/utils": "^8.3.0",
|
|
46
47
|
"async-mutex": "^0.2.6",
|
|
48
|
+
"bn.js": "^5.2.1",
|
|
47
49
|
"eth-method-registry": "^4.0.0",
|
|
48
|
-
"ethereumjs-util": "^7.0.10",
|
|
49
50
|
"fast-json-patch": "^3.1.1",
|
|
50
51
|
"lodash": "^4.17.21",
|
|
51
52
|
"nonce-tracker": "^3.0.0",
|
|
@@ -55,10 +56,12 @@
|
|
|
55
56
|
"@babel/runtime": "^7.23.9",
|
|
56
57
|
"@metamask/auto-changelog": "^3.4.4",
|
|
57
58
|
"@metamask/ethjs-provider-http": "^0.3.0",
|
|
59
|
+
"@types/bn.js": "^5.1.5",
|
|
58
60
|
"@types/jest": "^27.4.1",
|
|
59
61
|
"@types/node": "^16.18.54",
|
|
60
62
|
"deepmerge": "^4.2.2",
|
|
61
63
|
"jest": "^27.5.1",
|
|
64
|
+
"nock": "^13.3.1",
|
|
62
65
|
"sinon": "^9.2.4",
|
|
63
66
|
"ts-jest": "^27.1.4",
|
|
64
67
|
"typedoc": "^0.24.8",
|