mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Fix malformed REST response/parsing
This commit is contained in:
parent
5677976463
commit
13c708e787
@ -468,7 +468,7 @@ describe("RestClient", () => {
|
||||
.catch(error => expect(error).toMatch(`No contract with address ${beneficiaryAddress}`));
|
||||
});
|
||||
|
||||
fdescribe("contract state", () => {
|
||||
describe("contract state", () => {
|
||||
const client = new RestClient(httpUrl);
|
||||
const noContract = makeRandomAddress();
|
||||
const expectedKey = toAscii("config");
|
||||
@ -496,9 +496,8 @@ describe("RestClient", () => {
|
||||
const state = await client.getAllContractState(contractAddress!);
|
||||
expect(state.length).toEqual(1);
|
||||
const data = state[0];
|
||||
expect(data.key.toLowerCase()).toEqual(toHex(expectedKey));
|
||||
const value = JSON.parse(fromAscii(fromBase64(data.val)));
|
||||
console.log(value);
|
||||
expect(data.key).toEqual(expectedKey);
|
||||
const value = JSON.parse(fromAscii(data.val));
|
||||
expect(value.verifier).toBeDefined();
|
||||
expect(value.beneficiary).toBeDefined();
|
||||
|
||||
|
@ -1,7 +1,17 @@
|
||||
import { Encoding } from "@iov/encoding";
|
||||
import axios, { AxiosError, AxiosInstance } from "axios";
|
||||
|
||||
import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, isAminoStdTx, StdTx, WasmData } from "./types";
|
||||
import {
|
||||
AminoTx,
|
||||
CodeInfo,
|
||||
ContractInfo,
|
||||
CosmosSdkAccount,
|
||||
isAminoStdTx,
|
||||
Model,
|
||||
parseWasmData,
|
||||
StdTx,
|
||||
WasmData,
|
||||
} from "./types";
|
||||
|
||||
const { fromBase64, fromUtf8, toHex, toUtf8 } = Encoding;
|
||||
|
||||
@ -311,15 +321,11 @@ export class RestClient {
|
||||
|
||||
// Returns all contract state.
|
||||
// This is an empty array if no such contract, or contract has no data.
|
||||
public async getAllContractState(address: string): Promise<readonly WasmData[]> {
|
||||
public async getAllContractState(address: string): Promise<readonly Model[]> {
|
||||
const path = `/wasm/contract/${address}/state`;
|
||||
const responseData = (await this.get(path)) as WasmResponse<WasmData[]>;
|
||||
console.log("all state");
|
||||
console.log(responseData);
|
||||
console.log("***");
|
||||
const r = unwrapWasmResponse(responseData);
|
||||
console.log(r);
|
||||
return r || [];
|
||||
return r ? r.map(parseWasmData) : [];
|
||||
}
|
||||
|
||||
// Returns the data at the key if present (unknown decoded json),
|
||||
@ -340,7 +346,6 @@ export class RestClient {
|
||||
const responseData = (await this.get(path)) as WasmResponse<SmartQueryResponse>;
|
||||
const result = unwrapWasmResponse(responseData);
|
||||
// no extra parse here for now, see https://github.com/confio/cosmwasm/issues/144
|
||||
console.log(result);
|
||||
return fromBase64(result.smart);
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,7 @@
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
const { fromBase64, fromHex } = Encoding;
|
||||
|
||||
// We will move all needed *interfaces* from amino-js here
|
||||
// This means bcp can just import them from here (if needed at all)
|
||||
export interface Tx {
|
||||
@ -193,3 +197,16 @@ export interface WasmData {
|
||||
// value is base64 encoded
|
||||
readonly val: string;
|
||||
}
|
||||
|
||||
// Model is a parsed WasmData object
|
||||
export interface Model {
|
||||
readonly key: Uint8Array;
|
||||
readonly val: Uint8Array;
|
||||
}
|
||||
|
||||
export function parseWasmData({ key, val }: WasmData): Model {
|
||||
return {
|
||||
key: fromHex(key),
|
||||
val: fromBase64(val),
|
||||
};
|
||||
}
|
||||
|
4
packages/sdk/types/restclient.d.ts
vendored
4
packages/sdk/types/restclient.d.ts
vendored
@ -1,4 +1,4 @@
|
||||
import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, StdTx, WasmData } from "./types";
|
||||
import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, Model, StdTx } from "./types";
|
||||
interface NodeInfo {
|
||||
readonly network: string;
|
||||
}
|
||||
@ -102,7 +102,7 @@ export declare class RestClient {
|
||||
listContractAddresses(): Promise<readonly string[]>;
|
||||
listContractsByCodeId(id: number): Promise<readonly ContractInfo[]>;
|
||||
getContractInfo(address: string): Promise<ContractInfo>;
|
||||
getAllContractState(address: string): Promise<readonly WasmData[]>;
|
||||
getAllContractState(address: string): Promise<readonly Model[]>;
|
||||
queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null>;
|
||||
queryContractSmart(address: string, query: object): Promise<Uint8Array>;
|
||||
}
|
||||
|
5
packages/sdk/types/types.d.ts
vendored
5
packages/sdk/types/types.d.ts
vendored
@ -143,4 +143,9 @@ export interface WasmData {
|
||||
readonly key: string;
|
||||
readonly val: string;
|
||||
}
|
||||
export interface Model {
|
||||
readonly key: Uint8Array;
|
||||
readonly val: Uint8Array;
|
||||
}
|
||||
export declare function parseWasmData({ key, val }: WasmData): Model;
|
||||
export {};
|
||||
|
Loading…
x
Reference in New Issue
Block a user