Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SERVICES-2655] Migrate code from mx-exchange-analytics-indexer-repo #1515

Open
wants to merge 3 commits into
base: feat/analytics-indexer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/modules/analytics-indexer/analytics.indexer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Module } from '@nestjs/common';
import { PairModule } from '../pair/pair.module';
import { RouterModule } from '../router/router.module';
import { PriceDiscoveryModule } from '../price-discovery/price.discovery.module';
import { TokenModule } from '../tokens/token.module';
import { ElasticSearchModule } from 'src/services/elastic-search/elastic.search.module';
import { IndexerService } from './services/indexer.service';
import { IndexerStateService } from './services/indexer.state.service';
import { IndexerPairService } from './services/indexer.pair.service';
import { IndexerRouterService } from './services/indexer.router.service';
import { IndexerTokenService } from './services/indexer.token.service';
import { IndexerPriceDiscoveryService } from './services/indexer.price.discovery.service';
import { IndexerSwapHandlerService } from './services/event-handlers/indexer.swap.handler.service';
import { IndexerLiquidityHandlerService } from './services/event-handlers/indexer.liquidity.handler.service';
import { IndexerPriceDiscoveryHandlerService } from './services/event-handlers/indexer.price.discovery.handler.service';

@Module({
imports: [
PairModule,
RouterModule,
TokenModule,
PriceDiscoveryModule,
ElasticSearchModule,
],
providers: [
IndexerService,
IndexerStateService,
IndexerPairService,
IndexerRouterService,
IndexerTokenService,
IndexerPriceDiscoveryService,
IndexerSwapHandlerService,
IndexerLiquidityHandlerService,
IndexerPriceDiscoveryHandlerService,
],
exports: [IndexerService],
})
export class AnalyticsIndexerModule {}
14 changes: 14 additions & 0 deletions src/modules/analytics-indexer/entities/indexer.event.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export enum IndexerEventIdentifiers {
SWAP_FIXED_INPUT = 'swapTokensFixedInput',
SWAP_FIXED_OUTPUT = 'swapTokensFixedOutput',
ADD_LIQUIDITY = 'addLiquidity',
REMOVE_LIQUIDITY = 'removeLiquidity',
PRICE_DISCOVERY_DEPOSIT = 'deposit',
PRICE_DISCOVERY_WITHDRAW = 'withdraw',
}

export enum IndexerEventTypes {
SWAP_EVENTS = 'SWAP_EVENTS',
LIQUIDITY_EVENTS = 'LIQUIDITY_EVENTS',
PRICE_DISCOVERY_EVENTS = 'PRICE_DISCOVERY_EVENTS',
}
12 changes: 12 additions & 0 deletions src/modules/analytics-indexer/entities/pair.metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EsdtToken } from 'src/modules/tokens/models/esdtToken.model';

export class PairMetadata {
address: string;
firstToken: EsdtToken;
secondToken: EsdtToken;
totalFeePercent: number;

constructor(init?: Partial<PairMetadata>) {
Object.assign(this, init);
}
}
11 changes: 11 additions & 0 deletions src/modules/analytics-indexer/entities/price.discovery.metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EsdtToken } from 'src/modules/tokens/models/esdtToken.model';

export class PriceDiscoveryMetadata {
address: string;
launchedToken: EsdtToken;
acceptedToken: EsdtToken;

constructor(init?: Partial<PriceDiscoveryMetadata>) {
Object.assign(this, init);
}
}
13 changes: 13 additions & 0 deletions src/modules/analytics-indexer/global.state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class PairState {
firstTokenID: string;
secondTokenID: string;
firstTokenReserves: string;
secondTokenReserves: string;
liquidityPoolSupply: string;
}

export class GlobalStateSingleton {
public pairsState: { [key: string]: PairState } = {};
}

export const GlobalState = new GlobalStateSingleton();
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Injectable } from '@nestjs/common';
import {
AddLiquidityEvent,
RemoveLiquidityEvent,
} from '@multiversx/sdk-exchange';
import { computeValueUSD } from 'src/utils/token.converters';
import { GlobalState } from '../../global.state';
import { IndexerStateService } from '../indexer.state.service';
import { IndexerPairService } from '../indexer.pair.service';
import { IndexerRouterService } from '../indexer.router.service';
import { IndexerTokenService } from '../indexer.token.service';

