Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: supply historical fix #61

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "historical_supply" DROP COLUMN "change";
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ model HistoricalPrices {
model HistoricalSupply {
time DateTime @id @unique
supply String
change Float
@@map("historical_supply")
}

Expand Down
4 changes: 0 additions & 4 deletions src/modules/supply/dtos/change-interval.dto.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/modules/supply/dtos/current-supply.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface CurrentSupplyDto {
time: Date;
supply: string;
change: number;
}
4 changes: 4 additions & 0 deletions src/modules/supply/dtos/supply-interval.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface SupplyIntervalDto {
change: string;
time: Date;
}
2 changes: 1 addition & 1 deletion src/modules/supply/dtos/time-bucket.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type TimeBucketDto = {
sum_change: number;
avg_supply: string;
interval: Date;
};
37 changes: 16 additions & 21 deletions src/modules/supply/services/supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DBTimeInterval } from "@core/enums/db-time-interval.enum";
import { Log } from "@core/loggers/log";
import { SupplyCache } from "./supply.cache";
import { TimeBucketDto } from "../dtos/time-bucket.dto";
import { ChangeIntervalDto } from "../dtos/change-interval.dto";
import { SupplyIntervalDto } from "../dtos/supply-interval.dto";
import { SupplyChangeDto } from "../dtos/supply-change.dto";

@Injectable()
Expand Down Expand Up @@ -64,7 +64,7 @@ export class SupplyService implements OnModuleInit {
limit?: number,
) {
const bucket: TimeBucketDto[] = await this.prismaService.$queryRawUnsafe(`
SELECT time_bucket('${interval}', time) as interval, sum(change) as sum_change
SELECT time_bucket('${interval}', time) as interval, avg(CAST(supply AS DECIMAL)) as avg_supply
FROM historical_supply
GROUP BY interval
ORDER BY interval ${order}
Expand All @@ -73,10 +73,10 @@ export class SupplyService implements OnModuleInit {
return this.fromBucket(bucket);
}

private fromBucket(bucket: TimeBucketDto[]): ChangeIntervalDto[] {
private fromBucket(bucket: TimeBucketDto[]): SupplyIntervalDto[] {
return bucket.map((item) => ({
time: item.interval,
change: item.sum_change,
change: item.avg_supply,
}));
}

Expand All @@ -95,12 +95,10 @@ export class SupplyService implements OnModuleInit {
private async fetchCurrentSupply(): Promise<CurrentSupplyDto> {
const { amount: { amount: supply } } = await this.okp4Service.getSupplyByDenom(config.app.tokenDenom);
const time = new Date();
const change = await this.calculateSupplyChange(supply);

return {
time,
supply,
change,
}
}

Expand All @@ -112,15 +110,14 @@ export class SupplyService implements OnModuleInit {
});
}

private async calculateSupplyChange(newSupply: string) {
const currentSupply = await this.getSupplyByOrder();
private async calculateSupplyChange(newSupply?: string, pastSupply?: string) {
let change = 0;

if (currentSupply && newSupply) {
if (Number.parseFloat(currentSupply.supply) === 0) {
if (newSupply && pastSupply) {
if (Number.parseFloat(pastSupply) === 0) {
change = Big(newSupply).toNumber();
} else {
change = Big(newSupply).minus(currentSupply.supply).div(currentSupply.supply).toNumber();
change = Big(newSupply).minus(pastSupply).div(pastSupply).mul(100).toNumber();
}
}

Expand Down Expand Up @@ -192,22 +189,20 @@ export class SupplyService implements OnModuleInit {

async getSupplyGrowth(range: Range) {
const pastDate = this.calculatePastDateByRange(range);
const supplyChangeByPeriod = await this.prismaService.historicalSupply.aggregate({
const pastSupply = await this.prismaService.historicalSupply.findFirst({
where: {
time: {
gte: pastDate,
gte: pastDate
}
},
_sum: {
change: true,
orderBy: {
time: 'asc'
}
});

const currentSupply = await this.getSupplyByOrder();

if (supplyChangeByPeriod._sum.change) {
return Big(supplyChangeByPeriod._sum.change).toFixed(2);
}

return "0";
return this.calculateSupplyChange(currentSupply?.supply, pastSupply?.supply);
}

async getCharts(range: Range) {
Expand All @@ -216,7 +211,7 @@ export class SupplyService implements OnModuleInit {

return {
issuance: res.issuance,
growth: Number.parseFloat(growth),
growth: Big(growth).toFixed(2),
burnt: res.burnt,
}
}
Expand Down
Loading