Skip to content

Instantly share code, notes, and snippets.

@adairrr
Last active August 2, 2022 11:12
Show Gist options
  • Save adairrr/93ea2982bea27b5bb8bf2474de30cd90 to your computer and use it in GitHub Desktop.
Save adairrr/93ea2982bea27b5bb8bf2474de30cd90 to your computer and use it in GitHub Desktop.
An example of a paged react-query with a custom cosmwasm client generated with cosmwasm/ts-codegen
export type Addr = string;
export type AssetInfoBase_for_Addr = {
cw20: Addr;
} | {
native: string;
};
export interface AssetListResponse {
assets: [string, AssetInfoBase_for_Addr][];
}
// elsewhere...
export class MemoryQueryClient implements MemoryReadOnlyInterface {
client: CosmWasmClient;
contractAddress: string;
// ...
assetList = async ({
iterLimit,
lastAssetName
}: {
iterLimit?: number;
lastAssetName?: string;
}): Promise<AssetListResponse> => {
return this.client.queryContractSmart(this.contractAddress, {
asset_list: {
iter_limit: iterLimit,
last_asset_name: lastAssetName
}
});
};
}
interface MemoryAssetListInfiniteQuery {
client?: MemoryQueryClient
options?: Omit<
UseInfiniteQueryOptions<
AssetListResponse,
Error,
AssetListResponse,
AssetListResponse,
(string | undefined)[]
>,
"'queryKey' | 'queryFn' | 'getNextPageParam'"
>
args: {
iterLimit?: number
lastAssetName?: string
}
}
const query = useInfiniteQuery<
AssetListResponse,
Error,
AssetListResponse,
(string | undefined)[]
>(
['memoryAssetList', client?.contractAddress],
({ pageParam: lastAssetName }) => {
return client!.assetList({
iterLimit: 25,
lastAssetName,
})
},
{
enabled: !!client,
getNextPageParam: (lastPage: AssetListResponse, pages: AssetListResponse[]) => {
return lastPage.assets[lastPage.assets.length - 1]?.[0]
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment