import { ObservableChainQuery } from "../chain-query" ; import { KVStore } from "@keplr-wallet/common" ; import { ChainGetter } from "../../common" ; import { CancelToken } from "axios" ; import { QueryResponse } from "../../common" ; import { Buffer } from "buffer/" ; export class ObservableCosmwasmContractChainQuery < T > extends ObservableChainQuery < T > { constructor ( kvStore , chainId , chainGetter , protected readonly contractAddress , protected obj ) { super ( kvStore , chainId , chainGetter , ObservableCosmwasmContractChainQuery . getUrlFromObj ( contractAddress , obj ) ) ; } protected static getUrlFromObj ( contractAddress , obj ) { const msg = JSON . stringify ( obj ) ; const query = Buffer . from ( msg ) . toString ( "base64" ) ; return `/wasm/v1/contract/${ contractAddress }/smart/${ query }` ; } protected setObj ( obj ) { this . obj = obj ; this . setUrl ( ObservableCosmwasmContractChainQuery . getUrlFromObj ( this . contractAddress , this . obj ) ) ; } protected canFetch ( ) { return this . contractAddress . length !== 0 ; } protected async fetchResponse ( cancelToken ) { const response = await super . fetchResponse ( cancelToken ) ; const wasmResult = ( response . data as unknown ) as { data : any ; } | undefined ; if ( ! wasmResult ) { throw new Error ( "Failed to get the response from the contract" ) ; } return { data : wasmResult . data as T , status : response . status , staled : false , timestamp : Date . now ( ) , } ; } }