import { computed , makeObservable , override } from "mobx" ; import { DenomHelper , KVStore } from "@keplr-wallet/common" ; import { ChainGetter , QueryResponse } from "../../common" ; import { ObservableQuerySecretContractCodeHash } from "./contract-hash" ; import { QueryError } from "../../common" ; import { CoinPretty , Int } from "@keplr-wallet/unit" ; import { BalanceRegistry , ObservableQueryBalanceInner } from "../balances" ; import { ObservableSecretContractChainQuery } from "./contract-query" ; import { CancelToken } from "axios" ; import { WrongViewingKeyError } from "./errors" ; import { Keplr } from "@keplr-wallet/types" ; export class ObservableQuerySecret20Balance extends ObservableSecretContractChainQuery < { balance : { amount : string ; } ; [ "viewing_key_error" ] ? : { msg : string ; } ; } > { constructor ( kvStore , chainId , chainGetter , protected readonly apiGetter , protected readonly contractAddress , protected readonly bech32Address , protected readonly parent , protected readonly querySecretContractCodeHash ) { super ( kvStore , chainId , chainGetter , apiGetter , contractAddress , { } , querySecretContractCodeHash ) ; makeObservable ( this ) ; if ( ! this . viewingKey ) { this . setError ( { status : 0 , statusText : "Viewing key is empty" , message : "Viewing key is empty" , } ) ; } else { this . setObj ( { balance : { address : bech32Address , key : this . viewingKey } , } ) ; } } @ computed get viewingKey ( ) : string { const currency = this . parent . currency ; if ( "type" in currency && currency . type === "secret20" ) { return currency . viewingKey ; } return "" ; } protected canFetch ( ) { return ( super . canFetch ( ) && this . bech32Address !== "" && this . viewingKey !== "" ) ; } protected async fetchResponse ( cancelToken ) { const result = await super . fetchResponse ( cancelToken ) ; if ( result . data [ "viewing_key_error" ] ) { throw new WrongViewingKeyError ( result . data [ "viewing_key_error" ] ?. msg ) ; } return result ; } } export class ObservableQuerySecret20BalanceInner extends ObservableQueryBalanceInner { protected readonly querySecret20Balance : ObservableQuerySecret20Balance ; constructor ( kvStore , chainId , chainGetter , protected readonly apiGetter , denomHelper , protected readonly bech32Address , protected readonly querySecretContractCodeHash ) { super ( kvStore , chainId , chainGetter , "" , denomHelper ) ; makeObservable ( this ) ; this . querySecret20Balance = new ObservableQuerySecret20Balance ( kvStore , chainId , chainGetter , this . apiGetter , denomHelper . contractAddress , bech32Address , this , this . querySecretContractCodeHash ) ; } protected canFetch ( ) { return false ; } @ override * fetch ( ) { yield this . querySecret20Balance . fetch ( ) ; } get isFetching ( ) : boolean { return ( this . querySecretContractCodeHash . getQueryContract ( this . denomHelper . contractAddress ) . isFetching || this . querySecret20Balance . isFetching ) ; } get error ( ) : Readonly < QueryError < unknown > > | undefined { return ( this . querySecretContractCodeHash . getQueryContract ( this . denomHelper . contractAddress ) . error || this . querySecret20Balance . error ) ; } @ computed get balance ( ) : CoinPretty { const denom = this . denomHelper . denom ; const chainInfo = this . chainGetter . getChain ( this . chainId ) ; const currency = chainInfo . findCurrency ( denom ) ; if ( ! currency ) { throw new Error ( `Unknown currency: ${ denom }` ) ; } if ( ! this . querySecret20Balance . response || ! this . querySecret20Balance . response . data . balance ) { return new CoinPretty ( currency , new Int ( 0 ) ) . ready ( false ) ; } return new CoinPretty ( currency , new Int ( this . querySecret20Balance . response . data . balance . amount ) ) ; } } export class ObservableQuerySecret20BalanceRegistry implements BalanceRegistry { constructor ( protected readonly kvStore , protected readonly apiGetter , protected readonly querySecretContractCodeHash ) { } getBalanceInner ( chainId , chainGetter , bech32Address , minimalDenom ) { const denomHelper = new DenomHelper ( minimalDenom ) ; if ( denomHelper . type === "secret20" ) { return new ObservableQuerySecret20BalanceInner ( this . kvStore , chainId , chainGetter , this . apiGetter , denomHelper , bech32Address , this . querySecretContractCodeHash ) ; } } }