UNPKG

@vechain.energy/gas

Version:

calculate estimated gas usage for transactions

24 lines (23 loc) 874 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = intrinsic; const constants_1 = require("./constants"); function intrinsic(clauses) { return clauses.reduce((sum, c) => { sum += c.to === null ? constants_1.CLAUSE_GAS_CONTRACT_CREATION : constants_1.CLAUSE_GAS; sum += dataGas(c.data); return sum; }, constants_1.TX_GAS); } function dataGas(data) { if (data === undefined) { return 0; } const bytes = data.slice(2).match(/.{1,2}/g); if (bytes === null) { return 0; } const amountZeroBytes = bytes.reduce((amountZeroBytes, byte) => amountZeroBytes + (byte === '00' ? 1 : 0), 0); const amountNonZeroBytes = bytes.length - amountZeroBytes; return (amountNonZeroBytes * constants_1.NON_ZERO_BYTES_GAS) + (amountZeroBytes * constants_1.ZERO_BYTES_GAS); }