Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast generation code for the new helper functions [WIP] #671

Draft
wants to merge 13 commits into
base: feat/helper-func
Choose a base branch
from
Draft
139 changes: 139 additions & 0 deletions packages/ast/src/clients/helper-funcs/__snapshots__/funcs.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`msg funcs works 1`] = `
"const createSend = (getSigningClient: () => ISigningClient | undefined) => buildTx<MsgSend>({
getSigningClient: getSigningClient,
typeUrl: : MsgSend.typeUrl,
encoders: "toEncoders(MsgSend)",
converters: "toConverters(MsgSend)"
});
const createMultiSend = (getSigningClient: () => ISigningClient | undefined) => buildTx<MsgMultiSend>({
getSigningClient: getSigningClient,
typeUrl: : MsgMultiSend.typeUrl,
encoders: "toEncoders(MsgMultiSend)",
converters: "toConverters(MsgMultiSend)"
});"
`;

exports[`msg funcs works 2`] = `
"const useSend = buildUseMutation<MsgSend, Error>({
builderMutationFn: createSend
});
const useMultiSend = buildUseMutation<MsgMultiSend, Error>({
builderMutationFn: createMultiSend
});"
`;

exports[`query funcs works 1`] = `
"type UseBalanceQuery = UseQueryParams<QueryBalanceRequest, QueryBalanceResponse>;
type UseAllBalancesQuery = UseQueryParams<QueryAllBalancesRequest, QueryAllBalancesResponse>;
type UseSpendableBalancesQuery = UseQueryParams<QuerySpendableBalancesRequest, QuerySpendableBalancesResponse>;
type UseTotalSupplyQuery = UseQueryParams<QueryTotalSupplyRequest, QueryTotalSupplyResponse>;
type UseSupplyOfQuery = UseQueryParams<QuerySupplyOfRequest, QuerySupplyOfResponse>;
type UseParamsQuery = UseQueryParams<QueryParamsRequest, QueryParamsResponse>;
type UseDenomMetadataQuery = UseQueryParams<QueryDenomMetadataRequest, QueryDenomMetadataResponse>;
type UseDenomsMetadataQuery = UseQueryParams<QueryDenomsMetadataRequest, QueryDenomsMetadataResponse>;
type UseDenomOwnersQuery = UseQueryParams<QueryDenomOwnersRequest, QueryDenomOwnersResponse>;
const createGetBalance = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryBalanceRequest, QueryBalanceResponse>({
reqEncoderFn: QueryBalanceRequest.encode,
j-yw marked this conversation as resolved.
Show resolved Hide resolved
resDecoderFn: QueryBalanceResponse.decode,
j-yw marked this conversation as resolved.
Show resolved Hide resolved
service: "cosmos.bank.v1beta1.Query",
method: "Balance",
getRpcInstance: getRpcInstance
});
const createGetAllBalances = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryAllBalancesRequest, QueryAllBalancesResponse>({
reqEncoderFn: QueryAllBalancesRequest.encode,
resDecoderFn: QueryAllBalancesResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "AllBalances",
getRpcInstance: getRpcInstance
});
const createGetSpendableBalances = (getRpcInstance: () => Rpc | undefined) => buildQuery<QuerySpendableBalancesRequest, QuerySpendableBalancesResponse>({
reqEncoderFn: QuerySpendableBalancesRequest.encode,
resDecoderFn: QuerySpendableBalancesResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "SpendableBalances",
getRpcInstance: getRpcInstance
});
const createGetTotalSupply = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryTotalSupplyRequest, QueryTotalSupplyResponse>({
reqEncoderFn: QueryTotalSupplyRequest.encode,
resDecoderFn: QueryTotalSupplyResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "TotalSupply",
getRpcInstance: getRpcInstance
});
const createGetSupplyOf = (getRpcInstance: () => Rpc | undefined) => buildQuery<QuerySupplyOfRequest, QuerySupplyOfResponse>({
reqEncoderFn: QuerySupplyOfRequest.encode,
resDecoderFn: QuerySupplyOfResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "SupplyOf",
getRpcInstance: getRpcInstance
});
const createGetParams = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryParamsRequest, QueryParamsResponse>({
reqEncoderFn: QueryParamsRequest.encode,
resDecoderFn: QueryParamsResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "Params",
getRpcInstance: getRpcInstance
});
const createGetDenomMetadata = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryDenomMetadataRequest, QueryDenomMetadataResponse>({
reqEncoderFn: QueryDenomMetadataRequest.encode,
resDecoderFn: QueryDenomMetadataResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "DenomMetadata",
getRpcInstance: getRpcInstance
});
const createGetDenomsMetadata = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryDenomsMetadataRequest, QueryDenomsMetadataResponse>({
reqEncoderFn: QueryDenomsMetadataRequest.encode,
resDecoderFn: QueryDenomsMetadataResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "DenomsMetadata",
getRpcInstance: getRpcInstance
});
const createGetDenomOwners = (getRpcInstance: () => Rpc | undefined) => buildQuery<QueryDenomOwnersRequest, QueryDenomOwnersResponse>({
reqEncoderFn: QueryDenomOwnersRequest.encode,
resDecoderFn: QueryDenomOwnersResponse.decode,
service: "cosmos.bank.v1beta1.Query",
method: "DenomOwners",
getRpcInstance: getRpcInstance
});"
`;

