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

Feat/client #26

Merged
merged 16 commits into from
Apr 22, 2022
16 changes: 16 additions & 0 deletions packages/ast-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.1.7](https://github.com/osmosis-labs/telescope/compare/@osmonauts/[email protected]...@osmonauts/[email protected]) (2022-04-22)

**Note:** Version bump only for package @osmonauts/ast-gen





## [0.1.6](https://github.com/osmosis-labs/telescope/compare/@osmonauts/[email protected]...@osmonauts/[email protected]) (2022-04-22)

**Note:** Version bump only for package @osmonauts/ast-gen





## [0.1.5](https://github.com/osmosis-labs/telescope/compare/@osmonauts/[email protected]...@osmonauts/[email protected]) (2022-04-21)

**Note:** Version bump only for package @osmonauts/ast-gen
Expand Down
10 changes: 5 additions & 5 deletions packages/ast-gen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@osmonauts/ast-gen",
"version": "0.1.5",
"version": "0.1.7",
"description": "Cosmos TypeScript AST generation",
"author": "Dan Lynch <[email protected]>",
"homepage": "https://github.com/osmosis-labs/telescope/tree/master/packages/ast-gen#readme",
Expand Down Expand Up @@ -85,10 +85,10 @@
"dependencies": {
"@babel/runtime": "^7.11.2",
"@babel/types": "7.17.0",
"@cosmjs/amino": "0.27.1",
"@cosmjs/proto-signing": "0.27.1",
"@cosmjs/stargate": "0.27.1",
"@cosmjs/tendermint-rpc": "^0.28.0",
"@cosmjs/amino": "0.28.4",
"@cosmjs/proto-signing": "0.28.4",
"@cosmjs/stargate": "0.28.4",
"@cosmjs/tendermint-rpc": "^0.28.4",
"ast-stringify": "0.1.0",
"case": "1.6.3",
"long": "^5.2.0"
Expand Down
11 changes: 3 additions & 8 deletions packages/ast-gen/src/__snapshots__/messages.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,13 @@ exports[`addToJSONMethod 1`] = `

exports[`createRegistryLoader 1`] = `
"export const load = (protoRegistry: Registry) => {
Object.keys(registry).forEach(typeUrl => {
protoRegistry.register(typeUrl, registry[typeUrl]);
registry.forEach(([typeUrl, mod]) => {
protoRegistry.register(typeUrl, mod);
});
};"
`;

exports[`createTypeRegistry 1`] = `
"export const registry = {
\\"/cosmos.pools.transfer.v1.MsgJoinPool\\": MsgJoinPool,
\\"/cosmos.pools.transfer.v1.MsgExitPool\\": MsgExitPool
};"
`;
exports[`createTypeRegistry 1`] = `"export const registry: ReadonlyArray<[string, GeneratedType]> = [[\\"/cosmos.pools.transfer.v1.MsgJoinPool\\", MsgJoinPool], [\\"/cosmos.pools.transfer.v1.MsgExitPool\\", MsgExitPool]];"`;

exports[`toObjectWithEncodedMethods 1`] = `
"export const encoded = {
Expand Down
33 changes: 33 additions & 0 deletions packages/ast-gen/src/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
createClient
} from './client';
import generate from '@babel/generator';

import { mutations } from './__fixtures__';

const expectCode = (ast) => {
expect(
generate(ast).code
).toMatchSnapshot();
}
const printCode = (ast) => {
console.log(
generate(ast).code
);
}

it('createClient', async () => {
printCode(createClient({
name: 'getSigningOsmosisClient',
registries: [
'osmosis.gamm.v1beta1',
'osmosis.superfluid.v1beta1',
'osmosis.lockup'
],
aminos: [
'osmosis.gamm.v1beta1',
'osmosis.superfluid.v1beta1',
'osmosis.lockup'
]
}));
});
188 changes: 188 additions & 0 deletions packages/ast-gen/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@

import * as t from '@babel/types';
import { memberExpressionOrIdentifier } from './utils';

const tsObjectPattern = (
properties: (t.RestElement | t.ObjectProperty)[],
typeAnnotation: t.TSTypeAnnotation
) => {
const obj = t.objectPattern(properties);
obj.typeAnnotation = typeAnnotation;
return obj;
}
interface CreateClient {
name: string;
registries: string[];
aminos: string[];
}
export const createClient = ({ name, registries, aminos }: CreateClient) => {

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(
[
tsObjectPattern(
[
t.objectProperty(
t.identifier('rpcEndpoint'),
t.identifier('rpcEndpoint'),
false,
true
),
t.objectProperty(
t.identifier('signer'),
t.identifier('signer'),
false,
true
),
t.objectProperty(
t.identifier('defaultTypes'),
t.assignmentPattern(
t.identifier('defaultTypes'),
t.identifier('defaultRegistryTypes')
),
false,
true
)
],
t.tsTypeAnnotation(
t.tsTypeLiteral(
[
t.tsPropertySignature(
t.identifier('rpcEndpoint'),
t.tsTypeAnnotation(t.tsStringKeyword())
),
t.tsPropertySignature(
t.identifier('signer'),
t.tsTypeAnnotation(t.tsTypeReference(
t.identifier('OfflineSigner')
))
),
prop

]
)
)
)
],
t.blockStatement(
[
t.variableDeclaration(
'const',
[
t.variableDeclarator(
t.identifier('registry'),
t.newExpression(
t.identifier('Registry'),
[
t.arrayExpression(
[
t.spreadElement(
t.identifier('defaultTypes')
),
...registries.map(pkg =>
t.spreadElement(
memberExpressionOrIdentifier(
`${pkg}.registry`.split('.').reverse()
)
)
)
]
)
]
)
)
]
),
// amino
t.variableDeclaration(
'const',
[
t.variableDeclarator(
t.identifier('aminoTypes'),
t.newExpression(
t.identifier('AminoTypes'),
[
t.objectExpression(
[
...aminos.map(pkg =>
t.spreadElement(
memberExpressionOrIdentifier(
`${pkg}.AminoConverter`.split('.').reverse()
)
)
)
]
)
]
)
)
]
),
// 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.identifier('registry'),
false,
true
),
t.objectProperty(
t.identifier('aminoTypes'),
t.identifier('aminoTypes'),
false,
true
),
])

]
))
)
]
),

