Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
name: typecheck · lint · test · build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- name: Install dependencies
run: npm ci

- name: Typecheck
run: npm run typecheck

- name: Lint (Biome)
run: npm run lint

- name: Test (Vitest)
run: npm test

- name: Build
run: npm run build
556 changes: 496 additions & 60 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/dry-run-swap.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from "node:fs";
import { Client } from "@hiero-ledger/sdk";
import { AgentMode } from "@hashgraph/hedera-agent-kit";
import { Client } from "@hiero-ledger/sdk";
import { saucerswapPlugin } from "../dist/index.js";

const requiredEnv = (name) => {
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PoolVersion, SaucerSwapConfig } from "./types";
import { NETWORK_DEFAULTS, type SaucerSwapNetworkDefaults } from "./networks";
import type { PoolVersion, SaucerSwapConfig } from "./types";

const DEFAULT_CONFIG: SaucerSwapConfig = {
baseUrl: "https://api.saucerswap.finance",
Expand Down Expand Up @@ -68,7 +68,9 @@ const readContextConfig = (context: unknown): Partial<SaucerSwapConfig> => {
export const resolveSaucerSwapConfig = (context?: unknown): SaucerSwapConfig => {
const ctxConfig = readContextConfig(context);
const network = ctxConfig.network ?? readNetwork(process.env.SAUCERSWAP_NETWORK);
const networkDefaults: Partial<SaucerSwapNetworkDefaults> = network ? (NETWORK_DEFAULTS[network] ?? {}) : {};
const networkDefaults: Partial<SaucerSwapNetworkDefaults> = network
? (NETWORK_DEFAULTS[network] ?? {})
: {};
const envAliases = readTokenAliases(process.env.SAUCERSWAP_TOKEN_ALIASES);
const envDefaultVersion = readPoolVersion(process.env.SAUCERSWAP_DEFAULT_POOL_VERSION);

Expand Down
4 changes: 2 additions & 2 deletions src/tools/liquidity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
BaseTool,
type Context,
handleTransaction,
type RawTransactionResponse,
handleTransaction,
} from "@hashgraph/hedera-agent-kit";
import {
type Client,
ContractExecuteTransaction,
ContractFunctionParameters,
Transaction,
type Transaction,
} from "@hiero-ledger/sdk";
import { z } from "zod";
import { createSaucerSwapClient } from "../api/client";
Expand Down
11 changes: 3 additions & 8 deletions src/tools/swap.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
BaseTool,
type Context,
handleTransaction,
type RawTransactionResponse,
handleTransaction,
} from "@hashgraph/hedera-agent-kit";
import {
type Client,
ContractExecuteTransaction,
ContractFunctionParameters,
Transaction,
type Transaction,
} from "@hiero-ledger/sdk";
import { z } from "zod";
import { createSaucerSwapClient } from "../api/client";
Expand Down Expand Up @@ -187,12 +187,7 @@ export class SwapTool extends BaseTool<SwapInput, SwapInput> {
}

async secondaryAction(payload: SwapCorePayload, client: Client, context: Context) {
const result = await handleTransaction(
payload.transaction,
client,
context,
swapPostProcess,
);
const result = await handleTransaction(payload.transaction, client, context, swapPostProcess);
return { ...result, ...payload.extras };
}
}
Expand Down
74 changes: 60 additions & 14 deletions tests/liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ const makeClient = (accountId?: string) => ({

describe("AddLiquidityTool.coreAction", () => {
const tool = new AddLiquidityTool();
const baseArgs = { tokenA: TOKEN_A, tokenB: TOKEN_B, amountA: "1", amountB: "1", slippageTolerance: 0.5 };
const baseArgs = {
tokenA: TOKEN_A,
tokenB: TOKEN_B,
amountA: "1",
amountB: "1",
slippageTolerance: 0.5,
};

it("returns error when client has no operator account", async () => {
const result = await tool.coreAction(baseArgs, makeContext(ROUTER) as never, makeClient() as never);
const result = await tool.coreAction(
baseArgs,
makeContext(ROUTER) as never,
makeClient() as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("operator") });
});

Expand All @@ -54,24 +64,37 @@ describe("AddLiquidityTool.coreAction", () => {
makeContext(ROUTER, { getTokenByIdOrSymbol: async () => null }) as never,
makeClient(OPERATOR) as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("token metadata") });
expect(result).toMatchObject({
success: false,
error: expect.stringContaining("token metadata"),
});
});

it("returns error when router contract ID is missing", async () => {
const result = await tool.coreAction(baseArgs, makeContext(undefined) as never, makeClient(OPERATOR) as never);
const result = await tool.coreAction(
baseArgs,
makeContext(undefined) as never,
makeClient(OPERATOR) as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("router") });
});