exports[`query funcs works 2`] = `
"const useBalance = buildUseQuery<QueryBalanceRequest, QueryBalanceResponse>({
builderQueryFn: createGetBalance,
queryKeyPrefix: "BalanceQuery"
});
const useAllBalances = buildUseQuery<QueryAllBalancesRequest, QueryAllBalancesResponse>({
builderQueryFn: createGetAllBalances,
queryKeyPrefix: "AllBalancesQuery"
});
const useSpendableBalances = buildUseQuery<QuerySpendableBalancesRequest, QuerySpendableBalancesResponse>({
builderQueryFn: createGetSpendableBalances,
queryKeyPrefix: "SpendableBalancesQuery"
});
const useTotalSupply = buildUseQuery<QueryTotalSupplyRequest, QueryTotalSupplyResponse>({
builderQueryFn: createGetTotalSupply,
queryKeyPrefix: "TotalSupplyQuery"
});
const useSupplyOf = buildUseQuery<QuerySupplyOfRequest, QuerySupplyOfResponse>({
builderQueryFn: createGetSupplyOf,
queryKeyPrefix: "SupplyOfQuery"
});
const useParams = buildUseQuery<QueryParamsRequest, QueryParamsResponse>({
builderQueryFn: createGetParams,
queryKeyPrefix: "ParamsQuery"
});
const useDenomMetadata = buildUseQuery<QueryDenomMetadataRequest, QueryDenomMetadataResponse>({
builderQueryFn: createGetDenomMetadata,
queryKeyPrefix: "DenomMetadataQuery"
});
const useDenomsMetadata = buildUseQuery<QueryDenomsMetadataRequest, QueryDenomsMetadataResponse>({
builderQueryFn: createGetDenomsMetadata,
queryKeyPrefix: "DenomsMetadataQuery"
});
const useDenomOwners = buildUseQuery<QueryDenomOwnersRequest, QueryDenomOwnersResponse>({
builderQueryFn: createGetDenomOwners,
queryKeyPrefix: "DenomOwnersQuery"
});"
`;
83 changes: 81 additions & 2 deletions packages/ast/src/clients/helper-funcs/funcs.test.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
it('works', async () => {
});
import * as ast from "@babel/types";
import { GenericParseContext } from "../../encoding";
import { getNestedProto } from "@cosmology/utils";
import { traverse } from "@cosmology/proto-parser";
import { expectCode, getTestProtoStore, printCode } from "../../../test-utils";
import { defaultTelescopeOptions, ProtoService } from "@cosmology/types";
import { createMsgHelperCreator, createMsgHooks } from "./msg-funcs";
import { createQueryHelperCreator, createQueryHooks } from "./query-funcs";

const store = getTestProtoStore();
store.traverseAll();

