import { ObservableChainQuery } from "../../chain-query" ; import { GovProposals } from "./types" ; import { computed , makeObservable , observable , runInAction } from "mobx" ; import { ObservableQueryGovParamDeposit , ObservableQueryGovParamTally , ObservableQueryGovParamVoting , } from "./params" ; import { KVStore } from "@keplr-wallet/common" ; import { ChainGetter } from "../../../common" ; import { StakingPool } from "../staking/types" ; import { DeepReadonly } from "utility-types" ; import { Dec , DecUtils , Int , IntPretty } from "@keplr-wallet/unit" ; import { computedFn } from "mobx-utils" ; import { ObservableQueryProposal } from "./proposal" ; export class ObservableQueryGovernance extends ObservableChainQuery < GovProposals > { @ observable . ref protected paramDeposit ? : ObservableQueryGovParamDeposit = undefined ; @ observable . ref protected paramVoting ? : ObservableQueryGovParamVoting = undefined ; @ observable . ref protected paramTally ? : ObservableQueryGovParamTally = undefined ; constructor ( kvStore , chainId , chainGetter , protected readonly _queryPool ) { super ( kvStore , chainId , chainGetter , "/gov/proposals?limit=1000" ) ; makeObservable ( this ) ; } getQueryPool ( ) { return this . _queryPool ; } getQueryParamDeposit ( ) { if ( ! this . paramDeposit ) { runInAction ( ( ) => { this . paramDeposit = new ObservableQueryGovParamDeposit ( this . kvStore , this . chainId , this . chainGetter ) ; } ) ; } return this . paramDeposit ! ; } getQueryParamVoting ( ) { if ( ! this . paramVoting ) { runInAction ( ( ) => { this . paramVoting = new ObservableQueryGovParamVoting ( this . kvStore , this . chainId , this . chainGetter ) ; } ) ; } return this . paramVoting ! ; } getQueryParamTally ( ) { if ( ! this . paramTally ) { runInAction ( ( ) => { this . paramTally = new ObservableQueryGovParamTally ( this . kvStore , this . chainId , this . chainGetter ) ; } ) ; } return this . paramTally ! ; } @ computed get quorum ( ) : IntPretty { const paramTally = this . getQueryParamTally ( ) ; if ( ! paramTally . response ) { return new IntPretty ( new Int ( 0 ) ) . ready ( false ) ; } let quorum = new Dec ( paramTally . response . data . result . quorum ) ; quorum = quorum . mulTruncate ( DecUtils . getPrecisionDec ( 2 ) ) ; return new IntPretty ( quorum ) ; } @ computed get proposals ( ) : DeepReadonly < ObservableQueryProposal [ ] > { if ( ! this . response ) { return [ ] ; } const result = [ ] ; for ( const raw of this . response . data . result ) { result . push ( new ObservableQueryProposal ( this . kvStore , this . chainId , this . chainGetter , raw , this ) ) ; } return result . reverse ( ) ; } readonly getProposal = computedFn ( ( id ) : DeepReadonly < ObservableQueryProposal > | undefined => { return this . proposals . find ( ( proposal ) => proposal . id === id ) ; } ) ; }