import { DenomHelper , KVStore } from "@keplr-wallet/common" ; import { ChainGetter , QueryResponse } from "../../../common" ; import { computed , makeObservable , override } from "mobx" ; import { CoinPretty , Int } from "@keplr-wallet/unit" ; import { StoreUtils } from "../../../common" ; import { BalanceRegistry , ObservableQueryBalanceInner } from "../../balances" ; import { ObservableChainQuery } from "../../chain-query" ; import { Balances } from "./types" ; export class ObservableQueryBalanceNative extends ObservableQueryBalanceInner { constructor ( kvStore , chainId , chainGetter , denomHelper , protected readonly nativeBalances ) { super ( kvStore , chainId , chainGetter , "" , denomHelper ) ; makeObservable ( this ) ; } protected canFetch ( ) { return false ; } get isFetching ( ) : boolean { return this . nativeBalances . isFetching ; } get error ( ) { return this . nativeBalances . error ; } get response ( ) { return this . nativeBalances . response ; } @ override * fetch ( ) { yield this . nativeBalances . fetch ( ) ; } @ computed get balance ( ) : CoinPretty { const currency = this . currency ; if ( ! this . nativeBalances . response ) { return new CoinPretty ( currency , new Int ( 0 ) ) . ready ( false ) ; } return StoreUtils . getBalanceFromCurrency ( currency , this . nativeBalances . response . data . result ) ; } } export class ObservableQueryCosmosBalances extends ObservableChainQuery < Balances > { protected bech32Address : string ; protected duplicatedFetchCheck : boolean = false ; constructor ( kvStore , chainId , chainGetter , bech32Address ) { super ( kvStore , chainId , chainGetter , `/bank/balances/${ bech32Address }` ) ; this . bech32Address = bech32Address ; makeObservable ( this ) ; } protected canFetch ( ) { return this . bech32Address . length > 0 ; } @ override * fetch ( ) { if ( ! this . duplicatedFetchCheck ) { this . duplicatedFetchCheck = true ; setTimeout ( ( ) => { this . duplicatedFetchCheck = false ; } , 1 ) ; yield super . fetch ( ) ; } } protected setResponse ( response ) { super . setResponse ( response ) ; const chainInfo = this . chainGetter . getChain ( this . chainId ) ; const denoms = response . data . result . map ( ( coin ) => coin . denom ) ; chainInfo . addUnknownCurrencies ( ... denoms ) ; } } export class ObservableQueryCosmosBalanceRegistry implements BalanceRegistry { protected nativeBalances : Map < string , ObservableQueryCosmosBalances > = new Map ( ) ; constructor ( protected readonly kvStore ) { } getBalanceInner ( chainId , chainGetter , bech32Address , minimalDenom ) { const denomHelper = new DenomHelper ( minimalDenom ) ; if ( denomHelper . type !== "native" ) { return ; } const key = `${ chainId }/${ bech32Address }` ; if ( ! this . nativeBalances . has ( key ) ) { this . nativeBalances . set ( key , new ObservableQueryCosmosBalances ( this . kvStore , chainId , chainGetter , bech32Address ) ) ; } return new ObservableQueryBalanceNative ( this . kvStore , chainId , chainGetter , denomHelper , this . nativeBalances . get ( key ) ! ) ; } }