@osmonauts/ast-gen
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@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

      }
    }
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @osmonauts/ast-gen

Weekly Downloads

1

Version

0.2.0

License

SEE LICENSE IN LICENSE

Unpacked Size

118 kB

Total Files

42

Last publish

Collaborators

  • ljun
  • zetazz
  • daniel-farinax
  • pyramation