it("query funcs works", async () => {
const ref = store.findProto("cosmos/bank/v1beta1/query.proto");

const res = traverse(store, ref);

const { Query } = getNestedProto(res);

const mockService: ProtoService = Query;

const mockContext = new GenericParseContext(
ref,
store,
defaultTelescopeOptions
);

expectCode(
createQueryHelperCreator(
mockContext,
mockService.methods["Balance"],
"Balance",
"createGetBalance"
)
);

expectCode(
createQueryHooks(
mockContext,
mockService.methods["Balance"],
"Balance",
"createGetBalance",
"useBalance"
)
);
});

it("msg funcs works", async () => {
const ref = store.findProto("cosmos/bank/v1beta1/tx.proto");

const res = traverse(store, ref);

const { Msg } = getNestedProto(res);

const mockService: ProtoService = Msg;

const mockContext = new GenericParseContext(
ref,
store,
defaultTelescopeOptions
);

expectCode(
createMsgHelperCreator(
mockContext,
mockService,
"MsgSend",
"createSend"
)
);

expectCode(
createMsgHooks(
mockContext,
mockService,
"MsgSend",
"createSend",
"useSend"
)
);
});
89 changes: 76 additions & 13 deletions packages/ast/src/clients/helper-funcs/msg-funcs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as t from "@babel/types";
import * as ast from "@babel/types";
import { ProtoService } from "@cosmology/types";
import { GenericParseContext } from "../../encoding";

Expand All @@ -13,16 +13,60 @@ import { GenericParseContext } from "../../encoding";
export function createMsgHelperCreator(
context: GenericParseContext,
service: ProtoService,
methodKey: string,
helperCreatorName: string
methodKey?: string,
helperCreatorName?: string
) {
// get related encoders.
// e.g.: encoders: toEncoders(MsgMultiSend),
context.addUtil("buildTx");
context.addUtil("ISigningClient");
context.addUtil("buildUseMutation");
context.addUtil("SigningClientResolver");
context.addUtil("toEncoders");
context.addUtil("toConverters");

// get related converters.
// e.g.: converters: toConverters(MsgMultiSend),

return t.returnStatement(t.identifier(helperCreatorName));
const callExpression = ast.callExpression(ast.identifier("buildTx"), [
ast.objectExpression([
ast.objectProperty(
ast.identifier("getSigningClient"),
ast.identifier("getSigningClient")
),
ast.objectProperty(
ast.identifier("typeUrl: "),
ast.memberExpression(
ast.identifier(methodKey),
ast.identifier("typeUrl")
)
),
ast.objectProperty(
ast.identifier("encoders"),
ast.callExpression(ast.identifier("toEncoders"), [
ast.identifier(methodKey),
])
),
ast.objectProperty(
ast.identifier("converters"),
ast.callExpression(ast.identifier("toConverters"), [
ast.identifier(methodKey),
])
),
]),
]);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(methodKey)),
]);
const customHookArgumentsType = ast.tsTypeAnnotation(
ast.tsTypeReference(ast.identifier("SigningClientResolver"))
);
const arg = ast.identifier("getSigningClient");
arg.typeAnnotation = customHookArgumentsType;
const arrowFuncExp = ast.arrowFunctionExpression([arg], callExpression);
return ast.exportNamedDeclaration(
ast.variableDeclaration("const", [
ast.variableDeclarator(
ast.identifier(helperCreatorName),
arrowFuncExp
),
])
);
}

/**
Expand All @@ -37,9 +81,28 @@ export function createMsgHelperCreator(
export function createMsgHooks(
context: GenericParseContext,
service: ProtoService,
methodKey: string,
helperCreatorName: string,
hookName: string
methodKey?: string,
helperCreatorName?: string,
hookName?: string
) {
return t.returnStatement(t.identifier(hookName));
const callExpression = ast.callExpression(
ast.identifier("buildUseMutation"),
[
ast.objectExpression([
ast.objectProperty(
ast.identifier("builderMutationFn"),
ast.identifier(helperCreatorName)
),
]),
]
);
callExpression.typeParameters = ast.tsTypeParameterInstantiation([
ast.tsTypeReference(ast.identifier(methodKey)),
ast.tsTypeReference(ast.identifier(`Error`)),
]);
return ast.exportNamedDeclaration(
ast.variableDeclaration("const", [
ast.variableDeclarator(ast.identifier(hookName), callExpression),
])
);
}
Loading
Loading