import { computed , makeObservable } from "mobx" ; import { Dec , DecUtils , Int , IntPretty } from "@keplr-wallet/unit" ; import { ObservableQuerySupplyTotal } from "./supply" ; import { MintingInflation } from "./types" ; import { StakingPool } from "../staking/types" ; import { ObservableChainQuery } from "../../chain-query" ; import { ChainGetter } from "../../../common" ; import { ObservableQueryIrisMintingInfation } from "./iris-minting" ; import { ObservableQuerySifchainLiquidityAPY } from "./sifchain" ; import { ObservableQueryOsmosisEpochProvisions , ObservableQueryOsmosisEpochs , ObservableQueryOsmosisMintParmas , } from "./osmosis" ; export class ObservableQueryInflation { constructor ( protected readonly chainId , protected readonly chainGetter , protected readonly _queryMint , protected readonly _queryPool , protected readonly _querySupplyTotal , protected readonly _queryIrisMint , protected readonly _querySifchainAPY , protected readonly _queryOsmosisEpochs , protected readonly _queryOsmosisEpochProvisions , protected readonly _queryOsmosisMintParams ) { makeObservable ( this ) ; } get error ( ) { return ( this . _queryMint . error ?? this . _queryPool . error ?? this . _querySupplyTotal . getQueryStakeDenom ( ) . error ) ; } get isFetching ( ) { return ( this . _queryMint . isFetching || this . _queryPool . isFetching || this . _querySupplyTotal . getQueryStakeDenom ( ) . isFetching ) ; } @ computed get inflation ( ) : IntPretty { try { let dec ; const chainInfo = this . chainGetter . getChain ( this . chainId ) ; if ( chainInfo . chainId . startsWith ( "irishub" ) ) { dec = new Dec ( this . _queryIrisMint . response ?. data . result . inflation ?? "0" ) . mul ( DecUtils . getPrecisionDec ( 2 ) ) ; } else if ( chainInfo . chainId . startsWith ( "sifchain" ) ) { return new IntPretty ( new Dec ( this . _querySifchainAPY . liquidityAPY . toString ( ) ) ) ; } else if ( chainInfo . chainId . startsWith ( "osmosis" ) ) { const mintParams = this . _queryOsmosisMintParams ; if ( mintParams . epochIdentifier ) { const epochDuration = this . _queryOsmosisEpochs . getEpoch ( mintParams . epochIdentifier ) . duration ; if ( epochDuration ) { const epochProvision = this . _queryOsmosisEpochProvisions . epochProvisions ; if ( epochProvision && this . _querySupplyTotal . getQueryStakeDenom ( ) . response ) { const mintingEpochProvision = new Dec ( epochProvision . toDec ( ) . mul ( mintParams . distributionProportions . staking ) . truncate ( ) . toString ( ) ) ; const yearMintingProvision = mintingEpochProvision . mul ( new Dec ( ( ( 365 * 24 * 3600 ) / epochDuration ) . toString ( ) ) ) ; const total = DecUtils . getPrecisionDec ( 8 ) ; dec = yearMintingProvision . quo ( total ) . mul ( DecUtils . getPrecisionDec ( 2 ) ) ; } } } } else { dec = new Dec ( this . _queryMint . response ?. data . result ?? "0" ) . mul ( DecUtils . getPrecisionDec ( 2 ) ) ; } if ( ! dec || dec . equals ( new Dec ( 0 ) ) ) { return new IntPretty ( new Int ( 0 ) ) . ready ( false ) ; } if ( this . _queryPool . response && this . _querySupplyTotal . getQueryStakeDenom ( ) . response ) { const bondedToken = new Dec ( this . _queryPool . response . data . result . bonded_tokens ) ; const totalStr = ( ( ) => { if ( chainInfo . chainId . startsWith ( "osmosis" ) ) { return DecUtils . getPrecisionDec ( 8 + 6 ) . toString ( ) ; } const response = this . _querySupplyTotal . getQueryStakeDenom ( ) . response ! . data . result ; if ( typeof response === "string" ) { return response ; } else { return response . amount ; } } ) ( ) ; const total = new Dec ( totalStr ) ; if ( total . gt ( new Dec ( 0 ) ) ) { const ratio = bondedToken . quo ( total ) ; dec = dec . quo ( ratio ) ; } } return new IntPretty ( dec ) ; } catch ( e ) { console . log ( e ) ; return new IntPretty ( new Int ( 0 ) ) . ready ( false ) ; } } }