import { eip155_1 } from "@ethernauta/chain/eip155-1" import { AddressSchema, BytesSchema, } from "@ethernauta/core" import { create_testing_reader } from "@ethernauta/testing" import type { Call, Response } from "@ethernauta/transport" import { encode_chain_id } from "@ethernauta/transport" import { parse } from "valibot" import { describe, expect, it } from "vitest" import { buffer_gas_limit } from "./buffer-gas-limit" function stub_http( response_result: T, ): (_call: Call) => Promise { return async (_call: Call) => ({ id: "test", jsonrpc: "2.0" as const, result: response_result, }) } const CHAIN_ID = encode_chain_id({ namespace: "eip155", reference: eip155_1.chainId, }) const testing_reader = create_testing_reader({ chain_id: CHAIN_ID, }) const TRANSFER_TARGET = parse( AddressSchema, "0x0000000000000000000000000000000000000001", ) const CONTRACT_TARGET = parse( AddressSchema, "0x0000000000000000000000000000000000000002", ) const INPUT_DATA = parse(BytesSchema, "0xdeadbeef") describe("buffer_gas_limit", () => { it("returns the estimate unchanged at multiplier 1.0", async () => { // eth_estimateGas → 0x5208 (21000 — native transfer baseline). const resolved = testing_reader(stub_http("0x5208")) const buffered = await buffer_gas_limit({ tx: { to: TRANSFER_TARGET }, multiplier: 1.0, })(resolved) expect(buffered).toBe("0x5208") }) it("buffers by 1.2× and rounds up", async () => { // 21000 × 1.2 = 25200 = 0x6270. const resolved = testing_reader(stub_http("0x5208")) const buffered = await buffer_gas_limit({ tx: { to: TRANSFER_TARGET }, multiplier: 1.2, })(resolved) expect(buffered).toBe("0x6270") }) it("buffers by 2.0× on a contract call", async () => { // 0x186a0 = 100_000. × 2 = 200_000 = 0x30d40. const resolved = testing_reader(stub_http("0x186a0")) const buffered = await buffer_gas_limit({ tx: { to: CONTRACT_TARGET, input: INPUT_DATA, }, multiplier: 2.0, })(resolved) expect(buffered).toBe("0x30d40") }) })