@cosmology/ast
Version:
Cosmos TypeScript AST generation
195 lines (194 loc) • 10.5 kB
JavaScript
import * as t from "@babel/types";
import { arrowFunctionExpression, identifier, memberExpressionOrIdentifier, objectPattern, } from "../../utils";
export const createStargateClient = ({ name, options, context, }) => {
const includeDefaults = context.pluginValue("stargateClients.includeCosmosDefaultTypes");
let optsFuncArgs = [];
if (includeDefaults) {
context.addUtil("defaultRegistryTypes");
optsFuncArgs = [
t.objectExpression([
t.objectProperty(t.identifier("defaultTypes"), t.identifier("defaultTypes"), false, true),
]),
];
}
context.addUtil("GeneratedType");
context.addUtil("OfflineSigner");
context.addUtil("Registry");
context.addUtil("AminoTypes");
context.addUtil("SigningStargateClient");
context.addUtil("HttpEndpoint");
const prop = t.tsPropertySignature(t.identifier("defaultTypes"), t.tsTypeAnnotation(t.tsTypeReference(t.identifier("ReadonlyArray"), t.tsTypeParameterInstantiation([
t.tsTupleType([
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("GeneratedType")),
]),
]))));
prop.optional = true;
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier(name), t.arrowFunctionExpression([
objectPattern([
t.objectProperty(t.identifier("rpcEndpoint"), t.identifier("rpcEndpoint"), false, true),
t.objectProperty(t.identifier("signer"), t.identifier("signer"), false, true),
includeDefaults &&
t.objectProperty(t.identifier("defaultTypes"), t.assignmentPattern(t.identifier("defaultTypes"), t.identifier("defaultRegistryTypes")), false, true),
].filter(Boolean), t.tsTypeAnnotation(t.tsTypeLiteral([
t.tsPropertySignature(t.identifier("rpcEndpoint"), t.tsTypeAnnotation(t.tsUnionType([
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("HttpEndpoint")),
]))),
t.tsPropertySignature(t.identifier("signer"), t.tsTypeAnnotation(t.tsTypeReference(t.identifier("OfflineSigner")))),
includeDefaults && prop,
].filter(Boolean)))),
], t.blockStatement([
// props
t.variableDeclaration("const", [
t.variableDeclarator(t.objectPattern([
t.objectProperty(t.identifier("registry"), t.identifier("registry"), false, true),
t.objectProperty(t.identifier("aminoTypes"), t.identifier("aminoTypes"), false, true),
]), t.callExpression(t.identifier(options), optsFuncArgs)),
]),
// client
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("client"), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier("SigningStargateClient"), t.identifier("connectWithSigner")), [
t.identifier("rpcEndpoint"),
t.identifier("signer"),
t.objectExpression([
t.objectProperty(t.identifier("registry"), t.tsAsExpression(t.identifier("registry"), t.tsAnyKeyword())),
t.objectProperty(t.identifier("aminoTypes"), t.identifier("aminoTypes"), false, true),
]),
]))),
]),
// return
t.returnStatement(t.identifier("client")),
]), true)),
]));
};
export const createStargateClientAminoRegistry = ({ aminos, aminoConverters, context, }) => {
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier(aminoConverters), t.objectExpression([
...aminos.map((pkg) => t.spreadElement(memberExpressionOrIdentifier(`${pkg}.AminoConverter`.split(".").reverse()))),
])),
]));
};
export const createStargateClientProtoRegistry = ({ registries, protoTypeRegistry, context, }) => {
context.addUtil("GeneratedType");
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(identifier(protoTypeRegistry, t.tsTypeAnnotation(t.tsTypeReference(t.identifier("ReadonlyArray"), t.tsTypeParameterInstantiation([
t.tsTupleType([
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("GeneratedType")),
]),
])))), t.arrayExpression([
...registries.map((pkg) => t.spreadElement(memberExpressionOrIdentifier(`${pkg}.registry`.split(".").reverse()))),
])),
]));
};
export const createStargateClientOptions = ({ name, aminoConverters, protoTypeRegistry, context, }) => {
const includeDefaults = context.pluginValue("stargateClients.includeCosmosDefaultTypes");
if (includeDefaults) {
context.addUtil("defaultRegistryTypes");
}
context.addUtil("GeneratedType");
context.addUtil("Registry");
context.addUtil("AminoTypes");
context.addUtil("SigningStargateClient");
const prop = t.tsPropertySignature(t.identifier("defaultTypes"), t.tsTypeAnnotation(t.tsTypeReference(t.identifier("ReadonlyArray"), t.tsTypeParameterInstantiation([
t.tsTupleType([
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("GeneratedType")),
]),
]))));
prop.optional = true;
let funcArgs = [];
if (includeDefaults) {
const args = [
t.objectProperty(t.identifier("defaultTypes"), t.assignmentPattern(t.identifier("defaultTypes"), t.identifier("defaultRegistryTypes")), false, true),
];
//
const funcTypes = t.tsTypeAnnotation(t.tsTypeLiteral([prop].filter(Boolean)));
funcArgs = [
t.assignmentPattern(objectPattern(args, funcTypes), t.objectExpression([])),
];
}
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier(name), arrowFunctionExpression(funcArgs, t.blockStatement([
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("registry"), t.newExpression(t.identifier("Registry"), [
t.arrayExpression([
includeDefaults &&
t.spreadElement(t.identifier("defaultTypes")),
t.spreadElement(t.identifier(protoTypeRegistry)),
].filter(Boolean)),
])),
]),
// amino
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("aminoTypes"), t.newExpression(t.identifier("AminoTypes"), [
t.objectExpression([
t.spreadElement(t.identifier(aminoConverters)),
]),
])),
]),
// NEW CODE
// return
t.returnStatement(t.objectExpression([
t.objectProperty(t.identifier("registry"), t.identifier("registry"), false, true),
t.objectProperty(t.identifier("aminoTypes"), t.identifier("aminoTypes"), false, true),
])),
]), t.tsTypeAnnotation(t.tsTypeLiteral([
t.tsPropertySignature(t.identifier("registry"), t.tsTypeAnnotation(t.tsTypeReference(t.identifier("Registry")))),
t.tsPropertySignature(t.identifier("aminoTypes"), t.tsTypeAnnotation(t.tsTypeReference(t.identifier("AminoTypes")))),
])), false)),
]));
};
export const createGetTxRpc = (ctx, name, clientName) => {
ctx.addUtil("createRpcClient");
ctx.addUtil("DeliverTxResponse");
ctx.addUtil("EncodeObject");
ctx.addUtil("StdFee");
ctx.addUtil("TxRpc");
ctx.addUtil("SigningClientParams");
const paramExpr = t.objectPattern([
t.objectProperty(t.identifier("rpcEndpoint"), t.identifier("rpcEndpoint"), false, true),
t.objectProperty(t.identifier("signer"), t.identifier("signer"), false, true),
]);
paramExpr.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier("SigningClientParams")));
const methodArgs = [
identifier("signerAddress", t.tsTypeAnnotation(t.tsStringKeyword())),
identifier("messages", t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier("EncodeObject"))))),
identifier("fee", t.tsTypeAnnotation(t.tsUnionType([
t.tsNumberKeyword(),
t.tsTypeReference(t.identifier("StdFee")),
t.tsLiteralType(t.stringLiteral("auto")),
]))),
identifier("memo", t.tsTypeAnnotation(t.tsStringKeyword()), true),
];
return t.exportNamedDeclaration(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier(name), t.arrowFunctionExpression([paramExpr], t.blockStatement([
t.variableDeclaration("let", [
t.variableDeclarator(t.identifier("txRpc"), t.tSAsExpression(t.awaitExpression(t.callExpression(t.identifier("createRpcClient"), [
t.identifier("rpcEndpoint"),
])), t.tSTypeReference(t.identifier("TxRpc"), null))),
]),
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("signingClient"), t.awaitExpression(t.callExpression(t.identifier(clientName), [
t.objectExpression([
t.objectProperty(t.identifier("rpcEndpoint"), t.identifier("rpcEndpoint"), false, true),
t.objectProperty(t.identifier("signer"), t.identifier("signer"), false, true),
]),
]))),
]),
t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.identifier("txRpc"), t.identifier("signAndBroadcast")), t.arrowFunctionExpression(methodArgs, t.blockStatement([
t.returnStatement(t.tSAsExpression(t.callExpression(t.memberExpression(t.identifier("signingClient"), t.identifier("signAndBroadcast")), [
t.identifier("signerAddress"),
t.identifier("messages"),
t.identifier("fee"),
t.identifier("memo"),
]), t.tSTypeReference(t.identifier("Promise"), t.tSTypeParameterInstantiation([
t.tSTypeReference(t.identifier("DeliverTxResponse")),
])))),
], [])))),
t.returnStatement(t.identifier("txRpc")),
], []), true)),
]), []);
};