Skip to content

Commit 1019a0b

Browse files
authored
Merge pull request #59 from tinymanorg/fix/price-impact-calculation
fix(swap): Fix calculation of price impact
2 parents a145722 + 0c7518b commit 1019a0b

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tinyman",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Tinyman JS SDK",
55
"author": "Tinyman Core Team",
66
"license": "MIT",

src/swap.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,11 @@ function getFixedInputSwapQuote({
280280
convertFromBaseUnits(decimals.assetOut, Number(assetOutAmount)) /
281281
convertFromBaseUnits(decimals.assetIn, Number(assetInAmount));
282282

283-
const swapPrice = 1 / rate;
284-
285283
const poolPrice =
286-
convertFromBaseUnits(decimals.assetIn, Number(inputSupply)) /
287-
convertFromBaseUnits(decimals.assetOut, Number(outputSupply));
284+
convertFromBaseUnits(decimals.assetOut, Number(outputSupply)) /
285+
convertFromBaseUnits(decimals.assetIn, Number(inputSupply));
288286

289-
const priceImpact = roundNumber(
290-
{decimalPlaces: 5},
291-
Math.abs(swapPrice / poolPrice - 1)
292-
);
287+
const priceImpact = roundNumber({decimalPlaces: 5}, Math.abs(rate / poolPrice - 1));
293288

294289
return {
295290
round: reserves.round,
@@ -441,16 +436,11 @@ function getFixedOutputSwapQuote({
441436
convertFromBaseUnits(decimals.assetOut, Number(assetOutAmount)) /
442437
convertFromBaseUnits(decimals.assetIn, Number(assetInAmount));
443438

444-
const swapPrice = 1 / rate;
445-
446439
const poolPrice =
447-
convertFromBaseUnits(decimals.assetIn, Number(inputSupply)) /
448-
convertFromBaseUnits(decimals.assetOut, Number(outputSupply));
440+
convertFromBaseUnits(decimals.assetOut, Number(outputSupply)) /
441+
convertFromBaseUnits(decimals.assetIn, Number(inputSupply));
449442

450-
const priceImpact = roundNumber(
451-
{decimalPlaces: 5},
452-
Math.abs(swapPrice / poolPrice - 1)
453-
);
443+
const priceImpact = roundNumber({decimalPlaces: 5}, Math.abs(rate / poolPrice - 1));
454444

455445
return {
456446
round: reserves.round,

0 commit comments

Comments
 (0)