Skip to content

Commit

Permalink
react query
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Aug 4, 2023
1 parent 8d62bba commit d65a556
Show file tree
Hide file tree
Showing 41 changed files with 4,337 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/osmo-query/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "osmo-query",
"name": "osmojs-react-query",
"version": "16.5.1",
"description": "Typescript Protobuf Messages for Osmosis with react-query",
"author": "Dan Lynch <pyramation@gmail.com>",
Expand Down
3 changes: 3 additions & 0 deletions packages/osmo-query/scripts/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ telescope({
rpcClients: {
enabled: true,
camelCase: true
},
reactQuery: {
enabled: true
}
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rpc } from "../../../helpers";
import { BinaryReader } from "../../../binary";
import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate";
import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from "@cosmjs/stargate";
import { ReactQueryParams } from "../../../react-query";
import { useQuery } from "@tanstack/react-query";
import { QueryAccountsRequest, QueryAccountsResponse, QueryAccountRequest, QueryAccountResponse, QueryParamsRequest, QueryParamsResponse, QueryModuleAccountsRequest, QueryModuleAccountsResponse } from "./query";
/** Query defines the gRPC querier service. */
export interface Query {
Expand Down Expand Up @@ -66,4 +68,76 @@ export const createRpcQueryExtension = (base: QueryClient) => {
return queryService.moduleAccounts(request);
}
};
};
export interface UseAccountsQuery<TData> extends ReactQueryParams<QueryAccountsResponse, TData> {
request?: QueryAccountsRequest;
}
export interface UseAccountQuery<TData> extends ReactQueryParams<QueryAccountResponse, TData> {
request: QueryAccountRequest;
}
export interface UseParamsQuery<TData> extends ReactQueryParams<QueryParamsResponse, TData> {
request?: QueryParamsRequest;
}
export interface UseModuleAccountsQuery<TData> extends ReactQueryParams<QueryModuleAccountsResponse, TData> {
request?: QueryModuleAccountsRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, QueryClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): QueryClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new QueryClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useAccounts = <TData = QueryAccountsResponse,>({
request,
options
}: UseAccountsQuery<TData>) => {
return useQuery<QueryAccountsResponse, Error, TData>(["accountsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.accounts(request);
}, options);
};
const useAccount = <TData = QueryAccountResponse,>({
request,
options
}: UseAccountQuery<TData>) => {
return useQuery<QueryAccountResponse, Error, TData>(["accountQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.account(request);
}, options);
};
const useParams = <TData = QueryParamsResponse,>({
request,
options
}: UseParamsQuery<TData>) => {
return useQuery<QueryParamsResponse, Error, TData>(["paramsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.params(request);
}, options);
};
const useModuleAccounts = <TData = QueryModuleAccountsResponse,>({
request,
options
}: UseModuleAccountsQuery<TData>) => {
return useQuery<QueryModuleAccountsResponse, Error, TData>(["moduleAccountsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.moduleAccounts(request);
}, options);
};
return {
/**
* Accounts returns all the existing accounts
*
* Since: cosmos-sdk 0.43
*/
useAccounts,
/** Account returns account details based on address. */useAccount,
/** Params queries all parameters. */useParams,
/** ModuleAccounts returns all the existing module accounts. */useModuleAccounts
};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rpc } from "../../../helpers";
import { BinaryReader } from "../../../binary";
import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate";
import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from "@cosmjs/stargate";
import { ReactQueryParams } from "../../../react-query";
import { useQuery } from "@tanstack/react-query";
import { QueryGrantsRequest, QueryGrantsResponse, QueryGranterGrantsRequest, QueryGranterGrantsResponse, QueryGranteeGrantsRequest, QueryGranteeGrantsResponse } from "./query";
/** Query defines the gRPC querier service. */
export interface Query {
Expand Down Expand Up @@ -49,4 +51,58 @@ export const createRpcQueryExtension = (base: QueryClient) => {
return queryService.granteeGrants(request);
}
};
};
export interface UseGrantsQuery<TData> extends ReactQueryParams<QueryGrantsResponse, TData> {
request: QueryGrantsRequest;
}
export interface UseGranterGrantsQuery<TData> extends ReactQueryParams<QueryGranterGrantsResponse, TData> {
request: QueryGranterGrantsRequest;
}
export interface UseGranteeGrantsQuery<TData> extends ReactQueryParams<QueryGranteeGrantsResponse, TData> {
request: QueryGranteeGrantsRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, QueryClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): QueryClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new QueryClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useGrants = <TData = QueryGrantsResponse,>({
request,
options
}: UseGrantsQuery<TData>) => {
return useQuery<QueryGrantsResponse, Error, TData>(["grantsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.grants(request);
}, options);
};
const useGranterGrants = <TData = QueryGranterGrantsResponse,>({
request,
options
}: UseGranterGrantsQuery<TData>) => {
return useQuery<QueryGranterGrantsResponse, Error, TData>(["granterGrantsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.granterGrants(request);
}, options);
};
const useGranteeGrants = <TData = QueryGranteeGrantsResponse,>({
request,
options
}: UseGranteeGrantsQuery<TData>) => {
return useQuery<QueryGranteeGrantsResponse, Error, TData>(["granteeGrantsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.granteeGrants(request);
}, options);
};
return {
/** Returns list of `Authorization`, granted to the grantee by the granter. */useGrants,
/** GranterGrants returns list of `Authorization`, granted by granter. */useGranterGrants,
/** GranteeGrants returns a list of `GrantAuthorization` by grantee. */useGranteeGrants
};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rpc } from "../../../helpers";
import { BinaryReader } from "../../../binary";
import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate";
import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from "@cosmjs/stargate";
import { ReactQueryParams } from "../../../react-query";
import { useQuery } from "@tanstack/react-query";
import { QueryBalanceRequest, QueryBalanceResponse, QueryAllBalancesRequest, QueryAllBalancesResponse, QueryTotalSupplyRequest, QueryTotalSupplyResponse, QuerySupplyOfRequest, QuerySupplyOfResponse, QueryTotalSupplyWithoutOffsetRequest, QueryTotalSupplyWithoutOffsetResponse, QuerySupplyOfWithoutOffsetRequest, QuerySupplyOfWithoutOffsetResponse, QueryParamsRequest, QueryParamsResponse, QueryDenomMetadataRequest, QueryDenomMetadataResponse, QueryDenomsMetadataRequest, QueryDenomsMetadataResponse, QueryBaseDenomRequest, QueryBaseDenomResponse } from "./query";
/** Query defines the gRPC querier service. */
export interface Query {
Expand Down Expand Up @@ -135,4 +137,153 @@ export const createRpcQueryExtension = (base: QueryClient) => {
return queryService.baseDenom(request);
}
};
};
export interface UseBalanceQuery<TData> extends ReactQueryParams<QueryBalanceResponse, TData> {
request: QueryBalanceRequest;
}
export interface UseAllBalancesQuery<TData> extends ReactQueryParams<QueryAllBalancesResponse, TData> {
request: QueryAllBalancesRequest;
}
export interface UseTotalSupplyQuery<TData> extends ReactQueryParams<QueryTotalSupplyResponse, TData> {
request?: QueryTotalSupplyRequest;
}
export interface UseSupplyOfQuery<TData> extends ReactQueryParams<QuerySupplyOfResponse, TData> {
request: QuerySupplyOfRequest;
}
export interface UseTotalSupplyWithoutOffsetQuery<TData> extends ReactQueryParams<QueryTotalSupplyWithoutOffsetResponse, TData> {
request?: QueryTotalSupplyWithoutOffsetRequest;
}
export interface UseSupplyOfWithoutOffsetQuery<TData> extends ReactQueryParams<QuerySupplyOfWithoutOffsetResponse, TData> {
request: QuerySupplyOfWithoutOffsetRequest;
}
export interface UseParamsQuery<TData> extends ReactQueryParams<QueryParamsResponse, TData> {
request?: QueryParamsRequest;
}
export interface UseDenomMetadataQuery<TData> extends ReactQueryParams<QueryDenomMetadataResponse, TData> {
request: QueryDenomMetadataRequest;
}
export interface UseDenomsMetadataQuery<TData> extends ReactQueryParams<QueryDenomsMetadataResponse, TData> {
request?: QueryDenomsMetadataRequest;
}
export interface UseBaseDenomQuery<TData> extends ReactQueryParams<QueryBaseDenomResponse, TData> {
request: QueryBaseDenomRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, QueryClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): QueryClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new QueryClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useBalance = <TData = QueryBalanceResponse,>({
request,
options
}: UseBalanceQuery<TData>) => {
return useQuery<QueryBalanceResponse, Error, TData>(["balanceQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.balance(request);
}, options);
};
const useAllBalances = <TData = QueryAllBalancesResponse,>({
request,
options
}: UseAllBalancesQuery<TData>) => {
return useQuery<QueryAllBalancesResponse, Error, TData>(["allBalancesQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.allBalances(request);
}, options);
};
const useTotalSupply = <TData = QueryTotalSupplyResponse,>({
request,
options
}: UseTotalSupplyQuery<TData>) => {
return useQuery<QueryTotalSupplyResponse, Error, TData>(["totalSupplyQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.totalSupply(request);
}, options);
};
const useSupplyOf = <TData = QuerySupplyOfResponse,>({
request,
options
}: UseSupplyOfQuery<TData>) => {
return useQuery<QuerySupplyOfResponse, Error, TData>(["supplyOfQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.supplyOf(request);
}, options);
};
const useTotalSupplyWithoutOffset = <TData = QueryTotalSupplyWithoutOffsetResponse,>({
request,
options
}: UseTotalSupplyWithoutOffsetQuery<TData>) => {
return useQuery<QueryTotalSupplyWithoutOffsetResponse, Error, TData>(["totalSupplyWithoutOffsetQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.totalSupplyWithoutOffset(request);
}, options);
};
const useSupplyOfWithoutOffset = <TData = QuerySupplyOfWithoutOffsetResponse,>({
request,
options
}: UseSupplyOfWithoutOffsetQuery<TData>) => {
return useQuery<QuerySupplyOfWithoutOffsetResponse, Error, TData>(["supplyOfWithoutOffsetQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.supplyOfWithoutOffset(request);
}, options);
};
const useParams = <TData = QueryParamsResponse,>({
request,
options
}: UseParamsQuery<TData>) => {
return useQuery<QueryParamsResponse, Error, TData>(["paramsQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.params(request);
}, options);
};
const useDenomMetadata = <TData = QueryDenomMetadataResponse,>({
request,
options
}: UseDenomMetadataQuery<TData>) => {
return useQuery<QueryDenomMetadataResponse, Error, TData>(["denomMetadataQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.denomMetadata(request);
}, options);
};
const useDenomsMetadata = <TData = QueryDenomsMetadataResponse,>({
request,
options
}: UseDenomsMetadataQuery<TData>) => {
return useQuery<QueryDenomsMetadataResponse, Error, TData>(["denomsMetadataQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.denomsMetadata(request);
}, options);
};
const useBaseDenom = <TData = QueryBaseDenomResponse,>({
request,
options
}: UseBaseDenomQuery<TData>) => {
return useQuery<QueryBaseDenomResponse, Error, TData>(["baseDenomQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.baseDenom(request);
}, options);
};
return {
/** Balance queries the balance of a single coin for a single account. */useBalance,
/** AllBalances queries the balance of all coins for a single account. */useAllBalances,
/** TotalSupply queries the total supply of all coins. */useTotalSupply,
/** SupplyOf queries the supply of a single coin. */useSupplyOf,
/** TotalSupplyWithoutOffset queries the total supply of all coins. */useTotalSupplyWithoutOffset,
/** SupplyOf queries the supply of a single coin. */useSupplyOfWithoutOffset,
/** Params queries the parameters of x/bank module. */useParams,
/** DenomsMetadata queries the client metadata of a given coin denomination. */useDenomMetadata,
/** DenomsMetadata queries the client metadata for all registered coin denominations. */useDenomsMetadata,
/**
* BaseDenom queries for a base denomination given a denom that can either be
* the base denom itself or a metadata denom unit that maps to the base denom.
*/
useBaseDenom
};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rpc } from "../../../../helpers";
import { BinaryReader } from "../../../../binary";
import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate";
import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from "@cosmjs/stargate";
import { ReactQueryParams } from "../../../../react-query";
import { useQuery } from "@tanstack/react-query";
import { ConfigRequest, ConfigResponse } from "./query";
/** Service defines the gRPC querier service for node related queries. */
export interface Service {
Expand All @@ -27,4 +29,32 @@ export const createRpcQueryExtension = (base: QueryClient) => {
return queryService.config(request);
}
};
};
export interface UseConfigQuery<TData> extends ReactQueryParams<ConfigResponse, TData> {
request?: ConfigRequest;
}
const _queryClients: WeakMap<ProtobufRpcClient, ServiceClientImpl> = new WeakMap();
const getQueryService = (rpc: ProtobufRpcClient | undefined): ServiceClientImpl | undefined => {
if (!rpc) return;
if (_queryClients.has(rpc)) {
return _queryClients.get(rpc);
}
const queryService = new ServiceClientImpl(rpc);
_queryClients.set(rpc, queryService);
return queryService;
};
export const createRpcQueryHooks = (rpc: ProtobufRpcClient | undefined) => {
const queryService = getQueryService(rpc);
const useConfig = <TData = ConfigResponse,>({
request,
options
}: UseConfigQuery<TData>) => {
return useQuery<ConfigResponse, Error, TData>(["configQuery", request], () => {
if (!queryService) throw new Error("Query Service not initialized");
return queryService.config(request);
}, options);
};
return {
/** Config queries for the operator configuration. */useConfig
};
};
Loading

0 comments on commit d65a556

Please sign in to comment.