Skip to content

Commit

Permalink
thala-router support multi chains
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQZQ committed Jul 15, 2024
1 parent 785fe33 commit c28b213
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 27 deletions.
5 changes: 4 additions & 1 deletion packages/thalaswap-router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ $ npm i @thalalabs/router-sdk
## Examples

```
const router = new ThalaswapRouter("https://fullnode.mainnet.aptoslabs.com/v1");
const router = new ThalaswapRouter(
Network.MAINNET,
"https://fullnode.mainnet.aptoslabs.com/v1"
);
const fromToken = "0x1::aptos_coin::AptosCoin";
const toToken = "0xec84c05cc40950c86d8a8bed19552f1e8ebb783196bb021c916161d22dc179f7::asset::USDC";
const amountIn = 0.1;
Expand Down
5 changes: 4 additions & 1 deletion packages/thalaswap-router/examples/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const account = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey(privateKey),
});

const router = new ThalaswapRouter("https://fullnode.mainnet.aptoslabs.com/v1");
const router = new ThalaswapRouter(
Network.MAINNET,
"https://fullnode.mainnet.aptoslabs.com/v1",
);

// Example 1: Exact input. 1 hop
async function example1() {
Expand Down
28 changes: 19 additions & 9 deletions packages/thalaswap-router/src/PoolDataClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Coin, Pool, PoolData } from "./types";
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
import { THALASWAP_RESOURCE_ACCOUNT_ADDRESS } from "./constants";
import { uniq } from "lodash";
import { fp64ToFloat, parsePoolMetadata, scaleDown } from "./utils";
import { THALASWAP_RESOURCE_ACCOUNT_ADDRESS } from "./constants";

class PoolDataClient {
private poolData: PoolData | null = null;
Expand All @@ -11,12 +11,16 @@ class PoolDataClient {
private retryLimit = 3;
private client: Aptos;
private coins: Coin[] = [];
private resourceAddress: string;

constructor(network: Network, fullnode: string, resourceAddress?: string) {
this.resourceAddress =
resourceAddress || THALASWAP_RESOURCE_ACCOUNT_ADDRESS;

constructor(aptosRpc: string) {
this.client = new Aptos(
new AptosConfig({
network: Network.MAINNET,
fullnode: aptosRpc,
network: network,
fullnode: fullnode,
}),
);
}
Expand All @@ -27,16 +31,16 @@ class PoolDataClient {
for (let i = 0; i < this.retryLimit; i++) {
try {
const resources = await this.client.getAccountResources({
accountAddress: THALASWAP_RESOURCE_ACCOUNT_ADDRESS,
accountAddress: this.resourceAddress,
});

const poolResources = resources.filter(
(r) =>
r.type.startsWith(
`${THALASWAP_RESOURCE_ACCOUNT_ADDRESS}::weighted_pool::WeightedPool<`,
`${this.resourceAddress}::weighted_pool::WeightedPool<`,
) ||
r.type.startsWith(
`${THALASWAP_RESOURCE_ACCOUNT_ADDRESS}::stable_pool::StablePool<`,
`${this.resourceAddress}::stable_pool::StablePool<`,
),
) as {
type: string;
Expand All @@ -52,7 +56,10 @@ class PoolDataClient {

const allCoinAddress = uniq(
poolResources.reduce((acc, resource) => {
const metadata = parsePoolMetadata(resource.type);
const metadata = parsePoolMetadata(
resource.type,
this.resourceAddress,
);
metadata.coinAddresses.forEach((coin) => {
coin && acc.push(coin);
});
Expand Down Expand Up @@ -83,7 +90,10 @@ class PoolDataClient {

const pools = poolResources.reduce((acc, resource) => {
try {
const metadata = parsePoolMetadata(resource.type);
const metadata = parsePoolMetadata(
resource.type,
this.resourceAddress,
);
const [coin0, coin1, coin2, coin3] = metadata.coinAddresses.map(
(addr) => this.coins.find((c) => c.address === addr),
);
Expand Down
9 changes: 7 additions & 2 deletions packages/thalaswap-router/src/ThalaswapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { EntryPayload, createEntryPayload } from "@thalalabs/surf";
import { STABLE_POOL_SCRIPTS_ABI } from "./abi/stable_pool_scripts";
import { WEIGHTED_POOL_SCRIPTS_ABI } from "./abi/weighted_pool_scripts";
import { MULTIHOP_ROUTER_ABI } from "./abi/multihop_router";
import { Network } from "@aptos-labs/ts-sdk";
import { THALASWAP_RESOURCE_ACCOUNT_ADDRESS } from "./constants";

const NULL_TYPE = `${STABLE_POOL_SCRIPTS_ABI.address}::base_pool::Null`;
const NULL_4 = Array(4).fill(NULL_TYPE);
Expand Down Expand Up @@ -61,9 +63,12 @@ class ThalaswapRouter {
private client: PoolDataClient;
private graph: Graph | null = null;
private coins: Coin[] | null = null;
private resourceAddress: string;

constructor(dataURL: string) {
this.client = new PoolDataClient(dataURL);
constructor(network: Network, fullnode: string, resourceAddress?: string) {
this.resourceAddress =
resourceAddress || THALASWAP_RESOURCE_ACCOUNT_ADDRESS;
this.client = new PoolDataClient(network, fullnode);
}

setPoolDataClient(client: PoolDataClient) {
Expand Down
32 changes: 19 additions & 13 deletions packages/thalaswap-router/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BigNumber from "bignumber.js";
import { THALASWAP_RESOURCE_ACCOUNT_ADDRESS } from "./constants";
import { LiquidityPoolMetadata } from "./types";
import { THALASWAP_RESOURCE_ACCOUNT_ADDRESS } from "./constants";

export const BN_TEN = new BigNumber(10);

Expand All @@ -10,8 +10,16 @@ export function scaleDown(v: number | string, decimals: number): number {
.toNumber();
}

export function parsePoolMetadata(poolType: string): LiquidityPoolMetadata {
const [liquidityPoolType, poolTypeArgs] = parseLiquidityPoolType(poolType);
export function parsePoolMetadata(
poolType: string,
resourceAddress: string,
): LiquidityPoolMetadata {
const NULL_PATTERN = new RegExp(`${resourceAddress}::base_pool::Null`);

const [liquidityPoolType, poolTypeArgs] = parseLiquidityPoolType(
poolType,
resourceAddress,
);

// if first n coins are not dummycoin, then numCoins = n
const nullIndex = poolTypeArgs
Expand All @@ -37,7 +45,15 @@ export function parsePoolMetadata(poolType: string): LiquidityPoolMetadata {

export function parseLiquidityPoolType(
poolType: string,
resourceAddress: string,
): ["Weighted" | "Stable", string[]] {
const WEIGHTED_POOL_PATTERN = new RegExp(
`${resourceAddress}::weighted_pool::WeightedPool<(.*)>`,
);
const STABLE_POOL_PATTERN = new RegExp(
`${resourceAddress}::stable_pool::StablePool<(.*)>`,
);

const matchWeightedPool = poolType.match(WEIGHTED_POOL_PATTERN);
if (matchWeightedPool) {
return ["Weighted", matchWeightedPool[1].split(",").map((e) => e.trim())];
Expand All @@ -49,16 +65,6 @@ export function parseLiquidityPoolType(
throw new Error(`Invalid poolType: ${poolType}`);
}

const WEIGHTED_POOL_PATTERN = new RegExp(
`${THALASWAP_RESOURCE_ACCOUNT_ADDRESS}::weighted_pool::WeightedPool<(.*)>`,
);
const STABLE_POOL_PATTERN = new RegExp(
`${THALASWAP_RESOURCE_ACCOUNT_ADDRESS}::stable_pool::StablePool<(.*)>`,
);
const NULL_PATTERN = new RegExp(
`${THALASWAP_RESOURCE_ACCOUNT_ADDRESS}::base_pool::Null`,
);

// input cannot be larger the 2^31
// this should allow at least 6 digits precision in the fractional part
// https://stackoverflow.com/questions/45929493/node-js-maximum-safe-floating-point-number
Expand Down
3 changes: 2 additions & 1 deletion packages/thalaswap-router/test/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import poolData from "./test-pools.json";
import { ThalaswapRouter } from "../src";
import { Network } from "@aptos-labs/ts-sdk";

const coins = poolData.coins;
const pools = poolData.pools.map((pool: any) => {
Expand All @@ -23,7 +24,7 @@ const mockPoolDataClient = {
}),
};

const router = new ThalaswapRouter("example-url");
const router = new ThalaswapRouter(Network.MAINNET, "test");
router.setPoolDataClient(mockPoolDataClient as any);

test("Exact input 1 hop", async () => {
Expand Down

0 comments on commit c28b213

Please sign in to comment.