Skip to content

Commit

Permalink
Merge pull request #778 from ElrondNetwork/development
Browse files Browse the repository at this point in the history
Development to main
  • Loading branch information
danielailie authored Dec 20, 2022
2 parents 1c9f949 + 8cf43ca commit e3e2ef1
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 162 deletions.
12 changes: 11 additions & 1 deletion src/common/services/caching/entities/cache.info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,19 @@ export class CacheInfo {
ttl: 5 * TimeConstants.oneMinute,
};

static AllDexTokens: CacheInfo = {
key: 'allDexTokens',
ttl: 10 * TimeConstants.oneMinute,
};

static AllApiTokens: CacheInfo = {
key: 'allApiTokens',
ttl: 10 * TimeConstants.oneMinute,
};

static AllTokens: CacheInfo = {
key: 'allTokens',
ttl: 10 * TimeConstants.oneMinute,
ttl: TimeConstants.oneMinute,
};

static EgldToken: CacheInfo = {
Expand Down
39 changes: 9 additions & 30 deletions src/common/services/elrond-communication/elrond-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,41 +616,20 @@ export class ElrondApiService {
);
}

async getAllMexTokens(): Promise<Token[]> {
async getAllDexTokens(): Promise<Token[]> {
const allTokens = await this.doGetGeneric(
this.getAllMexTokens.name,
this.getAllDexTokens.name,
'mex/tokens?size=10000',
);
return allTokens.map((t) => Token.fromElrondApiToken(t));
return allTokens.map((t) => Token.fromElrondApiDexToken(t));
}

async getAllMexTokensWithDecimals(): Promise<Token[]> {
const batchSize = constants.getTokensFromApiBatchSize;
const tokens: Token[] = await this.getAllMexTokens();

const tokenChunks = BatchUtils.splitArrayIntoChunks(tokens, batchSize);
for (const tokenChunk of tokenChunks) {
const identifiersParam = tokenChunk.map((t) => t.identifier).join(',');
const tokensWithDecimals = await this.doGetGeneric(
this.getAllMexTokensWithDecimals.name,
`tokens?identifiers=${identifiersParam}&fields=identifier,decimals&size=${tokenChunk.length}`,
);

if (!tokensWithDecimals) {
continue;
}

for (const tokenWithDecimals of tokensWithDecimals) {
const token = tokens.find(
(t) => t.identifier === tokenWithDecimals.identifier,
);
if (token) {
token.decimals = tokenWithDecimals.decimals;
}
}
}

return tokens;
async getAllTokens(): Promise<Token[]> {
const allTokens = await this.doGetGeneric(
this.getAllTokens.name,
'tokens?size=10000&fields=identifier,name,ticker,decimals',
);
return allTokens.map((t) => Token.fromElrondApiToken(t));
}

async getEgldPriceFromEconomics(): Promise<string> {
Expand Down
14 changes: 10 additions & 4 deletions src/common/services/elrond-communication/elrond-tools.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export class ElrondToolsService {
});
}

async getEgldHistoricalPrice(isoDateOnly: string): Promise<string> {
async getEgldHistoricalPrice(
isoDateOnly: string,
): Promise<string | undefined> {
return await this.getTokenPriceByTimestamp(
elrondConfig.wegld,
elrondConfig.usdc,
Expand All @@ -37,12 +39,15 @@ export class ElrondToolsService {
token: string,
isoDateOnly: string,
cachedEgldPriceUsd?: string,
): Promise<string> {
): Promise<string | undefined> {
const priceInEgld = await this.getTokenPriceByTimestamp(
token,
elrondConfig.wegld,
isoDateOnly,
);
if (!priceInEgld) {
return;
}
const egldPriceUsd =
cachedEgldPriceUsd ?? (await this.getEgldHistoricalPrice(isoDateOnly));
return new BigNumber(priceInEgld).multipliedBy(egldPriceUsd).toFixed();
Expand All @@ -68,20 +73,21 @@ export class ElrondToolsService {
firstToken: string,
secondToken: string,
isoDateOnly: string,
): Promise<string> {
): Promise<string | undefined> {
const query = this.getTokenPriceByTimestampQuery(
firstToken,
secondToken,
isoDateOnly,
);
const res = await this.doPost(this.getTokenPriceByTimestamp.name, query);
return res.data.trading.pair.price[0].last.toFixed(20);
return res?.data?.trading?.pair?.price?.[0]?.last?.toFixed(20) ?? undefined;
}

private async getConfig(): Promise<ApiSettings> {
const accessTokenInfo = await this.nativeAuthSigner.getToken();
return {
authorization: `Bearer ${accessTokenInfo.token}`,
timeout: 500,
};
}

Expand Down
12 changes: 10 additions & 2 deletions src/common/services/elrond-communication/models/Token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ export class Token {
Object.assign(this, init);
}

static fromElrondApiToken(token: any): Token {
static fromElrondApiDexToken(token: any): Token {
return new Token({
identifier: token.id,
symbol: token.symbol,
name: token.name,
priceUsd: token.price,
decimals: token.decimals ?? undefined,
});
}

static fromElrondApiToken(token: any): Token {
return new Token({
identifier: token.identifier,
symbol: token.ticker,
name: token.name,
decimals: token.decimals,
});
}
}
23 changes: 19 additions & 4 deletions src/crons/cache.warmer/tokens.warmer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ export class TokensWarmerService {
);
}

@Cron(CronExpression.EVERY_MINUTE)
@Cron(CronExpression.EVERY_10_MINUTES)
async updateTokens() {
await Locker.lock(
'MEX Tokens invalidations',
'Tokens invalidations',
async () => {
const tokens =
await this.elrondApiService.getAllMexTokensWithDecimals();
const tokens = await this.elrondApiService.getAllTokens();
await this.invalidateKey(
CacheInfo.AllTokens.key,
tokens,
Expand All @@ -38,6 +37,22 @@ export class TokensWarmerService {
);
}

@Cron(CronExpression.EVERY_MINUTE)
async updateDexTokens() {
await Locker.lock(
'DEX Tokens invalidations',
async () => {
const tokens = await this.elrondApiService.getAllDexTokens();
await this.invalidateKey(
CacheInfo.AllDexTokens.key,
tokens,
CacheInfo.AllDexTokens.ttl,
);
},
true,
);
}

@Cron(CronExpression.EVERY_MINUTE)
async updateEgldTokens() {
await Locker.lock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class AssetsHistoryNftEventService {
});
}
}
case NftEventEnum.ESDTNFTBurn:
case NftEventEnum.ESDTNFTUpdateAttributes: {
break;
}
default: {
return this.mapNftEventLog(nonce, transferEvent.identifier, mainEvent);
}
Expand Down
9 changes: 9 additions & 0 deletions src/modules/assets/assets-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ export class AssetsGetterService {
offset: number,
sortByRank?: Sort,
): Promise<CollectionType<Asset>> {
for (let i = 0; i < traits.length; i++) {
const multipleAttributesPerTraitFilter = traits.find(
(t) => t.name === traits[i].name && t.value !== traits[i].value,
);
if (multipleAttributesPerTraitFilter) {
return new CollectionType({ items: [], count: 0 });
}
}

const [nfts, count] =
await this.nftTraitsService.getCollectionNftsByTraitsAndRanks(
collection,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/assets/content.validation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { VerifyContentService } from './verify-content.service';
export class ContentValidation {
private status: boolean = true;
private verifyContentService: VerifyContentService;
constructor(private logger: Logger) {
private readonly logger: Logger;
constructor() {
this.logger = new Logger(ContentValidation.name);
this.verifyContentService = new VerifyContentService();
}

Expand Down
22 changes: 8 additions & 14 deletions src/modules/auctions/auctions-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,20 +681,14 @@ export class AuctionsGetterService {
marketplaceKey?: string,
collectionIdentifier?: string,
): Promise<Token[]> {
const [currentPaymentTokenIds, allMexTokens, egldToken, lkmexToken] =
await Promise.all([
this.persistenceService.getCurrentPaymentTokenIdsWithCounts(
marketplaceKey,
collectionIdentifier,
),
this.usdPriceService.getCachedMexTokensWithDecimals(),
this.usdPriceService.getToken(elrondConfig.egld),
this.usdPriceService.getToken(elrondConfig.lkmex),
]);

const allTokens: Token[] = allMexTokens
.concat(egldToken)
.concat(lkmexToken);
const [currentPaymentTokenIds, allTokens] = await Promise.all([
this.persistenceService.getCurrentPaymentTokenIdsWithCounts(
marketplaceKey,
collectionIdentifier,
),
this.usdPriceService.getAllCachedTokens(),
]);

let mappedTokens = [];
for (const payment of currentPaymentTokenIds) {
const token = allTokens.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class CollectionsNftsRedisHandler extends BaseCollectionsAssetsRedisHandl
let nftsGroupByCollection: { [key: string]: any[] } = {};

for (const nfts of nftsResponse) {
nftsGroupByCollection[nfts[0]?.collection] = nfts;
nftsGroupByCollection[nfts?.[0]?.collection] = nfts;
}
return nftsGroupByCollection;
}
Expand Down
8 changes: 7 additions & 1 deletion src/modules/usdPrice/usd-price.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { ElrondCommunicationModule } from 'src/common/services/elrond-communicat
import { UsdPriceService } from './usd-price.service';
import { UsdPriceRedisHandler } from './usd-price.redis-handler';
import { UsdPriceResolver } from './usd-price.resolver';
import { UsdTokenPriceResolver } from './usd-token-price.resolver';

@Module({
providers: [UsdPriceResolver, UsdPriceRedisHandler, UsdPriceService],
providers: [
UsdPriceResolver,
UsdTokenPriceResolver,
UsdPriceRedisHandler,
UsdPriceService,
],
imports: [ElrondCommunicationModule],
exports: [UsdPriceService],
})
Expand Down
Loading

0 comments on commit e3e2ef1

Please sign in to comment.