@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/browser/index.js +6 -6
- package/dist/browser/index.js.map +1 -1
- package/dist/index.cjs +120 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +125 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/dist/index.d.cts
CHANGED
|
@@ -788,7 +788,12 @@ declare class Quoting {
|
|
|
788
788
|
private getSingleQuote;
|
|
789
789
|
private getSingleQuoteExactInput;
|
|
790
790
|
private getSingleQuoteExactOutput;
|
|
791
|
-
private
|
|
791
|
+
private sendGetCompositePoolRequest;
|
|
792
|
+
/**
|
|
793
|
+
* Converts response data from GetCompositePool API into a proper CompositePoolDto
|
|
794
|
+
* Based on the working implementation in quote-local.mjs
|
|
795
|
+
*/
|
|
796
|
+
private createCompositePoolDtoFromResponse;
|
|
792
797
|
}
|
|
793
798
|
|
|
794
799
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -788,7 +788,12 @@ declare class Quoting {
|
|
|
788
788
|
private getSingleQuote;
|
|
789
789
|
private getSingleQuoteExactInput;
|
|
790
790
|
private getSingleQuoteExactOutput;
|
|
791
|
-
private
|
|
791
|
+
private sendGetCompositePoolRequest;
|
|
792
|
+
/**
|
|
793
|
+
* Converts response data from GetCompositePool API into a proper CompositePoolDto
|
|
794
|
+
* Based on the working implementation in quote-local.mjs
|
|
795
|
+
*/
|
|
796
|
+
private createCompositePoolDtoFromResponse;
|
|
792
797
|
}
|
|
793
798
|
|
|
794
799
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1322,6 +1322,16 @@ var Positions = class {
|
|
|
1322
1322
|
};
|
|
1323
1323
|
|
|
1324
1324
|
// src/classes/quoting.ts
|
|
1325
|
+
import { createValidDTO, TokenBalance, TokenClassKey } from "@gala-chain/api";
|
|
1326
|
+
import {
|
|
1327
|
+
CompositePoolDto,
|
|
1328
|
+
DexFeePercentageTypes,
|
|
1329
|
+
GetCompositePoolDto,
|
|
1330
|
+
Pool,
|
|
1331
|
+
quoteExactAmount,
|
|
1332
|
+
QuoteExactAmountDto,
|
|
1333
|
+
TickData
|
|
1334
|
+
} from "@gala-chain/dex";
|
|
1325
1335
|
import BigNumber4 from "bignumber.js";
|
|
1326
1336
|
|
|
1327
1337
|
// src/types/fees.ts
|
|
@@ -1410,14 +1420,59 @@ var Quoting = class {
|
|
|
1410
1420
|
}
|
|
1411
1421
|
async getSingleQuote(tokenIn, tokenOut, fee, amount, isExactInput) {
|
|
1412
1422
|
const tokenInClass = parseTokenClassKey(tokenIn);
|
|
1423
|
+
const tokenInClassKey = await createValidDTO(TokenClassKey, tokenInClass);
|
|
1413
1424
|
const tokenOutClass = parseTokenClassKey(tokenOut);
|
|
1425
|
+
const tokenOutClassKey = await createValidDTO(TokenClassKey, tokenOutClass);
|
|
1414
1426
|
const ordering = getTokenOrdering(tokenInClass, tokenOutClass, false);
|
|
1427
|
+
const token0ClassKey = await createValidDTO(
|
|
1428
|
+
TokenClassKey,
|
|
1429
|
+
ordering.token0
|
|
1430
|
+
);
|
|
1431
|
+
const token1ClassKey = await createValidDTO(
|
|
1432
|
+
TokenClassKey,
|
|
1433
|
+
ordering.token1
|
|
1434
|
+
);
|
|
1415
1435
|
const formattedAmount = isExactInput ? BigNumber4(amount).toFixed() : BigNumber4(amount).multipliedBy(-1).toFixed();
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1436
|
+
let feeType;
|
|
1437
|
+
switch (fee) {
|
|
1438
|
+
case 500 /* PERCENT_00_05 */:
|
|
1439
|
+
feeType = DexFeePercentageTypes.FEE_0_05_PERCENT;
|
|
1440
|
+
break;
|
|
1441
|
+
case 3e3 /* PERCENT_00_30 */:
|
|
1442
|
+
feeType = DexFeePercentageTypes.FEE_0_3_PERCENT;
|
|
1443
|
+
break;
|
|
1444
|
+
case 1e4 /* PERCENT_01_00 */:
|
|
1445
|
+
feeType = DexFeePercentageTypes.FEE_1_PERCENT;
|
|
1446
|
+
break;
|
|
1447
|
+
default:
|
|
1448
|
+
throw new Error(`Invalid fee tier: ${fee}`);
|
|
1449
|
+
}
|
|
1450
|
+
const zeroForOne = compareTokens(tokenIn, tokenOut) < 0;
|
|
1451
|
+
const getCompositePoolDto = new GetCompositePoolDto(
|
|
1452
|
+
zeroForOne ? tokenInClassKey : tokenOutClassKey,
|
|
1453
|
+
zeroForOne ? tokenOutClassKey : tokenInClassKey,
|
|
1454
|
+
feeType
|
|
1455
|
+
);
|
|
1456
|
+
const compositePoolResponse = await this.sendGetCompositePoolRequest(getCompositePoolDto);
|
|
1457
|
+
const compositePool = this.createCompositePoolDtoFromResponse(compositePoolResponse);
|
|
1458
|
+
const quoteDto = new QuoteExactAmountDto(
|
|
1459
|
+
token0ClassKey,
|
|
1460
|
+
token1ClassKey,
|
|
1461
|
+
feeType,
|
|
1462
|
+
BigNumber4(formattedAmount),
|
|
1463
|
+
zeroForOne,
|
|
1464
|
+
compositePool
|
|
1465
|
+
);
|
|
1466
|
+
const context = {
|
|
1467
|
+
callingUser: "eth|123...abc",
|
|
1468
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1469
|
+
stub: null,
|
|
1470
|
+
clientIdentity: {
|
|
1471
|
+
getMSPID: () => "UsersOrg"
|
|
1472
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1475
|
+
const response = await quoteExactAmount(context, quoteDto);
|
|
1421
1476
|
const tokenInAmount = ordering.zeroForOne ? response.amount0 : response.amount1;
|
|
1422
1477
|
const tokenOutAmount = ordering.zeroForOne ? response.amount1 : response.amount0;
|
|
1423
1478
|
let currentPrice = response.currentSqrtPrice.pow(2);
|
|
@@ -1446,20 +1501,76 @@ var Quoting = class {
|
|
|
1446
1501
|
async getSingleQuoteExactOutput(tokenIn, tokenOut, amountOut, fee) {
|
|
1447
1502
|
return this.getSingleQuote(tokenIn, tokenOut, fee, amountOut, false);
|
|
1448
1503
|
}
|
|
1449
|
-
async
|
|
1504
|
+
async sendGetCompositePoolRequest(getCompositePoolDto) {
|
|
1450
1505
|
const response = await this.httpClient.sendPostRequest(
|
|
1451
1506
|
this.gatewayBaseUrl,
|
|
1452
1507
|
this.dexContractBasePath,
|
|
1453
|
-
|
|
1454
|
-
|
|
1508
|
+
"/GetCompositePool",
|
|
1509
|
+
getCompositePoolDto
|
|
1455
1510
|
);
|
|
1456
1511
|
const responseBody = response;
|
|
1457
|
-
return
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1512
|
+
return responseBody.Data;
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* Converts response data from GetCompositePool API into a proper CompositePoolDto
|
|
1516
|
+
* Based on the working implementation in quote-local.mjs
|
|
1517
|
+
*/
|
|
1518
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1519
|
+
createCompositePoolDtoFromResponse(responseData) {
|
|
1520
|
+
const pool = new Pool(
|
|
1521
|
+
responseData.pool.token0,
|
|
1522
|
+
responseData.pool.token1,
|
|
1523
|
+
responseData.pool.token0ClassKey,
|
|
1524
|
+
responseData.pool.token1ClassKey,
|
|
1525
|
+
responseData.pool.fee,
|
|
1526
|
+
new BigNumber4(responseData.pool.sqrtPrice),
|
|
1527
|
+
responseData.pool.protocolFees
|
|
1528
|
+
);
|
|
1529
|
+
pool.bitmap = responseData.pool.bitmap;
|
|
1530
|
+
pool.grossPoolLiquidity = new BigNumber4(responseData.pool.grossPoolLiquidity);
|
|
1531
|
+
pool.liquidity = new BigNumber4(responseData.pool.liquidity);
|
|
1532
|
+
pool.feeGrowthGlobal0 = new BigNumber4(responseData.pool.feeGrowthGlobal0);
|
|
1533
|
+
pool.feeGrowthGlobal1 = new BigNumber4(responseData.pool.feeGrowthGlobal1);
|
|
1534
|
+
pool.protocolFeesToken0 = new BigNumber4(responseData.pool.protocolFeesToken0);
|
|
1535
|
+
pool.protocolFeesToken1 = new BigNumber4(responseData.pool.protocolFeesToken1);
|
|
1536
|
+
pool.tickSpacing = responseData.pool.tickSpacing;
|
|
1537
|
+
pool.maxLiquidityPerTick = new BigNumber4(responseData.pool.maxLiquidityPerTick);
|
|
1538
|
+
const tickDataMap = {};
|
|
1539
|
+
Object.keys(responseData.tickDataMap).forEach((tickKey) => {
|
|
1540
|
+
const tickData = responseData.tickDataMap[tickKey];
|
|
1541
|
+
tickDataMap[tickKey] = new TickData(tickData.poolHash, tickData.tick);
|
|
1542
|
+
tickDataMap[tickKey].initialised = tickData.initialised;
|
|
1543
|
+
tickDataMap[tickKey].liquidityNet = new BigNumber4(tickData.liquidityNet);
|
|
1544
|
+
tickDataMap[tickKey].liquidityGross = new BigNumber4(tickData.liquidityGross);
|
|
1545
|
+
tickDataMap[tickKey].feeGrowthOutside0 = new BigNumber4(tickData.feeGrowthOutside0);
|
|
1546
|
+
tickDataMap[tickKey].feeGrowthOutside1 = new BigNumber4(tickData.feeGrowthOutside1);
|
|
1547
|
+
});
|
|
1548
|
+
const token0Balance = new TokenBalance({
|
|
1549
|
+
owner: responseData.token0Balance.owner,
|
|
1550
|
+
collection: responseData.token0Balance.collection,
|
|
1551
|
+
category: responseData.token0Balance.category,
|
|
1552
|
+
type: responseData.token0Balance.type,
|
|
1553
|
+
additionalKey: responseData.token0Balance.additionalKey
|
|
1554
|
+
});
|
|
1555
|
+
token0Balance.quantity = new BigNumber4(responseData.token0Balance.quantity);
|
|
1556
|
+
const token1Balance = new TokenBalance({
|
|
1557
|
+
owner: responseData.token1Balance.owner,
|
|
1558
|
+
collection: responseData.token1Balance.collection,
|
|
1559
|
+
category: responseData.token1Balance.category,
|
|
1560
|
+
type: responseData.token1Balance.type,
|
|
1561
|
+
additionalKey: responseData.token1Balance.additionalKey
|
|
1562
|
+
});
|
|
1563
|
+
token1Balance.quantity = new BigNumber4(responseData.token1Balance.quantity);
|
|
1564
|
+
return new CompositePoolDto(
|
|
1565
|
+
pool,
|
|
1566
|
+
tickDataMap,
|
|
1567
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1568
|
+
token0Balance,
|
|
1569
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1570
|
+
token1Balance,
|
|
1571
|
+
responseData.token0Decimals,
|
|
1572
|
+
responseData.token1Decimals
|
|
1573
|
+
);
|
|
1463
1574
|
}
|
|
1464
1575
|
};
|
|
1465
1576
|
|