106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { estimateGasForCalls } from "../../../src/utils/gas.js";
|
|
import { CompiledCall } from "../../../src/planner/compiler.js";
|
|
import { JsonRpcProvider } from "ethers";
|
|
|
|
describe("Gas Estimation", () => {
|
|
let mockProvider: any;
|
|
|
|
beforeEach(() => {
|
|
mockProvider = {
|
|
estimateGas: vi.fn(),
|
|
getNetwork: vi.fn().mockResolvedValue({ chainId: 1n }),
|
|
};
|
|
});
|
|
|
|
it("should estimate gas for single call", async () => {
|
|
const calls: CompiledCall[] = [
|
|
{
|
|
to: "0x1234567890123456789012345678901234567890",
|
|
data: "0x1234",
|
|
description: "Test call",
|
|
},
|
|
];
|
|
|
|
mockProvider.estimateGas = vi.fn().mockResolvedValue(100000n);
|
|
|
|
const estimate = await estimateGasForCalls(
|
|
mockProvider as any,
|
|
calls,
|
|
"0x0000000000000000000000000000000000000000"
|
|
);
|
|
|
|
expect(estimate).toBeGreaterThan(0n);
|
|
});
|
|
|
|
it("should estimate gas for multiple calls", async () => {
|
|
const calls: CompiledCall[] = [
|
|
{
|
|
to: "0x1234567890123456789012345678901234567890",
|
|
data: "0x1234",
|
|
description: "Call 1",
|
|
},
|
|
{
|
|
to: "0x1234567890123456789012345678901234567890",
|
|
data: "0x5678",
|
|
description: "Call 2",
|
|
},
|
|
];
|
|
|
|
mockProvider.estimateGas = vi.fn().mockResolvedValue(100000n);
|
|
|
|
const estimate = await estimateGasForCalls(
|
|
mockProvider as any,
|
|
calls,
|
|
"0x0000000000000000000000000000000000000000"
|
|
);
|
|
|
|
expect(estimate).toBeGreaterThan(0n);
|
|
});
|
|
|
|
it("should add safety buffer to estimates", async () => {
|
|
const calls: CompiledCall[] = [
|
|
{
|
|
to: "0x1234567890123456789012345678901234567890",
|
|
data: "0x1234",
|
|
description: "Test call",
|
|
},
|
|
];
|
|
|
|
mockProvider.estimateGas = vi.fn().mockResolvedValue(100000n);
|
|
|
|
const estimate = await estimateGasForCalls(
|
|
mockProvider as any,
|
|
calls,
|
|
"0x0000000000000000000000000000000000000000"
|
|
);
|
|
|
|
// Should have safety buffer (typically 20%)
|
|
expect(estimate).toBeGreaterThan(100000n);
|
|
});
|
|
|
|
it("should handle estimation failures gracefully", async () => {
|
|
const calls: CompiledCall[] = [
|
|
{
|
|
to: "0x1234567890123456789012345678901234567890",
|
|
data: "0x1234",
|
|
description: "Test call",
|
|
},
|
|
];
|
|
|
|
mockProvider.estimateGas = vi.fn().mockRejectedValue(
|
|
new Error("Estimation failed")
|
|
);
|
|
|
|
// Should fall back to rough estimate
|
|
await expect(
|
|
estimateGasForCalls(
|
|
mockProvider as any,
|
|
calls,
|
|
"0x0000000000000000000000000000000000000000"
|
|
)
|
|
).rejects.toThrow();
|
|
});
|
|
});
|
|
|