Skip to content

Commit

Permalink
finished instant ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Nov 27, 2023
1 parent a3b0cfd commit e7d7c08
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface CosmosAuthAccount {
/** Account returns account details based on address. */
account(request: QueryAccountRequest): Promise<QueryAccountResponse>;
/** ModuleAccounts returns all the existing module accounts. */
useAuthModuleAccounts(request?: QueryModuleAccountsRequest): Promise<QueryModuleAccountsResponse>;
authModuleAccounts(request?: QueryModuleAccountsRequest): Promise<QueryModuleAccountsResponse>;
}
export class QueryClientImpl implements Query {
private readonly rpc: Rpc;
Expand Down
1 change: 1 addition & 0 deletions __fixtures__/v-next/outputinstantrpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from "./ibc/client";
export * from "./osmosis/bundle";
export * from "./osmosis/client";
export * from "./tendermint/bundle";
export * from "./service-ops";
export * from "./hooks";
export * from "./mobx.stores";
export * from "./extern";
Expand Down
30 changes: 15 additions & 15 deletions __fixtures__/v-next/outputinstantrpc/service-ops.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import * as _AkashCertV1beta2Queryrpc from "./akash/cert/v1beta2/query.rpc.Query";
import * as _CosmosAuthV1beta1Queryrpc from "./cosmos/auth/v1beta1/query.rpc.Query";
import * as _CosmosBankV1beta1Queryrpc from "./cosmos/bank/v1beta1/query.rpc.Query";
import * as _CosmosNftV1beta1Queryrpc from "./cosmos/nft/v1beta1/query.rpc.Query";
import { Rpc } from "./helpers";
import * as _OsmosisGammV1beta1Queryrpc from "./osmosis/gamm/v1beta1/query.rpc.Query";
import * as _OsmosisGammV2Queryrpc from "./osmosis/gamm/v2/query.rpc.Query";

export interface CosmosAuthAccount extends _CosmosAuthV1beta1Queryrpc.CosmosAuthAccountQuery {

import * as _OsmosisClaimV1beta1Queryrpc from "./osmosis/claim/v1beta1/query.rpc.Query";
import * as _CosmosAuthV1beta1Queryrpc from "./cosmos/auth/v1beta1/query.rpc.Query";
export interface OsmosisClaim extends _OsmosisClaimV1beta1Queryrpc.OsmosisClaim {}
export class OsmosisClaim {
readonly rpc: Rpc;
constructor(rpc: Rpc) {
this.rpc = rpc;
this.claimRecord = _OsmosisClaimV1beta1Queryrpc.createClientImpl(rpc).claimRecord;
this.claimableForAction = _OsmosisClaimV1beta1Queryrpc.createClientImpl(rpc).claimableForAction;
}
}

export interface CosmosAuthAccount extends _CosmosAuthV1beta1Queryrpc.CosmosAuthAccount {}
export class CosmosAuthAccount {
readonly rpc: Rpc

readonly rpc: Rpc;
constructor(rpc: Rpc) {
this.rpc = rpc;

this.account = _CosmosAuthV1beta1Queryrpc.createClientImpl(rpc).account;
this.accounts = _CosmosAuthV1beta1Queryrpc.createClientImpl(rpc).accounts;
this.account = _CosmosAuthV1beta1Queryrpc.createClientImpl(rpc).account;
this.authModuleAccounts = _CosmosAuthV1beta1Queryrpc.createClientImpl(rpc).moduleAccounts;
}
}
}
3 changes: 2 additions & 1 deletion packages/ast/src/clients/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './class';
export * from './extension';
export * from './scoped';
export * from './scoped';
export * from './instant/instant';
5 changes: 3 additions & 2 deletions packages/ast/src/clients/rpc/instant/instant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as t from "@babel/types";
import { identifier } from "../../../utils";
import { classProperty, identifier } from "../../../utils";
import { GenericParseContext } from "../../../encoding";

export const createInstantRpcInterface = (
Expand Down Expand Up @@ -41,12 +41,13 @@ export const createInstantRpcClass = (
t.identifier(name),
null,
t.classBody([
t.classProperty(
classProperty(
t.identifier("rpc"),
null,
t.tsTypeAnnotation(t.tsTypeReference(t.identifier("Rpc"))),
null,
false,
false,
true
),

Expand Down
1 change: 1 addition & 0 deletions packages/ast/types/clients/rpc/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './class';
export * from './extension';
export * from './scoped';
export * from './instant/instant';
2 changes: 1 addition & 1 deletion packages/telescope/__tests__/telescope-instant-rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const options: TelescopeOptions = {
patterns: ["cosmos.auth.**.*account*", "cosmos.auth.**.*Account*"],
},
nameMapping: {
useAuthModuleAccounts: "cosmos.auth.v1beta1.moduleAccounts",
authModuleAccounts: "cosmos.auth.v1beta1.moduleAccounts",
useBankBalance: "cosmos.bank.v1beta1.balance",
useNftBalance: "cosmos.nft.v1beta1.balance",
},
Expand Down
2 changes: 2 additions & 0 deletions packages/telescope/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { plugin as createHelpers } from './generators/create-helpers';
import { plugin as createCosmWasmBundle } from './generators/create-cosmwasm-bundle';
import { plugin as createPiniaStore } from './generators/create-pinia-store'
import { plugin as createPiniaStoreBundle } from './generators/create-pinia-store-bundle'
import { plugin as createRpcOpsBundle } from './generators/create-rpc-ops-bundle'

const sanitizeOptions = (options: TelescopeOptions): TelescopeOptions => {
// If an element at the same key is present for both x and y, the value from y will appear in the result.
Expand Down Expand Up @@ -155,6 +156,7 @@ export class TelescopeBuilder {
createBundle(this, bundler);
});

createRpcOpsBundle(this);
createReactQueryBundle(this);
createMobxBundle(this);
createAggregatedLCDClient(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const plugin = (
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
instantRpcBundlerFiles[item.className].push({...bundlerFile});
});

asts.push(createRpcClientClass(ctx.generic, proto.Msg))
Expand Down
25 changes: 14 additions & 11 deletions packages/telescope/src/generators/create-rpc-ops-bundle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { aggregateImports, getImportStatements } from "../imports";
import { join, dirname, extname, basename } from "path";
import { TelescopeBuilder } from "../builder";
import { createScopedRpcHookFactory } from "@cosmology/ast";
import {
createInstantRpcInterface,
createInstantRpcClass,
} from "@cosmology/ast";
import { ImportUsage, ProtoRef } from "@cosmology/types";
import { TelescopeParseContext } from "../build";
import { writeAstToFile } from "../utils/files";
Expand All @@ -11,6 +14,7 @@ import { createEmptyProtoRef } from "@cosmology/proto-parser";
import { camel, makeUseHookName, makeUsePkgHookName } from "@cosmology/utils";
import { variableSlug } from "@cosmology/utils";
import { swapKeyValue } from "@cosmology/utils";
import { buildImports } from "@cosmology/utils";
import { BundlerFile } from "src/types";

export const plugin = (builder: TelescopeBuilder) => {
Expand Down Expand Up @@ -55,7 +59,7 @@ export const plugin = (builder: TelescopeBuilder) => {
instantOpsConfig.className,
pkgImports,
nameMapping,
bundlerFiles,
bundlerFiles
)
);
},
Expand All @@ -67,7 +71,7 @@ export const plugin = (builder: TelescopeBuilder) => {
const importStmts = getImportStatements(localname, imports);

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

// write the file.
const filename = join(builder.outPath, localname);
Expand All @@ -81,7 +85,7 @@ function createRpcOpsAst(
className: string,
pkgImports: ImportUsage[],
nameMapping,
bundlerFiles: BundlerFile[],
bundlerFiles: BundlerFile[]
) {
const extendInterfaces = [];
const instantMapping: {
Expand All @@ -91,7 +95,7 @@ function createRpcOpsAst(
comment?: string | undefined;
};
} = {};
const camelRpcMethods = context.options.rpcClients?.camelCase;
const camelRpcMethods = context.generic.pluginValue("rpcClients.camelCase");

bundlerFiles.forEach((bundlerFile) => {
const path = `./${bundlerFile.localname.replace(/\.ts$/, "")}`;
Expand All @@ -116,22 +120,21 @@ function createRpcOpsAst(

bundlerFile.instantExportedMethods?.forEach((method) => {
const methodName = camelRpcMethods ? camel(method.name) : method.name;
const nameWithPkg = `${context.ref.proto.package}.${methodName}`;
const nameWithPkg = `${bundlerFile.package}.${methodName}`;
const methodAlias =
nameMapping && nameMapping[nameWithPkg]
? nameMapping[nameWithPkg]
: methodName;

instantMapping[methodAlias] = {
methodName,
importedVarName,
};
dotty.put(instantMapping, methodAlias, {
methodName,
importedVarName,
});
});
});

return [];
return [
createInstantRpcInterface(className, extendInterfaces),
createInstantRpcClass(context.generic, className, instantMapping),
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const plugin = (
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
instantRpcBundlerFiles[item.className].push({...bundlerFile});
});

asts.push(createRpcClientClass(ctx.generic, svc));
Expand Down
4 changes: 2 additions & 2 deletions packages/telescope/src/generators/create-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const plugin = (
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
instantRpcBundlerFiles[item.className].push({...bundlerFile});
});

context.body.push(createRpcClientClass(context.generic, proto[svcKey]));
Expand Down Expand Up @@ -140,7 +140,7 @@ export const plugin = (
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
instantRpcBundlerFiles[item.className].push({...bundlerFile});
});

context.body.push(createRpcClientClass(context.generic, proto.Msg))
Expand Down

0 comments on commit e7d7c08

Please sign in to comment.