|
| 1 | +import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; |
| 2 | +import { QueryClient } from "@cosmjs/stargate"; |
| 3 | +import { WasmExtension, setupWasmExtension } from "@cosmjs/cosmwasm-stargate"; |
| 4 | + |
| 5 | +// TODO: expose the querier and consume them in price pusher |
| 6 | + |
| 7 | +/** |
| 8 | + * Interface for classes implementing a querier for a cosmwasm chain. |
| 9 | + * |
| 10 | + * The querier interacts with contracts only and can get it's info, stored state. |
| 11 | + * It can also query the `Query methods` defined in the contract. |
| 12 | + * |
| 13 | + * For contract dependent response, you need to look into contract specific schema in order |
| 14 | + * to know what to expect |
| 15 | + * |
| 16 | + * @interface ChainQuerier |
| 17 | + */ |
| 18 | +export interface ChainQuerier { |
| 19 | + /** |
| 20 | + * `getContractInfo` gets the contract info for the give contract address. |
| 21 | + * @param {ContractInfoRequest} req |
| 22 | + * @param {string} req.contractAddr |
| 23 | + * @returns {ContractInfoResponse} |
| 24 | + * - {number} res.codeId. The codeId of the contract's code. |
| 25 | + * - {string} res.creator. The creator of the contract. |
| 26 | + * - {string} res.adminAddr - Address of the current admin. |
| 27 | + * - {string} res.label - The label with which the contract was instantiated |
| 28 | + * |
| 29 | + * @throws an error if it fails. |
| 30 | + */ |
| 31 | + getContractInfo(req: ContractInfoRequest): Promise<ContractInfoResponse>; |
| 32 | + |
| 33 | + /** |
| 34 | + * `getSmartContractState` query the `Query methods` implemented in the contract |
| 35 | + * @param {SmartContractRequest} req |
| 36 | + * @param {string} req.contractAddr |
| 37 | + * @param {Object} req.query - The query object for the contract. It accepts any object as it is contract dependent. |
| 38 | + * @returns {Object} - It returns an object. As the response is contract dependent we can't have a structure for it. |
| 39 | + * |
| 40 | + * @throws an error if it fails. |
| 41 | + */ |
| 42 | + getSmartContractState(req: SmartContractRequest): Promise<Object>; |
| 43 | + |
| 44 | + /** |
| 45 | + * Contracts local storage on chain is structured as key-value pairs. |
| 46 | + * `getSmartContractState` query the local storage of a contract for the given key. |
| 47 | + * @param {RawContractStateRequest} req |
| 48 | + * @param {string} req.contractAddr |
| 49 | + * @param {Buffer} req.key |
| 50 | + * @returns {Object} - It returns an object. As the response is contract dependent we can't have a structure for it. |
| 51 | + * |
| 52 | + * @throws an error if it fails. |
| 53 | + */ |
| 54 | + getRawContractState(req: RawContractStateRequest): Promise<Object>; |
| 55 | + |
| 56 | + /** |
| 57 | + * Contracts local storage on chain is structured as key-value pairs. |
| 58 | + * `getAllContractState` query the local storage of a contract for all such pairs. |
| 59 | + * @param {AllContractStateRequest} req |
| 60 | + * @param {string} req.contractAddr |
| 61 | + * @returns {Object} - It returns an object. As the response is contract dependent we can't have a structure for it. |
| 62 | + * |
| 63 | + * @throws an error if it fails. |
| 64 | + */ |
| 65 | + getAllContractState(req: AllContractStateRequest): Promise<Object>; |
| 66 | +} |
| 67 | + |
| 68 | +export type ContractInfoRequest = { |
| 69 | + contractAddr: string; |
| 70 | +}; |
| 71 | + |
| 72 | +export type ContractInfoResponse = { |
| 73 | + codeId: number; |
| 74 | + creator: string; |
| 75 | + adminAddr: string; |
| 76 | + label: string; |
| 77 | +}; |
| 78 | + |
| 79 | +export type SmartContractRequest = { |
| 80 | + contractAddr: string; |
| 81 | + query: Object; |
| 82 | +}; |
| 83 | + |
| 84 | +export type RawContractStateRequest = { |
| 85 | + contractAddr: string; |
| 86 | + key: Buffer; |
| 87 | +}; |
| 88 | + |
| 89 | +export type AllContractStateRequest = { |
| 90 | + contractAddr: string; |
| 91 | +}; |
| 92 | + |
| 93 | +export class CosmwasmQuerier implements ChainQuerier { |
| 94 | + private readonly wasmQueryClient: WasmExtension; |
| 95 | + private constructor(readonly tendermintClient: Tendermint34Client) { |
| 96 | + this.wasmQueryClient = setupWasmExtension( |
| 97 | + new QueryClient(tendermintClient) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + async getContractInfo( |
| 102 | + req: ContractInfoRequest |
| 103 | + ): Promise<ContractInfoResponse> { |
| 104 | + const { contractAddr } = req; |
| 105 | + |
| 106 | + const { wasm: wasmQueryClient } = this.wasmQueryClient; |
| 107 | + |
| 108 | + const { contractInfo } = await wasmQueryClient.getContractInfo( |
| 109 | + contractAddr |
| 110 | + ); |
| 111 | + |
| 112 | + if (contractInfo === undefined) |
| 113 | + throw new Error("error fetching contract info"); |
| 114 | + |
| 115 | + return { |
| 116 | + ...contractInfo, |
| 117 | + codeId: contractInfo.codeId.toNumber(), |
| 118 | + adminAddr: contractInfo.admin, |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + async getSmartContractState(req: SmartContractRequest): Promise<Object> { |
| 123 | + const { contractAddr, query } = req; |
| 124 | + |
| 125 | + const { wasm: wasmQueryClient } = this.wasmQueryClient; |
| 126 | + |
| 127 | + return await wasmQueryClient.queryContractSmart(contractAddr, query); |
| 128 | + } |
| 129 | + |
| 130 | + async getRawContractState(req: RawContractStateRequest): Promise<Object> { |
| 131 | + const { contractAddr, key } = req; |
| 132 | + |
| 133 | + const { wasm: wasmQueryClient } = this.wasmQueryClient; |
| 134 | + |
| 135 | + const { data } = await wasmQueryClient.queryContractRaw(contractAddr, key); |
| 136 | + return JSON.parse(Buffer.from(data).toString()); |
| 137 | + } |
| 138 | + |
| 139 | + async getAllContractState(req: AllContractStateRequest): Promise<Object> { |
| 140 | + const { contractAddr } = req; |
| 141 | + |
| 142 | + const { wasm: wasmQueryClient } = this.wasmQueryClient; |
| 143 | + |
| 144 | + const { models } = await wasmQueryClient.getAllContractState(contractAddr); |
| 145 | + |
| 146 | + const state = models.reduce((prevValue, model) => { |
| 147 | + const key = Buffer.from(model.key).toString(); |
| 148 | + const value = Buffer.from(model.value).toString(); |
| 149 | + |
| 150 | + prevValue[key] = value; |
| 151 | + |
| 152 | + return prevValue; |
| 153 | + }, {} as any); |
| 154 | + |
| 155 | + return state; |
| 156 | + } |
| 157 | + |
| 158 | + static async connect(endpoint: string): Promise<CosmwasmQuerier> { |
| 159 | + const tendermintClient = await Tendermint34Client.connect(endpoint); |
| 160 | + return new CosmwasmQuerier(tendermintClient); |
| 161 | + } |
| 162 | +} |
0 commit comments