Skip to content

Commit

Permalink
Merge pull request #62 from axone-protocol/refactor/supply-historical
Browse files Browse the repository at this point in the history
refactor: add percent change into response
  • Loading branch information
yevhen-burkovskyi authored Jul 15, 2024
2 parents 0909fa0 + 11f1ef1 commit c2b1062
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/modules/supply/dtos/change-supply.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SupplyIntervalDto } from "./supply-interval.dto";

export type ChangeSupplyDto = SupplyIntervalDto & { percentChange: string; };
26 changes: 23 additions & 3 deletions src/modules/supply/services/supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SupplyCache } from "./supply.cache";
import { TimeBucketDto } from "../dtos/time-bucket.dto";
import { SupplyIntervalDto } from "../dtos/supply-interval.dto";
import { SupplyChangeDto } from "../dtos/supply-change.dto";
import { ChangeSupplyDto } from "../dtos/change-supply.dto";

@Injectable()
export class SupplyService implements OnModuleInit {
Expand Down Expand Up @@ -50,7 +51,7 @@ export class SupplyService implements OnModuleInit {

private async calculateAndCacheSupplyHistoricalPrice(range: Range, { interval, count }: HistoricalChartConf) {
try {
const historicalPrice = await this.timeBucket(interval, DBOrder.DESC, count);
const historicalPrice = await this.timeBucket(interval, DBOrder.ASC, count);
await this.cache.setSupplyHistorical(range, historicalPrice);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
Expand All @@ -70,7 +71,26 @@ export class SupplyService implements OnModuleInit {
ORDER BY interval ${order}
${limit ? `LIMIT ${limit}` : ''};
`);
return this.fromBucket(bucket);
return this.addChangePercent(this.fromBucket(bucket));
}

private addChangePercent(intervalSupply: SupplyIntervalDto[]): ChangeSupplyDto[] {
const changeSupply: ChangeSupplyDto[] = [];
for (let i = 0; i < intervalSupply.length; i++) {
if(i === 0) {
changeSupply[i] = {
...intervalSupply[i],
percentChange: "0"
}
} else {
changeSupply[i] = {
...intervalSupply[i],
percentChange: Big(this.calculateSupplyChange(intervalSupply[i].change, intervalSupply[i-1].change)).toFixed(2),
}
}
}

return changeSupply;
}

private fromBucket(bucket: TimeBucketDto[]): SupplyIntervalDto[] {
Expand Down Expand Up @@ -110,7 +130,7 @@ export class SupplyService implements OnModuleInit {
});
}

private async calculateSupplyChange(newSupply?: string, pastSupply?: string) {
private calculateSupplyChange(newSupply?: string, pastSupply?: string) {
let change = 0;

if (newSupply && pastSupply) {
Expand Down

0 comments on commit c2b1062

Please sign in to comment.