-
Notifications
You must be signed in to change notification settings - Fork 310
/
queries.ts
137 lines (129 loc) · 4.9 KB
/
queries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { fromUtf8, toUtf8 } from "@cosmjs/encoding";
import { createPagination, createProtobufRpcClient, QueryClient } from "@cosmjs/stargate";
import {
QueryAllContractStateResponse,
QueryClientImpl,
QueryCodeResponse,
QueryCodesResponse,
QueryContractHistoryResponse,
QueryContractInfoResponse,
QueryContractsByCodeResponse,
QueryRawContractStateResponse,
} from "cosmjs-types/cosmwasm/wasm/v1/query";
import Long from "long";
/**
* An object containing a parsed JSON document. The result of JSON.parse().
* This doesn't provide any type safety over `any` but expresses intent in the code.
*
* This type is returned by `queryContractSmart`.
*/
export type JsonObject = any;
export interface WasmExtension {
readonly wasm: {
readonly listCodeInfo: (paginationKey?: Uint8Array) => Promise<QueryCodesResponse>;
/**
* Downloads the original wasm bytecode by code ID.
*
* Throws an error if no code with this id
*/
readonly getCode: (id: number) => Promise<QueryCodeResponse>;
readonly listContractsByCodeId: (
id: number,
paginationKey?: Uint8Array,
) => Promise<QueryContractsByCodeResponse>;
/**
* Returns null when contract was not found at this address.
*/
readonly getContractInfo: (address: string) => Promise<QueryContractInfoResponse>;
/**
* Returns null when contract history was not found for this address.
*/
readonly getContractCodeHistory: (
address: string,
paginationKey?: Uint8Array,
) => Promise<QueryContractHistoryResponse>;
/**
* Returns all contract state.
* This is an empty array if no such contract, or contract has no data.
*/
readonly getAllContractState: (
address: string,
paginationKey?: Uint8Array,
) => Promise<QueryAllContractStateResponse>;
/**
* Returns the data at the key if present (unknown decoded json),
* or null if no data at this (contract address, key) pair
*/
readonly queryContractRaw: (address: string, key: Uint8Array) => Promise<QueryRawContractStateResponse>;
/**
* Makes a smart query on the contract and parses the response as JSON.
* Throws error if no such contract exists, the query format is invalid or the response is invalid.
*/
readonly queryContractSmart: (address: string, query: JsonObject) => Promise<JsonObject>;
};
}
export function setupWasmExtension(base: QueryClient): WasmExtension {
const rpc = createProtobufRpcClient(base);
// Use this service to get easy typed access to query methods
// This cannot be used for proof verification
const queryService = new QueryClientImpl(rpc);
return {
wasm: {
listCodeInfo: async (paginationKey?: Uint8Array) => {
const request = {
pagination: createPagination(paginationKey),
};
return queryService.Codes(request);
},
getCode: async (id: number) => {
const request = { codeId: Long.fromNumber(id) };
return queryService.Code(request);
},
listContractsByCodeId: async (id: number, paginationKey?: Uint8Array) => {
const request = {
codeId: Long.fromNumber(id),
pagination: createPagination(paginationKey),
};
return queryService.ContractsByCode(request);
},
getContractInfo: async (address: string) => {
const request = { address: address };
return queryService.ContractInfo(request);
},
getContractCodeHistory: async (address: string, paginationKey?: Uint8Array) => {
const request = {
address: address,
pagination: createPagination(paginationKey),
};
return queryService.ContractHistory(request);
},
getAllContractState: async (address: string, paginationKey?: Uint8Array) => {
const request = {
address: address,
pagination: createPagination(paginationKey),
};
return queryService.AllContractState(request);
},
queryContractRaw: async (address: string, key: Uint8Array) => {
const request = { address: address, queryData: key };
return queryService.RawContractState(request);
},
queryContractSmart: async (address: string, query: JsonObject) => {
const request = { address: address, queryData: toUtf8(JSON.stringify(query)) };
const { data } = await queryService.SmartContractState(request);
// By convention, smart queries must return a valid JSON document (see https://github.com/CosmWasm/cosmwasm/issues/144)
let responseText: string;
try {
responseText = fromUtf8(data);
} catch (error) {
throw new Error(`Could not UTF-8 decode smart query response from contract: ${error}`);
}
try {
return JSON.parse(responseText);
} catch (error) {
throw new Error(`Could not JSON parse smart query response from contract: ${error}`);
}
},
},
};
}