Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Update Readme & move some files #4

Merged
merged 1 commit into from
Apr 26, 2024
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
5 changes: 5 additions & 0 deletions .changeset/weak-plums-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thalalabs/thalaswap-math": patch
---

Update Readme & move some files
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

SDK to calculate swap amounts and pool price.

# install

```
$ npm i @thalalabs/thalaswap-math
```

# example

```typescript
import {
calcInGivenOutStable,
calcInGivenOutWeighted,
calcOutGivenInStable,
calcOutGivenInWeighted,
} from "@thalalabs/thalaswap-math";

// Calculate amount out given amount in:
// A stable pool with three tokens having balances of [100, 200, 300],
// The pool's amplification coefficient is 1.
Expand Down
2 changes: 1 addition & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
calcInGivenOutWeighted,
calcOutGivenInStable,
calcOutGivenInWeighted,
} from "thalaswap-math";
} from "@thalalabs/thalaswap-math";

// Calculate amount out given amount in:
// A stable pool with three tokens having balances of [100, 200, 300],
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"typescript": "^5.4.5"
},
"dependencies": {
"thalaswap-math": "link:.."
"@thalalabs/thalaswap-math": "link:.."
}
}
5 changes: 1 addition & 4 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/internal/constants.ts

This file was deleted.

91 changes: 0 additions & 91 deletions src/internal/utils.ts

This file was deleted.

95 changes: 93 additions & 2 deletions src/stablePoolMath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getD, getY } from "./internal/utils";

export function calcOutGivenInStable(
amountIn: number,
indexIn: number,
Expand Down Expand Up @@ -134,3 +132,96 @@ function getPriceStableWithKnownD(
let naxx = n * amp * balances[i] * balances[i] * balances[j] * balances[j];
return (balances[i] * b + naxx) / (balances[j] * b + naxx);
}

const EPSILON = 0.000001; // 1e-6, for detecting convergence in stableswap math
const MAX_LOOP_LIMIT = 100;

// see `get_Y` in https://github.com/ThalaLabs/thala-modules/blob/main/thalaswap_math/sources/stable_math.move
function getY(
xp: number[],
x: number,
a: number,
i: number,
j: number
): number {
const d = getD(xp, a);

const n = xp.length;
const ann = a * n;

let c = d;
let s = 0;

let k = 0;
while (k < n) {
if (k == j) {
k = k + 1;
continue;
}

let x_k = k == i ? x : xp[k];

s = s + x_k;
c = (c * d) / (x_k * n);

k = k + 1;
}

// in the above loop, there's only (n - 1) iterations
// therefore we add the last iteration that times (d / n), then divided by ann
c = (c * d) / (ann * n);
let b = s + d / ann;

let y = d;
k = 0;
while (k < MAX_LOOP_LIMIT) {
let prev_y = y;
y = (y * y + c) / (2 * y + b - d);
if (Math.abs(y - prev_y) < EPSILON) {
return y;
}

k = k + 1;
}

throw new Error(
`not converged in getY, xp: ${xp}, x: ${x}, a: ${a}, i: ${i}, j: ${j}`
);
}

// see `compute_invarient` in https://github.com/ThalaLabs/thala-modules/blob/main/thalaswap_math/sources/stable_math.move
function getD(xp: number[], a: number): number {
const n = xp.length;

// sum
const s = xp.reduce((partialSum, a) => partialSum + a, 0);

if (s == 0) {
return 0;
}

let prev_d: number;
let d = s;
const ann = a * n;

let i = 0;
while (i < MAX_LOOP_LIMIT) {
let dp = d;

let j = 0;
while (j < n) {
dp = (dp * d) / (xp[j] * n);
j = j + 1;
}

prev_d = d;
d = ((ann * s + n * dp) * d) / ((ann - 1) * d + (n + 1) * dp);
if (Math.abs(prev_d - d) < EPSILON) {
return d;
}

i = i + 1;
}

throw new Error(`not converged in getD, xp: ${xp}, a: ${a}`);
}
Loading