UNPKG

@vechain.energy/gas

Version:

calculate estimated gas usage for transactions

46 lines (45 loc) 2.3 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = estimate; const intrinsic_1 = __importDefault(require("./intrinsic")); const vmPrice_1 = __importDefault(require("./vmPrice")); const feeMarket_1 = __importDefault(require("./feeMarket")); const defaultOptions = { nodeOrConnex: "https://mainnet.veblocks.net", gasPriceCoef: 0 }; function estimate(clauses_1) { return __awaiter(this, arguments, void 0, function* (clauses, _options = {}) { const options = Object.assign(Object.assign({}, defaultOptions), _options); const intrinsicGas = (0, intrinsic_1.default)(clauses); const vmGas = yield (0, vmPrice_1.default)(clauses, options.nodeOrConnex, options.caller); const transactionGas = intrinsicGas + vmGas; // Always use dynamic fee market (post-Galactica fork) const feeMarketOptions = {}; if (options.maxFeePerGas !== undefined) { feeMarketOptions.maxFeePerGas = options.maxFeePerGas; } if (options.maxPriorityFeePerGas !== undefined) { feeMarketOptions.maxPriorityFeePerGas = options.maxPriorityFeePerGas; } const feePerGas = yield (0, feeMarket_1.default)(options.nodeOrConnex, feeMarketOptions); // Calculate final gas cost const gasCost = feePerGas .times(transactionGas) .dividedBy(1e13) .decimalPlaces(0); return gasCost.toNumber(); }); }