Skip to content

Commit 73150b9

Browse files
authored
Merge pull request #102 from adairrr/fix/tupleEnumVariants
Generate parameters for tuple types
2 parents dccd0ed + 85e69d3 commit 73150b9

File tree

16 files changed

+583
-87
lines changed

16 files changed

+583
-87
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ codegen({
119119
},
120120
messageComposer: {
121121
enabled: false
122+
},
123+
msgBuilder: {
124+
enabled: false
122125
}
123126
}
124127
}).then(() => {
@@ -252,6 +255,28 @@ cosmwasm-ts-codegen generate \
252255
| ------------------------------ | ------------------------------------------------------------------- |
253256
| `messageComposer.enabled` | enable the messageComposer plugin |
254257

258+
### Msg Builder
259+
260+
Generate raw message jsons for use in your application with the `msg-builder` command.
261+
262+
[see example output code](https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26)
263+
264+
#### Msg Builder via CLI
265+
266+
```sh
267+
cosmwasm-ts-codegen generate \
268+
--plugin msg-builder \
269+
--schema ./schema \
270+
--out ./ts \
271+
--name MyContractName
272+
```
273+
#### Message Composer Options
274+
275+
| option | description |
276+
-------------| ------------------------------ | ------------------------------------------------------------------- |
277+
| `msgBuilder.enabled` | enable the msgBilder plugin |
278+
279+
255280
### Bundles
256281

257282
The bundler will make a nice package of all your contracts. For example:
@@ -389,6 +414,10 @@ https://gist.github.com/pyramation/a9520ccf131177b1841e02a97d7d3731
389414

390415
https://gist.github.com/pyramation/43320e8b952751a0bd5a77dbc5b601f4
391416

417+
- `cosmwasm-ts-codegen generate --plugin msg-builder`
418+
419+
https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26
420+
392421

393422
### JSON Schema
394423

__fixtures__/basic/ownership.json

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "ExecuteMsg",
4+
"description": "Ownership Execute msg",
5+
"oneOf": [
6+
{
7+
"description": "Sets a new Factory",
8+
"type": "object",
9+
"required": [
10+
"set_factory"
11+
],
12+
"properties": {
13+
"set_factory": {
14+
"type": "object",
15+
"required": [
16+
"new_factory"
17+
],
18+
"properties": {
19+
"new_factory": {
20+
"type": "string"
21+
}
22+
},
23+
"additionalProperties": false
24+
}
25+
},
26+
"additionalProperties": false
27+
},
28+
{
29+
"description": "Update the contract's ownership. The `action` to be provided can be either to propose transferring ownership to an account, accept a pending ownership transfer, or renounce the ownership permanently.",
30+
"type": "object",
31+
"required": [
32+
"update_ownership"
33+
],
34+
"properties": {
35+
"update_ownership": {
36+
"$ref": "#/definitions/Action"
37+
}
38+
},
39+
"additionalProperties": false
40+
}
41+
],
42+
"definitions": {
43+
"Action": {
44+
"description": "Actions that can be taken to alter the contract's ownership",
45+
"oneOf": [
46+
{
47+
"description": "Propose to transfer the contract's ownership to another account, optionally with an expiry time.\n\nCan only be called by the contract's current owner.\n\nAny existing pending ownership transfer is overwritten.",
48+
"type": "object",
49+
"required": [
50+
"transfer_ownership"
51+
],
52+
"properties": {
53+
"transfer_ownership": {
54+
"type": "object",
55+
"required": [
56+
"new_owner"
57+
],
58+
"properties": {
59+
"expiry": {
60+
"anyOf": [
61+
{
62+
"$ref": "#/definitions/Expiration"
63+
},
64+
{
65+
"type": "null"
66+
}
67+
]
68+
},
69+
"new_owner": {
70+
"type": "string"
71+
}
72+
},
73+
"additionalProperties": false
74+
}
75+
},
76+
"additionalProperties": false
77+
},
78+
{
79+
"description": "Accept the pending ownership transfer.\n\nCan only be called by the pending owner.",
80+
"type": "string",
81+
"enum": [
82+
"accept_ownership"
83+
]
84+
},
85+
{
86+
"description": "Give up the contract's ownership and the possibility of appointing a new owner.\n\nCan only be invoked by the contract's current owner.\n\nAny existing pending ownership transfer is canceled.",
87+
"type": "string",
88+
"enum": [
89+
"renounce_ownership"
90+
]
91+
}
92+
]
93+
},
94+
"Expiration": {
95+
"description": "Expiration represents a point in time when some event happens. It can compare with a BlockInfo and will return is_expired() == true once the condition is hit (and for every block in the future)",
96+
"oneOf": [
97+
{
98+
"description": "AtHeight will expire when `env.block.height` >= height",
99+
"type": "object",
100+
"required": [
101+
"at_height"
102+
],
103+
"properties": {
104+
"at_height": {
105+
"type": "integer",
106+
"format": "uint64",
107+
"minimum": 0.0
108+
}
109+
},
110+
"additionalProperties": false
111+
},
112+
{
113+
"description": "AtTime will expire when `env.block.time` >= time",
114+
"type": "object",
115+
"required": [
116+
"at_time"
117+
],
118+
"properties": {
119+
"at_time": {
120+
"$ref": "#/definitions/Timestamp"
121+
}
122+
},
123+
"additionalProperties": false
124+
},
125+
{
126+
"description": "Never will never expire. Used to express the empty variant",
127+
"type": "object",
128+
"required": [
129+
"never"
130+
],
131+
"properties": {
132+
"never": {
133+
"type": "object",
134+
"additionalProperties": false
135+
}
136+
},
137+
"additionalProperties": false
138+
}
139+
]
140+
},
141+
"Timestamp": {
142+
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
143+
"allOf": [
144+
{
145+
"$ref": "#/definitions/Uint64"
146+
}
147+
]
148+
},
149+
"Uint64": {
150+
"description": "A thin wrapper around u64 that is using strings for JSON encoding/decoding, such that the full u64 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u64` to get the value out:\n\n``` # use cosmwasm_std::Uint64; let a = Uint64::from(42u64); assert_eq!(a.u64(), 42);\n\nlet b = Uint64::from(70u32); assert_eq!(b.u64(), 70); ```",
151+
"type": "string"
152+
}
153+
}
154+
}

packages/ts-codegen/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The quickest and easiest way to interact with CosmWasm Contracts. `@cosmwasm/ts-
3636
- [React Query](#react-query)
3737
- [Recoil](#recoil)
3838
- [Message Composer](#message-composer)
39+
- [Msg Builder](#msg-builder)
3940
- [Bundles](#bundles)
4041
- [CLI Usage and Examples](#cli-usage-and-examples)
4142
- [Advanced Usage](#advanced-usage)
@@ -119,6 +120,9 @@ codegen({
119120
},
120121
messageComposer: {
121122
enabled: false
123+
},
124+
msgBuilder: {
125+
enabled: false
122126
}
123127
}
124128
}).then(() => {
@@ -252,6 +256,28 @@ cosmwasm-ts-codegen generate \
252256
| ------------------------------ | ------------------------------------------------------------------- |
253257
| `messageComposer.enabled` | enable the messageComposer plugin |
254258

259+
### Msg Builder
260+
261+
Generate raw message jsons for use in your application with the `msg-builder` command.
262+
263+
[see example output code](https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26)
264+
265+
#### Msg Builder via CLI
266+
267+
```sh
268+
cosmwasm-ts-codegen generate \
269+
--plugin msg-builder \
270+
--schema ./schema \
271+
--out ./ts \
272+
--name MyContractName
273+
```
274+
#### Message Composer Options
275+
276+
| option | description |
277+
-------------| ------------------------------ | ------------------------------------------------------------------- |
278+
| `msgBuilder.enabled` | enable the msgBilder plugin |
279+
280+
255281
### Bundles
256282

257283
The bundler will make a nice package of all your contracts. For example:

packages/wasm-ast-types/src/client/client.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import * as t from '@babel/types';
2-
import { camel, pascal } from 'case';
2+
import { camel } from 'case';
33
import {
4+
arrowFunctionExpression,
45
bindMethod,
5-
typedIdentifier,
6-
promiseTypeAnnotation,
76
classDeclaration,
87
classProperty,
9-
arrowFunctionExpression,
10-
getMessageProperties
11-
} from '../utils'
8+
getMessageProperties,
9+
promiseTypeAnnotation,
10+
typedIdentifier
11+
} from '../utils';
1212

13-
import {
14-
QueryMsg,
15-
ExecuteMsg
16-
} from '../types';
13+
import { ExecuteMsg, JSONSchema, QueryMsg } from '../types';
1714

18-
import { getPropertyType, getType, createTypedObjectParams, getResponseType } from '../utils/types';
15+
import { createTypedObjectParams, getPropertyType, getResponseType, getType } from '../utils/types';
1916
import { RenderContext } from '../context';
20-
import { JSONSchema } from '../types';
2117
import { identifier, propertySignature } from '../utils/babel';
2218

2319
export const CONSTANT_EXEC_PARAMS = [
@@ -89,7 +85,7 @@ export const createWasmQueryMethod = (
8985
const methodName = camel(underscoreName);
9086
const responseType = getResponseType(context, underscoreName);
9187

92-
const obj = createTypedObjectParams(
88+
const param = createTypedObjectParams(
9389
context,
9490
jsonschema.properties[underscoreName]
9591
);
@@ -99,13 +95,14 @@ export const createWasmQueryMethod = (
9995
jsonschema.properties[underscoreName]
10096
);
10197

102-
const actionArg =
103-
t.objectProperty(t.identifier(underscoreName), t.objectExpression(args));
98+
const msgAction = t.identifier(underscoreName);
99+
// If the param is an identifier, we can just use it as is
100+
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)
104101

105102
return t.classProperty(
106103
t.identifier(methodName),
107104
arrowFunctionExpression(
108-
obj ? [obj] : [],
105+
param ? [param] : [],
109106
t.blockStatement(
110107
[
111108
t.returnStatement(
@@ -120,7 +117,7 @@ export const createWasmQueryMethod = (
120117
[
121118
t.memberExpression(t.thisExpression(), t.identifier('contractAddress')),
122119
t.objectExpression([
123-
actionArg
120+
t.objectProperty(msgAction, msgActionValue)
124121
])
125122
]
126123
)
@@ -237,9 +234,15 @@ export const getWasmMethodArgs = (
237234
// only 1 degree $ref-lookup
238235
if (!keys.length && jsonschema.$ref) {
239236
const obj = context.refLookup(jsonschema.$ref);
237+
// properties
240238
if (obj) {
241239
keys = Object.keys(obj.properties ?? {})
242240
}
241+
242+
// tuple struct or otherwise, use the name of the reference
243+
if (!keys.length && obj?.oneOf) {
244+
// TODO????? ADAIR
245+
}
243246
}
244247

245248
const args = keys.map(prop => {
@@ -265,7 +268,7 @@ export const createWasmExecMethod = (
265268

266269
const underscoreName = Object.keys(jsonschema.properties)[0];
267270
const methodName = camel(underscoreName);
268-
const obj = createTypedObjectParams(
271+
const param = createTypedObjectParams(
269272
context,
270273
jsonschema.properties[underscoreName]
271274
);
@@ -274,12 +277,16 @@ export const createWasmExecMethod = (
274277
jsonschema.properties[underscoreName]
275278
);
276279

280+
const msgAction = t.identifier(underscoreName);
281+
// If the param is an identifier, we can just use it as is
282+
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)
283+
277284
return t.classProperty(
278285
t.identifier(methodName),
279286
arrowFunctionExpression(
280-
obj ? [
287+
param ? [
281288
// props
282-
obj,
289+
param,
283290
...CONSTANT_EXEC_PARAMS
284291
] : CONSTANT_EXEC_PARAMS,
285292
t.blockStatement(
@@ -306,10 +313,8 @@ export const createWasmExecMethod = (
306313
t.objectExpression(
307314
[
308315
t.objectProperty(
309-
t.identifier(underscoreName),
310-
t.objectExpression([
311-
...args
312-
])
316+
msgAction,
317+
msgActionValue
313318
)
314319

315320
]

0 commit comments

Comments
 (0)