-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from 0LNetworkCommunity/feat/accounts
Feat: New Accounts Page
- Loading branch information
Showing
72 changed files
with
2,502 additions
and
1,755 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Field, ObjectType, Int, Float } from "@nestjs/graphql"; | ||
|
||
export interface CumulativeShareInput { | ||
amount: number; | ||
percentage: number; | ||
} | ||
|
||
@ObjectType() | ||
export class CumulativeShare { | ||
@Field(() => Float) | ||
public amount: number; | ||
|
||
@Field(() => Float) | ||
public percentage: number; | ||
|
||
public constructor(input: CumulativeShareInput) { | ||
this.amount = input.amount; | ||
this.percentage = input.percentage; | ||
} | ||
} | ||
|
||
export interface TopAccountInput { | ||
rank: number; | ||
address: string; | ||
publicName: string; | ||
balance: number; | ||
cumulativeShare: CumulativeShare; | ||
} | ||
|
||
@ObjectType() | ||
export class TopAccount { | ||
@Field(() => Int) | ||
public rank: number; | ||
|
||
@Field() | ||
public address: string; | ||
|
||
@Field() | ||
public publicName: string; | ||
|
||
@Field(() => Float) | ||
public balance: number; | ||
|
||
@Field(() => CumulativeShare) | ||
public cumulativeShare: CumulativeShare; | ||
|
||
public constructor(input: TopAccountInput) { | ||
this.rank = input.rank; | ||
this.address = input.address; | ||
this.publicName = input.publicName; | ||
this.balance = input.balance; | ||
this.cumulativeShare = input.cumulativeShare; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { InjectQueue, Processor, WorkerHost } from "@nestjs/bullmq"; | ||
import { OnModuleInit } from "@nestjs/common"; | ||
import { Job, Queue } from "bullmq"; | ||
|
||
import { AccountsService } from "./accounts.service.js"; | ||
import { redisClient } from "../../redis/redis.service.js"; | ||
import { TOP_BALANCE_ACCOUNTS_CACHE_KEY } from "../constants.js"; | ||
|
||
@Processor("accounts") | ||
export class AccountsProcessor extends WorkerHost implements OnModuleInit { | ||
public constructor( | ||
@InjectQueue("accounts") | ||
private readonly accountsQueue: Queue, | ||
|
||
private readonly accountsService: AccountsService, | ||
) { | ||
super(); | ||
} | ||
|
||
public async onModuleInit() { | ||
await this.accountsQueue.add("updateAccountsCache", undefined, { | ||
repeat: { | ||
every: 60 * 60 * 1_000, // 1 hour | ||
}, | ||
}); | ||
|
||
// Execute the job immediately on startup | ||
await this.updateAccountsCache(); | ||
} | ||
|
||
public async process(job: Job<void, any, string>) { | ||
switch (job.name) { | ||
case "updateAccountsCache": | ||
await this.updateAccountsCache(); | ||
break; | ||
|
||
default: | ||
throw new Error(`invalid job name ${job.name}`); | ||
} | ||
} | ||
|
||
private async updateAccountsCache() { | ||
const accounts = await this.accountsService.getTopBalanceAccounts(100); | ||
await redisClient.set( | ||
TOP_BALANCE_ACCOUNTS_CACHE_KEY, | ||
JSON.stringify(accounts), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Query, Resolver, Args } from "@nestjs/graphql"; | ||
import { Inject } from "@nestjs/common"; | ||
import { ServiceUnavailableException } from "@nestjs/common"; | ||
import { ConfigService } from "@nestjs/config"; | ||
|
||
import { CumulativeShare, TopAccount } from "./accounts.model.js"; | ||
import { AccountsService } from "./accounts.service.js"; | ||
import { redisClient } from "../../redis/redis.service.js"; | ||
import { TOP_BALANCE_ACCOUNTS_CACHE_KEY } from "../constants.js"; | ||
|
||
@Resolver(() => TopAccount) | ||
export class AccountsResolver { | ||
private readonly cacheEnabled: boolean; | ||
|
||
public constructor( | ||
@Inject(AccountsService) | ||
private readonly accountsService: AccountsService, | ||
config: ConfigService, | ||
) { | ||
this.cacheEnabled = config.get<boolean>("cacheEnabled")!; | ||
} | ||
|
||
@Query(() => [TopAccount]) | ||
async getTopAccounts( | ||
@Args("limit", { type: () => Number, defaultValue: 100 }) limit: number, | ||
): Promise<TopAccount[]> { | ||
// Check if caching is enabled and the query is not present | ||
if (this.cacheEnabled) { | ||
const cachedAccounts = await redisClient.get( | ||
TOP_BALANCE_ACCOUNTS_CACHE_KEY, | ||
); | ||
if (cachedAccounts) { | ||
const accounts = JSON.parse(cachedAccounts); | ||
return accounts.slice(0, limit).map( | ||
(account: any) => | ||
new TopAccount({ | ||
rank: account.rank, | ||
address: account.address, | ||
publicName: account.publicName, | ||
balance: account.balance, | ||
cumulativeShare: new CumulativeShare(account.cumulativeShare), | ||
}), | ||
); | ||
} | ||
throw new ServiceUnavailableException("Cache not ready"); | ||
} | ||
|
||
// If cache is not enabled, get data from the service | ||
const accounts = await this.accountsService.getTopBalanceAccounts(limit); | ||
return accounts.map( | ||
(account) => | ||
new TopAccount({ | ||
rank: account.rank, | ||
address: account.address, | ||
publicName: account.publicName, | ||
balance: account.balance, | ||
cumulativeShare: new CumulativeShare(account.cumulativeShare), | ||
}), | ||
); | ||
} | ||
} |
Oops, something went wrong.