import { ObservableChainQuery, ObservableChainQueryMap, } from "../../chain-query"; import { ChainGetter } from "../../../chain"; import { QuerySharedContext } from "../../../common"; import { computed, makeObservable } from "mobx"; import { ModuleAccountResponse } from "./types"; export function getModuleAccountAddress( response: ModuleAccountResponse | undefined, moduleName: string ): string | undefined { const account = response?.account; if (account?.name !== moduleName) { return undefined; } return account.base_account?.address; } export class ObservableQueryModuleAccountInner extends ObservableChainQuery { constructor( sharedContext: QuerySharedContext, chainId: string, chainGetter: ChainGetter, protected readonly moduleName: string ) { super( sharedContext, chainId, chainGetter, `/cosmos/auth/v1beta1/module_accounts/${encodeURIComponent(moduleName)}` ); makeObservable(this); } protected override canFetch(): boolean { return super.canFetch() && this.moduleName.length > 0; } @computed get moduleAccountAddress(): string | undefined { return getModuleAccountAddress(this.response?.data, this.moduleName); } } export class ObservableQueryModuleAccount extends ObservableChainQueryMap { constructor( sharedContext: QuerySharedContext, chainId: string, chainGetter: ChainGetter ) { super(sharedContext, chainId, chainGetter, (moduleName) => { return new ObservableQueryModuleAccountInner( this.sharedContext, this.chainId, this.chainGetter, moduleName ); }); } getQueryModuleName(moduleName: string): ObservableQueryModuleAccountInner { return this.get(moduleName) as ObservableQueryModuleAccountInner; } }