@osmonauts/ast-gen
v0.2.0
Published
Cosmos TypeScript AST generation
Downloads
15
Readme
@osmonauts/ast-gen
Cosmos Typescript ASTs
messages
import generate from '@babel/generator';
import { messages } from '@osmonauts/ast-gen';
export const mutations: Mutation[] = [
{ methodName: 'joinPool', typeUrl: '/cosmos.pools.transfer.v1.MsgJoinPool', TypeName: 'MsgJoinPool' },
{ methodName: 'exitPool', typeUrl: '/cosmos.pools.transfer.v1.MsgExitPool', TypeName: 'MsgExitPool' }
];
const ast = messages(mutations)
console.log(generate(myModule).code)
produces:
export const messages = {
joinPool(value: MsgJoinPool) {
return {
typeUrl: "/cosmos.pools.transfer.v1.MsgJoinPool",
value: MsgJoinPool.fromPartial(value)
};
},
exitPool(value: MsgExitPool) {
return {
typeUrl: "/cosmos.pools.transfer.v1.MsgExitPool",
value: MsgExitPool.fromPartial(value)
};
}
};
encoded
import generate from '@babel/generator';
import { encoded } from '@osmonauts/ast-gen';
export const mutations: Mutation[] = [
{ methodName: 'joinPool', typeUrl: '/cosmos.pools.transfer.v1.MsgJoinPool', TypeName: 'MsgJoinPool' },
{ methodName: 'exitPool', typeUrl: '/cosmos.pools.transfer.v1.MsgExitPool', TypeName: 'MsgExitPool' }
];
const ast = encoded(mutations)
console.log(generate(myModule).code)
produces:
export const encoded = {
joinPool(value: MsgJoinPool) {
return {
type_url: "/cosmos.pools.transfer.v1.MsgJoinPool",
value: MsgJoinPool.encode(value).finish()
};
},
exitPool(value: MsgExitPool) {
return {
type_url: "/cosmos.pools.transfer.v1.MsgExitPool",
value: MsgExitPool.encode(value).finish()
};
}
};
namespaces
import generate from '@babel/generator';
import * as t from '@babel/types';
import { recursiveNamespace } from '@osmonauts/ast-gen';
const myModule = recursiveNamespace(
[
'osmosis',
'gamm',
'v1beta',
'pools'
].reverse(), [astBody]);
console.log(generate(t.program(myModule)).code)
produces:
export namespace osmosis {
export namespace gamm {
export namespace v1beta {
export namespace pools {
// astBody here
}
}
}
}