import { ObservableChainQuery , ObservableChainQueryMap , } from "../../chain-query" ; import { UnbondingDelegation , UnbondingDelegations } from "./types" ; import { KVStore } from "@keplr-wallet/common" ; import { ChainGetter } from "../../../common" ; import { CoinPretty , Int } from "@keplr-wallet/unit" ; import { computed , makeObservable } from "mobx" ; export class ObservableQueryUnbondingDelegationsInner extends ObservableChainQuery < UnbondingDelegations > { protected bech32Address : string ; constructor ( kvStore , chainId , chainGetter , bech32Address ) { super ( kvStore , chainId , chainGetter , `/staking/delegators/${ bech32Address }/unbonding_delegations` ) ; makeObservable ( this ) ; this . bech32Address = bech32Address ; } protected canFetch ( ) { return this . bech32Address . length > 0 ; } @ computed get total ( ) : CoinPretty { const stakeCurrency = this . chainGetter . getChain ( this . chainId ) . stakeCurrency ; if ( ! this . response ) { return new CoinPretty ( stakeCurrency , new Int ( 0 ) ) . ready ( false ) ; } let totalBalance = new Int ( 0 ) ; for ( const unbondingDelegation of this . response . data . result ) { for ( const entry of unbondingDelegation . entries ) { totalBalance = totalBalance . add ( new Int ( entry . balance ) ) ; } } return new CoinPretty ( stakeCurrency , totalBalance ) ; } @ computed get unbondingBalances ( ) : { validatorAddress : string ; entries : { creationHeight : Int ; completionTime : string ; balance : CoinPretty ; } [ ] ; } [ ] { const unbondings = this . unbondings ; const stakeCurrency = this . chainGetter . getChain ( this . chainId ) . stakeCurrency ; const result = [ ] ; for ( const unbonding of unbondings ) { const entries = [ ] ; for ( const entry of unbonding . entries ) { entries . push ( { creationHeight : new Int ( entry . creation_height ) , completionTime : entry . completion_time , balance : new CoinPretty ( stakeCurrency , new Int ( entry . balance ) ) , } ) ; } result . push ( { validatorAddress : unbonding . validator_address , entries , } ) ; } return result ; } @ computed get unbondings ( ) : UnbondingDelegation [ ] { if ( ! this . response ) { return [ ] ; } return this . response . data . result ; } } export class ObservableQueryUnbondingDelegations extends ObservableChainQueryMap < UnbondingDelegations > { constructor ( protected readonly kvStore , protected readonly chainId , protected readonly chainGetter ) { super ( kvStore , chainId , chainGetter , ( bech32Address ) => { return new ObservableQueryUnbondingDelegationsInner ( this . kvStore , this . chainId , this . chainGetter , bech32Address ) ; } ) ; } getQueryBech32Address ( bech32Address ) { return this . get ( bech32Address ) as ObservableQueryUnbondingDelegationsInner ; } }