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

Add up all external incentive APYs for a specific duration #916

Merged
merged 7 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
51 changes: 28 additions & 23 deletions packages/stores/src/queries/pool-incentives/incentivized-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
ObservableQueryEpochProvisions,
ObservableQueryMintParmas,
} from "../mint";
import { ObservableQueryPools, ExternalGauge } from "../pools";
import { ObservableQueryPools } from "../pools";
import { IPriceStore } from "../../price";
import { ObservableQueryDistrInfo } from "./distr-info";
import { ObservableQueryLockableDurations } from "./lockable-durations";
import { ObservableQueryGuage } from "../incentives";
import { IncentivizedPools } from "./types";

export class ObservableQueryIncentivizedPools extends ObservableChainQuery<IncentivizedPools> {
Expand All @@ -27,7 +28,8 @@ export class ObservableQueryIncentivizedPools extends ObservableChainQuery<Incen
protected readonly queryPools: ObservableQueryPools,
protected readonly queryMintParmas: ObservableQueryMintParmas,
protected readonly queryEpochProvision: ObservableQueryEpochProvisions,
protected readonly queryEpochs: ObservableQueryEpochs
protected readonly queryEpochs: ObservableQueryEpochs,
protected readonly queryGauge: ObservableQueryGuage
) {
super(
kvStore,
Expand Down Expand Up @@ -119,51 +121,54 @@ export class ObservableQueryIncentivizedPools extends ObservableChainQuery<Incen
);

/**
* Computes external incentive APY for the given duration
* Computes the external incentive APR for the given duration
*/
readonly computeExternalIncentiveAPYForSpecificDuration = computedFn(
readonly computeExternalIncentiveGaugeAPR = computedFn(
jonator marked this conversation as resolved.
Show resolved Hide resolved
(
poolId: string,
gaugeId: string,
denom: string,
duration: Duration,
priceStore: IPriceStore,
fiatCurrency: FiatCurrency,
allowedGauges: ExternalGauge[]
fiatCurrency: FiatCurrency
): RatePretty => {
if (!allowedGauges.length) {
const observableGauge = this.queryGauge.get(gaugeId);

if (
duration.asMilliseconds() !==
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm realizing that since we take in the gaugeId, there's no real reason to take in the duration as well and perform this check. Can we remove the duration from the params?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonator Good catch, working on it =D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure the comment/docs is updated as well.

Copy link
Contributor Author

@srph srph Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main reason why we need duration is - on the pool page - we're simply iterating through the ExternalIncentiveGaugeAllowList[pool.id] (which would contain all gauges regardless of gauge)

Would you like me to move the duration checking there?

Copy link
Member

@jonator jonator Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we'd need to. I think we agree though it's a pointless constraint on anyone who wants to use this API to find the gauge's duration before being able to use the API.

observableGauge.lockupDuration.asMilliseconds()
) {
return new RatePretty(new Dec(0));
}

const externalGauge = allowedGauges.find((externalGauge) => {
return (
duration.asMilliseconds() === externalGauge.duration.asMilliseconds()
);
});
if (observableGauge.remainingEpoch < 1) {
return new RatePretty(new Dec(0));
}

if (!externalGauge?.rewardAmount) {
const chainInfo = this.chainGetter.getChain(this.chainId);

const mintCurrency = chainInfo.findCurrency(denom);

if (!mintCurrency?.coinGeckoId) {
return new RatePretty(new Dec(0));
}

const rewardAmount = observableGauge.getRemainingCoin(mintCurrency);

const pool = this.queryPools.getPool(poolId);

if (!pool) {
return new RatePretty(new Dec(0));
}

const mintDenom = this.queryMintParmas.mintDenom;
const epochIdentifier = this.queryMintParmas.epochIdentifier;

if (!mintDenom || !epochIdentifier) {
if (!epochIdentifier) {
return new RatePretty(new Dec(0));
}

const epoch = this.queryEpochs.getEpoch(epochIdentifier);

const chainInfo = this.chainGetter.getChain(this.chainId);

const mintCurrency = chainInfo.findCurrency(
externalGauge.rewardAmount.currency.coinMinimalDenom
);

if (!mintCurrency?.coinGeckoId || !epoch.duration) {
return new RatePretty(new Dec(0));
}
Expand Down Expand Up @@ -193,11 +198,11 @@ export class ObservableQueryIncentivizedPools extends ObservableChainQuery<Incen
.asMilliseconds() / epoch.duration.asMilliseconds();

const externalIncentivePrice = new Dec(mintPrice.toString()).mul(
externalGauge.rewardAmount.toDec()
rewardAmount.toDec()
);

const yearProvision = new Dec(numEpochPerYear.toString()).quo(
new Dec(externalGauge.remainingEpochs)
new Dec(observableGauge.remainingEpoch)
);

// coins = (X coin's price in USD * remaining incentives in X tokens * (365 / remaining days in gauge))
Expand Down
5 changes: 3 additions & 2 deletions packages/stores/src/queries/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export class OsmosisQueriesImpl {
chainId,
chainGetter
);
this.queryGauge = new ObservableQueryGuage(kvStore, chainId, chainGetter);
this.queryIncentivizedPools = new ObservableQueryIncentivizedPools(
kvStore,
chainId,
Expand All @@ -176,9 +177,9 @@ export class OsmosisQueriesImpl {
this.queryGammPools,
this.queryMintParams,
this.queryEpochProvisions,
this.queryEpochs
this.queryEpochs,
this.queryGauge
);
this.queryGauge = new ObservableQueryGuage(kvStore, chainId, chainGetter);
this.queryPoolsGaugeIds = new ObservableQueryPoolsGaugeIds(
kvStore,
chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { RatePretty } from "@keplr-wallet/unit";
import { Duration } from "dayjs/plugin/duration";
import { ObservableQueryEpochs } from "../epochs";
import { ObservableQueryEpochProvisions, ObservableQueryMintParmas } from "../mint";
import { ObservableQueryPools, ExternalGauge } from "../pools";
import { ObservableQueryPools } from "../pools";
import { IPriceStore } from "../../price";
import { ObservableQueryDistrInfo } from "./distr-info";
import { ObservableQueryLockableDurations } from "./lockable-durations";
import { ObservableQueryGuage } from "../incentives";
import { IncentivizedPools } from "./types";
export declare class ObservableQueryIncentivizedPools extends ObservableChainQuery<IncentivizedPools> {
protected readonly queryLockableDurations: ObservableQueryLockableDurations;
Expand All @@ -17,7 +18,8 @@ export declare class ObservableQueryIncentivizedPools extends ObservableChainQue
protected readonly queryMintParmas: ObservableQueryMintParmas;
protected readonly queryEpochProvision: ObservableQueryEpochProvisions;
protected readonly queryEpochs: ObservableQueryEpochs;
constructor(kvStore: KVStore, chainId: string, chainGetter: ChainGetter, queryLockableDurations: ObservableQueryLockableDurations, queryDistrInfo: ObservableQueryDistrInfo, queryPools: ObservableQueryPools, queryMintParmas: ObservableQueryMintParmas, queryEpochProvision: ObservableQueryEpochProvisions, queryEpochs: ObservableQueryEpochs);
protected readonly queryGauge: ObservableQueryGuage;
constructor(kvStore: KVStore, chainId: string, chainGetter: ChainGetter, queryLockableDurations: ObservableQueryLockableDurations, queryDistrInfo: ObservableQueryDistrInfo, queryPools: ObservableQueryPools, queryMintParmas: ObservableQueryMintParmas, queryEpochProvision: ObservableQueryEpochProvisions, queryEpochs: ObservableQueryEpochs, queryGauge: ObservableQueryGuage);
/** Internally incentivized pools. */
get incentivizedPools(): string[];
/** Is incentivized internally. */
Expand All @@ -29,9 +31,9 @@ export declare class ObservableQueryIncentivizedPools extends ObservableChainQue
*/
readonly computeMostAPY: (poolId: string, priceStore: IPriceStore) => RatePretty;
/**
* Computes external incentive APY for the given duration
* Computes the external incentive APR for the given duration
*/
readonly computeExternalIncentiveAPYForSpecificDuration: (poolId: string, duration: Duration, priceStore: IPriceStore, fiatCurrency: FiatCurrency, allowedGauges: ExternalGauge[]) => RatePretty;
readonly computeExternalIncentiveGaugeAPR: (poolId: string, gaugeId: string, denom: string, duration: Duration, priceStore: IPriceStore, fiatCurrency: FiatCurrency) => RatePretty;
/**
* 리워드를 받을 수 있는 풀의 연당 이익률을 반환한다.
* 리워드를 받을 수 없는 풀일 경우 0를 리턴한다.
Expand Down
23 changes: 16 additions & 7 deletions packages/web/pages/pool/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,23 @@ const Pool: FunctionComponent = observer(() => {
fiat
);

const externalApy =
queryOsmosis.queryIncentivizedPools.computeExternalIncentiveAPYForSpecificDuration(
pool?.id ?? "",
gauge.duration,
priceStore,
fiat,
allowedGauges
const allowedExternalGauges =
pool?.id && ExternalIncentiveGaugeAllowList[pool.id]
? ExternalIncentiveGaugeAllowList[pool.id]
: [];

const externalApy = allowedExternalGauges.reduce((apy, allowList) => {
return apy.add(
queryOsmosis.queryIncentivizedPools.computeExternalIncentiveGaugeAPR(
pool?.id ?? "",
allowList.gaugeId,
allowList.denom,
gauge.duration,
priceStore,
fiat
)
);
}, new RatePretty(new Dec(0)));

const totalApr = baseApy.add(externalApy);

Expand Down