@etcswapv2/sdk-legacy 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -334,7 +334,7 @@ var Price = class _Price extends Fraction {
334
334
  quote(currencyAmount) {
335
335
  return new CurrencyAmount(
336
336
  this.quoteCurrency,
337
- this.adjusted.numerator * currencyAmount.raw / this.adjusted.denominator
337
+ currencyAmount.raw * this.numerator / this.denominator
338
338
  );
339
339
  }
340
340
  toSignificant(significantDigits = 6) {
@@ -455,18 +455,60 @@ var Route = class {
455
455
  });
456
456
  }
457
457
  };
458
+ function getAmountOut(amountIn, reserveIn, reserveOut) {
459
+ if (amountIn <= 0n) throw new Error("INSUFFICIENT_INPUT_AMOUNT");
460
+ if (reserveIn <= 0n || reserveOut <= 0n) throw new Error("INSUFFICIENT_LIQUIDITY");
461
+ const amountInWithFee = amountIn * 997n;
462
+ const numerator = amountInWithFee * reserveOut;
463
+ const denominator = reserveIn * 1000n + amountInWithFee;
464
+ return numerator / denominator;
465
+ }
466
+ function getAmountIn(amountOut, reserveIn, reserveOut) {
467
+ if (amountOut <= 0n) throw new Error("INSUFFICIENT_OUTPUT_AMOUNT");
468
+ if (reserveIn <= 0n || reserveOut <= 0n) throw new Error("INSUFFICIENT_LIQUIDITY");
469
+ const numerator = reserveIn * amountOut * 1000n;
470
+ const denominator = (reserveOut - amountOut) * 997n;
471
+ return numerator / denominator + 1n;
472
+ }
458
473
  var Trade = class _Trade {
459
474
  constructor(route, amount, tradeType) {
460
475
  this.route = route;
461
476
  this.tradeType = tradeType;
462
477
  if (tradeType === 0 /* EXACT_INPUT */) {
463
478
  this.inputAmount = amount;
464
- this.outputAmount = new CurrencyAmount(route.output, 0n);
479
+ let currentAmount = amount.raw;
480
+ for (let i = 0; i < route.pairs.length; i++) {
481
+ const pair = route.pairs[i];
482
+ const inputToken = route.path[i];
483
+ const [reserveIn, reserveOut] = inputToken.equals(pair.token0) ? [pair.reserve0.raw, pair.reserve1.raw] : [pair.reserve1.raw, pair.reserve0.raw];
484
+ currentAmount = getAmountOut(currentAmount, reserveIn, reserveOut);
485
+ }
486
+ this.outputAmount = new CurrencyAmount(route.output, currentAmount);
465
487
  } else {
466
488
  this.outputAmount = amount;
467
- this.inputAmount = new CurrencyAmount(route.input, 0n);
489
+ let currentAmount = amount.raw;
490
+ for (let i = route.pairs.length - 1; i >= 0; i--) {
491
+ const pair = route.pairs[i];
492
+ const outputToken = route.path[i + 1];
493
+ const [reserveIn, reserveOut] = outputToken.equals(pair.token0) ? [pair.reserve1.raw, pair.reserve0.raw] : [pair.reserve0.raw, pair.reserve1.raw];
494
+ currentAmount = getAmountIn(currentAmount, reserveIn, reserveOut);
495
+ }
496
+ this.inputAmount = new CurrencyAmount(route.input, currentAmount);
497
+ }
498
+ const midPrice = route.midPrice;
499
+ const executionPrice = this.executionPrice;
500
+ const midPriceValue = Number(midPrice.adjusted.numerator) / Number(midPrice.adjusted.denominator);
501
+ const execPriceValue = Number(executionPrice.adjusted.numerator) / Number(executionPrice.adjusted.denominator);
502
+ let priceImpactPct = 0;
503
+ if (execPriceValue > 0 && midPriceValue > 0) {
504
+ if (tradeType === 0 /* EXACT_INPUT */) {
505
+ priceImpactPct = Math.abs((execPriceValue - midPriceValue) / midPriceValue) * 100;
506
+ } else {
507
+ priceImpactPct = Math.abs((midPriceValue - execPriceValue) / midPriceValue) * 100;
508
+ }
468
509
  }
469
- this.priceImpact = new Percent(0n, 10000n);
510
+ const priceImpactBips = Math.round(priceImpactPct * 100);
511
+ this.priceImpact = new Percent(BigInt(priceImpactBips), 10000n);
470
512
  }
471
513
  get executionPrice() {
472
514
  return new Price(
package/dist/index.mjs CHANGED
@@ -287,7 +287,7 @@ var Price = class _Price extends Fraction {
287
287
  quote(currencyAmount) {
288
288
  return new CurrencyAmount(
289
289
  this.quoteCurrency,
290
- this.adjusted.numerator * currencyAmount.raw / this.adjusted.denominator
290
+ currencyAmount.raw * this.numerator / this.denominator
291
291
  );
292
292
  }
293
293
  toSignificant(significantDigits = 6) {
@@ -408,18 +408,60 @@ var Route = class {
408
408
  });
409
409
  }
410
410
  };
411
+ function getAmountOut(amountIn, reserveIn, reserveOut) {
412
+ if (amountIn <= 0n) throw new Error("INSUFFICIENT_INPUT_AMOUNT");
413
+ if (reserveIn <= 0n || reserveOut <= 0n) throw new Error("INSUFFICIENT_LIQUIDITY");
414
+ const amountInWithFee = amountIn * 997n;
415
+ const numerator = amountInWithFee * reserveOut;
416
+ const denominator = reserveIn * 1000n + amountInWithFee;
417
+ return numerator / denominator;
418
+ }
419
+ function getAmountIn(amountOut, reserveIn, reserveOut) {
420
+ if (amountOut <= 0n) throw new Error("INSUFFICIENT_OUTPUT_AMOUNT");
421
+ if (reserveIn <= 0n || reserveOut <= 0n) throw new Error("INSUFFICIENT_LIQUIDITY");
422
+ const numerator = reserveIn * amountOut * 1000n;
423
+ const denominator = (reserveOut - amountOut) * 997n;
424
+ return numerator / denominator + 1n;
425
+ }
411
426
  var Trade = class _Trade {
412
427
  constructor(route, amount, tradeType) {
413
428
  this.route = route;
414
429
  this.tradeType = tradeType;
415
430
  if (tradeType === 0 /* EXACT_INPUT */) {
416
431
  this.inputAmount = amount;
417
- this.outputAmount = new CurrencyAmount(route.output, 0n);
432
+ let currentAmount = amount.raw;
433
+ for (let i = 0; i < route.pairs.length; i++) {
434
+ const pair = route.pairs[i];
435
+ const inputToken = route.path[i];
436
+ const [reserveIn, reserveOut] = inputToken.equals(pair.token0) ? [pair.reserve0.raw, pair.reserve1.raw] : [pair.reserve1.raw, pair.reserve0.raw];
437
+ currentAmount = getAmountOut(currentAmount, reserveIn, reserveOut);
438
+ }
439
+ this.outputAmount = new CurrencyAmount(route.output, currentAmount);
418
440
  } else {
419
441
  this.outputAmount = amount;
420
- this.inputAmount = new CurrencyAmount(route.input, 0n);
442
+ let currentAmount = amount.raw;
443
+ for (let i = route.pairs.length - 1; i >= 0; i--) {
444
+ const pair = route.pairs[i];
445
+ const outputToken = route.path[i + 1];
446
+ const [reserveIn, reserveOut] = outputToken.equals(pair.token0) ? [pair.reserve1.raw, pair.reserve0.raw] : [pair.reserve0.raw, pair.reserve1.raw];
447
+ currentAmount = getAmountIn(currentAmount, reserveIn, reserveOut);
448
+ }
449
+ this.inputAmount = new CurrencyAmount(route.input, currentAmount);
450
+ }
451
+ const midPrice = route.midPrice;
452
+ const executionPrice = this.executionPrice;
453
+ const midPriceValue = Number(midPrice.adjusted.numerator) / Number(midPrice.adjusted.denominator);
454
+ const execPriceValue = Number(executionPrice.adjusted.numerator) / Number(executionPrice.adjusted.denominator);
455
+ let priceImpactPct = 0;
456
+ if (execPriceValue > 0 && midPriceValue > 0) {
457
+ if (tradeType === 0 /* EXACT_INPUT */) {
458
+ priceImpactPct = Math.abs((execPriceValue - midPriceValue) / midPriceValue) * 100;
459
+ } else {
460
+ priceImpactPct = Math.abs((midPriceValue - execPriceValue) / midPriceValue) * 100;
461
+ }
421
462
  }
422
- this.priceImpact = new Percent(0n, 10000n);
463
+ const priceImpactBips = Math.round(priceImpactPct * 100);
464
+ this.priceImpact = new Percent(BigInt(priceImpactBips), 10000n);
423
465
  }
424
466
  get executionPrice() {
425
467
  return new Price(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etcswapv2/sdk-legacy",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Legacy V2 SDK for ETCswap - non-generic API compatible with Uniswap V2 interface",
5
5
  "repository": {
6
6
  "type": "git",
@@ -37,6 +37,14 @@
37
37
  "dependencies": {
38
38
  "viem": "^2.0.0"
39
39
  },
40
+ "scripts": {
41
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
42
+ "clean": "rimraf dist",
43
+ "lint": "eslint src --ext .ts",
44
+ "test": "vitest run",
45
+ "typecheck": "tsc --noEmit",
46
+ "prepublishOnly": "pnpm run build"
47
+ },
40
48
  "devDependencies": {
41
49
  "@types/node": "^20.0.0",
42
50
  "rimraf": "^5.0.0",
@@ -46,12 +54,5 @@
46
54
  },
47
55
  "publishConfig": {
48
56
  "access": "public"
49
- },
50
- "scripts": {
51
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
52
- "clean": "rimraf dist",
53
- "lint": "eslint src --ext .ts",
54
- "test": "vitest run",
55
- "typecheck": "tsc --noEmit"
56
57
  }
57
- }
58
+ }