UNPKG

@vechain.energy/gas

Version:

calculate estimated gas usage for transactions

184 lines (183 loc) 7.32 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 }); // Mock setup must be at the top level const getNodeMock = jest.fn(); const postNodeMock = jest.fn(); const bentMock = jest.fn().mockImplementation((url, method) => { if (method === 'GET') { return getNodeMock; } return postNodeMock; }); jest.mock('bent', () => bentMock, { virtual: true }); const index_1 = __importDefault(require("./index")); describe('estimate(clause[]) with dynamic fee market', () => { beforeEach(() => { jest.clearAllMocks(); }); it('calculates correctly with dynamic fees using base fee + priority fee', () => __awaiter(void 0, void 0, void 0, function* () { // Mock base fee: 10 gwei getNodeMock.mockResolvedValueOnce({ baseFeePerGas: '0x2540be400' // 10 gwei in hex }); // Mock priority fee: 1 gwei getNodeMock.mockResolvedValueOnce({ maxPriorityFeePerGas: '0x3b9aca00' // 1 gwei in hex }); // Mock VM gas response postNodeMock.mockResolvedValueOnce([ { data: '0x000000000000000000000000000000000000000000000000000009184e72a000', events: [], transfers: [], gasUsed: 591, reverted: false, vmError: '' } ]); const gas = yield (0, index_1.default)([ { to: "0x0000000000000000000000000000456E65726779", value: '0x01', data: "0x" } ]); // Intrinsic gas: 21046, VM gas: 591, Total: 21637 // Fee per gas: 10 + 0.46 = 10.46 gwei = 10460000000 wei (4.6% rule) // Gas cost: 21637 * 10460000000 / 1e13 = 38 expect(gas).toEqual(38); })); it('uses custom priority fee when provided', () => __awaiter(void 0, void 0, void 0, function* () { getNodeMock.mockResolvedValueOnce({ baseFeePerGas: '0x2540be400' }); postNodeMock.mockResolvedValueOnce([ { data: '0x000000000000000000000000000000000000000000000000000009184e72a000', events: [], transfers: [], gasUsed: 591, reverted: false, vmError: '' } ]); const gas = yield (0, index_1.default)([ { to: "0x0000000000000000000000000000456E65726779", value: '0x01', data: "0x" } ], { maxPriorityFeePerGas: '2000000000' // 2 gwei }); // Intrinsic gas: 21046, VM gas: 591, Total: 21637 // Fee per gas: 10 + 2 = 12 gwei = 12000000000 wei // Gas cost: 21637 * 12000000000 / 1e13 = 44 expect(gas).toEqual(44); })); it('applies maxFeePerGas cap when provided', () => __awaiter(void 0, void 0, void 0, function* () { getNodeMock.mockResolvedValueOnce({ baseFeePerGas: '0x2540be400' }); getNodeMock.mockResolvedValueOnce({ maxPriorityFeePerGas: '0x3b9aca00' }); postNodeMock.mockResolvedValueOnce([ { data: '0x000000000000000000000000000000000000000000000000000009184e72a000', events: [], transfers: [], gasUsed: 591, reverted: false, vmError: '' } ]); const gas = yield (0, index_1.default)([ { to: "0x0000000000000000000000000000456E65726779", value: '0x01', data: "0x" } ], { maxFeePerGas: '8000000000' // 8 gwei cap }); // Intrinsic gas: 21046, VM gas: 591, Total: 21637 // Fee per gas: min(10 + 0.46, 8) = 8 gwei = 8000000000 wei // Gas cost: 21637 * 8000000000 / 1e13 = 29 expect(gas).toEqual(29); })); it('falls back to legacy calculation when baseFeePerGas not available', () => __awaiter(void 0, void 0, void 0, function* () { getNodeMock.mockResolvedValueOnce({}); postNodeMock.mockResolvedValueOnce([ { data: '0x000000000000000000000000000000000000000000000000000009184e72a000', events: [], transfers: [], gasUsed: 591, reverted: false, vmError: '' } ]); getNodeMock.mockResolvedValueOnce({ maxPriorityFeePerGas: '0x3b9aca00' }); postNodeMock.mockResolvedValueOnce([ { data: '0x000000000000000000000000000000000000000000000000000009184e72a000', events: [], transfers: [], gasUsed: 591, reverted: false, vmError: '' } ]); const gas = yield (0, index_1.default)([ { to: "0x0000000000000000000000000000456E65726779", value: '0x01', data: "0x" } ]); // Legacy base price: 10000000000000 (10 gwei) // Priority fee: 1000000000 (1 gwei) // Total fee: 10001000000000 // Gas cost: 21637 * 10001000000000 / 1e13 = 21637 expect(gas).toEqual(36595); })); it('calculates optimal priority fee using fee history', () => __awaiter(void 0, void 0, void 0, function* () { getNodeMock.mockResolvedValueOnce({ baseFeePerGas: '0xe8d4a51000' }); getNodeMock.mockResolvedValueOnce({ oldestBlock: '0x1', baseFeePerGas: ['0x1', '0x2', '0x3'], gasUsedRatio: ['0.5', '0.6', '0.7'], reward: [ ['0x20', '0x20', '0x20'] ] }); postNodeMock.mockResolvedValueOnce([ { data: '0x000000000000000000000000000000000000000000000000000009184e72a000', events: [], transfers: [], gasUsed: 591, reverted: false, vmError: '' } ]); const gas = yield (0, index_1.default)([ { to: "0x0000000000000000000000000000456E65726779", value: '0x01', data: "0x" } ]); // Intrinsic gas: 21046, VM gas: 591, Total: 21637 // Fee per gas: 1000 + 0.000000032 = 1000.000000032 gwei // Gas cost: 21637 * 1000000000032 / 1e13 = 3659 expect(gas).toEqual(3659); })); });