136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { StrategyCompiler } from "../../src/planner/compiler.js";
|
|
|
|
describe("Flash Loan Integration", () => {
|
|
it("should compile flash loan with swap", async () => {
|
|
const strategy = {
|
|
name: "Flash Loan Swap",
|
|
chain: "mainnet",
|
|
steps: [
|
|
{
|
|
id: "flashLoan",
|
|
action: {
|
|
type: "aaveV3.flashLoan",
|
|
assets: ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"],
|
|
amounts: ["1000000"],
|
|
},
|
|
},
|
|
{
|
|
id: "swap",
|
|
action: {
|
|
type: "uniswapV3.swap",
|
|
tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
tokenOut: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
fee: 3000,
|
|
amountIn: "1000000",
|
|
exactInput: true,
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const compiler = new StrategyCompiler("mainnet");
|
|
const plan = await compiler.compile(
|
|
strategy as any,
|
|
"0x1234567890123456789012345678901234567890"
|
|
);
|
|
|
|
expect(plan.requiresFlashLoan).toBe(true);
|
|
expect(plan.flashLoanAsset).toBe(
|
|
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
|
);
|
|
expect(plan.flashLoanAmount).toBe(1000000n);
|
|
});
|
|
|
|
it("should compile flash loan with multiple operations", async () => {
|
|
const strategy = {
|
|
name: "Flash Loan Multi-Op",
|
|
chain: "mainnet",
|
|
steps: [
|
|
{
|
|
id: "flashLoan",
|
|
action: {
|
|
type: "aaveV3.flashLoan",
|
|
assets: ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"],
|
|
amounts: ["1000000"],
|
|
},
|
|
},
|
|
{
|
|
id: "swap1",
|
|
action: {
|
|
type: "uniswapV3.swap",
|
|
tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
tokenOut: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
fee: 3000,
|
|
amountIn: "500000",
|
|
exactInput: true,
|
|
},
|
|
},
|
|
{
|
|
id: "swap2",
|
|
action: {
|
|
type: "uniswapV3.swap",
|
|
tokenIn: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
tokenOut: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
fee: 3000,
|
|
amountIn: "500000",
|
|
exactInput: true,
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const compiler = new StrategyCompiler("mainnet");
|
|
const plan = await compiler.compile(
|
|
strategy as any,
|
|
"0x1234567890123456789012345678901234567890"
|
|
);
|
|
|
|
expect(plan.requiresFlashLoan).toBe(true);
|
|
// Both swaps should be in the callback
|
|
expect(plan.calls.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("should validate flash loan repayment requirements", async () => {
|
|
const strategy = {
|
|
name: "Flash Loan Validation",
|
|
chain: "mainnet",
|
|
steps: [
|
|
{
|
|
id: "flashLoan",
|
|
action: {
|
|
type: "aaveV3.flashLoan",
|
|
assets: ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"],
|
|
amounts: ["1000000"],
|
|
},
|
|
},
|
|
{
|
|
id: "swap",
|
|
action: {
|
|
type: "uniswapV3.swap",
|
|
tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
tokenOut: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
fee: 3000,
|
|
amountIn: "1000000",
|
|
exactInput: true,
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const compiler = new StrategyCompiler("mainnet");
|
|
const plan = await compiler.compile(
|
|
strategy as any,
|
|
"0x1234567890123456789012345678901234567890"
|
|
);
|
|
|
|
// Flash loan should require repayment
|
|
expect(plan.requiresFlashLoan).toBe(true);
|
|
// Should have executeFlashLoan call
|
|
expect(plan.calls.some((c) => c.description.includes("flash loan"))).toBe(
|
|
true
|
|
);
|
|
});
|
|
});
|
|
|