Skip to content

Commit

Permalink
Merge pull request #83 from zkLinkProtocol/feat-projecPointsList
Browse files Browse the repository at this point in the history
opt api:leadbord ranking list.
  • Loading branch information
zkcarter authored Jul 10, 2024
2 parents 1874d8a + 5629b40 commit 7e5230e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 59 deletions.
9 changes: 6 additions & 3 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MagpieService } from "./magpie/magpie.service";
import { RsethService } from "./rseth/rseth.service";
import { GraphQueryService } from "./common/service/graphQuery.service";
import { SwethService } from "./sweth/sweth.service";
import { NovaBalanceService } from "./nova/nova.balance.service";

@Injectable()
export class AppService implements OnModuleInit, OnModuleDestroy {
Expand All @@ -24,6 +25,7 @@ export class AppService implements OnModuleInit, OnModuleDestroy {
private readonly rsethService: RsethService,
private readonly swethService: SwethService,
private readonly graphQueryService: GraphQueryService,
private readonly novaBalanceService: NovaBalanceService,
private readonly dataSource: DataSource,
private readonly configService: ConfigService,
) {
Expand All @@ -39,15 +41,15 @@ export class AppService implements OnModuleInit, OnModuleDestroy {
}

private startWorkers() {
const tasks = [
return Promise.all([
this.graphQueryService.start(),
this.puffPointsService.start(),
this.renzoService.start(),
this.magpieService.start(),
this.rsethService.start(),
this.swethService.start(),
];
return Promise.all(tasks);
this.novaBalanceService.start(),
]);
}

private stopWorkers() {
Expand All @@ -58,6 +60,7 @@ export class AppService implements OnModuleInit, OnModuleDestroy {
this.rsethService.stop(),
this.swethService.stop(),
this.graphQueryService.stop(),
this.novaBalanceService.stop(),
]);
}
}
91 changes: 67 additions & 24 deletions src/nova/nova.balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@ import projectCategoryConfig from "src/config/projectCategory.config";
import { ProjectCategoryPoints } from "src/type/points";
import { SeasonTotalPointRepository } from "src/repositories/seasonTotalPoint.repository";
import { ProjectService } from "src/common/service/project.service";
import { Worker } from "src/common/worker";
import waitFor from "src/utils/waitFor";

interface ProjectPoints {
name: string;
totalPoints: number;
}

interface UserPoints {
userAddress: string;
userName: string;
totalPoints: number;
}

export interface AddressPoints {
address: string;
totalPoints: number;
projects: ProjectPoints[];
}

@Injectable()
export class NovaBalanceService {
export class NovaBalanceService extends Worker {
private readonly logger: Logger;
private categoryUserList: Map<string, UserPoints[]> = new Map();

public constructor(
private readonly projectRepository: ProjectRepository,
Expand All @@ -32,9 +41,40 @@ export class NovaBalanceService {
private readonly projectService: ProjectService,
private readonly balanceOfLp: BalanceOfLpRepository,
) {
super();
this.logger = new Logger(NovaBalanceService.name);
}

public async runProcess() {
this.logger.log(`Init ${NovaBalanceService.name} onmoduleinit`);
try {
await this.loadCategoryUserList();
} catch (err) {
this.logger.error(`${NovaBalanceService.name} init failed.`, err.stack);
}
await waitFor(() => !this.currentProcessPromise, 300 * 1000, 300 * 1000);
if (!this.currentProcessPromise) {
return;
}
return this.runProcess();
}

public async loadCategoryUserList() {
const season = 2;
const categoryPairAddresses =
await this.projectService.getCategoryPairAddress();
for (const item of categoryPairAddresses) {
const category = item.category;
const pairAddresses = item.pairAddresses;
const result =
await this.seasonTotalPointRepository.getSeasonTotalPointByPairAddresses(
pairAddresses,
season,
);
this.categoryUserList.set(category, result);
}
}

public async getPoints(
address: string,
projectName: string,
Expand Down Expand Up @@ -352,31 +392,34 @@ export class NovaBalanceService {
totalPoints: number;
}[];
}> {
// 1. get all projects in the category
// 2. get all pairAddress in the projects
// 3. get sum points in pairAddress group by address
// 4. sort by points
// 5. return
const categoryPairAddresses =
await this.projectService.getCategoryPairAddress();
const pairAddresses = categoryPairAddresses.find(
(x) => x.category === category,
)?.pairAddresses;
const result =
await this.seasonTotalPointRepository.getSeasonTotalPointByPairAddresses(
pairAddresses,
season,
pageSize,
address,
const result = this.categoryUserList.get(category);
if (!result || result.length === 0) {
return {
current: null,
data: [],
};
}

let current = null;
if (address) {
const userIndex = result.findIndex(
(item) => item.userAddress === address,
);
if (userIndex !== -1) {
const currentData = result[userIndex];
current = {
userIndex,
address: currentData.userAddress,
userName: currentData.userName,
totalPoints: currentData.totalPoints,
};
}
}

const resultData = result.slice(0, pageSize);
return {
current: {
userIndex: result.current?.userIndex,
username: result.current?.userName,
address: result.current?.userAddress,
totalPoints: result.current?.totalPoints,
},
data: result.data.map((item) => {
current,
data: resultData.map((item) => {
return {
username: item.userName,
address: item.userAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/nova/nova.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ export class NovaController {
errmsg: "no error",
data: {
current:
address && data.current.userIndex >= 0
address && (data?.current?.userIndex ?? -1) >= 0
? {
userIndex: data.current.userIndex + 1,
address: data.current.address,
Expand Down
36 changes: 5 additions & 31 deletions src/repositories/seasonTotalPoint.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,13 @@ export class SeasonTotalPointRepository extends BaseRepository<SeasonTotalPoint>
public async getSeasonTotalPointByPairAddresses(
pairAddresses: string[],
season: number,
limit: number,
address: string,
): Promise<{
current: {
userIndex: number;
userAddress: string;
userName: string;
totalPoints: number;
};
data: {
): Promise<
{
userAddress: string;
userName: string;
totalPoints: number;
}[];
}> {
}[]
> {
const pairAddressesBuffer = pairAddresses.map((address) =>
Buffer.from(address.slice(2), "hex"),
);
Expand All @@ -77,25 +69,7 @@ export class SeasonTotalPointRepository extends BaseRepository<SeasonTotalPoint>
: 0;
return row;
});
let current = null;
if (address) {
const userIndex = data.findIndex((item) => item.userAddress === address);
if (userIndex !== -1) {
const currentData = data[userIndex];
current = {
userIndex,
userAddress: currentData.userAddress,
userName: currentData.userName,
totalPoints: currentData.totalPoints,
};
}
}

const resultData = data.slice(0, limit);
return {
current,
data: resultData,
};
return data;
}

public async getSeasonTotalPointGroupByPairAddresses(season: number): Promise<
Expand Down

0 comments on commit 7e5230e

Please sign in to comment.