@rarible/estimate-middleware
Advanced tools
Comparing version 0.10.0-alpha.1 to 0.10.0-alpha.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.estimate = void 0; | ||
var eth_json_rpc_middleware_1 = require("eth-json-rpc-middleware"); | ||
var json_rpc_engine_1 = require("json-rpc-engine"); | ||
var middleware_1 = require("./middleware"); | ||
var utils_1 = require("./utils"); | ||
function estimate(provider, options) { | ||
if (options === void 0) { options = {}; } | ||
const eth_json_rpc_middleware_1 = require("eth-json-rpc-middleware"); | ||
const json_rpc_engine_1 = require("json-rpc-engine"); | ||
const middleware_1 = require("./middleware"); | ||
const utils_1 = require("./utils"); | ||
function estimate(provider, options = {}) { | ||
if ((0, utils_1.isSafeEventEmitterProvider)(provider)) { | ||
var engine = new json_rpc_engine_1.JsonRpcEngine(); | ||
var estimateEngine = getEstimateEngine(provider, options.estimation); | ||
const engine = new json_rpc_engine_1.JsonRpcEngine(); | ||
const estimateEngine = getEstimateEngine(provider, options.estimation); | ||
engine.push((0, middleware_1.createEstimateGasMiddleware)(estimateEngine, options.force, options.threshold)); | ||
@@ -22,3 +21,3 @@ engine.push((0, eth_json_rpc_middleware_1.providerAsMiddleware)(provider)); | ||
if (estimate === undefined) { | ||
var estimateEngine = new json_rpc_engine_1.JsonRpcEngine(); | ||
const estimateEngine = new json_rpc_engine_1.JsonRpcEngine(); | ||
estimateEngine.push((0, eth_json_rpc_middleware_1.providerAsMiddleware)(provider)); | ||
@@ -28,3 +27,3 @@ return estimateEngine; | ||
else if (typeof estimate === "string") { | ||
var engine = new json_rpc_engine_1.JsonRpcEngine(); | ||
const engine = new json_rpc_engine_1.JsonRpcEngine(); | ||
engine.push((0, eth_json_rpc_middleware_1.createFetchMiddleware)({ rpcUrl: estimate })); | ||
@@ -31,0 +30,0 @@ return engine; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createEstimateGasMiddleware = void 0; | ||
var tslib_1 = require("tslib"); | ||
var json_rpc_engine_1 = require("json-rpc-engine"); | ||
var bn_1 = require("@rarible/utils/build/bn"); | ||
var utils_1 = require("./utils"); | ||
const tslib_1 = require("tslib"); | ||
const json_rpc_engine_1 = require("json-rpc-engine"); | ||
const bn_1 = require("@rarible/utils/build/bn"); | ||
const utils_1 = require("./utils"); | ||
/** | ||
@@ -13,54 +13,38 @@ * Creates async middleware for gas estimation if gas not defined | ||
*/ | ||
function createEstimateGasMiddleware(engine, force, threshold, multiplier) { | ||
var _this = this; | ||
if (force === void 0) { force = false; } | ||
if (threshold === void 0) { threshold = 1.03; } | ||
if (multiplier === void 0) { multiplier = 2; } | ||
return (0, json_rpc_engine_1.createAsyncMiddleware)(function (req, res, next) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var params, gasLimitResponse, limitRaw, limitHex, multiplied, maxPriorityFeePerGasResponse, maxPriorityFeePerGasResponseRaw, blockResponse, baseFeeRaw, baseFee, maxPriorityFeePerGas, maxFeePerGasHex, error_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (req.method === "eth_subscribe") { | ||
res.error = new utils_1.RpcError("Notifications not supported", -32000); | ||
return [2 /*return*/, next()]; | ||
} | ||
if (!(req.method === "eth_sendTransaction")) return [3 /*break*/, 7]; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 6, , 7]); | ||
params = getTransactionParams(req); | ||
if (!(force || !params.gas)) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, engine.handle({ | ||
jsonrpc: "2.0", | ||
id: (0, json_rpc_engine_1.getUniqueId)(), | ||
params: [getEstimateParams(params)], | ||
method: "eth_estimateGas", | ||
})]; | ||
case 2: | ||
gasLimitResponse = _a.sent(); | ||
limitRaw = handleHexResponse(gasLimitResponse); | ||
function createEstimateGasMiddleware(engine, force = false, threshold = 1.03, multiplier = 2) { | ||
return (0, json_rpc_engine_1.createAsyncMiddleware)((req, res, next) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (req.method === "eth_subscribe") { | ||
res.error = new utils_1.RpcError("Notifications not supported", -32000); | ||
return next(); | ||
} | ||
if (req.method === "eth_sendTransaction") { | ||
try { | ||
const params = getTransactionParams(req); | ||
if (force || !params.gas) { | ||
const gasLimitResponse = yield engine.handle({ | ||
jsonrpc: "2.0", | ||
id: (0, json_rpc_engine_1.getUniqueId)(), | ||
params: [getEstimateParams(params)], | ||
method: "eth_estimateGas", | ||
}); | ||
const limitRaw = handleHexResponse(gasLimitResponse); | ||
if (limitRaw) { | ||
limitHex = extractHex(limitRaw); | ||
multiplied = (0, bn_1.toBn)(limitHex, 16).multipliedBy(threshold).toFixed(0); | ||
const limitHex = extractHex(limitRaw); | ||
const multiplied = (0, bn_1.toBn)(limitHex, 16).multipliedBy(threshold).toFixed(0); | ||
params["gas"] = withPrefix((0, bn_1.toBn)(multiplied).toString(16)); | ||
} | ||
return [4 /*yield*/, engine.handle({ | ||
jsonrpc: "2.0", | ||
id: (0, json_rpc_engine_1.getUniqueId)(), | ||
params: [], | ||
method: "eth_maxPriorityFeePerGas", | ||
})]; | ||
case 3: | ||
maxPriorityFeePerGasResponse = _a.sent(); | ||
maxPriorityFeePerGasResponseRaw = handleHexResponse(maxPriorityFeePerGasResponse); | ||
return [4 /*yield*/, engine.handle({ | ||
jsonrpc: "2.0", | ||
id: (0, json_rpc_engine_1.getUniqueId)(), | ||
params: ["pending", false], | ||
method: "eth_getBlockByNumber", | ||
})]; | ||
case 4: | ||
blockResponse = _a.sent(); | ||
baseFeeRaw = extractBaseFeePerGas(blockResponse); | ||
const maxPriorityFeePerGasResponse = yield engine.handle({ | ||
jsonrpc: "2.0", | ||
id: (0, json_rpc_engine_1.getUniqueId)(), | ||
params: [], | ||
method: "eth_maxPriorityFeePerGas", | ||
}); | ||
const maxPriorityFeePerGasResponseRaw = handleHexResponse(maxPriorityFeePerGasResponse); | ||
const blockResponse = yield engine.handle({ | ||
jsonrpc: "2.0", | ||
id: (0, json_rpc_engine_1.getUniqueId)(), | ||
params: ["pending", false], | ||
method: "eth_getBlockByNumber", | ||
}); | ||
const baseFeeRaw = extractBaseFeePerGas(blockResponse); | ||
if (maxPriorityFeePerGasResponseRaw && baseFeeRaw) { | ||
@@ -70,22 +54,20 @@ if (params.gasPrice !== undefined) { | ||
} | ||
baseFee = (0, bn_1.toBn)(extractHex(baseFeeRaw), 16).multipliedBy(multiplier).toFixed(0); | ||
maxPriorityFeePerGas = extractHex(maxPriorityFeePerGasResponseRaw); | ||
maxFeePerGasHex = (0, bn_1.toBn)(maxPriorityFeePerGas, 16).plus(baseFee).toString(16); | ||
const baseFee = (0, bn_1.toBn)(extractHex(baseFeeRaw), 16).multipliedBy(multiplier).toFixed(0); | ||
const maxPriorityFeePerGas = extractHex(maxPriorityFeePerGasResponseRaw); | ||
const maxFeePerGasHex = (0, bn_1.toBn)(maxPriorityFeePerGas, 16).plus(baseFee).toString(16); | ||
params["maxPriorityFeePerGas"] = maxPriorityFeePerGasResponseRaw; | ||
params["maxFeePerGas"] = withPrefix(maxFeePerGasHex); | ||
} | ||
_a.label = 5; | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: | ||
error_1 = _a.sent(); | ||
res.error = (0, utils_1.extractError)(error_1); | ||
return [3 /*break*/, 7]; | ||
case 7: return [2 /*return*/, next()]; | ||
} | ||
} | ||
}); | ||
}); }); | ||
catch (error) { | ||
res.error = (0, utils_1.extractError)(error); | ||
} | ||
} | ||
return next(); | ||
})); | ||
} | ||
exports.createEstimateGasMiddleware = createEstimateGasMiddleware; | ||
function withPrefix(value) { | ||
return "0x".concat(value); | ||
return `0x${value}`; | ||
} | ||
@@ -119,3 +101,3 @@ function extractHex(value) { | ||
if (request.params) { | ||
var tx = request.params[0]; | ||
const [tx] = request.params; | ||
if (isSendParams(tx)) { | ||
@@ -122,0 +104,0 @@ return tx; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.extractError = exports.RpcError = exports.isSafeEventEmitterProvider = void 0; | ||
var tslib_1 = require("tslib"); | ||
function isSafeEventEmitterProvider(x) { | ||
@@ -9,13 +8,10 @@ return typeof x === "object" && x !== null && "send" in x && "sendAsync" in x; | ||
exports.isSafeEventEmitterProvider = isSafeEventEmitterProvider; | ||
var RpcError = /** @class */ (function (_super) { | ||
tslib_1.__extends(RpcError, _super); | ||
function RpcError(message, code) { | ||
var _this = _super.call(this, message) || this; | ||
_this.code = code; | ||
class RpcError extends Error { | ||
constructor(message, code) { | ||
super(message); | ||
this.code = code; | ||
// eslint-disable-next-line unicorn/custom-error-definition | ||
_this.name = "JsonRpcError"; | ||
return _this; | ||
this.name = "JsonRpcError"; | ||
} | ||
return RpcError; | ||
}(Error)); | ||
} | ||
exports.RpcError = RpcError; | ||
@@ -25,3 +21,3 @@ function extractError(error) { | ||
if ("data" in error) { | ||
var data = error.data; | ||
const { data } = error; | ||
if (isObject(data) && isJsonRpcError(data)) { | ||
@@ -28,0 +24,0 @@ return new RpcError(data.message, data.code); |
{ | ||
"name": "@rarible/estimate-middleware", | ||
"version": "0.10.0-alpha.1", | ||
"version": "0.10.0-alpha.2", | ||
"keywords": [ | ||
@@ -26,3 +26,3 @@ "rarible", | ||
"dependencies": { | ||
"@rarible/utils": "^0.10.0-alpha.1", | ||
"@rarible/utils": "^0.10.0-alpha.2", | ||
"eth-json-rpc-middleware": "^8.0.1", | ||
@@ -40,3 +40,3 @@ "json-rpc-engine": "^6.1.0" | ||
}, | ||
"gitHead": "4db59b352e3e4e4bb12833d964087006dc9e4d81" | ||
"gitHead": "5a2ed593bab16bf08356ee2b85f83a4c1b45cf16" | ||
} |
Sorry, the diff of this file is not supported yet
41501
226