UNPKG

@swaptoshi/dex-module

Version:

Klayr decentralized exchange (dex) on-chain module

58 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DexSwapFeeConversionMethod = void 0; const codec_1 = require("@klayr/codec"); const fee_conversion_module_1 = require("@swaptoshi/fee-conversion-module"); const context_1 = require("../stores/context"); const lens_1 = require("../stores/library/lens"); const periphery_1 = require("../stores/library/periphery"); const schema_1 = require("../schema"); class DexSwapFeeConversionMethod extends fee_conversion_module_1.BaseFeeConversionMethod { async verifyFeeConversion(context) { const senderFeeBalance = await this.tokenMethod.getAvailableBalance(context, context.transaction.senderAddress, this.feeMethod.getFeeTokenID()); const feeDifference = senderFeeBalance - context.transaction.fee; if (feeDifference < BigInt(0)) { let tokenIn = Buffer.alloc(0); let txAmount = '0'; let params; const ctx = (0, context_1.immutableMethodSwapContext)(context, context.transaction.senderAddress, context.header.timestamp); const quoter = new lens_1.Quoter(ctx, this.stores); switch (context.transaction.command) { case 'exactInput': params = codec_1.codec.decode(schema_1.exactInputCommandSchema, context.transaction.params); [tokenIn] = periphery_1.Path.decodeFirstPool(periphery_1.Path.getFirstPool(params.path)); txAmount = params.amountIn; break; case 'exactOutput': params = codec_1.codec.decode(schema_1.exactOutputCommandSchema, context.transaction.params); [, tokenIn] = periphery_1.Path.decodeFirstPool(periphery_1.Path.getLastPool(params.path)); ({ amountIn: txAmount } = await quoter.quoteExactOutput(params.path, params.amountOut)); break; case 'exactInputSingle': params = codec_1.codec.decode(schema_1.exactInputSingleCommandSchema, context.transaction.params); tokenIn = params.tokenIn; txAmount = params.amountIn; break; case 'exactOutputSingle': params = codec_1.codec.decode(schema_1.exactOutputSingleCommandSchema, context.transaction.params); tokenIn = params.tokenIn; ({ amountIn: txAmount } = await quoter.quoteExactOutputSingle({ ...params, tokenIn: params.tokenIn.toString('hex'), tokenOut: params.tokenOut.toString('hex'), amount: params.amountOut, })); break; default: break; } return { status: fee_conversion_module_1.FeeConversionVerifyStatus.WITH_CONVERSION, payload: { tokenId: tokenIn, txAmount: BigInt(txAmount) }, }; } return { status: fee_conversion_module_1.FeeConversionVerifyStatus.NO_CONVERSION }; } } exports.DexSwapFeeConversionMethod = DexSwapFeeConversionMethod; //# sourceMappingURL=dex_swap.js.map