// return
t.returnStatement(t.identifier('client'))
]
),
true
)
)
]
)
)
};
1 change: 1 addition & 0 deletions packages/ast-gen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './types';
export * from './amino';
export * from './client';
export * from './messages';
export * from './utils';
67 changes: 40 additions & 27 deletions packages/ast-gen/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ export const addEncodedMethod = ({ methodName, typeUrl, TypeName }) => {
)]));
};

export const createTypeRegistryObject = (mutation: Mutation) => {
return t.objectProperty(
t.stringLiteral(mutation.typeUrl),
t.identifier(mutation.TypeName)
);
};

export const toObjectWithPartialMethods = (mutations: Mutation[]) => t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('messages'), t.objectExpression(
Expand All @@ -166,10 +160,40 @@ export const toObjectWithFromJSONMethods = (mutations: Mutation[]) => t.exportNa
mutations.map(mutation => addFromJSONMethod(mutation))
))]));

export const createTypeRegistry = (mutations: Mutation[]) => t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('registry'), t.objectExpression(
mutations.map(mutation => createTypeRegistryObject(mutation))
))]));
export const createTypeRegistryObject = (mutation: Mutation) => {
return t.objectProperty(
t.stringLiteral(mutation.typeUrl),
t.identifier(mutation.TypeName)
);
};

export const createTypeRegistry = (mutations: Mutation[]) => t.exportNamedDeclaration(
t.variableDeclaration('const', [
t.variableDeclarator(tsIdentifier(
'registry',
t.tsTypeAnnotation(
t.tsTypeReference(t.identifier('ReadonlyArray'), t.tsTypeParameterInstantiation(
[
t.tsTupleType([
t.tsStringKeyword(),
t.tsTypeReference(
t.identifier('GeneratedType')
)]
)
]
))
)
), t.arrayExpression(
[
...mutations.map(mutation => t.arrayExpression(
[
t.stringLiteral(mutation.typeUrl),
t.identifier(mutation.TypeName)
]
))
]
))
]));

export const createRegistryLoader = () => {
return t.exportNamedDeclaration(t.variableDeclaration(
Expand All @@ -190,23 +214,16 @@ export const createRegistryLoader = () => {
t.expressionStatement(
t.callExpression(
t.memberExpression(
t.callExpression(
t.memberExpression(
t.identifier(
'Object'
),
t.identifier('keys')
),
[
t.identifier('registry')
]
),
t.identifier('registry'),
t.identifier('forEach')
),
[
t.arrowFunctionExpression(
[
t.identifier('typeUrl')
t.arrayPattern([
t.identifier('typeUrl'),
t.identifier('mod')
])
],
t.blockStatement(
[
Expand All @@ -220,11 +237,7 @@ export const createRegistryLoader = () => {
),
[
t.identifier('typeUrl'),
t.memberExpression(
t.identifier('registry'),
t.identifier('typeUrl'),
true
)
t.identifier('mod')
]
)
)
Expand Down
Loading