Skip to content

Commit

Permalink
protocol tvl
Browse files Browse the repository at this point in the history
  • Loading branch information
zk-robin committed Aug 21, 2024
1 parent 5facad6 commit 4b929df
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ REFER_DATABASE_CONNECTION_POOL_SIZE=100


EXPLORER_API_URL=https://explorer-api.zklink.io
POINTS_API_URL=https://app-api.zklink.io/points

# PUFF
PUFF_POINTS_TOKEN_ADDRESS=0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3Fc
Expand Down
15 changes: 15 additions & 0 deletions src/entities/blockTokenPrice.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Entity, Column, PrimaryColumn } from "typeorm";
import { BaseEntity } from "./base.entity";
import { bigIntNumberTransformer } from "../transformers/bigIntNumber.transformer";

@Entity({ name: "blockTokenPrice" })
export class BlockTokenPrice extends BaseEntity {
@PrimaryColumn({ type: "bigint", transformer: bigIntNumberTransformer })
public readonly blockNumber: number;

@PrimaryColumn({ type: "varchar" })
public readonly priceId: string;

@Column({ type: "double precision" })
public readonly usdPrice: number;
}
6 changes: 5 additions & 1 deletion src/entities/dau.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Entity, Column, PrimaryGeneratedColumn, Unique } from "typeorm";
import { BaseEntity } from "./base.entity";

@Entity({ name: "protocolDau" })
@Unique(["date", "name"])
@Unique(["date", "name", "type"])
export class ProtocolDau extends BaseEntity {
@PrimaryGeneratedColumn({ type: "int" })
public readonly id: number;
Expand All @@ -15,4 +15,8 @@ export class ProtocolDau extends BaseEntity {

@Column({ type: "date" })
public readonly date: String;

// 1: dau, 2: cumulative dau, 3: tvl
@Column({ type: "smallint", default: 1 })
public readonly type: number;
}
2 changes: 1 addition & 1 deletion src/migrations/1723508522029-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class Migrations1723508522029 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "protocolDau" ("createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "id" SERIAL NOT NULL, "name" character varying NOT NULL, "amount" integer NOT NULL, "date" date NOT NULL, CONSTRAINT "UQ_1aaab071aea8a9562cb547fd5b5" UNIQUE ("date", "name"), CONSTRAINT "PK_20d32ec3e9e341c3fcaa9f3d267" PRIMARY KEY ("id"))`,
`CREATE TABLE "protocolDau" ("createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "id" SERIAL NOT NULL, "name" character varying NOT NULL, "amount" integer NOT NULL, "date" date NOT NULL, "type" smallint NOT NULL DEFAULT '1', CONSTRAINT "UQ_1aaab071aea8a9562cb547fd5b5" UNIQUE ("date", "name", "type"), CONSTRAINT "PK_20d32ec3e9e341c3fcaa9f3d267" PRIMARY KEY ("id"))`,
);
}

Expand Down
54 changes: 54 additions & 0 deletions src/statistics/statistic.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
ApiTags,
} from "@nestjs/swagger";
import { InjectRepository } from "@nestjs/typeorm";
import { categoryBaseConfig } from "src/config/projectCategory.config";
import { ProtocolDau } from "src/entities/dau.entity";
import { Project } from "src/entities/project.entity";
import { Between, Repository } from "typeorm";

@ApiTags("statistic")
Expand All @@ -19,6 +21,8 @@ export class StatisticController {
constructor(
@InjectRepository(ProtocolDau)
private readonly protocolDauRepository: Repository<ProtocolDau>,
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
private readonly statisticService: StatisticService,
) {}

Expand All @@ -39,6 +43,7 @@ export class StatisticController {
where: {
name,
date: Between(startDate, endDate),
type: 1,
},
order: {
date: "desc",
Expand All @@ -51,6 +56,55 @@ export class StatisticController {
};
}

@Get("/protocol/cumulativeDau")
@ApiQuery({ name: "name", type: String, required: false })
@ApiOperation({ summary: "get protocol cumulative dau" })
public async getProtocolCumulativeDau(
@Query("page") page: number = 1,
@Query("size") size: number = 30,
@Query("name") name?: string,
) {
const startDate = new Date();
startDate.setDate(startDate.getDate() - page * size);
const endDate = new Date();
endDate.setDate(endDate.getDate() - 1 - (page - 1) * size);

const data = await this.protocolDauRepository.find({
where: {
name,
date: Between(startDate, endDate),
type: 2,
},
order: {
date: "desc",
},
});
return {
errno: 0,
errmsg: "no error",
data,
};
}

@Get("/protocol/sector")
public async getSector() {
return {
errno: 0,
errmsg: "no error",
data: categoryBaseConfig,
};
}

@Get("/protocol/list")
public async getProjectList() {
const all = await this.projectRepository.find();
return {
errno: 0,
errmsg: "no error",
data: all,
};
}

@Get("/protocol/test")
public async test() {
await this.statisticService.statisticHistoryProtocolDau();
Expand Down
10 changes: 9 additions & 1 deletion src/statistics/statistic.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { StatisticController } from "./statistic.controller";
import { ProtocolDau } from "src/entities/dau.entity";
import { BalanceOfLp } from "src/entities/balanceOfLp.entity";
import { Project } from "src/entities/project.entity";
import { ExplorerService } from "src/common/service/explorer.service";
import { BlockTokenPrice } from "src/entities/blockTokenPrice.entity";

@Module({
imports: [
TypeOrmModule.forFeature([ProtocolDau, BalanceOfLp, Project]),
TypeOrmModule.forFeature([
ProtocolDau,
BalanceOfLp,
Project,
BlockTokenPrice,
]),
ScheduleModule.forRoot(),
ExplorerService,
],
providers: [StatisticService],
controllers: [StatisticController],
Expand Down
Loading

0 comments on commit 4b929df

Please sign in to comment.