@gala-chain/gswap-sdk 0.0.9 → 0.0.10

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.cjs CHANGED
@@ -1383,6 +1383,8 @@ var Positions = class {
1383
1383
  };
1384
1384
 
1385
1385
  // src/classes/quoting.ts
1386
+ var import_api = require("@gala-chain/api");
1387
+ var import_dex = require("@gala-chain/dex");
1386
1388
  var import_bignumber4 = __toESM(require("bignumber.js"), 1);
1387
1389
 
1388
1390
  // src/types/fees.ts
@@ -1471,14 +1473,59 @@ var Quoting = class {
1471
1473
  }
1472
1474
  async getSingleQuote(tokenIn, tokenOut, fee, amount, isExactInput) {
1473
1475
  const tokenInClass = parseTokenClassKey(tokenIn);
1476
+ const tokenInClassKey = await (0, import_api.createValidDTO)(import_api.TokenClassKey, tokenInClass);
1474
1477
  const tokenOutClass = parseTokenClassKey(tokenOut);
1478
+ const tokenOutClassKey = await (0, import_api.createValidDTO)(import_api.TokenClassKey, tokenOutClass);
1475
1479
  const ordering = getTokenOrdering(tokenInClass, tokenOutClass, false);
1480
+ const token0ClassKey = await (0, import_api.createValidDTO)(
1481
+ import_api.TokenClassKey,
1482
+ ordering.token0
1483
+ );
1484
+ const token1ClassKey = await (0, import_api.createValidDTO)(
1485
+ import_api.TokenClassKey,
1486
+ ordering.token1
1487
+ );
1476
1488
  const formattedAmount = isExactInput ? (0, import_bignumber4.default)(amount).toFixed() : (0, import_bignumber4.default)(amount).multipliedBy(-1).toFixed();
1477
- const response = await this.sendQuoteRequest("/QuoteExactAmount", {
1478
- ...ordering,
1479
- fee,
1480
- amount: formattedAmount
1481
- });
1489
+ let feeType;
1490
+ switch (fee) {
1491
+ case 500 /* PERCENT_00_05 */:
1492
+ feeType = import_dex.DexFeePercentageTypes.FEE_0_05_PERCENT;
1493
+ break;
1494
+ case 3e3 /* PERCENT_00_30 */:
1495
+ feeType = import_dex.DexFeePercentageTypes.FEE_0_3_PERCENT;
1496
+ break;
1497
+ case 1e4 /* PERCENT_01_00 */:
1498
+ feeType = import_dex.DexFeePercentageTypes.FEE_1_PERCENT;
1499
+ break;
1500
+ default:
1501
+ throw new Error(`Invalid fee tier: ${fee}`);
1502
+ }
1503
+ const zeroForOne = compareTokens(tokenIn, tokenOut) < 0;
1504
+ const getCompositePoolDto = new import_dex.GetCompositePoolDto(
1505
+ zeroForOne ? tokenInClassKey : tokenOutClassKey,
1506
+ zeroForOne ? tokenOutClassKey : tokenInClassKey,
1507
+ feeType
1508
+ );
1509
+ const compositePoolResponse = await this.sendGetCompositePoolRequest(getCompositePoolDto);
1510
+ const compositePool = this.createCompositePoolDtoFromResponse(compositePoolResponse);
1511
+ const quoteDto = new import_dex.QuoteExactAmountDto(
1512
+ token0ClassKey,
1513
+ token1ClassKey,
1514
+ feeType,
1515
+ (0, import_bignumber4.default)(formattedAmount),
1516
+ zeroForOne,
1517
+ compositePool
1518
+ );
1519
+ const context = {
1520
+ callingUser: "eth|123...abc",
1521
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1522
+ stub: null,
1523
+ clientIdentity: {
1524
+ getMSPID: () => "UsersOrg"
1525
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1526
+ }
1527
+ };
1528
+ const response = await (0, import_dex.quoteExactAmount)(context, quoteDto);
1482
1529
  const tokenInAmount = ordering.zeroForOne ? response.amount0 : response.amount1;
1483
1530
  const tokenOutAmount = ordering.zeroForOne ? response.amount1 : response.amount0;
1484
1531
  let currentPrice = response.currentSqrtPrice.pow(2);
@@ -1507,20 +1554,76 @@ var Quoting = class {
1507
1554
  async getSingleQuoteExactOutput(tokenIn, tokenOut, amountOut, fee) {
1508
1555
  return this.getSingleQuote(tokenIn, tokenOut, fee, amountOut, false);
1509
1556
  }
1510
- async sendQuoteRequest(endpoint, body) {
1557
+ async sendGetCompositePoolRequest(getCompositePoolDto) {
1511
1558
  const response = await this.httpClient.sendPostRequest(
1512
1559
  this.gatewayBaseUrl,
1513
1560
  this.dexContractBasePath,
1514
- endpoint,
1515
- body
1561
+ "/GetCompositePool",
1562
+ getCompositePoolDto
1516
1563
  );
1517
1564
  const responseBody = response;
1518
- return {
1519
- amount0: (0, import_bignumber4.default)(responseBody.Data.amount0),
1520
- amount1: (0, import_bignumber4.default)(responseBody.Data.amount1),
1521
- currentSqrtPrice: (0, import_bignumber4.default)(responseBody.Data.currentSqrtPrice),
1522
- newSqrtPrice: (0, import_bignumber4.default)(responseBody.Data.newSqrtPrice)
1523
- };
1565
+ return responseBody.Data;
1566
+ }
1567
+ /**
1568
+ * Converts response data from GetCompositePool API into a proper CompositePoolDto
1569
+ * Based on the working implementation in quote-local.mjs
1570
+ */
1571
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1572
+ createCompositePoolDtoFromResponse(responseData) {
1573
+ const pool = new import_dex.Pool(
1574
+ responseData.pool.token0,
1575
+ responseData.pool.token1,
1576
+ responseData.pool.token0ClassKey,
1577
+ responseData.pool.token1ClassKey,
1578
+ responseData.pool.fee,
1579
+ new import_bignumber4.default(responseData.pool.sqrtPrice),
1580
+ responseData.pool.protocolFees
1581
+ );
1582
+ pool.bitmap = responseData.pool.bitmap;
1583
+ pool.grossPoolLiquidity = new import_bignumber4.default(responseData.pool.grossPoolLiquidity);
1584
+ pool.liquidity = new import_bignumber4.default(responseData.pool.liquidity);
1585
+ pool.feeGrowthGlobal0 = new import_bignumber4.default(responseData.pool.feeGrowthGlobal0);
1586
+ pool.feeGrowthGlobal1 = new import_bignumber4.default(responseData.pool.feeGrowthGlobal1);
1587
+ pool.protocolFeesToken0 = new import_bignumber4.default(responseData.pool.protocolFeesToken0);
1588
+ pool.protocolFeesToken1 = new import_bignumber4.default(responseData.pool.protocolFeesToken1);
1589
+ pool.tickSpacing = responseData.pool.tickSpacing;
1590
+ pool.maxLiquidityPerTick = new import_bignumber4.default(responseData.pool.maxLiquidityPerTick);
1591
+ const tickDataMap = {};
1592
+ Object.keys(responseData.tickDataMap).forEach((tickKey) => {
1593
+ const tickData = responseData.tickDataMap[tickKey];
1594
+ tickDataMap[tickKey] = new import_dex.TickData(tickData.poolHash, tickData.tick);
1595
+ tickDataMap[tickKey].initialised = tickData.initialised;
1596
+ tickDataMap[tickKey].liquidityNet = new import_bignumber4.default(tickData.liquidityNet);
1597
+ tickDataMap[tickKey].liquidityGross = new import_bignumber4.default(tickData.liquidityGross);
1598
+ tickDataMap[tickKey].feeGrowthOutside0 = new import_bignumber4.default(tickData.feeGrowthOutside0);
1599
+ tickDataMap[tickKey].feeGrowthOutside1 = new import_bignumber4.default(tickData.feeGrowthOutside1);
1600
+ });
1601
+ const token0Balance = new import_api.TokenBalance({
1602
+ owner: responseData.token0Balance.owner,
1603
+ collection: responseData.token0Balance.collection,
1604
+ category: responseData.token0Balance.category,
1605
+ type: responseData.token0Balance.type,
1606
+ additionalKey: responseData.token0Balance.additionalKey
1607
+ });
1608
+ token0Balance.quantity = new import_bignumber4.default(responseData.token0Balance.quantity);
1609
+ const token1Balance = new import_api.TokenBalance({
1610
+ owner: responseData.token1Balance.owner,
1611
+ collection: responseData.token1Balance.collection,
1612
+ category: responseData.token1Balance.category,
1613
+ type: responseData.token1Balance.type,
1614
+ additionalKey: responseData.token1Balance.additionalKey
1615
+ });
1616
+ token1Balance.quantity = new import_bignumber4.default(responseData.token1Balance.quantity);
1617
+ return new import_dex.CompositePoolDto(
1618
+ pool,
1619
+ tickDataMap,
1620
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1621
+ token0Balance,
1622
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1623
+ token1Balance,
1624
+ responseData.token0Decimals,
1625
+ responseData.token1Decimals
1626
+ );
1524
1627
  }
1525
1628
  };
1526
1629
 
@@ -1752,15 +1855,15 @@ var GSwap = class {
1752
1855
  };
1753
1856
 
1754
1857
  // src/classes/signers.ts
1755
- var import_api = require("@gala-chain/api");
1858
+ var import_api2 = require("@gala-chain/api");
1756
1859
  var import_connect = require("@gala-chain/connect");
1757
1860
  var PrivateKeySigner = class {
1758
1861
  keyBuffer;
1759
1862
  constructor(privateKey) {
1760
- this.keyBuffer = import_api.signatures.normalizePrivateKey(privateKey);
1863
+ this.keyBuffer = import_api2.signatures.normalizePrivateKey(privateKey);
1761
1864
  }
1762
1865
  async signObject(_methodName, obj) {
1763
- const signature = import_api.signatures.getSignature(obj, this.keyBuffer);
1866
+ const signature = import_api2.signatures.getSignature(obj, this.keyBuffer);
1764
1867
  return { ...obj, signature };
1765
1868
  }
1766
1869
  };