Skip to content

Commit

Permalink
Merge pull request #31 from ThalaLabs/samuel/upgrade-surf
Browse files Browse the repository at this point in the history
upgrade surf to 1.0.1
  • Loading branch information
SamuelQZQ authored Dec 5, 2023
2 parents cfa1fb1 + b5beaee commit 4136c24
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 85 deletions.
Binary file modified bun.lockb
Binary file not shown.
46 changes: 29 additions & 17 deletions examples/examples.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { createClient } from "@thalalabs/surf";
import { AptosAccount } from "aptos";
import { createSurfClient } from "@thalalabs/surf";
import { ThalaswapRouter } from "../src";
import {
Account,
Aptos,
AptosConfig,
Network,
Ed25519PrivateKey,
} from "@aptos-labs/ts-sdk";

const client = createClient({
nodeUrl: "https://fullnode.testnet.aptoslabs.com/v1",
});
const client = createSurfClient(
new Aptos(
new AptosConfig({
network: Network.TESTNET,
}),
),
);

const privateKey = Buffer.from("your_private_key_here", "hex");
const account = new AptosAccount(privateKey);
const privateKey = "your_private_key_here";
const account = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey(privateKey),
});

// `testnet-example-pools.json` contains some testnet pools. The data is not up-to-date.
const router = new ThalaswapRouter(
Expand All @@ -30,12 +42,12 @@ async function example1() {
console.log("Route:", route);

const entryPayload = router.encodeRoute(route!, 0.5);
console.log(
"Entry function payload with 0.5% slippage:",
entryPayload.rawPayload,
);
console.log("Entry function payload with 0.5% slippage:", entryPayload);

await client.submitTransaction(entryPayload, { account });
await client.submitTransaction({
payload: entryPayload,
signer: account,
});
}

// Example 2: Exact output. Multi hop
Expand All @@ -54,12 +66,12 @@ async function example2() {
console.log("Route:", route);

const entryPayload = router.encodeRoute(route!, 0.5);
console.log(
"Entry function payload with 0.5% slippage:",
entryPayload.rawPayload,
);
console.log("Entry function payload with 0.5% slippage:", entryPayload);

await client.submitTransaction(entryPayload, { account });
await client.submitTransaction({
payload: entryPayload,
signer: account,
});
}

example1();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"license": "MIT",
"dependencies": {
"@thalalabs/surf": "^0.0.16",
"aptos": "^1.20.0",
"@aptos-labs/ts-sdk": "^1.0.0",
"@thalalabs/surf": "^1.0.1",
"axios": "^1.5.1"
},
"lint-staged": {
Expand Down
12 changes: 6 additions & 6 deletions src/ThalaswapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ class ThalaswapRouter {

return createEntryPayload(abi, {
function: functionName,
type_arguments: typeArgs as any,
arguments: [amountInArg, amountOutArg],
typeArguments: typeArgs as any,
functionArguments: [amountInArg, amountOutArg],
});
} else if (route.path.length == 2) {
const path0 = route.path[0];
Expand All @@ -282,8 +282,8 @@ class ThalaswapRouter {

return createEntryPayload(MULTIHOP_ROUTER_ABI, {
function: functionName,
type_arguments: typeArgs as any,
arguments: [amountInArg, amountOutArg],
typeArguments: typeArgs as any,
functionArguments: [amountInArg, amountOutArg],
});
} else {
// route.path.length == 3
Expand All @@ -299,8 +299,8 @@ class ThalaswapRouter {

return createEntryPayload(MULTIHOP_ROUTER_ABI, {
function: functionName,
type_arguments: typeArgs as any,
arguments: [amountInArg, amountOutArg],
typeArguments: typeArgs as any,
functionArguments: [amountInArg, amountOutArg],
});
}
}
Expand Down
87 changes: 27 additions & 60 deletions test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ const mockPoolDataClient = {
const router = new ThalaswapRouter("example-url");
router.setPoolDataClient(mockPoolDataClient as any);

function parseU8Array(arr: Uint8Array): bigint {
const view = new DataView(arr.buffer);
return view.getBigUint64(0, true); // true indicates little endian
}

test("Exact input 1 hop", async () => {
const startToken =
"0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC";
Expand All @@ -55,13 +50,9 @@ test("Exact input 1 hop", async () => {
expect(route!.amountOut).toBeCloseTo(112, 0);

const payload = router.encodeRoute(route!, 0);
expect(payload.entryRequest.module_name.name.value).toBe(
"weighted_pool_scripts",
);
expect(
payload.entryRequest.function_name.value.search("swap_exact_in"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[0])).toBe(1000000000n);
expect(payload.function).toInclude("weighted_pool_scripts");
expect(payload.function).toInclude("swap_exact_in");
expect(payload.functionArguments[0]).toBe(1000000000);
});

test("Exact input 2 hop", async () => {
Expand All @@ -88,11 +79,9 @@ test("Exact input 2 hop", async () => {
expect(route!.amountOut).toBeCloseTo(153, 0);

const payload = router.encodeRoute(route!, 0);
expect(payload.entryRequest.module_name.name.value).toBe("router");
expect(
payload.entryRequest.function_name.value.search("swap_exact_in_2"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[0])).toBe(1000000n);
expect(payload.function).toInclude("router");
expect(payload.function).toInclude("swap_exact_in_2");
expect(payload.functionArguments[0]).toBe(1000000);
});

test("Exact input 3 hop", async () => {
Expand Down Expand Up @@ -121,11 +110,9 @@ test("Exact input 3 hop", async () => {
expect(route!.amountOut).toBeCloseTo(2.35, 2);

const payload = router.encodeRoute(route!, 0);
expect(payload.entryRequest.module_name.name.value).toBe("router");
expect(
payload.entryRequest.function_name.value.search("swap_exact_in_3"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[0])).toBe(1000000n);
expect(payload.function).toInclude("router");
expect(payload.function).toInclude("swap_exact_in_3");
expect(payload.functionArguments[0]).toBe(1000000);
});

test("Exact output 1 hop", async () => {
Expand All @@ -151,13 +138,9 @@ test("Exact output 1 hop", async () => {
expect(route!.amountIn).toBeCloseTo(326, 0);

const payload = router.encodeRoute(route!, 0);
expect(payload.entryRequest.module_name.name.value).toBe(
"weighted_pool_scripts",
);
expect(
payload.entryRequest.function_name.value.search("swap_exact_out"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[1])).toBe(1000000000n);
expect(payload.function).toInclude("weighted_pool_scripts");
expect(payload.function).toInclude("swap_exact_out");
expect(payload.functionArguments[1]).toBe(1000000000);
});

test("Exact output 2 hop", async () => {
Expand All @@ -184,11 +167,9 @@ test("Exact output 2 hop", async () => {
expect(route!.amountIn).toBeCloseTo(1566, 0);

const payload = router.encodeRoute(route!, 0);
expect(payload.entryRequest.module_name.name.value).toBe("router");
expect(
payload.entryRequest.function_name.value.search("swap_exact_out_2"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[1])).toBe(1000000n);
expect(payload.function).toInclude("router");
expect(payload.function).toInclude("swap_exact_out_2");
expect(payload.functionArguments[1]).toBe(1000000);
});

test("Exact output 3 hop", async () => {
Expand Down Expand Up @@ -217,11 +198,9 @@ test("Exact output 3 hop", async () => {
expect(route!.amountIn).toBeCloseTo(0.36, 2);

const payload = router.encodeRoute(route!, 0);
expect(payload.entryRequest.module_name.name.value).toBe("router");
expect(
payload.entryRequest.function_name.value.search("swap_exact_out_3"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[1])).toBe(1000000n);
expect(payload.function).toInclude("router");
expect(payload.function).toInclude("swap_exact_out_3");
expect(payload.functionArguments[1]).toBe(1000000);
});

test("Low price impact for MOD-USDC stable pool", async () => {
Expand Down Expand Up @@ -429,13 +408,9 @@ test("encodeRouter with balance input for exact-in swap", async () => {

// 1. should succeed if user has enough balance
const payload = router.encodeRoute(route!, 0, 1000000);
expect(payload.entryRequest.module_name.name.value).toBe(
"weighted_pool_scripts",
);
expect(
payload.entryRequest.function_name.value.search("swap_exact_in"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[0])).toBe(100000000n);
expect(payload.function).toInclude("weighted_pool_scripts");
expect(payload.function).toInclude("swap_exact_in");
expect(payload.functionArguments[0]).toBe(100000000);

// 2. should fail if user doesn't have enough balance
expect(() => {
Expand All @@ -458,21 +433,13 @@ test("encodeRouter with balance input for exact-out swap", async () => {

// 1. should succeed if user has enough balance. Slippage percentage is 50%
const payload = router.encodeRoute(route!, 50, 1000000);
expect(payload.entryRequest.module_name.name.value).toBe(
"weighted_pool_scripts",
);
expect(
payload.entryRequest.function_name.value.search("swap_exact_out"),
).not.toBe(-1);
expect(parseU8Array(payload.entryRequest.args[0])).toBe(9043083n);
expect(payload.function).toInclude("weighted_pool_scripts");
expect(payload.function).toInclude("swap_exact_out");
expect(payload.functionArguments[0]).toBe(9043083);

// 2. should set "amountIn" argument to user's balance if user's balance is smaller than expected input amount + slippage
const payload2 = router.encodeRoute(route!, 50, 9);
expect(payload2.entryRequest.module_name.name.value).toBe(
"weighted_pool_scripts",
);
expect(
payload2.entryRequest.function_name.value.search("swap_exact_out"),
).not.toBe(-1);
expect(parseU8Array(payload2.entryRequest.args[0])).toBe(9000000n);
expect(payload2.function).toInclude("weighted_pool_scripts");
expect(payload2.function).toInclude("swap_exact_out");
expect(payload2.functionArguments[0]).toBe(9000000);
});

0 comments on commit 4136c24

Please sign in to comment.