Skip to content

Commit

Permalink
complete local api
Browse files Browse the repository at this point in the history
  • Loading branch information
rickycodes committed Jun 30, 2021
1 parent 43f724c commit 0029514
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 185 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"build:watch": "yarn build --watch",
"build:link": "yarn build && cd dist && yarn link && rm -rf node_modules && cd ..",
"doc": "typedoc && touch docs/.nojekyll",
"start-eip1559-server": "node ./src/apis/eip-1559-mock.js"
"test:eip1559-server": "node ./src/apis/eip-1559-mock.js"
},
"homepage": "https://github.com/MetaMask/controllers#readme",
"repository": {
Expand Down
172 changes: 130 additions & 42 deletions src/apis/eip-1559-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,151 @@ const http = require('http');

// eslint-disable-next-line
const { PORT, HOSTNAME } = process.env;

const hostname = HOSTNAME || '127.0.0.1';
const port = PORT || 3000;

const range = (min, max) => Math.random() * (max - min) + min;
const ms = 1000;

const end = (res, json) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
console.log({ json });
res.end(json);
};

// base Eip1559GasFee
const Eip1559GasFee = {
minWaitTimeEstimate: Number(1),
maxWaitTimeEstimate: Number(1),
suggestedMaxPriorityFeePerGas: Number(1),
suggestedMaxFeePerGas: Number(1),
calculatedTotalMinFee: Number(1),
};

const get_payload = () => {
const low = Math.floor(range(1, 5));
const medium = low * 2;
const high = medium * 2;

// minWaitTimeEstimate
const minWaitTimeEstimate_low = low * ms;
const minWaitTimeEstimate_medium = medium * ms;
const minWaitTimeEstimate_high = high * ms;

// maxWaitTimeEstimate
const maxWaitTimeEstimate_low = low * (ms * 2);
const maxWaitTimeEstimate_medium = medium * (ms * 2);
const maxWaitTimeEstimate_high = high * (ms * 2);

return {
const mockEIP1559ApiResponses = [
{
low: {
minWaitTimeEstimate: 120000,
maxWaitTimeEstimate: 300000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '35',
},
medium: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 30000,
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '40',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '60',
},
estimatedBaseFee: '30',
},
{
low: {
minWaitTimeEstimate: 180000,
maxWaitTimeEstimate: 360000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '40',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '45',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '65',
},
estimatedBaseFee: '32',
},
{
low: {
minWaitTimeEstimate: 60000,
maxWaitTimeEstimate: 240000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '42',
},
medium: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 30000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '47',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '4',
suggestedMaxFeePerGas: '67',
},
estimatedBaseFee: '35',
},
{
low: {
minWaitTimeEstimate: 180000,
maxWaitTimeEstimate: 300000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '53',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '7',
suggestedMaxFeePerGas: '70',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '10',
suggestedMaxFeePerGas: '100',
},
estimatedBaseFee: '50',
},
{
low: {
...Eip1559GasFee,
suggestedMaxFeePerGas: low,
minWaitTimeEstimate: minWaitTimeEstimate_low,
maxWaitTimeEstimate: maxWaitTimeEstimate_low,
minWaitTimeEstimate: 120000,
maxWaitTimeEstimate: 360000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '35',
},
medium: {
...Eip1559GasFee,
suggestedMaxFeePerGas: medium,
minWaitTimeEstimate: minWaitTimeEstimate_medium,
maxWaitTimeEstimate: maxWaitTimeEstimate_medium,
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '40',
},
high: {
...Eip1559GasFee,
suggestedMaxFeePerGas: high,
minWaitTimeEstimate: minWaitTimeEstimate_high,
maxWaitTimeEstimate: maxWaitTimeEstimate_high,
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '4',
suggestedMaxFeePerGas: '60',
},
};
estimatedBaseFee: '30',
},
{
low: {
minWaitTimeEstimate: 60000,
maxWaitTimeEstimate: 600000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '35',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '1.8',
suggestedMaxFeePerGas: '38',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '50',
},
estimatedBaseFee: '28',
},
];

const getMockApiResponse = () => {
return mockEIP1559ApiResponses[Math.floor(Math.random() * 6)];
};

const get_payload = () => {
return getMockApiResponse();
};

const server = http.createServer((_, res) => {
Expand Down
146 changes: 4 additions & 142 deletions src/gas/gas-util.ts
Original file line number Diff line number Diff line change
@@ -1,154 +1,16 @@
import { BN } from 'ethereumjs-util';
import { query } from '../util';
import { query, handleFetch } from '../util';
import {
GasFeeEstimates,
LegacyGasPriceEstimate,
EstimatedGasFeeTimeBounds,
unknownString,
} from './GasFeeController';

// import { handleFetch } from '../util';
const GAS_FEE_API = 'http://127.0.0.1:3000';

// const GAS_FEE_API = 'https://gas-fee-api-goes-here';

const mockEIP1559ApiResponses = [
{
low: {
minWaitTimeEstimate: 120000,
maxWaitTimeEstimate: 300000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '35',
},
medium: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 30000,
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '40',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '60',
},
estimatedBaseFee: '30',
},
{
low: {
minWaitTimeEstimate: 180000,
maxWaitTimeEstimate: 360000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '40',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '45',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '65',
},
estimatedBaseFee: '32',
},
{
low: {
minWaitTimeEstimate: 60000,
maxWaitTimeEstimate: 240000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '42',
},
medium: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 30000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '47',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '4',
suggestedMaxFeePerGas: '67',
},
estimatedBaseFee: '35',
},
{
low: {
minWaitTimeEstimate: 180000,
maxWaitTimeEstimate: 300000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '53',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '7',
suggestedMaxFeePerGas: '70',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '10',
suggestedMaxFeePerGas: '100',
},
estimatedBaseFee: '50',
},
{
low: {
minWaitTimeEstimate: 120000,
maxWaitTimeEstimate: 360000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '35',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '3',
suggestedMaxFeePerGas: '40',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '4',
suggestedMaxFeePerGas: '60',
},
estimatedBaseFee: '30',
},
{
low: {
minWaitTimeEstimate: 60000,
maxWaitTimeEstimate: 600000,
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '35',
},
medium: {
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
suggestedMaxPriorityFeePerGas: '1.8',
suggestedMaxFeePerGas: '38',
},
high: {
minWaitTimeEstimate: 0,
maxWaitTimeEstimate: 150000,
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '50',
},
estimatedBaseFee: '28',
},
];

const getMockApiResponse = (): GasFeeEstimates => {
return mockEIP1559ApiResponses[Math.floor(Math.random() * 6)];
};

export function fetchGasEstimates(): Promise<GasFeeEstimates> {
// return handleFetch(GAS_FEE_API)
return new Promise((resolve) => {
resolve(getMockApiResponse());
});
export async function fetchGasEstimates(): Promise<GasFeeEstimates> {
return await handleFetch(GAS_FEE_API);
}

export async function fetchLegacyGasPriceEstimate(
Expand Down

0 comments on commit 0029514

Please sign in to comment.