Skip to content

Commit

Permalink
feat: Add query suffix to all query methods in generated clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
octalmage committed Sep 23, 2022
1 parent e39a93e commit 90da27f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 25 deletions.
24 changes: 7 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,16 @@ npm run start

## Run Contract Functions with Terrain

Once you have successfully deployed your project, you can interact with the deployed contract and the underlying blockchain by utilizing functions defined in the `lib/index.js` file. You may also create your own abstractions in this file for querying or executing transactions. The default contents of the `lib/index.js` file are presented below.
Once you have successfully deployed your project, you can interact with the deployed contract and the underlying blockchain by utilizing functions defined in the `lib/index.js` file. You may also create your own abstractions in this file for querying or executing transactions.

```js
// lib/index.js

module.exports = ({ wallets, refs, config, client }) => ({
getCount: () => client.query("counter", { get_count: {} }),
increment: (signer = wallets.test1) =>
client.execute(signer, "counter", { increment: {} }),
});
```

You can call the functions defined above inside of the `terrain console`. An example using the `counter` smart contract is shown below.
You can call the functions defined in `lib/index.js` inside of the `terrain console`. An example using the `counter` smart contract is shown below.

```sh
terrain console
terrain > await lib.getCount()
terrain > await lib.getCountQuery()
{ count: 0 }
terrain > await lib.increment()
terrain > await lib.getCount()
terrain > await lib.getCountQuery()
{ count: 1 }
```
Expand All @@ -296,10 +286,10 @@ const { task } = require("@terra-money/terrain");
const lib = require("../lib");
task(async (env) => {
const { getCount, increment } = lib(env);
console.log("count 1 = ", await getCount());
const { getCountQuery, increment } = lib(env);
console.log("count 1 = ", await getCountQuery());
await increment();
console.log("count 2 = ", await getCount());
console.log("count 2 = ", await getCountQuery());
});
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@oclif/core": "^1.0.11",
"@oclif/plugin-help": "^5.1.12",
"@oclif/plugin-not-found": "^2.3.1",
"@octalmage/terra-cosmwasm-typescript-gen": "^0.1.1",
"@octalmage/terra-cosmwasm-typescript-gen": "^0.2.0",
"@terra-money/template-scaffolding": "1.0.1",
"@terra-money/terra.js": "^3.1.5",
"adm-zip": "^0.5.9",
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmwasm-typescript-gen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@octalmage/terra-cosmwasm-typescript-gen",
"version": "0.1.1",
"version": "0.2.0",
"description": "A TypeScript Transpiler for CosmWasm Smart Contracts on Terra",
"author": "Dan Lynch <pyramation@gmail.com>",
"homepage": "https://github.com/pyramation/cosmwasm-typescript-gen",
Expand Down Expand Up @@ -78,7 +78,7 @@
"@babel/runtime": "^7.19.0",
"@babel/traverse": "7.19.0",
"@babel/types": "7.18.4",
"@octalmage/wasm-ast-types": "^0.3.3",
"@octalmage/wasm-ast-types": "^0.4.0",
"case": "1.6.3",
"dargs": "7.0.0",
"fuzzy": "0.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/wasm-ast-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@octalmage/wasm-ast-types",
"version": "0.3.3",
"version": "0.4.0",
"description": "CosmWasm TypeScript AST generation",
"author": "Dan Lynch <pyramation@gmail.com>",
"homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
Expand Down
4 changes: 3 additions & 1 deletion packages/wasm-ast-types/src/utils/babel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as t from '@babel/types';
import { snake } from "case";
import { snake, camel } from "case";
import { Field, QueryMsg, ExecuteMsg } from '../types';
import { TSTypeAnnotation, TSExpressionWithTypeArguments } from '@babel/types';

Expand Down Expand Up @@ -67,6 +67,8 @@ export const callExpression = (
return callExpr;
};

export const convertToQueryMethod = (name: string) => `${camel(name)}Query`;

export const bindMethod = (name: string) => {
return t.expressionStatement(
t.assignmentExpression('=', t.memberExpression(
Expand Down
8 changes: 5 additions & 3 deletions packages/wasm-ast-types/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
classProperty,
arrowFunctionExpression,
getMessageProperties,
convertToQueryMethod,
} from "./utils";

import { QueryMsg, ExecuteMsg } from "./types";
Expand Down Expand Up @@ -49,7 +50,7 @@ export const createWasmQueryMethod = (jsonschema: any, responses: string[]) => {
);

return t.classProperty(
t.identifier(methodName),
t.identifier(convertToQueryMethod(methodName)),
arrowFunctionExpression(
obj ? [obj] : [],
t.blockStatement([
Expand Down Expand Up @@ -95,7 +96,7 @@ export const createQueryClass = (
.map((method) => Object.keys(method.properties)?.[0])
.filter(Boolean);

const bindings = propertyNames.map(camel).map(bindMethod);
const bindings = propertyNames.map(camel).map(convertToQueryMethod).map(bindMethod);

const methods = getMessageProperties(queryMsg).map((schema) => {
return createWasmQueryMethod(schema, responses);
Expand Down Expand Up @@ -529,6 +530,7 @@ export const createQueryInterface = (className: string, queryMsg: QueryMsg, resp
const methods = getMessageProperties(queryMsg).map((jsonschema) => {
const underscoreName = Object.keys(jsonschema.properties)[0];
const methodName = camel(underscoreName);
const queryMethodName = convertToQueryMethod(underscoreName);
let responseType = pascal(`${methodName}Response`);

// Support naming query responses exactly like the method, or without a preceding "Get".
Expand All @@ -538,7 +540,7 @@ export const createQueryInterface = (className: string, queryMsg: QueryMsg, resp
}

return createPropertyFunctionWithObjectParams(
methodName,
queryMethodName,
responseType,
jsonschema.properties[underscoreName]
);
Expand Down

0 comments on commit 90da27f

Please sign in to comment.