diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 0037792..3c87901 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -10,4 +10,5 @@ jobs: - uses: actions/checkout@v2 - uses: oven-sh/setup-bun@v1 - run: bun install + - run: bun run typecheck - run: bun run build diff --git a/bun.lockb b/bun.lockb index ef10a2d..0bdad8f 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index d975cfc..59cfccf 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { + "typecheck": "bun run tsc --noEmit", "build": "bun run tsup ./src/index.ts", "format": "prettier . --write", "release": "bun run build && changeset publish" @@ -21,6 +22,7 @@ "devDependencies": { "@changesets/cli": "^2.26.2", "bun-plugin-dts": "^0.2.1", + "bun-types": "^1.0.7", "prettier": "3.0.3", "tsup": "^7.2.0", "typescript": "^5.2.2" diff --git a/src/PoolDataClient.ts b/src/PoolDataClient.ts index 617f8ec..ed24c69 100644 --- a/src/PoolDataClient.ts +++ b/src/PoolDataClient.ts @@ -1,5 +1,5 @@ import axios from "axios"; -import { Coin, RawPool, PoolData } from "./types"; +import { Coin, PoolData, RawPool, RawPoolData } from "./types"; class PoolDataClient { private poolData: PoolData | null = null; @@ -17,7 +17,7 @@ class PoolDataClient { if (!this.poolData || currentTime - this.lastUpdated > this.expiry) { for (let i = 0; i < this.retryLimit; i++) { try { - const response = await axios.get(this.URL); + const response = await axios.get(this.URL); // Convert the indices in the pools to the actual coin addresses const coins = response.data.coins as Coin[]; diff --git a/src/ThalaswapRouter.ts b/src/ThalaswapRouter.ts index 93e7cc2..01eb28b 100644 --- a/src/ThalaswapRouter.ts +++ b/src/ThalaswapRouter.ts @@ -8,6 +8,7 @@ import { BalanceIndex, RawPool, LiquidityPool, + Pool, } from "./types"; import { EntryPayload, createEntryPayload } from "@thalalabs/surf"; import { STABLE_POOL_SCRIPTS_ABI } from "./abi/stable_pool_scripts"; @@ -21,9 +22,8 @@ const NULL_TYPE = `${STABLE_POOL_SCRIPTS_ABI.address}::base_pool::Null`; const NULL_4 = Array(4).fill(NULL_TYPE); const encodeWeight = (weight: number): string => { - return `${ - WEIGHTED_POOL_SCRIPTS_ABI.address - }::weighted_pool::Weight_${Math.floor(weight * 100).toString()}`; + return `${WEIGHTED_POOL_SCRIPTS_ABI.address + }::weighted_pool::Weight_${Math.floor(weight * 100).toString()}`; }; // Encode the pool type arguments for a given pool @@ -103,15 +103,14 @@ class ThalaswapRouter { return weights; } - // TODO: remove any - async buildGraph(pools: any[]): Promise { + async buildGraph(pools: Pool[]): Promise { const tokens: Set = new Set(); const graph: Graph = {}; for (const pool of pools) { const assets = ["asset0", "asset1", "asset2", "asset3"] .filter((a) => pool[a as AssetIndex]) - .map((a) => pool[a as AssetIndex]); + .map((a) => pool[a as AssetIndex]!); const balances = ["balance0", "balance1", "balance2", "balance3"] .filter((b, i) => assets[i]) @@ -219,7 +218,7 @@ class ThalaswapRouter { tokenOutDecimals, ), ] - : [ + : [ scaleUp(route.amountOut, tokenInDecimals), scaleUp( calcMaxSoldValue(route.amountIn, slippagePercentage), diff --git a/src/types.ts b/src/types.ts index 2ed621d..b654de0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,24 +32,38 @@ type Coin = { decimals: number; }; -type RawPool = { +type PoolBase = { name: string; - asset0: number; - asset1: number; - asset2?: number; - asset3?: number; balance0: number; balance1: number; balance2?: number; balance3?: number; amp?: number; }; +type RawPool = PoolBase & { + asset0: number; + asset1: number; + asset2?: number; + asset3?: number; +}; -type PoolData = { +type Pool = PoolBase & { + asset0: Coin; + asset1: Coin; + asset2?: Coin; + asset3?: Coin; +}; + +type RawPoolData = { pools: RawPool[]; coins: Coin[]; }; +type PoolData = { + pools: Pool[]; + coins: Coin[]; +}; + type RouteType = "exact_input" | "exact_output"; type PoolType = "stable_pool" | "weighted_pool"; type Graph = Record; @@ -77,5 +91,7 @@ export type { BalanceIndex, Coin, RawPool, + RawPoolData, PoolData, + Pool }; diff --git a/tsconfig.json b/tsconfig.json index 8cc9f71..7834230 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,7 @@ { "compilerOptions": { + "types": ["bun-types"], + /* Visit https://aka.ms/tsconfig to read more about this file */ /* Projects */