-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
DefaultGasFeeFlow.ts
136 lines (120 loc) · 3.88 KB
/
DefaultGasFeeFlow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type {
LegacyGasPriceEstimate,
GasFeeEstimates as FeeMarketGasPriceEstimate,
EthGasPriceEstimate,
} from '@metamask/gas-fee-controller';
import { GAS_ESTIMATE_TYPES } from '@metamask/gas-fee-controller';
import type { Hex } from '@metamask/utils';
import { createModuleLogger } from '@metamask/utils';
import { projectLogger } from '../logger';
import type {
FeeMarketGasFeeEstimateForLevel,
FeeMarketGasFeeEstimates,
GasFeeEstimates,
GasFeeFlow,
GasFeeFlowRequest,
GasFeeFlowResponse,
GasPriceGasFeeEstimates,
LegacyGasFeeEstimates,
TransactionMeta,
} from '../types';
import { GasFeeEstimateLevel, GasFeeEstimateType } from '../types';
import { gweiDecimalToWeiHex } from '../utils/gas-fees';
const log = createModuleLogger(projectLogger, 'default-gas-fee-flow');
/**
* The standard implementation of a gas fee flow that obtains gas fee estimates using only the GasFeeController.
*/
export class DefaultGasFeeFlow implements GasFeeFlow {
matchesTransaction(_transactionMeta: TransactionMeta): boolean {
return true;
}
async getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse> {
const { gasFeeControllerData } = request;
const { gasEstimateType, gasFeeEstimates } = gasFeeControllerData;
let response: GasFeeEstimates;
switch (gasEstimateType) {
case GAS_ESTIMATE_TYPES.FEE_MARKET:
log('Using fee market estimates', gasFeeEstimates);
response = this.#getFeeMarkEstimates(gasFeeEstimates);
break;
case GAS_ESTIMATE_TYPES.LEGACY:
log('Using legacy estimates', gasFeeEstimates);
response = this.#getLegacyEstimates(
gasFeeEstimates as LegacyGasPriceEstimate,
);
break;
case GAS_ESTIMATE_TYPES.ETH_GASPRICE:
log('Using eth_gasPrice estimates', gasFeeEstimates);
response = this.#getGasPriceEstimates(
gasFeeEstimates as EthGasPriceEstimate,
);
break;
default:
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Unsupported gas estimate type: ${gasEstimateType}`);
}
return {
estimates: response,
};
}
#getFeeMarkEstimates(
gasFeeEstimates: FeeMarketGasPriceEstimate,
): FeeMarketGasFeeEstimates {
const levels = Object.values(GasFeeEstimateLevel).reduce(
(result, level) => ({
...result,
[level]: this.#getFeeMarketLevel(gasFeeEstimates, level),
}),
{} as Omit<FeeMarketGasFeeEstimates, 'type'>,
);
return {
type: GasFeeEstimateType.FeeMarket,
...levels,
};
}
#getLegacyEstimates(
gasFeeEstimates: LegacyGasPriceEstimate,
): LegacyGasFeeEstimates {
const levels = Object.values(GasFeeEstimateLevel).reduce(
(result, level) => ({
...result,
[level]: this.#getLegacyLevel(gasFeeEstimates, level),
}),
{} as Omit<LegacyGasFeeEstimates, 'type'>,
);
return {
type: GasFeeEstimateType.Legacy,
...levels,
};
}
#getGasPriceEstimates(
gasFeeEstimates: EthGasPriceEstimate,
): GasPriceGasFeeEstimates {
return {
type: GasFeeEstimateType.GasPrice,
gasPrice: gweiDecimalToWeiHex(gasFeeEstimates.gasPrice),
};
}
#getFeeMarketLevel(
gasFeeEstimates: FeeMarketGasPriceEstimate,
level: GasFeeEstimateLevel,
): FeeMarketGasFeeEstimateForLevel {
const maxFeePerGas = gweiDecimalToWeiHex(
gasFeeEstimates[level].suggestedMaxFeePerGas,
);
const maxPriorityFeePerGas = gweiDecimalToWeiHex(
gasFeeEstimates[level].suggestedMaxPriorityFeePerGas,
);
return {
maxFeePerGas,
maxPriorityFeePerGas,
};
}
#getLegacyLevel(
gasFeeEstimates: LegacyGasPriceEstimate,
level: GasFeeEstimateLevel,
): Hex {
return gweiDecimalToWeiHex(gasFeeEstimates[level]);
}
}