Skip to content

Commit

Permalink
Merge pull request #40 from axone-protocol/refactor/apr-formula
Browse files Browse the repository at this point in the history
refactor: updated apr formula
  • Loading branch information
yevhen-burkovskyi authored Jun 24, 2024
2 parents e458959 + e4b5897 commit df96ca1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/core/lib/okp4/enums/endpoints.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export enum Endpoints {
GOV_PROPOSALS = 'cosmos/gov/v1/proposals',
GOV_PROPOSAL = 'cosmos/gov/v1/proposals/:proposal_id',
STAKING_POOL = 'cosmos/staking/v1beta1/pool',
INFLATION = '/cosmos/mint/v1beta1/inflation',
DISTRIBUTION_PARAMS = '/cosmos/distribution/v1beta1/params',
}
26 changes: 26 additions & 0 deletions src/core/lib/okp4/okp4.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { GetProposalsResponse } from "./responses/get-proposals.response";
import { GetProposalResponse } from "@core/lib/okp4/responses/get-proposal.response";
import { StakingPoolResponse } from "./responses/staking-pool.response";
import { ValidatorStatus } from "./enums/validator-status.enum";
import { InflationResponse } from "./responses/inflation.response";
import { DistributionParamsResponse } from "./responses/distribution-params.response";
import Big from "big.js";

@Injectable()
export class Okp4Service {
Expand Down Expand Up @@ -245,4 +248,27 @@ export class Okp4Service {
async getStakingPool(): Promise<StakingPoolResponse> {
return this.getWithErrorHandling(this.constructUrl(Endpoints.STAKING_POOL));
}

async getApr() {
const promises = [
this.getWithErrorHandling(this.constructUrl(Endpoints.INFLATION)),
this.getWithErrorHandling(
this.constructUrl(Endpoints.DISTRIBUTION_PARAMS)
),
this.getStakingPool(),
];
const res = (await Promise.all(promises)) as [
InflationResponse,
DistributionParamsResponse,
StakingPoolResponse
];

if (res[2]?.pool?.bonded_tokens) {
Big(res[0].inflation)
.mul(Big(1).minus(res[1].params.community_tax))
.div(res[2].pool.bonded_tokens)
.toString();
}
return "0";
}
}
8 changes: 8 additions & 0 deletions src/core/lib/okp4/responses/distribution-params.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface DistributionParamsResponse {
params: {
community_tax: string;
base_proposer_reward: string;
bonus_proposer_reward: string;
withdraw_addr_enabled: boolean;
};
}
3 changes: 3 additions & 0 deletions src/core/lib/okp4/responses/inflation.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface InflationResponse {
inflation: string;
}
4 changes: 1 addition & 3 deletions src/modules/staking/services/staking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BadRequestException, Injectable, OnModuleInit } from "@nestjs/common";
import { StakingCache } from "./staking.cache";
import { config } from "@core/config/config";
import { MyStakedOverviewDto } from "../dtos/my-staked-overview.dto";
import { OsmosisService } from "@core/lib/osmosis/osmosis.service";
import { Validator } from "@core/lib/okp4/responses/delegators-validators.response";
import Big from "big.js";
import { GlobalStakedOverviewDto } from "../dtos/global-staked-overview.dto";
Expand Down Expand Up @@ -35,7 +34,6 @@ export class StakingService implements OnModuleInit {
constructor(
private readonly okp4Service: Okp4Service,
private readonly cache: StakingCache,
private readonly osmosisService: OsmosisService,
private readonly keybaseService: KeybaseService,
private eventEmitter: EventEmitter2
) {}
Expand Down Expand Up @@ -155,7 +153,7 @@ export class StakingService implements OnModuleInit {
private async fetchAndCacheGlobalStakedOverview(): Promise<GlobalStakedOverviewDto> {
const rez = await Promise.all([
this.okp4Service.getBondValidators(),
this.osmosisService.getStakingApr(),
this.okp4Service.getApr(),
this.fetchTotalSupply(),
this.okp4Service.getStakingPool(),
]);
Expand Down
8 changes: 2 additions & 6 deletions src/modules/staking/staking.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StakingService } from "./services/staking.service";
import { StakingController } from "./staking.controller";
import { HttpService } from "@core/lib/http.service";
import { StakingCache } from "./services/staking.cache";
import { OsmosisService } from "@core/lib/osmosis/osmosis.service";
import { KeybaseService } from "@core/lib/keybase/keybase.service";
import { RedisService } from "@core/lib/redis.service";
import { StakingGateway } from "./staking.gateway";
Expand All @@ -13,16 +12,13 @@ import { StakingGateway } from "./staking.gateway";
imports: [],
providers: [
Okp4Service,
OsmosisService,
KeybaseService,
RedisService,
StakingService,
StakingCache,
HttpService,
StakingGateway,
],
controllers: [
StakingController,
],
controllers: [StakingController],
})
export class StakingModule {}
export class StakingModule {}
4 changes: 3 additions & 1 deletion src/modules/token/services/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import {
import { HistoricalPrice } from "../dtos/historical-price.dto";
import { TimeBucketDto } from "../dtos/time-bucket.dto";
import { Log } from "@core/loggers/log";
import { Okp4Service } from "@core/lib/okp4/okp4.service";

@Injectable()
export class TokenService implements OnModuleInit {
private rangeTimeIntervalMap: RangeHistoricalChartConf;

constructor(
private readonly osmosisService: OsmosisService,
private readonly okp4Service: Okp4Service,
private readonly cache: TokenCache,
private readonly prismaService: PrismaService
) {
Expand Down Expand Up @@ -95,7 +97,7 @@ export class TokenService implements OnModuleInit {
async fetchAndCacheTokenInfo() {
const res = (await this.osmosisService.getTokenInfo(config.app.token))[0];
const mcap = await this.getMcapByOrder();
const apr = await this.osmosisService.getStakingApr();
const apr = await this.okp4Service.getApr();

const tokenInfoDto: TokenInfoDto = {
price: {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/token/token.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import { TokenJobs } from "./services/token.jobs";
import { PrismaService } from "@core/lib/prisma.service";
import { TokenController } from "./token.controller";
import { RedisService } from "@core/lib/redis.service";
import { Okp4Service } from "@core/lib/okp4/okp4.service";

@Module({
imports: [],
providers: [
PrismaService,
HttpService,
OsmosisService,
Okp4Service,
TokenService,
TokenCache,
TokenJobs,
RedisService,
],
controllers: [
TokenController,
]
controllers: [TokenController],
})
export class TokenModule {}
export class TokenModule {}

0 comments on commit df96ca1

Please sign in to comment.