it("returns a core payload with transaction and slippage-adjusted amounts on happy path", async () => {
const result = await tool.coreAction(baseArgs, makeContext(ROUTER) as never, makeClient(OPERATOR) as never);
const result = await tool.coreAction(
baseArgs,
makeContext(ROUTER) as never,
makeClient(OPERATOR) as never,
);
expect(result).toHaveProperty("transaction");
expect(result).toHaveProperty("extras");
if ("extras" in result) {
expect(result.extras).toHaveProperty("amountADesired");
expect(result.extras).toHaveProperty("amountBDesired");
expect(result.extras).toHaveProperty("amountAMin");
expect(result.extras).toHaveProperty("amountBMin");
expect(BigInt(result.extras.amountAMin as string)).toBeLessThan(BigInt(result.extras.amountADesired as string));
expect(BigInt(result.extras.amountAMin as string)).toBeLessThan(
BigInt(result.extras.amountADesired as string),
);
}
});
});
Expand All @@ -80,11 +103,15 @@ describe("AddLiquidityTool.shouldSecondaryAction", () => {
const tool = new AddLiquidityTool();

it("returns true for a payload with a transaction", async () => {
expect(await tool.shouldSecondaryAction({ transaction: {}, extras: {} }, {} as never)).toBe(true);
expect(await tool.shouldSecondaryAction({ transaction: {}, extras: {} }, {} as never)).toBe(
true,
);
});

it("returns false for an error result", async () => {
expect(await tool.shouldSecondaryAction({ success: false, error: "oops" }, {} as never)).toBe(false);
expect(await tool.shouldSecondaryAction({ success: false, error: "oops" }, {} as never)).toBe(
false,
);
});
});

Expand All @@ -101,7 +128,11 @@ describe("RemoveLiquidityTool.coreAction", () => {
};

it("returns error when client has no operator account", async () => {
const result = await tool.coreAction(baseArgs, makeContext(ROUTER) as never, makeClient() as never);
const result = await tool.coreAction(
baseArgs,
makeContext(ROUTER) as never,
makeClient() as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("operator") });
});

Expand All @@ -120,16 +151,27 @@ describe("RemoveLiquidityTool.coreAction", () => {
makeContext(ROUTER, { getTokenByIdOrSymbol: async () => null }) as never,
makeClient(OPERATOR) as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("token metadata") });
expect(result).toMatchObject({
success: false,
error: expect.stringContaining("token metadata"),
});
});

it("returns error when router contract ID is missing", async () => {
const result = await tool.coreAction(baseArgs, makeContext(undefined) as never, makeClient(OPERATOR) as never);
const result = await tool.coreAction(
baseArgs,
makeContext(undefined) as never,
makeClient(OPERATOR) as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("router") });
});

it("returns a core payload with transaction and LP amounts on happy path", async () => {
const result = await tool.coreAction(baseArgs, makeContext(ROUTER) as never, makeClient(OPERATOR) as never);
const result = await tool.coreAction(
baseArgs,
makeContext(ROUTER) as never,
makeClient(OPERATOR) as never,
);
expect(result).toHaveProperty("transaction");
expect(result).toHaveProperty("extras");
if ("extras" in result) {
Expand All @@ -144,10 +186,14 @@ describe("RemoveLiquidityTool.shouldSecondaryAction", () => {
const tool = new RemoveLiquidityTool();

it("returns true for a payload with a transaction", async () => {
expect(await tool.shouldSecondaryAction({ transaction: {}, extras: {} }, {} as never)).toBe(true);
expect(await tool.shouldSecondaryAction({ transaction: {}, extras: {} }, {} as never)).toBe(
true,
);
});

it("returns false for an error result", async () => {
expect(await tool.shouldSecondaryAction({ success: false, error: "oops" }, {} as never)).toBe(false);
expect(await tool.shouldSecondaryAction({ success: false, error: "oops" }, {} as never)).toBe(
false,
);
});
});
27 changes: 22 additions & 5 deletions tests/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ describe("SwapTool.coreAction", () => {
const tool = new SwapTool();

it("returns error when client has no operator account", async () => {
const result = await tool.coreAction(baseArgs, makeContext(ROUTER) as never, makeClient() as never);
const result = await tool.coreAction(
baseArgs,
makeContext(ROUTER) as never,
makeClient() as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("operator") });
});

Expand All @@ -46,16 +50,27 @@ describe("SwapTool.coreAction", () => {
makeContext(ROUTER, { getTokenByIdOrSymbol: async () => null }) as never,
makeClient(OPERATOR) as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("token metadata") });
expect(result).toMatchObject({
success: false,
error: expect.stringContaining("token metadata"),
});
});

it("returns error when router contract ID is missing", async () => {
const result = await tool.coreAction(baseArgs, makeContext(undefined) as never, makeClient(OPERATOR) as never);
const result = await tool.coreAction(
baseArgs,
makeContext(undefined) as never,
makeClient(OPERATOR) as never,
);
expect(result).toMatchObject({ success: false, error: expect.stringContaining("router") });
});

it("returns a core payload with a ContractExecuteTransaction on happy path", async () => {
const result = await tool.coreAction(baseArgs, makeContext(ROUTER) as never, makeClient(OPERATOR) as never);
const result = await tool.coreAction(
baseArgs,
makeContext(ROUTER) as never,
makeClient(OPERATOR) as never,
);
expect(result).toHaveProperty("transaction");
expect(result).toHaveProperty("extras");
if ("extras" in result) {
Expand All @@ -76,7 +91,9 @@ describe("SwapTool.shouldSecondaryAction", () => {
});

it("returns false for an error result", async () => {
expect(await tool.shouldSecondaryAction({ success: false, error: "oops" }, {} as never)).toBe(false);
expect(await tool.shouldSecondaryAction({ success: false, error: "oops" }, {} as never)).toBe(
false,
);
});

it("returns false for null", async () => {
Expand Down
Loading