Skip to content

Commit

Permalink
MEX-410: imporve position creator compute swap method
Browse files Browse the repository at this point in the history
Signed-off-by: Claudiu Lataretu <[email protected]>
  • Loading branch information
claudiulataretu committed Nov 27, 2023
1 parent eb5d78d commit 497a860
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/modules/position-creator/services/position.creator.compute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TypedValue } from '@multiversx/sdk-core/out';
import { EsdtTokenPayment } from '@multiversx/sdk-exchange';
import { PerformanceProfiler } from '@multiversx/sdk-nestjs-monitoring';
import { Injectable } from '@nestjs/common';
import BigNumber from 'bignumber.js';
import { SWAP_TYPE } from 'src/modules/auto-router/models/auto-route.model';
Expand All @@ -8,7 +9,6 @@ import { AutoRouterTransactionService } from 'src/modules/auto-router/services/a
import { PairAbiService } from 'src/modules/pair/services/pair.abi.service';
import { PairService } from 'src/modules/pair/services/pair.service';
import { RouterAbiService } from 'src/modules/router/services/router.abi.service';
import { RouterService } from 'src/modules/router/services/router.service';

export type PositionCreatorSingleTokenPairInput = {
swapRouteArgs: TypedValue[];
Expand All @@ -22,12 +22,12 @@ export class PositionCreatorComputeService {
private readonly pairAbi: PairAbiService,
private readonly pairService: PairService,
private readonly routerAbi: RouterAbiService,
private readonly routerService: RouterService,
private readonly autoRouterService: AutoRouterService,
private readonly autoRouterTransaction: AutoRouterTransactionService,
) {}

async computeSwap(
pairAddress: string,
fromTokenID: string,
toTokenID: string,
amount: string,
Expand All @@ -36,20 +36,8 @@ export class PositionCreatorComputeService {
return new BigNumber(amount);
}

const pairs = await this.routerService.getAllPairs(0, 1, {
address: null,
issuedLpToken: true,
firstTokenID: fromTokenID,
secondTokenID: toTokenID,
state: 'Active',
});

if (pairs.length === 0) {
throw new Error('Pair not found');
}

const amountOut = await this.pairService.getAmountOut(
pairs[0].address,
pairAddress,
fromTokenID,
amount,
);
Expand All @@ -74,13 +62,17 @@ export class PositionCreatorComputeService {
? firstTokenID
: secondTokenID;

const profiler = new PerformanceProfiler();

const swapRoute = await this.autoRouterService.swap({
tokenInID: payment.tokenIdentifier,
amountIn: payment.amount,
tokenOutID: swapToTokenID,
tolerance,
});

profiler.stop('swap route', true);

const halfPayment = new BigNumber(swapRoute.amountOut)
.dividedBy(2)
.integerValue()
Expand All @@ -92,11 +84,13 @@ export class PositionCreatorComputeService {

const [amount0, amount1] = await Promise.all([
await this.computeSwap(
pairAddress,
swapRoute.tokenOutID,
firstTokenID,
halfPayment,
),
await this.computeSwap(
pairAddress,
swapRoute.tokenOutID,
secondTokenID,
remainingPayment,
Expand Down

0 comments on commit 497a860

Please sign in to comment.