@Injectable()
export class IndexerLiquidityHandlerService {
constructor(
private readonly stateService: IndexerStateService,
private readonly pairService: IndexerPairService,
private readonly routerService: IndexerRouterService,
private readonly tokenService: IndexerTokenService,
) {}

public handleLiquidityEvent(
event: AddLiquidityEvent | RemoveLiquidityEvent,
): [any[], number] {
const pair = this.stateService.getPairMetadata(event.address);
if (!pair) {
return [[], 0];
}

this.updatePairStateForLiquidityEvent(event);

const firstTokenPriceUSD =
this.tokenService.computeTokenPriceDerivedUSD(
event.getFirstToken().tokenID,
);
const secondTokenPriceUSD =
this.tokenService.computeTokenPriceDerivedUSD(
event.getSecondToken().tokenID,
);
const newTotalLockedValueUSD =
this.routerService.computeTotalLockedValueUSD();

const data = [];
data['factory'] = {
totalLockedValueUSD: newTotalLockedValueUSD.toFixed(),
};
const firstTokenLockedValueUSD = computeValueUSD(
event.getFirstTokenReserves().toFixed(),
pair.firstToken.decimals,
firstTokenPriceUSD,
);
const secondTokenLockedValueUSD = computeValueUSD(
event.getSecondTokenReserves().toFixed(),
pair.secondToken.decimals,
secondTokenPriceUSD,
);
const lockedValueUSD = firstTokenLockedValueUSD.plus(
secondTokenLockedValueUSD,
);

data[event.address] = {
firstTokenLocked: event.getFirstTokenReserves().toFixed(),
firstTokenLockedValueUSD: firstTokenLockedValueUSD.toFixed(),
secondTokenLocked: event.getSecondTokenReserves().toFixed(),
secondTokenLockedValueUSD: secondTokenLockedValueUSD.toFixed(),
lockedValueUSD: lockedValueUSD.toFixed(),
liquidity: event.getLiquidityPoolSupply().toFixed(),
};

const firstTokenTotalLockedValue =
this.pairService.getTokenTotalLockedValue(
pair.firstToken.identifier,
);
const secondTokenTotalLockedValue =
this.pairService.getTokenTotalLockedValue(
pair.secondToken.identifier,
);

data[pair.firstToken.identifier] = {
lockedValue: firstTokenTotalLockedValue,
lockedValueUSD: computeValueUSD(
firstTokenTotalLockedValue,
pair.firstToken.decimals,
firstTokenPriceUSD,
).toFixed(),
};
data[pair.secondToken.identifier] = {
lockedValue: secondTokenTotalLockedValue,
lockedValueUSD: computeValueUSD(
secondTokenTotalLockedValue,
pair.secondToken.decimals,
secondTokenPriceUSD,
).toFixed(),
};

return [data, event.getTimestamp().toNumber()];
}

private updatePairStateForLiquidityEvent(
event: AddLiquidityEvent | RemoveLiquidityEvent,
): void {
GlobalState.pairsState[event.address] = {
firstTokenID: event.getFirstToken().tokenID,
secondTokenID: event.getSecondToken().tokenID,
firstTokenReserves: event.getFirstTokenReserves().toString(),
secondTokenReserves: event.getSecondTokenReserves().toString(),
liquidityPoolSupply: event.getLiquidityPoolSupply().toString(),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { DepositEvent, WithdrawEvent } from '@multiversx/sdk-exchange';
import { Injectable } from '@nestjs/common';
import { IndexerPriceDiscoveryService } from '../indexer.price.discovery.service';

@Injectable()
export class IndexerPriceDiscoveryHandlerService {
constructor(
private readonly priceDiscoveryService: IndexerPriceDiscoveryService,
) {}

handlePriceDiscoveryEvent(
event: DepositEvent | WithdrawEvent,
): [any[], number] {
const [
priceDiscoveryAddress,
launchedTokenAmount,
acceptedTokenAmount,
launchedTokenPrice,
] = [
event.getAddress(),
event.launchedTokenAmount.toFixed(),
event.acceptedTokenAmount.toFixed(),
event.launchedTokenPrice,
];

const acceptedTokenPrice =
this.priceDiscoveryService.computeAcceptedTokenPrice(
priceDiscoveryAddress,
event,
);
const launchedTokenPriceUSD =
this.priceDiscoveryService.computeLaunchedTokenPriceUSD(
priceDiscoveryAddress,
event,
);

const data = [];
const timestamp = event.getTopics().toJSON().timestamp;
data[priceDiscoveryAddress] = {
launchedTokenAmount,
acceptedTokenAmount,
launchedTokenPrice,
acceptedTokenPrice,
launchedTokenPriceUSD,
};

return [data, timestamp];
}
}
Loading
Loading