UNPKG

@rainbow-me/fee-suggestions

Version:

JavaScript library that suggest fees on Ethereum after EIP-1559 using historical data using ethers.js

194 lines (193 loc) 12.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.suggestFees = exports.suggestMaxPriorityFee = exports.suggestMaxBaseFee = void 0; var ethers_1 = require("ethers"); var moving_averages_1 = require("moving-averages"); var constants_1 = require("./constants"); var utils_1 = require("./utils"); var suggestMaxBaseFee = function (provider, fromBlock, blockCountHistory) { if (fromBlock === void 0) { fromBlock = 'latest'; } if (blockCountHistory === void 0) { blockCountHistory = 100; } return __awaiter(void 0, void 0, void 0, function () { var feeHistory, currentBaseFee, baseFees, order, i, baseFeeTrend, i, result, maxBaseFee, timeFactor, bf, baseFeeSuggestion, blocksToConfirmationByBaseFee; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, provider.send('eth_feeHistory', [ ethers_1.utils.hexStripZeros(ethers_1.utils.hexlify(blockCountHistory)), fromBlock, [], ])]; case 1: feeHistory = _a.sent(); currentBaseFee = (0, utils_1.weiToString)(feeHistory === null || feeHistory === void 0 ? void 0 : feeHistory.baseFeePerGas[(feeHistory === null || feeHistory === void 0 ? void 0 : feeHistory.baseFeePerGas.length) - 1]); baseFees = []; order = []; for (i = 0; i < feeHistory.baseFeePerGas.length; i++) { baseFees.push((0, utils_1.weiToGweiNumber)(feeHistory.baseFeePerGas[i])); order.push(i); } baseFeeTrend = (0, utils_1.calculateBaseFeeTrend)(baseFees, currentBaseFee); baseFees[baseFees.length - 1] *= 9 / 8; for (i = feeHistory.gasUsedRatio.length - 1; i >= 0; i--) { if (feeHistory.gasUsedRatio[i] > 0.9) { baseFees[i] = baseFees[i + 1]; } } order.sort(function (a, b) { var aa = baseFees[a]; var bb = baseFees[b]; if (aa < bb) { return -1; } if (aa > bb) { return 1; } return 0; }); result = []; maxBaseFee = 0; for (timeFactor = 15; timeFactor >= 0; timeFactor--) { bf = (0, utils_1.suggestBaseFee)(baseFees, order, timeFactor, 0.1, 0.3); if (bf > maxBaseFee) { maxBaseFee = bf; } else { bf = maxBaseFee; } result[timeFactor] = bf; } baseFeeSuggestion = (0, utils_1.gweiToWei)((0, utils_1.multiply)(Math.max.apply(Math, result), constants_1.BASE_FEE_ADDITIONAL_PADDING)); blocksToConfirmationByBaseFee = { 120: (0, utils_1.multiply)(baseFeeSuggestion, constants_1.BASE_FEE_BLOCKS_TO_CONFIRMATION_MULTIPLIERS[120]).toFixed(0), 240: (0, utils_1.multiply)(baseFeeSuggestion, constants_1.BASE_FEE_BLOCKS_TO_CONFIRMATION_MULTIPLIERS[240]).toFixed(0), 4: (0, utils_1.multiply)(baseFeeSuggestion, constants_1.BASE_FEE_BLOCKS_TO_CONFIRMATION_MULTIPLIERS[4]).toFixed(0), 40: (0, utils_1.multiply)(baseFeeSuggestion, constants_1.BASE_FEE_BLOCKS_TO_CONFIRMATION_MULTIPLIERS[40]).toFixed(0), 8: (0, utils_1.multiply)(baseFeeSuggestion, constants_1.BASE_FEE_BLOCKS_TO_CONFIRMATION_MULTIPLIERS[8]).toFixed(0), }; return [2 /*return*/, { baseFeeSuggestion: baseFeeSuggestion, baseFeeTrend: baseFeeTrend, blocksToConfirmationByBaseFee: blocksToConfirmationByBaseFee, currentBaseFee: currentBaseFee, }]; } }); }); }; exports.suggestMaxBaseFee = suggestMaxBaseFee; var suggestMaxPriorityFee = function (provider, fromBlock) { if (fromBlock === void 0) { fromBlock = 'latest'; } return __awaiter(void 0, void 0, void 0, function () { var feeHistory, blocksRewards, outlierBlocks, blocksRewardsPerc10, blocksRewardsPerc15, blocksRewardsPerc30, blocksRewardsPerc45, emaPerc10, emaPerc15, emaPerc30, emaPerc45, boundedNormalPriorityFee, boundedFastMaxPriorityFee, boundedUrgentPriorityFee; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, provider.send('eth_feeHistory', [ ethers_1.utils.hexStripZeros(ethers_1.utils.hexlify(10)), fromBlock, [10, 15, 30, 45], ])]; case 1: feeHistory = _a.sent(); blocksRewards = feeHistory.reward; if (!blocksRewards.length) throw new Error('Error: block reward was empty'); outlierBlocks = (0, utils_1.getOutlierBlocksToRemove)(blocksRewards, 0); blocksRewardsPerc10 = (0, utils_1.rewardsFilterOutliers)(blocksRewards, outlierBlocks, 0); blocksRewardsPerc15 = (0, utils_1.rewardsFilterOutliers)(blocksRewards, outlierBlocks, 1); blocksRewardsPerc30 = (0, utils_1.rewardsFilterOutliers)(blocksRewards, outlierBlocks, 2); blocksRewardsPerc45 = (0, utils_1.rewardsFilterOutliers)(blocksRewards, outlierBlocks, 3); emaPerc10 = (0, moving_averages_1.ema)(blocksRewardsPerc10, blocksRewardsPerc10.length)[blocksRewardsPerc10.length - 1]; emaPerc15 = (0, moving_averages_1.ema)(blocksRewardsPerc15, blocksRewardsPerc15.length)[blocksRewardsPerc15.length - 1]; emaPerc30 = (0, moving_averages_1.ema)(blocksRewardsPerc30, blocksRewardsPerc30.length)[blocksRewardsPerc30.length - 1]; emaPerc45 = (0, moving_averages_1.ema)(blocksRewardsPerc45, blocksRewardsPerc45.length)[blocksRewardsPerc45.length - 1]; if (emaPerc10 === undefined || emaPerc15 === undefined || emaPerc30 === undefined || emaPerc45 === undefined) throw new Error('Error: ema was undefined'); boundedNormalPriorityFee = Math.min(Math.max(emaPerc15, 1), 1.8); boundedFastMaxPriorityFee = Math.min(Math.max(emaPerc30, 1.5), 3); boundedUrgentPriorityFee = Math.min(Math.max(emaPerc45, 2), 9); return [2 /*return*/, { blocksToConfirmationByPriorityFee: { 1: (0, utils_1.gweiToWei)(emaPerc45), 2: (0, utils_1.gweiToWei)(emaPerc30), 3: (0, utils_1.gweiToWei)(emaPerc15), 4: (0, utils_1.gweiToWei)(emaPerc10), }, confirmationTimeByPriorityFee: { 15: (0, utils_1.gweiToWei)(emaPerc45), 30: (0, utils_1.gweiToWei)(emaPerc30), 45: (0, utils_1.gweiToWei)(emaPerc15), 60: (0, utils_1.gweiToWei)(emaPerc10), }, maxPriorityFeeSuggestions: { fast: (0, utils_1.gweiToWei)(boundedFastMaxPriorityFee), normal: (0, utils_1.gweiToWei)(boundedNormalPriorityFee), urgent: (0, utils_1.gweiToWei)(boundedUrgentPriorityFee), }, }]; } }); }); }; exports.suggestMaxPriorityFee = suggestMaxPriorityFee; var suggestFees = function (provider) { return __awaiter(void 0, void 0, void 0, function () { var _a, baseFeeSuggestion, baseFeeTrend, currentBaseFee, blocksToConfirmationByBaseFee, _b, maxPriorityFeeSuggestions, confirmationTimeByPriorityFee, blocksToConfirmationByPriorityFee; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, (0, exports.suggestMaxBaseFee)(provider)]; case 1: _a = _c.sent(), baseFeeSuggestion = _a.baseFeeSuggestion, baseFeeTrend = _a.baseFeeTrend, currentBaseFee = _a.currentBaseFee, blocksToConfirmationByBaseFee = _a.blocksToConfirmationByBaseFee; return [4 /*yield*/, (0, exports.suggestMaxPriorityFee)(provider)]; case 2: _b = _c.sent(), maxPriorityFeeSuggestions = _b.maxPriorityFeeSuggestions, confirmationTimeByPriorityFee = _b.confirmationTimeByPriorityFee, blocksToConfirmationByPriorityFee = _b.blocksToConfirmationByPriorityFee; return [2 /*return*/, { baseFeeSuggestion: baseFeeSuggestion, baseFeeTrend: baseFeeTrend, blocksToConfirmationByBaseFee: blocksToConfirmationByBaseFee, blocksToConfirmationByPriorityFee: blocksToConfirmationByPriorityFee, confirmationTimeByPriorityFee: confirmationTimeByPriorityFee, currentBaseFee: currentBaseFee, maxPriorityFeeSuggestions: maxPriorityFeeSuggestions, }]; } }); }); }; exports.suggestFees = suggestFees;