@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
124 lines • 7.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DexEndpoint = void 0;
const klayr_framework_1 = require("klayr-framework");
const cryptography = require("@klayr/cryptography");
const context_1 = require("./stores/context");
const lens_1 = require("./stores/library/lens");
const utils_1 = require("./utils");
const pool_1 = require("./stores/pool");
const position_manager_1 = require("./stores/position_manager");
const periphery_1 = require("./stores/library/periphery");
const config_1 = require("./config");
const schema_1 = require("./schema");
const utils_2 = require("@swaptoshi/utils");
class DexEndpoint extends klayr_framework_1.Modules.BaseEndpoint {
async getConfig(_context) {
const configStore = this.stores.get(config_1.DexGovernableConfig);
const config = await configStore.getConfig(_context);
return utils_2.object.serializer(config, schema_1.getConfigEndpointResponseSchema);
}
async quoteExactInput(context) {
const param = context.params;
(0, utils_1.verifyQuoteExactInputParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const quoter = new lens_1.Quoter(_context, this.stores);
return utils_2.object.serializer(await quoter.quoteExactInput(Buffer.from(param.path, 'hex'), param.amountIn), schema_1.quoteExactInputEndpointResponseSchema);
}
async quoteExactInputSingle(context) {
const param = context.params;
(0, utils_1.verifyQuoteExactInputSingleParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const quoter = new lens_1.Quoter(_context, this.stores);
return utils_2.object.serializer(await quoter.quoteExactInputSingle(param), schema_1.quoteExactInputSingleEndpointResponseSchema);
}
async quoteExactOutput(context) {
const param = context.params;
(0, utils_1.verifyQuoteExactOutputParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const quoter = new lens_1.Quoter(_context, this.stores);
return utils_2.object.serializer(await quoter.quoteExactOutput(Buffer.from(param.path, 'hex'), param.amountOut), schema_1.quoteExactOutputEndpointResponseSchema);
}
async quoteExactOutputSingle(context) {
const param = context.params;
(0, utils_1.verifyQuoteExactOutputSingleParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const quoter = new lens_1.Quoter(_context, this.stores);
return utils_2.object.serializer(await quoter.quoteExactOutputSingle(param), schema_1.quoteExactOutputSingleEndpointResponseSchema);
}
async quotePrice(context) {
const param = context.params;
(0, utils_1.verifyPriceParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const quoter = new lens_1.Quoter(_context, this.stores);
return utils_2.object.serializer(await quoter.quotePrice(Buffer.from(param.path, 'hex')), schema_1.quotePriceEndpointResponseSchema);
}
async getPoolAddressFromCollectionId(context) {
const param = context.params;
(0, utils_1.verifyGetPoolAddressFromCollectionIdParam)(param);
const positionManagerStore = this.stores.get(position_manager_1.PositionManagerStore);
const positionManager = await positionManagerStore.get(context, Buffer.from(param.collectionId, 'hex'));
return { poolAddress: cryptography.address.getKlayr32AddressFromAddress(positionManager.poolAddress) };
}
async getPool(context) {
const param = context.params;
(0, utils_1.verifyGetPoolParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const poolStore = this.stores.get(pool_1.PoolStore);
const pool = await poolStore.getImmutablePool(_context, Buffer.from(param.tokenA, 'hex'), Buffer.from(param.tokenB, 'hex'), param.fee);
return utils_2.object.serializer({
...pool.toJSON(),
address: pool.address,
collectionId: pool.collectionId,
}, schema_1.getPoolEndpointResponseSchema);
}
async getPosition(context) {
const param = context.params;
(0, utils_1.verifyGetPositionParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const positionManagerStore = this.stores.get(position_manager_1.PositionManagerStore);
const positionManager = await positionManagerStore.getImmutablePositionManager(_context, cryptography.address.getAddressFromKlayr32Address(param.poolAddress));
const poolStore = this.stores.get(pool_1.PoolStore);
const { token0, token1, fee } = periphery_1.PoolAddress.decodePoolAddress(cryptography.address.getAddressFromKlayr32Address(param.poolAddress));
const pool = await poolStore.getImmutablePool(_context, token0, token1, fee);
const position = await positionManager.getPositions(param.tokenId);
const [principal0, principal1] = await positionManager.principal(param.tokenId, pool.slot0.sqrtPriceX96);
const [fees0, fees1] = await positionManager.fees(param.tokenId);
return utils_2.object.serializer({
...position,
value: {
principal0,
principal1,
fees0,
fees1,
},
}, schema_1.getPositionEndpointResponseSchema);
}
async getTokenURI(context) {
const param = context.params;
(0, utils_1.verifyGetTokenURIParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const positionManagerStore = this.stores.get(position_manager_1.PositionManagerStore);
const positionManager = await positionManagerStore.getImmutablePositionManager(_context, cryptography.address.getAddressFromKlayr32Address(param.poolAddress));
return utils_2.object.serializer({ tokenURI: await positionManager.tokenURI(param.tokenId) }, schema_1.getTokenURIEndpointResponseSchema);
}
async getMetadata(context) {
const param = context.params;
(0, utils_1.verifyGetMetadataParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const positionManagerStore = this.stores.get(position_manager_1.PositionManagerStore);
const positionManager = await positionManagerStore.getImmutablePositionManager(_context, cryptography.address.getAddressFromKlayr32Address(param.poolAddress));
return utils_2.object.serializer(await positionManager.getMetadata(param.tokenId), schema_1.getMetadataEndpointResponseSchema);
}
async observe(context) {
const param = context.params;
(0, utils_1.verifyObserveParam)(param);
const _context = (0, context_1.endpointSwapContext)(context);
const poolStore = this.stores.get(pool_1.PoolStore);
const key = periphery_1.PoolAddress.decodePoolAddress(cryptography.address.getAddressFromKlayr32Address(param.poolAddress));
const pool = await poolStore.getImmutablePool(_context, key.token0, key.token1, key.fee);
return utils_2.object.serializer(await pool.observe(param.secondsAgos), schema_1.observeEndpointResponseSchema);
}
}
exports.DexEndpoint = DexEndpoint;
//# sourceMappingURL=endpoint.js.map