Skip to content

Commit

Permalink
fix: query-raw pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerAnsh committed Oct 11, 2024
1 parent 5490a8b commit 5e9c214
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
26 changes: 15 additions & 11 deletions packages/andrjs/src/AndromedaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SigningStargateClientOptions,
StdFee,
calculateFee,
createProtobufRpcClient,
} from "@cosmjs/stargate";
import { ADOAPI } from "./api";

Expand All @@ -18,6 +19,7 @@ import type { Fee, Msg } from "./types";
import ADOSchemaAPI from "api/ADOSchemaAPI";
import { RpcClient } from "@cosmjs/tendermint-rpc";
import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination";
import { QueryClientImpl as WasmQueryClientImpl } from 'cosmjs-types/cosmwasm/wasm/v1/query'

/**
* A helper class for interacting with the Andromeda ecosystem
Expand Down Expand Up @@ -221,18 +223,20 @@ export default class AndromedaClient {
*/
async queryContractRawAll(address: string, pagination: Partial<PageRequest>) {
this.preMessage();
const result = await this.chainClient!.rawQueryClient!.wasm.getAllContractState(
address,
await this.encodePagination(pagination)
);
return result.models.map(model => ({
key: Buffer.from(model.key).toString('utf8'),
value: Buffer.from(model.value).toString('utf8')
}))
}
const rpcClient = createProtobufRpcClient(this.chainClient!.rawQueryClient!);
const wasmQueryClient = new WasmQueryClientImpl(rpcClient);

async encodePagination(pagination: Partial<PageRequest>) {
return PageRequest.encode(PageRequest.fromPartial(pagination)).finish();
const result = await wasmQueryClient.AllContractState({
address,
pagination: PageRequest.fromPartial(pagination)
});
return {
states: result.models.map(model => ({
key: Buffer.from(model.key).toString('utf8'),
value: Buffer.from(model.value).toString('utf8')
})),
pagination: result.pagination
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/andrjs/src/clients/ChainClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default interface ChainClient {
// The client used to query the chain
queryClient?: CosmWasmClient;


rawQueryClient?: QueryClient & TxExtension & WasmExtension

// The current signer address
Expand Down
1 change: 1 addition & 0 deletions packages/andrjs/src/clients/CosmClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default class CosmClient extends BaseChainClient implements ChainClient {

/** Client for querying blockchain data */
public queryClient?: ChainClient["queryClient"];

/** Client for querying data with raw implementation */
public rawQueryClient?: ChainClient["rawQueryClient"];
/** Client for interacting with the Comet BFT consensus engine */
Expand Down
38 changes: 29 additions & 9 deletions packages/cli/src/handlers/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ export const commands: Commands = {
],
flags: {
limit: {
description: "Paginate response, 0 mean no limit",
description: "Limit number of results",
usage: "--limit 10",
},
offset: {
description: "Paginate response",
description: "Offset number of results (This might not be supported by all chains)",
usage: "--offset 0",
},
['next-key']: {
description: "Next key to paginate from",
usage: "--next-key <key>",
},
['next-key-bytes']: {
description: "Next key to paginate from (bytes)",
usage: "--next-key-bytes <key>",
}
}
},
execute: {
Expand Down Expand Up @@ -258,22 +266,34 @@ async function queryRawHandler(input: string[], flags: Flags) {
const [contractAddr] = input;
const limit = BigInt(flags.limit ?? '10');
const offset = BigInt(flags.offset ?? '0');
const nextKey = flags['next-key'] as string | undefined;
const nextKeyBytes = flags['next-key-bytes'] as string | undefined;

if (nextKeyBytes && nextKey) {
throw new Error("Cannot provide both next-key and next-key-bytes");
}

const key = nextKeyBytes ? Uint8Array.from(Buffer.from(nextKeyBytes, 'hex')) : nextKey ? Uint8Array.from(Buffer.from(nextKey, 'utf8')) : undefined;

const states = await displaySpinnerAsync(
"Querying contrac key...",
async () => await State.client.queryContractRawAll(contractAddr, { limit, offset })
async () => await State.client.queryContractRawAll(contractAddr, { limit, offset, key })
);
console.log();
const infoTable = new Table(logTableConfig);


infoTable.push([pc.bold("Key"), ":", pc.bold("Value")]);
for (const state of states) {
infoTable.push([pc.blue(state.key), ":", state.value]);
for (const state of states.states) {
console.log(pc.blue(state.key));
console.log(state.value);
console.log();
}
console.log(infoTable.toString());
console.log();
console.log(pc.gray(`Limit - ${limit}, Offset - ${offset}`))
console.log(pc.gray(`Limit - ${limit}, Offset - ${offset}, key - ${key ? Buffer.from(key).toString('utf8') : 'None'}`))
if (states.pagination) {
console.log(pc.gray(`Next Key - ${Buffer.from(states.pagination.nextKey).toString('utf8')}`));
console.log(pc.gray(`Next Key Bytes - ${Buffer.from(states.pagination.nextKey).toString('hex')}`));
console.log(pc.gray(`Total - ${states.pagination.total.toString()}`));
}
console.log()


Expand Down

0 comments on commit 5e9c214

Please sign in to comment.