Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a71c93e
First draft of gas fee controller
danjm Jun 14, 2021
c4a2606
Second draft of gas fee controller
danjm Jun 16, 2021
7886180
Third draft of gas fee controller
danjm Jun 17, 2021
2be94cb
Delete use of legacy gas fee
danjm Jun 18, 2021
ba8e275
Track whether polling should be instantiated or stopped with tokens
danjm Jun 18, 2021
179dee6
Network controller getEIP1559Compatibility can be called whenever a n…
danjm Jun 18, 2021
55d3607
update tests (#495)
rickycodes Jun 18, 2021
16075b0
Fetch estimate using eth_gasPrice if on a custom network
danjm Jun 21, 2021
b70d323
getGasFeeEstimatesAndStartPolling returns poll token, and a new one i…
danjm Jun 21, 2021
0a8a248
Add getTimeEstimate to GasFee controller
danjm Jun 21, 2021
cb88478
export GasFeeController
rickycodes Jun 21, 2021
1ccd62a
Add public method for calling and returning gas estimates without pol…
danjm Jun 23, 2021
dbf9f6c
Fix return of fetchLegacyGasPriceEstimate
danjm Jun 23, 2021
16b313b
Proper error handling and fallback for _fetchGasFeeEstimateData
danjm Jun 23, 2021
a782c9d
Include estimated time bounds in gas fee state
danjm Jun 23, 2021
32b2930
rename
rickycodes Jun 23, 2021
97e9e8f
Add GasFeeController.test.ts
rickycodes Jun 23, 2021
47f3659
remove TODO
rickycodes Jun 24, 2021
9971e68
Add result token length test
rickycodes Jun 24, 2021
91a2cd2
Add estimates property test
rickycodes Jun 24, 2021
0dec6e7
Add should fail to re-initialize test
rickycodes Jun 24, 2021
e4e962e
remove console.log
rickycodes Jun 24, 2021
afdc385
do not modify state directly
rickycodes Jun 24, 2021
fd4bd64
Use before/afterEach and fix messenger
rickycodes Jun 24, 2021
b1c5631
check gasFeeEstimates properties
rickycodes Jun 24, 2021
3646d62
check that state is empty to start
rickycodes Jun 24, 2021
51dad0d
Add one additional property check
rickycodes Jun 24, 2021
4ac4ffe
Adding TokenListController to fetch the token list from token service…
NiranjanaBinoy Jun 28, 2021
c1ca513
address feedback
rickycodes Jun 30, 2021
0458679
add mock server for eip1559
rickycodes Jun 15, 2021
728c039
get tests working again
rickycodes Jun 30, 2021
cd90799
Use heroku endpoint
rickycodes Jun 30, 2021
8d570bb
Handle fetch correctly in gasfeecontroller unit tests, using nock
danjm Jun 30, 2021
2e2b1a7
gasFee controller calculateTimeEstimate handles decimals, by way of u…
danjm Jun 30, 2021
1516d87
Lint fix
danjm Jun 30, 2021
6eac11c
Fix dec to gwi (#504)
brad-decker Jul 1, 2021
3df6baf
use ethjs-unit for unit conversions (#506)
brad-decker Jul 1, 2021
117d898
Add metaswaps API and normalize all gas fee units to dec gwei (#507)
brad-decker Jul 1, 2021
b4eebac
update types to be more identifiable (#508)
brad-decker Jul 2, 2021
6fbf024
use LegacyGasFeeEstimate type
brad-decker Jul 2, 2021
5d2908e
handle hex prefixed
brad-decker Jul 5, 2021
283fc88
baseFee -> baseFeePerGas
brad-decker Jul 5, 2021
cf82e70
make more configurable (#513)
brad-decker Jul 6, 2021
8d04cc5
use optional chaining
brad-decker Jul 6, 2021
41cdace
update test case wording
brad-decker Jul 7, 2021
3a42d98
use testdata instead of programmatic testing
brad-decker Jul 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add getTimeEstimate to GasFee controller
  • Loading branch information
danjm authored and brad-decker committed Jul 7, 2021
commit 0a8a2484795a92f35b257704c45e15b45c9d9d3a
47 changes: 47 additions & 0 deletions src/gas/GasFee.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ import type {
import {
fetchGasEstimates as defaultFetchGasEstimates,
fetchLegacyGasPriceEstimate as defaultFetchLegacyGasPriceEstimate,
calculateTimeEstimate,
} from './gas-util';

export type unknownString = 'unknown';

export interface EstimatedGasFeeTimeBounds {
lowerTimeBound: number | null;
upperTimeBound: number | unknownString;
}

/**
* @type LegacyGasPriceEstimate
*
Expand Down Expand Up @@ -44,6 +52,16 @@ interface Eip1559GasFee {
suggestedMaxFeePerGas: string; // a GWEI hex number
}

function isEIP1559GasFeee(object: any): object is Eip1559GasFee {
return (
'minWaitTimeEstimate' in object &&
'maxWaitTimeEstimate' in object &&
'suggestedMaxPriorityFeePerGas' in object &&
'suggestedMaxFeePerGas' in object &&
Object.keys(object).length === 4
);
}

/**
* @type GasFeeEstimates
*
Expand All @@ -62,6 +80,18 @@ export interface GasFeeEstimates {
estimatedBaseFee: string;
}

function isEIP1559Estimate(object: any): object is GasFeeEstimates {
return (
'low' in object &&
isEIP1559GasFeee(object.low) &&
'medium' in object &&
isEIP1559GasFeee(object.medium) &&
'high' in object &&
isEIP1559GasFeee(object.high) &&
'estimatedBaseFee' in object
);
}

const metadata = {
gasFeeEstimates: { persist: true, anonymous: false },
};
Expand Down Expand Up @@ -276,6 +306,23 @@ export class GasFeeController extends BaseController<typeof name, GasFeeState> {
currentNetworkIsEIP1559Compatible && currentAccountIsEIP1559Compatible
);
}

getTimeEstimate(
maxPriorityFeePerGas: string,
maxFeePerGas: string,
): EstimatedGasFeeTimeBounds | undefined {
if (
!this.state.gasFeeEstimates ||
!isEIP1559Estimate(this.state.gasFeeEstimates)
) {
return undefined;
}
return calculateTimeEstimate(
maxPriorityFeePerGas,
maxFeePerGas,
this.state.gasFeeEstimates,
);
}
}

export default GasFeeController;
71 changes: 70 additions & 1 deletion src/gas/gas-util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { BN } from 'ethereumjs-util';
import { query } from '../util';
import { GasFeeEstimates, LegacyGasPriceEstimate } from './GasFee.controller';
import {
GasFeeEstimates,
LegacyGasPriceEstimate,
EstimatedGasFeeTimeBounds,
unknownString,
} from './GasFee.controller';

// import { handleFetch } from '../util';

Expand Down Expand Up @@ -150,3 +156,66 @@ export async function fetchLegacyGasPriceEstimate(
): Promise<LegacyGasPriceEstimate> {
return await query(ethQuery, 'gasPrice');
}

function gweiHexToWEIBN(n: any) {
const BN_1000 = new BN(1000, 10);
return new BN(n, 16).mul(BN_1000);
}

export function calculateTimeEstimate(
maxPriorityFeePerGas: string,
maxFeePerGas: string,
gasFeeEstimates: GasFeeEstimates,
): EstimatedGasFeeTimeBounds {
const { low, medium, high, estimatedBaseFee } = gasFeeEstimates;

const maxPriorityFeePerGasInWEI = gweiHexToWEIBN(maxPriorityFeePerGas);
const maxFeePerGasInWEI = gweiHexToWEIBN(maxFeePerGas);
const estimatedBaseFeeInWEI = gweiHexToWEIBN(estimatedBaseFee);

const effectiveMaxPriorityFee = BN.min(
maxPriorityFeePerGasInWEI,
maxFeePerGasInWEI.sub(estimatedBaseFeeInWEI),
);

const lowMaxPriorityFeeInWEI = gweiHexToWEIBN(
low.suggestedMaxPriorityFeePerGas,
);
const mediumMaxPriorityFeeInWEI = gweiHexToWEIBN(
medium.suggestedMaxPriorityFeePerGas,
);
const highMaxPriorityFeeInWEI = gweiHexToWEIBN(
high.suggestedMaxPriorityFeePerGas,
);

let lowerTimeBound;
let upperTimeBound;

if (effectiveMaxPriorityFee.lt(lowMaxPriorityFeeInWEI)) {
lowerTimeBound = null;
upperTimeBound = 'unknown' as unknownString;
} else if (
effectiveMaxPriorityFee.gte(lowMaxPriorityFeeInWEI) &&
effectiveMaxPriorityFee.lt(mediumMaxPriorityFeeInWEI)
) {
lowerTimeBound = low.minWaitTimeEstimate;
upperTimeBound = low.maxWaitTimeEstimate;
} else if (
effectiveMaxPriorityFee.gte(mediumMaxPriorityFeeInWEI) &&
effectiveMaxPriorityFee.lt(highMaxPriorityFeeInWEI)
) {
lowerTimeBound = medium.minWaitTimeEstimate;
upperTimeBound = medium.maxWaitTimeEstimate;
} else if (effectiveMaxPriorityFee.eq(highMaxPriorityFeeInWEI)) {
lowerTimeBound = high.minWaitTimeEstimate;
upperTimeBound = high.maxWaitTimeEstimate;
} else {
lowerTimeBound = 0;
upperTimeBound = high.maxWaitTimeEstimate;
}

return {
lowerTimeBound,
upperTimeBound,
};
}