Skip to content

Commit

Permalink
enable s2-2.
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zklink committed Jul 16, 2024
1 parent c3c8d10 commit 06d8e88
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
UserHolding,
UserStaked,
UserWithdraw,
SeasonTotalPoint,
} from "./entities/index";
import { PositionsService } from "./positions/positions.service";
import { PositionsController } from "./positions/positions.controller";
Expand Down Expand Up @@ -103,6 +104,7 @@ import { SeasonTotalPointRepository } from "./repositories/seasonTotalPoint.repo
UserHolding,
UserStaked,
UserWithdraw,
SeasonTotalPoint,
]),
TypeOrmModule.forFeature([Referral], "referral"),
MetricsModule,
Expand Down
21 changes: 21 additions & 0 deletions src/common/pipes/parseNumber.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PipeTransform, Injectable, BadRequestException } from "@nestjs/common";

@Injectable()
export class ParseNumberPipe implements PipeTransform<string, number> {
private defaultValue: number;
public constructor(value: number) {
this.defaultValue = value;
}
transform(value: string): number {
if (!value) {
return this.defaultValue;
}
const val = parseInt(value, 10);
if (isNaN(val)) {
throw new BadRequestException(
`Validation failed. "${value}" is not a number.`,
);
}
return val;
}
}
38 changes: 38 additions & 0 deletions src/config/season2-1.milestone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export default [
{
name: "holding",
data: 715063449,
type: "tvl",
zkl: 2000000,
},
{
name: "spotdex",
data: 3185226.3652,
type: "tvl",
zkl: 100000,
},
{
name: "perpdex",
data: 4196768,
type: "volume",
zkl: 100000,
},
{
name: "lending",
data: 466891579.0847,
type: "tvl",
zkl: 1500000,
},
{
name: "nativeboost",
data: 0,
type: "tvl",
zkl: 50000,
},
{
name: "other",
data: 0,
type: "tvl",
zkl: 50000,
},
];
30 changes: 18 additions & 12 deletions src/nova/nova.balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export interface AddressPoints {
@Injectable()
export class NovaBalanceService extends Worker {
private readonly logger: Logger;
private categoryUserList: Map<string, UserPoints[]> = new Map();
private seasonCategoryUserList: Map<number, Map<string, UserPoints[]>> =
new Map();

public constructor(
private readonly projectRepository: ProjectRepository,
Expand Down Expand Up @@ -60,18 +61,23 @@ export class NovaBalanceService extends Worker {
}

public async loadCategoryUserList() {
const season = 2;
const seasons = await this.seasonTotalPointRepository.getSeasons();
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);

for (const season of seasons) {
const categoryUserList: Map<string, UserPoints[]> = new Map();
for (const item of categoryPairAddresses) {
const category = item.category;
const pairAddresses = item.pairAddresses;
const result =
await this.seasonTotalPointRepository.getSeasonTotalPointByPairAddresses(
pairAddresses,
season,
);
categoryUserList.set(category, result);
}
this.seasonCategoryUserList.set(season, categoryUserList);
}
}

Expand Down Expand Up @@ -415,7 +421,7 @@ export class NovaBalanceService extends Worker {
totalPoints: number;
}[];
}> {
const result = this.categoryUserList.get(category);
const result = this.seasonCategoryUserList.get(season)?.get(category) ?? [];
if (!result || result.length === 0) {
return {
current: null,
Expand Down
25 changes: 12 additions & 13 deletions src/nova/nova.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
UserPointsListDto,
} from "./nova.dto";
import { ReferralService } from "src/referral/referral.service";
import { ParseNumberPipe } from "src/common/pipes/parseNumber.pipe";

const options = {
// how long to live in ms
Expand Down Expand Up @@ -453,9 +454,9 @@ export class NovaController {
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getNovaCategoryUserProjectPoints(
@Query("season", new ParseNumberPipe(3)) season: number,
@Query("address", new ParseAddressPipe()) address: string,
): Promise<ResponseDto<CategoryPointsDto[]>> {
const season = 2;
const pointsData = await this.novaBalanceService.getPointsByAddress(
season,
address,
Expand All @@ -479,9 +480,9 @@ export class NovaController {
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getNovaCategoryUserTotalPoints(
@Query("season", new ParseNumberPipe(3)) season: number,
@Query("address", new ParseAddressPipe()) address: string,
): Promise<ResponseDto<CategoryTotalPointsListDto[]>> {
const season = 2;
const pointsData =
await this.novaBalanceService.getUserCategoryPointsDetail(
season,
Expand All @@ -502,10 +503,9 @@ export class NovaController {
@ApiNotFoundResponse({
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getNovaCategoryPoints(): Promise<
ResponseDto<CategoryPointsListDto[]>
> {
const season = 2;
public async getNovaCategoryPoints(
@Query("season", new ParseNumberPipe(3)) season: number,
): Promise<ResponseDto<CategoryPointsListDto[]>> {
const pointsData =
await this.novaBalanceService.getAllCategoryPoints(season);
return {
Expand Down Expand Up @@ -535,9 +535,9 @@ export class NovaController {
)
address: string,
@Query() pagingOptions: PagingOptionsDto,
@Query("season", new ParseNumberPipe(3)) season: number,
): Promise<ResponseDto<CategoryPointsUserListWithCurrentDto>> {
const { limit = 100 } = pagingOptions;
const season = 2;
const data = await this.novaBalanceService.getPointsListByCategory(
category,
season,
Expand Down Expand Up @@ -571,9 +571,9 @@ export class NovaController {
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getReferrerPointsRank(
@Query("season", new ParseNumberPipe(3)) season: number,
@Param("address", new ParseAddressPipe()) address: string,
): Promise<ResponseDto<UserPointsListDto[]>> {
const season = 2;
const data = await this.referralService.getReferralPoints(address, season);
return {
errno: 0,
Expand All @@ -598,8 +598,8 @@ export class NovaController {
})
public async getHoldPoint(
@Param("address", new ParseAddressPipe()) address: string,
@Query("season", new ParseNumberPipe(3)) season: number,
): Promise<ResponseDto<number>> {
const season = 2;
const data = await this.novaBalanceService.getHoldPointsByAddress(
address,
season,
Expand Down Expand Up @@ -640,10 +640,9 @@ export class NovaController {
@ApiNotFoundResponse({
description: '{ "errno": 1, "errmsg": "not found" }',
})
public async getNovaAllProjectPoints(): Promise<
ResponseDto<ProjectPointsListDto[]>
> {
const season = 2;
public async getNovaAllProjectPoints(
@Query("season", new ParseNumberPipe(3)) season: number,
): Promise<ResponseDto<ProjectPointsListDto[]>> {
const pointsData =
await this.novaBalanceService.getAllProjectPoints(season);
return {
Expand Down
13 changes: 12 additions & 1 deletion src/repositories/seasonTotalPoint.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from "@nestjs/common";
import { UnitOfWork } from "../unitOfWork";
import { BaseRepository } from "./base.repository";
import { SeasonTotalPoint } from "../entities";
import { SeasonTotalPoint } from "../entities/seasonTotalPoint.entity";
import removeAddress from "src/config/removeAddress";

@Injectable()
Expand Down Expand Up @@ -148,6 +148,7 @@ export class SeasonTotalPointRepository extends BaseRepository<SeasonTotalPoint>
? Number(result[0].totalPoints)
: 0;
}

public async getSeasonTotalPointByAddress(
season: number,
address: string,
Expand All @@ -172,4 +173,14 @@ export class SeasonTotalPointRepository extends BaseRepository<SeasonTotalPoint>
return row;
});
}

public async getSeasons(): Promise<number[]> {
const entityManager = this.unitOfWork.getTransactionManager();
const result = await entityManager
.createQueryBuilder(SeasonTotalPoint, "a")
.select("season")
.groupBy("season")
.getRawMany();
return result.map((row) => row.season);
}
}
21 changes: 21 additions & 0 deletions src/tvl/tvl.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiExcludeController, ApiOperation, ApiTags } from "@nestjs/swagger";
import { ResponseDto } from "src/common/response.dto";
import { CategoryMilestoneDto, CategoryTvlDto } from "./tvl.dto";
import { TvlService } from "./tvl.service";
import s2_1Milestone from "../config/season2-1.milestone";

@ApiTags("tvl")
@ApiExcludeController(false)
Expand Down Expand Up @@ -46,4 +47,24 @@ export class TvlController {
}),
};
}

@Get("/category/milestone/s2-1")
@ApiOperation({ summary: "Retrieve milestone of the s2-1 project category" })
public async getCategoryMilestoneS2_1(): Promise<
ResponseDto<CategoryMilestoneDto[]>
> {
const data = s2_1Milestone;
return {
errno: 0,
errmsg: "no error",
data: data.map((item) => {
return {
name: item.name,
data: item.data.toFixed(4),
type: item.type,
zkl: item.zkl,
};
}),
};
}
}
4 changes: 2 additions & 2 deletions src/tvl/tvl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class TvlService {
> {
const category = "perpdex";
let totalVolume = BigNumber(0);
const startTime = "2024-05-30 00:00:00";
const endTime = "2024-07-15 21:00:00";
const startTime = "2024-07-15 21:00:00";
const endTime = "2024-08-31 21:00:00";
const categoryTvl = await this.getCategoryTvl();
const result = categoryTvl.map((item) => ({
name: item.name,
Expand Down

0 comments on commit 06d8e88

Please sign in to comment.