Skip to content

Commit

Permalink
Merge pull request #9 from ThalaLabs/samuel/type-safety
Browse files Browse the repository at this point in the history
Add type safety
  • Loading branch information
SamuelQZQ authored Nov 1, 2023
2 parents 16818a2 + dc53916 commit 15a3fd1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/PoolDataClient.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<RawPoolData>(this.URL);

// Convert the indices in the pools to the actual coin addresses
const coins = response.data.coins as Coin[];
Expand Down
13 changes: 6 additions & 7 deletions src/ThalaswapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -103,15 +103,14 @@ class ThalaswapRouter {
return weights;
}

// TODO: remove any
async buildGraph(pools: any[]): Promise<Graph> {
async buildGraph(pools: Pool[]): Promise<Graph> {
const tokens: Set<string> = 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])
Expand Down Expand Up @@ -219,7 +218,7 @@ class ThalaswapRouter {
tokenOutDecimals,
),
]
: [
: [
scaleUp(route.amountOut, tokenInDecimals),
scaleUp(
calcMaxSoldValue(route.amountIn, slippagePercentage),
Expand Down
28 changes: 22 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Edge[]>;
Expand Down Expand Up @@ -77,5 +91,7 @@ export type {
BalanceIndex,
Coin,
RawPool,
RawPoolData,
PoolData,
Pool
};
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"types": ["bun-types"],

/* Visit https://aka.ms/tsconfig to read more about this file */

/* Projects */
Expand Down

0 comments on commit 15a3fd1

Please sign in to comment.