Skip to content

Commit

Permalink
WIP export instant ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Nov 27, 2023
1 parent 2b0511f commit b5859fe
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface Query {
addressStringToBytes(request: AddressStringToBytesRequest): Promise<AddressStringToBytesResponse>;
}
/** Query defines the gRPC querier service. */
export interface CosmosAuthAccountQuery {
export interface CosmosAuthAccount {
/**
* Accounts returns all the existing accounts
*
Expand All @@ -38,6 +38,8 @@ export interface CosmosAuthAccountQuery {
accounts(request?: QueryAccountsRequest): Promise<QueryAccountsResponse>;
/** Account returns account details based on address. */
account(request: QueryAccountRequest): Promise<QueryAccountResponse>;
/** ModuleAccounts returns all the existing module accounts. */
useAuthModuleAccounts(request?: QueryModuleAccountsRequest): Promise<QueryModuleAccountsResponse>;
}
export class QueryClientImpl implements Query {
private readonly rpc: Rpc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Query {
totalClaimable(request: QueryTotalClaimableRequest): Promise<QueryTotalClaimableResponse>;
}
/** Query defines the gRPC querier service. */
export interface OsmosisClaimQuery {
export interface OsmosisClaim {
claimRecord(request: QueryClaimRecordRequest): Promise<QueryClaimRecordResponse>;
claimableForAction(request: QueryClaimableForActionRequest): Promise<QueryClaimableForActionResponse>;
}
Expand Down
11 changes: 8 additions & 3 deletions packages/ast/src/clients/rpc/class/tendermint/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ export const createRpcClientInterface = (
context: GenericParseContext,
service: ProtoService,
name?: string,
methodKeys?: string[]
methodKeys?: string[],
nameMapping?: {
[key: string]: string
}
) => {
const camelRpcMethods = context.pluginValue('rpcClients.camelCase');
const keys = methodKeys && methodKeys.length ? methodKeys : Object.keys(service.methods ?? {});
Expand All @@ -332,11 +335,13 @@ export const createRpcClientInterface = (
return null;
}

const name = camelRpcMethods ? camel(key) : key;
const methodName = camelRpcMethods ? camel(key) : key;
const nameWithPkg = `${context.ref.proto.package}.${methodName}`;
const methodAlias = nameMapping && nameMapping[nameWithPkg] ? nameMapping[nameWithPkg] : methodName;
const leadingComments = method.comment ? [commentBlock(processRpcComment(method))] : [];
let trailingComments = [];
return rpcMethodDefinition(
name,
methodAlias,
method,
trailingComments,
leadingComments
Expand Down
4 changes: 3 additions & 1 deletion packages/ast/types/clients/rpc/class/tendermint/rpc.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as t from '@babel/types';
import { ProtoService } from '@cosmology/types';
import { GenericParseContext } from '../../../../encoding';
export declare const createRpcClientInterface: (context: GenericParseContext, service: ProtoService, name?: string, methodKeys?: string[]) => t.ExportNamedDeclaration;
export declare const createRpcClientInterface: (context: GenericParseContext, service: ProtoService, name?: string, methodKeys?: string[], nameMapping?: {
[key: string]: string;
}) => t.ExportNamedDeclaration;
export declare const getRpcClassName: (service: ProtoService) => string;
export declare const createRpcClientClass: (context: GenericParseContext, service: ProtoService) => t.ExportNamedDeclaration;
export declare const createRpcInterface: (context: GenericParseContext, service: ProtoService) => t.TSInterfaceDeclaration;
14 changes: 8 additions & 6 deletions packages/ast/types/clients/rpc/instant/instant.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as t from "@babel/types";
import { GenericParseContext } from "../../../encoding";
export declare const createInstantRpcClass: (context: GenericParseContext, obj: object, identifier: string, instantHooksMapping?: {
export declare const createInstantRpcInterface: (name: string, extendInterfaces: {
importedVarName: string;
interfaceName: string;
}[]) => t.ExportNamedDeclaration;
export declare const createInstantRpcClass: (context: GenericParseContext, name: string, instantMapping: {
[key: string]: {
useHookName: string;
methodName: string;
importedVarName: string;
comment?: string;
};
}) => {
imports: any[];
ast: any[];
};
}) => t.ExportNamedDeclaration;
12 changes: 11 additions & 1 deletion packages/telescope/__tests__/telescope-instant-rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,21 @@ const options: TelescopeOptions = {
include: {
patterns: ["osmosis.**.*claim*"],
},
nameMapping: {
useAuthModuleAccounts: "cosmos.auth.v1beta1.useModuleAccounts",
useBankBalance: "cosmos.bank.v1beta1.useBalance",
useNftBalance: "cosmos.nft.v1beta1.useBalance",
},
},
{
className: "CosmosAuthAccount",
include: {
patterns: ["cosmos.auth.**.*account*"],
patterns: ["cosmos.auth.**.*account*", "cosmos.auth.**.*Account*"],
},
nameMapping: {
useAuthModuleAccounts: "cosmos.auth.v1beta1.moduleAccounts",
useBankBalance: "cosmos.bank.v1beta1.balance",
useNftBalance: "cosmos.nft.v1beta1.balance",
},
},
],
Expand Down
11 changes: 8 additions & 3 deletions packages/telescope/src/generators/create-rpc-msg-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createRpcClientClass, createRpcClientInterface, createGRPCGatewayMsgCla
import { getNestedProto } from '@cosmology/proto-parser';
import { parse } from '../parse';
import { TelescopeBuilder } from '../builder';
import { getQueryMethodNames, makeRpcClientInterfaceName } from '@cosmology/utils';
import { getQueryMethodNames, swapKeyValue } from '@cosmology/utils';
import { BundlerFile } from 'src/types';

export const plugin = (
Expand Down Expand Up @@ -75,6 +75,10 @@ export const plugin = (
const instantOps = c.options.rpcClients?.instantOps ?? [];

instantOps.forEach((item) => {
let nameMapping = item.nameMapping;

nameMapping = swapKeyValue(nameMapping ?? {});

// get all query methods
const patterns = item.include?.patterns;
const methodKeys = getQueryMethodNames(
Expand All @@ -92,8 +96,9 @@ export const plugin = (
createRpcClientInterface(
ctx.generic,
svc,
makeRpcClientInterfaceName(item.className, svc.name),
methodKeys
item.className,
methodKeys,
nameMapping
)
);

Expand Down
79 changes: 42 additions & 37 deletions packages/telescope/src/generators/create-rpc-ops-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { aggregateImports, getImportStatements } from "../imports";
import { join, dirname, extname, basename } from "path";
import { TelescopeBuilder } from "../builder";
import { createScopedRpcHookFactory } from "@cosmology/ast";
import { ProtoRef } from "@cosmology/types";
import { ImportUsage, ProtoRef } from "@cosmology/types";
import { TelescopeParseContext } from "../build";
import { writeAstToFile } from "../utils/files";
import { fixlocalpaths } from "../utils";
Expand All @@ -24,7 +24,6 @@ export const plugin = (builder: TelescopeBuilder) => {
const localname = "service-ops.ts";

// get mapping of packages and rpc query filenames.
const obj = {};
const instantOpsMapping = {};
const methodSet = new Set();
const bundlerFiles = builder.stateManagers["instantRpc"];
Expand All @@ -44,6 +43,8 @@ export const plugin = (builder: TelescopeBuilder) => {
builder.options
);

const pkgImports = [];

const ast = builder.options.rpcClients!.instantOps!.reduce(
(ast, instantOpsConfig) => {
let nameMapping = instantOpsConfig.nameMapping;
Expand All @@ -53,11 +54,12 @@ export const plugin = (builder: TelescopeBuilder) => {
return ast.concat(
createRpcOpsAst(
context,
instantOpsConfig.className,
pkgImports,
nameMapping,
bundlerFiles,
methodSet,
instantOpsMapping,
obj
instantOpsMapping
)
);
},
Expand All @@ -69,7 +71,7 @@ export const plugin = (builder: TelescopeBuilder) => {
const importStmts = getImportStatements(localname, imports);

// construct the AST
const prog = [].concat(importStmts).concat(ast);
const prog = [].concat(importStmts).concat([]).concat(ast);

// write the file.
const filename = join(builder.outPath, localname);
Expand All @@ -80,44 +82,47 @@ export const plugin = (builder: TelescopeBuilder) => {

function createRpcOpsAst(
context: TelescopeParseContext,
className: string,
pkgImports: ImportUsage[],
nameMapping,
bundlerFiles: BundlerFile[],
methodSet: Set<unknown>,
instantOpsMapping,
obj
) {
const extendInterfaces = [];

bundlerFiles.forEach((bundlerFile) => {
const path = `./${bundlerFile.localname.replace(/\.ts$/, "")}`;
dotty.put(obj, bundlerFile.package, path);

// build instantOpsMapping
bundlerFile.instantExportedMethods?.forEach((method) => {
const methodName = method.name;

const useHookName = makeUseHookName(camel(methodName));
const hookNameWithPkg = `${bundlerFile.package}.${useHookName}`;
let instantHookName = null;

if (nameMapping[hookNameWithPkg]) {
instantHookName = nameMapping[hookNameWithPkg];
} else {
if (methodSet.has(useHookName)) {
instantHookName = makeUsePkgHookName(bundlerFile.package, methodName);
} else {
instantHookName = useHookName;
}
}

dotty.put(instantOpsMapping, instantHookName, {
useHookName,
importedVarName: variableSlug(path),
comment: `${bundlerFile.package}.${useHookName}\n${
method.comment ?? methodName
}`,
});

methodSet.add(instantHookName);
});
const path = `./${bundlerFile.localname.replace(/\.ts$/, '')}`;
const importedVarName = variableSlug(path)

// // build instantOpsMapping
// bundlerFile.instantExportedMethods?.forEach((method) => {
// const methodName = method.name;

// const useHookName = makeUseHookName(camel(methodName));
// const hookNameWithPkg = `${bundlerFile.package}.${useHookName}`;
// let instantHookName = null;

// if (nameMapping[hookNameWithPkg]) {
// instantHookName = nameMapping[hookNameWithPkg];
// } else {
// if (methodSet.has(useHookName)) {
// instantHookName = makeUsePkgHookName(bundlerFile.package, methodName);
// } else {
// instantHookName = useHookName;
// }
// }

// dotty.put(instantOpsMapping, instantHookName, {
// useHookName,
// importedVarName: variableSlug(path),
// comment: `${bundlerFile.package}.${useHookName}\n${
// method.comment ?? methodName
// }`,
// });

// methodSet.add(instantHookName);
// });
});

return [];
Expand Down
11 changes: 8 additions & 3 deletions packages/telescope/src/generators/create-rpc-query-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { getNestedProto, isRefIncluded } from '@cosmology/proto-parser';
import { parse } from '../parse';
import { TelescopeBuilder } from '../builder';
import { ProtoRoot, ProtoService } from '@cosmology/types';
import { getQueryMethodNames, makeRpcClientInterfaceName } from '@cosmology/utils';
import { getQueryMethodNames, swapKeyValue } from '@cosmology/utils';
import { BundlerFile } from '../types';

export const plugin = (
Expand Down Expand Up @@ -161,6 +161,10 @@ export const plugin = (
const instantOps = c.options.rpcClients?.instantOps ?? [];

instantOps.forEach((item) => {
let nameMapping = item.nameMapping;

nameMapping = swapKeyValue(nameMapping ?? {});

// get all query methods
const patterns = item.include?.patterns;
const methodKeys = getQueryMethodNames(
Expand All @@ -178,8 +182,9 @@ export const plugin = (
createRpcClientInterface(
ctx.generic,
svc,
makeRpcClientInterfaceName(item.className, svc.name),
methodKeys
item.className,
methodKeys,
nameMapping
)
);

Expand Down
20 changes: 15 additions & 5 deletions packages/telescope/src/generators/create-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mkdirp } from 'mkdirp';
import { getNestedProto, isRefExcluded } from '@cosmology/proto-parser';
import { createRpcClientClass, createRpcClientImpl, createRpcClientInterface, createRpcQueryExtension } from '@cosmology/ast';
import { BundlerFile } from 'src/types';
import { getQueryMethodNames, makeRpcClientInterfaceName } from '@cosmology/utils';
import { getQueryMethodNames, swapKeyValue } from '@cosmology/utils';

export const plugin = (
builder: TelescopeBuilder,
Expand Down Expand Up @@ -54,6 +54,10 @@ export const plugin = (
context.body.push(createRpcClientInterface(context.generic, proto[svcKey]));

instantOps.forEach((item) => {
let nameMapping = item.nameMapping;

nameMapping = swapKeyValue(nameMapping ?? {});

// get all query methods
const patterns = item.include?.patterns;
const methodKeys = getQueryMethodNames(
Expand All @@ -71,8 +75,9 @@ export const plugin = (
createRpcClientInterface(
context.generic,
svc,
makeRpcClientInterfaceName(item.className, svc.name),
methodKeys
item.className,
methodKeys,
nameMapping
)
);

Expand All @@ -96,6 +101,10 @@ export const plugin = (
context.body.push(createRpcClientInterface(context.generic, proto.Msg))

instantOps.forEach((item) => {
let nameMapping = item.nameMapping;

nameMapping = swapKeyValue(nameMapping ?? {});

// get all query methods
const patterns = item.include?.patterns;
const methodKeys = getQueryMethodNames(
Expand All @@ -113,8 +122,9 @@ export const plugin = (
createRpcClientInterface(
context.generic,
proto.Msg,
makeRpcClientInterfaceName(item.className, proto.Msg.name),
methodKeys
item.className,
methodKeys,
nameMapping
)
);

Expand Down
3 changes: 0 additions & 3 deletions packages/utils/src/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export const makeHookKeyName = (name: string) => {
return camel(name + "Query");
};

export const makeRpcClientInterfaceName = (className: string, serviceName: string) => {
return pascal(`${className}${serviceName}`);
};

// https://github.com/isaacs/minimatch/blob/main/src/index.ts#L61
// Optimized checking for the most common glob patterns.
Expand Down
1 change: 0 additions & 1 deletion packages/utils/types/proto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export declare const makeUsePkgHookName: (packageName: string, name: string) =>
export declare const makePkgMethodName: (packageName: string, name: string) => any;
export declare const makeUseHookTypeName: (name: string) => string;
export declare const makeHookKeyName: (name: string) => any;
export declare const makeRpcClientInterfaceName: (className: string, serviceName: string) => string;
/**
* Get a list of query method names that matches the given patterns.
* @param packagePath package path like 'cosmos.bank.v1beta1'
Expand Down

0 comments on commit b5859fe

Please sign in to comment.