UNPKG

@ethereumjs/tx

Version:
51 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDataGas = getDataGas; exports.verifyAccessList = verifyAccessList; const Legacy = require("./legacy.js"); const util_1 = require("@ethereumjs/util"); /** * The amount of gas paid for the data in this tx */ function getDataGas(tx) { return Legacy.getDataGas(tx) + getAccessListDataGas(tx); } /** * Calculates the data gas cost for the access list of a tx */ function getAccessListDataGas(tx) { const { common, accessList } = tx; const accessListStorageKeyCost = common.param('accessListStorageKeyGas'); const accessListAddressCost = common.param('accessListAddressGas'); const totalSlots = accessList.reduce((sum, item) => sum + BigInt(item[1].length), 0n); const addresses = BigInt(accessList.length); let cost = addresses * accessListAddressCost + totalSlots * accessListStorageKeyCost; // EIP-7981: add floor cost for access list bytes (20 bytes/address + 32 bytes/slot, 4 tokens/byte) if (common.isActivatedEIP(7981)) { const accessListBytes = addresses * 20n + totalSlots * 32n; cost += common.param('totalCostFloorPerToken') * accessListBytes * 4n; } return cost; } /** * Verifies an access list. Throws if invalid. * @param tx - Transaction whose access list should be validated */ function verifyAccessList(tx) { const accessList = tx.accessList; for (const accessListItem of accessList) { if (accessListItem.length !== 2) { throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid EIP-2930 transaction: access list item should have 2 elements'); } const [address, storageSlots] = accessListItem; if (address.length !== 20) { throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid EIP-2930 transaction: address length should be 20 bytes'); } for (const slot of storageSlots) { if (slot.length !== 32) { throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid EIP-2930 transaction: storage slot length should be 32 bytes'); } } } } //# sourceMappingURL=eip2930.js.map