Skip to content

Commit

Permalink
feat: add memory caching to proofs endpoint (prod) (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
amateima authored Nov 28, 2022
1 parent c0ae36a commit a485736
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/modules/airdrop/services/airdrop-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { BadRequestException, CACHE_MANAGER, Inject, Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { ethers } from "ethers";
import { readFile } from "fs/promises";
import BigNumber from "bignumber.js";
import { DataSource, IsNull, Not, QueryFailedError, Repository } from "typeorm";
import { Cache } from "cache-manager";

import { Deposit } from "../../scraper/model/deposit.entity";
import { CommunityRewards } from "../model/community-rewards.entity";
Expand All @@ -23,6 +24,12 @@ import { MerkleDistributorRecipient } from "../model/merkle-distributor-recipien
import { UserWallet } from "../../user/model/user-wallet.entity";
import { User } from "../../user/model/user.entity";

const getMerkleDistributorProofCacheKey = (address: string, windowIndex: number) =>
`distributor:proof:${address}:${windowIndex}`;
const getMerkleDistributorProofsCacheKey = (address: string, startWindowIndex: number) =>
`distributor:proofs:${address}:${startWindowIndex}`;
const DISTRIBUTOR_PROOFS_CACHE_SECONDS_DURATION = 300;

@Injectable()
export class AirdropService {
private logger = new Logger(AirdropService.name);
Expand All @@ -36,6 +43,7 @@ export class AirdropService {
private userService: UserService,
private appConfig: AppConfig,
private dataSource: DataSource,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}

public async getWelcomeTravellerEligibleWallets() {
Expand Down Expand Up @@ -231,6 +239,10 @@ export class AirdropService {

public async getMerkleDistributorProof(address: string, windowIndex: number, includeDiscord: boolean) {
const checksumAddress = ethers.utils.getAddress(address);
const cacheKey = getMerkleDistributorProofCacheKey(checksumAddress, windowIndex);
let data = await this.cacheManager.get(cacheKey);

if (data) return data;

const query = this.dataSource
.createQueryBuilder(MerkleDistributorRecipient, "recipient")
Expand All @@ -251,7 +263,7 @@ export class AirdropService {
.getOne();
}

return {
data = {
accountIndex: recipient.accountIndex,
address: recipient.address,
amount: recipient.amount,
Expand All @@ -268,10 +280,17 @@ export class AirdropService {
}
: null,
};

await this.cacheManager.set(cacheKey, data, DISTRIBUTOR_PROOFS_CACHE_SECONDS_DURATION);
return data;
}

public async getMerkleDistributorProofs(address: string, startWindowIndex = 0) {
const checksumAddress = ethers.utils.getAddress(address);
const cacheKey = getMerkleDistributorProofsCacheKey(checksumAddress, startWindowIndex);
let data = await this.cacheManager.get(cacheKey);

if (data) return data;

const query = this.dataSource
.createQueryBuilder(MerkleDistributorRecipient, "recipient")
Expand All @@ -282,7 +301,7 @@ export class AirdropService {

if (!recipients) return [];

return recipients.map((recipient) => ({
data = recipients.map((recipient) => ({
accountIndex: recipient.accountIndex,
address: recipient.address,
amount: recipient.amount,
Expand All @@ -293,6 +312,9 @@ export class AirdropService {
ipfsHash: recipient.merkleDistributorWindow.ipfsHash || null,
discord: null,
}));

await this.cacheManager.set(cacheKey, data, DISTRIBUTOR_PROOFS_CACHE_SECONDS_DURATION);
return data;
}

private async processWalletRewardsFile(walletRewardsFile: Express.Multer.File) {
Expand Down

0 comments on commit a485736

Please sign in to comment.