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

[MEX-527] Tokens memory store service #1540

Open
wants to merge 2 commits into
base: MEX-527-pairs-memory-store
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
23 changes: 23 additions & 0 deletions src/modules/memory-store/entities/global.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ export class GlobalStateSingleton {
public getPairsArray(): PairModel[] {
return Object.values(this.pairsState);
}

public getTokensArray(): EsdtToken[] {
return Object.values(this.tokensState);
}

public getPairsTokens(enabledSwaps: boolean): EsdtToken[] {
const pairAddresses = enabledSwaps
? Object.values(this.pairsState)
.filter((pair) => pair.state === 'Active')
.map((pair) => pair.address)
: Object.values(this.pairsState).map((pair) => pair.address);

let tokenIDs = [];
pairAddresses.forEach((address) => {
tokenIDs.push(
this.pairsEsdtTokens[address].firstTokenID,
this.pairsEsdtTokens[address].secondTokenID,
);
});

tokenIDs = [...new Set(tokenIDs)];
return tokenIDs.map((tokenID) => this.tokensState[tokenID]);
}
}

export const GlobalState = new GlobalStateSingleton();
13 changes: 11 additions & 2 deletions src/modules/memory-store/memory.store.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Module } from '@nestjs/common';
import { MemoryStoreFactoryService } from './services/memory.store.factory.service';
import { PairMemoryStoreService } from './services/pair.memory.store.service';
import { TokenMemoryStoreService } from './services/token.memory.store.service';

@Module({
providers: [MemoryStoreFactoryService, PairMemoryStoreService],
exports: [MemoryStoreFactoryService, PairMemoryStoreService],
providers: [
MemoryStoreFactoryService,
PairMemoryStoreService,
TokenMemoryStoreService,
],
exports: [
MemoryStoreFactoryService,
PairMemoryStoreService,
TokenMemoryStoreService,
],
})
export class MemoryStoreModule {}
22 changes: 20 additions & 2 deletions src/modules/memory-store/services/memory.store.factory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ import { IMemoryStoreService } from './interfaces';
import { PairMemoryStoreService } from './pair.memory.store.service';
import { PairModel } from 'src/modules/pair/models/pair.model';
import { PairsResponse } from 'src/modules/pair/models/pairs.response';
import { TokenMemoryStoreService } from './token.memory.store.service';
import { EsdtToken } from 'src/modules/tokens/models/esdtToken.model';
import { TokensResponse } from 'src/modules/tokens/models/tokens.response';

@Injectable()
export class MemoryStoreFactoryService {
private queryMapping: Record<string, IMemoryStoreService<any, any>> = {};

constructor(private readonly pairMemoryStore: PairMemoryStoreService) {
constructor(
private readonly pairMemoryStore: PairMemoryStoreService,
private readonly tokenMemoryStore: TokenMemoryStoreService,
) {
const pairQueries = Object.keys(
this.pairMemoryStore.getTargetedQueries(),
);
const tokenQueries = Object.keys(
this.tokenMemoryStore.getTargetedQueries(),
);

for (const query of pairQueries) {
this.queryMapping[query] = this
Expand All @@ -20,10 +29,19 @@ export class MemoryStoreFactoryService {
PairsResponse
>;
}
for (const query of tokenQueries) {
this.queryMapping[query] = this
.tokenMemoryStore as IMemoryStoreService<
EsdtToken[],
TokensResponse
>;
}
}

isReady(): boolean {
return this.pairMemoryStore.isReady();
return (
this.pairMemoryStore.isReady() && this.tokenMemoryStore.isReady()
);
}

getTargetedQueryNames(): string[] {
Expand Down
Loading