wasm-ast-types 0.21.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. package/main/client/client.js +4 -6
  2. package/main/message-composer/message-composer.js +3 -5
  3. package/main/react-query/react-query.js +4 -11
  4. package/main/utils/babel.js +1 -0
  5. package/main/utils/constants.js +25 -0
  6. package/main/utils/index.js +22 -1
  7. package/module/client/client.js +3 -4
  8. package/module/message-composer/message-composer.js +4 -5
  9. package/module/react-query/react-query.js +5 -6
  10. package/module/utils/babel.js +1 -0
  11. package/module/utils/constants.js +6 -0
  12. package/module/utils/index.js +3 -1
  13. package/package.json +2 -2
  14. package/src/client/client.ts +235 -323
  15. package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +45 -45
  16. package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +42 -42
  17. package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +3 -3
  18. package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +9 -9
  19. package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +27 -27
  20. package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +6 -6
  21. package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +30 -30
  22. package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +9 -9
  23. package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +78 -78
  24. package/src/client/test/__snapshots__/ts-client.overrides.spec.ts.snap +80 -80
  25. package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +24 -24
  26. package/src/client/test/__snapshots__/ts-client.spec.ts.snap +46 -46
  27. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +24 -24
  28. package/src/client/test/__snapshots__/ts-client.wager.spec.ts.snap +8 -8
  29. package/src/context/context.ts +2 -0
  30. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +30 -30
  31. package/src/message-composer/message-composer.ts +216 -267
  32. package/src/react-query/react-query.ts +28 -25
  33. package/src/utils/babel.ts +4 -3
  34. package/src/utils/constants.ts +30 -0
  35. package/src/utils/index.ts +2 -0
  36. package/types/context/context.d.ts +2 -0
@@ -1,313 +1,262 @@
1
1
  import * as t from '@babel/types';
2
+ import { Expression } from '@babel/types';
2
3
  import { camel } from 'case';
3
4
  import {
4
- bindMethod,
5
- typedIdentifier,
6
- classDeclaration,
7
- classProperty,
8
- arrowFunctionExpression,
9
- getMessageProperties
10
- } from '../utils'
11
- import { ExecuteMsg } from '../types';
5
+ arrowFunctionExpression,
6
+ bindMethod,
7
+ classDeclaration,
8
+ classProperty,
9
+ getMessageProperties,
10
+ OPTIONAL_FUNDS_PARAM,
11
+ typedIdentifier
12
+ } from '../utils';
13
+ import { ExecuteMsg, JSONSchema } from '../types';
12
14
  import { createTypedObjectParams } from '../utils/types';
13
- import { JSONSchema } from '../types';
14
15
  import { RenderContext } from '../context';
15
- import { identifier } from '../utils/babel';
16
16
  import { getWasmMethodArgs } from '../client/client';
17
- import { Expression } from '@babel/types';
18
17
 
19
18
  const createWasmExecMethodMessageComposer = (
20
- context: RenderContext,
21
- jsonschema: any
19
+ context: RenderContext,
20
+ jsonschema: any
22
21
  ) => {
22
+ context.addUtil('Coin');
23
+ context.addUtil('MsgExecuteContractEncodeObject');
24
+ context.addUtil('MsgExecuteContract');
25
+ context.addUtil('toUtf8');
23
26
 
24
- context.addUtil('Coin');
25
- context.addUtil('MsgExecuteContractEncodeObject');
26
- context.addUtil('MsgExecuteContract');
27
- context.addUtil('toUtf8');
28
-
29
- const underscoreName = Object.keys(jsonschema.properties)[0];
30
- const methodName = camel(underscoreName);
31
- const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
32
- const args = getWasmMethodArgs(
33
- context,
34
- jsonschema.properties[underscoreName]
35
- );
27
+ const underscoreName = Object.keys(jsonschema.properties)[0];
28
+ const methodName = camel(underscoreName);
29
+ const param = createTypedObjectParams(
30
+ context,
31
+ jsonschema.properties[underscoreName]
32
+ );
33
+ const args = getWasmMethodArgs(
34
+ context,
35
+ jsonschema.properties[underscoreName]
36
+ );
36
37
 
37
38
  // what the underscore named property in the message is assigned to
38
- let actionValue: Expression
39
+ let actionValue: Expression;
39
40
  if (param?.type === 'Identifier') {
40
41
  actionValue = t.identifier(param.name);
41
42
  } else {
42
- actionValue = t.objectExpression(args)
43
+ actionValue = t.objectExpression(args);
43
44
  }
44
45
 
45
- const constantParams = [
46
- identifier('funds', t.tsTypeAnnotation(
47
- t.tsArrayType(
48
- t.tsTypeReference(
49
- t.identifier('Coin')
50
- )
51
- )
52
- ), true)
53
- ];
46
+ const constantParams = [OPTIONAL_FUNDS_PARAM];
54
47
 
55
- return t.classProperty(
56
- t.identifier(methodName),
57
- arrowFunctionExpression(
58
- param ? [
59
- // props
60
- param,
61
- ...constantParams
62
- ] : constantParams,
63
- t.blockStatement(
48
+ return t.classProperty(
49
+ t.identifier(methodName),
50
+ arrowFunctionExpression(
51
+ param
52
+ ? [
53
+ // props
54
+ param,
55
+ ...constantParams
56
+ ]
57
+ : constantParams,
58
+ t.blockStatement([
59
+ t.returnStatement(
60
+ t.objectExpression([
61
+ t.objectProperty(
62
+ t.identifier('typeUrl'),
63
+ t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')
64
+ ),
65
+ t.objectProperty(
66
+ t.identifier('value'),
67
+ t.callExpression(
68
+ t.memberExpression(
69
+ t.identifier('MsgExecuteContract'),
70
+ t.identifier('fromPartial')
71
+ ),
64
72
  [
65
- t.returnStatement(
66
-
67
- t.objectExpression([
68
- t.objectProperty(
69
- t.identifier('typeUrl'),
70
- t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')
71
- ),
72
- t.objectProperty(
73
- t.identifier('value'),
74
- t.callExpression(
75
- t.memberExpression(
76
- t.identifier('MsgExecuteContract'),
77
- t.identifier('fromPartial')
78
- ),
79
- [
80
- t.objectExpression([
81
- t.objectProperty(
82
- t.identifier('sender'),
83
- t.memberExpression(
84
- t.thisExpression(),
85
- t.identifier('sender')
86
- )
87
- ),
88
- t.objectProperty(
89
- t.identifier('contract'),
90
- t.memberExpression(
91
- t.thisExpression(),
92
- t.identifier('contractAddress')
93
- )
94
- ),
95
- t.objectProperty(
96
- t.identifier('msg'),
97
- t.callExpression(
98
- t.identifier('toUtf8'),
99
- [
100
- t.callExpression(
101
- t.memberExpression(
102
- t.identifier('JSON'),
103
- t.identifier('stringify')
104
- ),
105
- [
106
- t.objectExpression(
107
- [
108
- t.objectProperty(
109
- t.identifier(underscoreName), actionValue
110
- )
111
- ]
112
-
113
- )
114
- ]
115
- )
116
- ]
117
- )
118
- ),
119
- t.objectProperty(
120
- t.identifier('funds'),
121
- t.identifier('funds'),
122
- false,
123
- true
124
- ),
125
- ])
126
- ]
127
- )
128
- )
129
- ])
73
+ t.objectExpression([
74
+ t.objectProperty(
75
+ t.identifier('sender'),
76
+ t.memberExpression(
77
+ t.thisExpression(),
78
+ t.identifier('sender')
79
+ )
80
+ ),
81
+ t.objectProperty(
82
+ t.identifier('contract'),
83
+ t.memberExpression(
84
+ t.thisExpression(),
85
+ t.identifier('contractAddress')
86
+ )
87
+ ),
88
+ t.objectProperty(
89
+ t.identifier('msg'),
90
+ t.callExpression(t.identifier('toUtf8'), [
91
+ t.callExpression(
92
+ t.memberExpression(
93
+ t.identifier('JSON'),
94
+ t.identifier('stringify')
95
+ ),
96
+ [
97
+ t.objectExpression([
98
+ t.objectProperty(
99
+ t.identifier(underscoreName),
100
+ actionValue
101
+ )
102
+ ])
103
+ ]
104
+ )
105
+ ])
106
+ ),
107
+ t.objectProperty(
108
+ t.identifier('funds'),
109
+ t.identifier('_funds')
130
110
  )
111
+ ])
131
112
  ]
132
- ),
133
- // return type
134
- t.tsTypeAnnotation(
135
- t.tsTypeReference(
136
- t.identifier('MsgExecuteContractEncodeObject')
137
- )
138
- ),
139
- false
113
+ )
114
+ )
115
+ ])
140
116
  )
141
- );
142
-
143
- }
117
+ ]),
118
+ // return type
119
+ t.tsTypeAnnotation(
120
+ t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))
121
+ ),
122
+ false
123
+ )
124
+ );
125
+ };
144
126
 
145
127
  export const createMessageComposerClass = (
146
- context: RenderContext,
147
- className: string,
148
- implementsClassName: string,
149
- execMsg: ExecuteMsg
128
+ context: RenderContext,
129
+ className: string,
130
+ implementsClassName: string,
131
+ execMsg: ExecuteMsg
150
132
  ) => {
133
+ const propertyNames = getMessageProperties(execMsg)
134
+ .map((method) => Object.keys(method.properties)?.[0])
135
+ .filter(Boolean);
151
136
 
152
- const propertyNames = getMessageProperties(execMsg)
153
- .map(method => Object.keys(method.properties)?.[0])
154
- .filter(Boolean);
137
+ const bindings = propertyNames.map(camel).map(bindMethod);
155
138
 
156
- const bindings = propertyNames
157
- .map(camel)
158
- .map(bindMethod);
139
+ const methods = getMessageProperties(execMsg).map((schema) => {
140
+ return createWasmExecMethodMessageComposer(context, schema);
141
+ });
159
142
 
160
- const methods = getMessageProperties(execMsg)
161
- .map(schema => {
162
- return createWasmExecMethodMessageComposer(context, schema)
163
- });
143
+ const blockStmt = [];
164
144
 
165
- const blockStmt = [];
145
+ [].push.apply(blockStmt, [
146
+ t.expressionStatement(
147
+ t.assignmentExpression(
148
+ '=',
149
+ t.memberExpression(t.thisExpression(), t.identifier('sender')),
150
+ t.identifier('sender')
151
+ )
152
+ ),
153
+ t.expressionStatement(
154
+ t.assignmentExpression(
155
+ '=',
156
+ t.memberExpression(t.thisExpression(), t.identifier('contractAddress')),
157
+ t.identifier('contractAddress')
158
+ )
159
+ ),
160
+ ...bindings
161
+ ]);
166
162
 
167
- [].push.apply(blockStmt, [
168
- t.expressionStatement(
169
- t.assignmentExpression(
170
- '=',
171
- t.memberExpression(
172
- t.thisExpression(),
173
- t.identifier('sender')
174
- ),
175
- t.identifier('sender')
176
- )
163
+ return t.exportNamedDeclaration(
164
+ classDeclaration(
165
+ className,
166
+ [
167
+ // sender
168
+ classProperty('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
169
+
170
+ // contractAddress
171
+ classProperty(
172
+ 'contractAddress',
173
+ t.tsTypeAnnotation(t.tsStringKeyword())
177
174
  ),
178
- t.expressionStatement(
179
- t.assignmentExpression(
180
- '=',
181
- t.memberExpression(
182
- t.thisExpression(),
183
- t.identifier('contractAddress')
184
- ),
185
- t.identifier('contractAddress')
175
+
176
+ // constructor
177
+ t.classMethod(
178
+ 'constructor',
179
+ t.identifier('constructor'),
180
+ [
181
+ typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
182
+ typedIdentifier(
183
+ 'contractAddress',
184
+ t.tsTypeAnnotation(t.tsStringKeyword())
186
185
  )
186
+ ],
187
+ t.blockStatement(blockStmt)
187
188
  ),
188
- ...bindings
189
- ]);
190
-
191
- return t.exportNamedDeclaration(
192
- classDeclaration(className,
193
- [
194
- // sender
195
- classProperty('sender', t.tsTypeAnnotation(
196
- t.tsStringKeyword()
197
- )),
198
-
199
- // contractAddress
200
- classProperty('contractAddress', t.tsTypeAnnotation(
201
- t.tsStringKeyword()
202
- )),
203
-
204
- // constructor
205
- t.classMethod('constructor',
206
- t.identifier('constructor'),
207
- [
208
- typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
209
- typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))
210
- ],
211
- t.blockStatement(
212
- blockStmt
213
- )),
214
- ...methods
215
- ],
216
- [
217
- t.tSExpressionWithTypeArguments(
218
- t.identifier(implementsClassName)
219
- )
220
- ],
221
- null
222
- )
223
- );
224
- }
189
+ ...methods
190
+ ],
191
+ [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))],
192
+ null
193
+ )
194
+ );
195
+ };
225
196
 
226
197
  export const createMessageComposerInterface = (
227
- context: RenderContext,
228
- className: string,
229
- execMsg: ExecuteMsg
198
+ context: RenderContext,
199
+ className: string,
200
+ execMsg: ExecuteMsg
230
201
  ) => {
202
+ const methods = getMessageProperties(execMsg).map((jsonschema) => {
203
+ const underscoreName = Object.keys(jsonschema.properties)[0];
204
+ const methodName = camel(underscoreName);
205
+ return createPropertyFunctionWithObjectParamsForMessageComposer(
206
+ context,
207
+ methodName,
208
+ 'MsgExecuteContractEncodeObject',
209
+ jsonschema.properties[underscoreName]
210
+ );
211
+ });
231
212
 
232
- const methods = getMessageProperties(execMsg)
233
- .map(jsonschema => {
234
- const underscoreName = Object.keys(jsonschema.properties)[0];
235
- const methodName = camel(underscoreName);
236
- return createPropertyFunctionWithObjectParamsForMessageComposer(
237
- context,
238
- methodName,
239
- 'MsgExecuteContractEncodeObject',
240
- jsonschema.properties[underscoreName]
241
- );
242
- });
243
-
244
- const extendsAst = [];
245
-
246
- return t.exportNamedDeclaration(
247
- t.tsInterfaceDeclaration(
248
- t.identifier(className),
249
- null,
250
- extendsAst,
251
- t.tSInterfaceBody(
252
- [
213
+ const extendsAst = [];
253
214
 
254
- // contract address
255
- t.tSPropertySignature(
256
- t.identifier('contractAddress'),
257
- t.tsTypeAnnotation(
258
- t.tsStringKeyword()
259
- )
260
- ),
215
+ return t.exportNamedDeclaration(
216
+ t.tsInterfaceDeclaration(
217
+ t.identifier(className),
218
+ null,
219
+ extendsAst,
220
+ t.tSInterfaceBody([
221
+ // contract address
222
+ t.tSPropertySignature(
223
+ t.identifier('contractAddress'),
224
+ t.tsTypeAnnotation(t.tsStringKeyword())
225
+ ),
261
226
 
262
- // contract address
263
- t.tSPropertySignature(
264
- t.identifier('sender'),
265
- t.tsTypeAnnotation(
266
- t.tsStringKeyword()
267
- )
268
- ),
227
+ // contract address
228
+ t.tSPropertySignature(
229
+ t.identifier('sender'),
230
+ t.tsTypeAnnotation(t.tsStringKeyword())
231
+ ),
269
232
 
270
- ...methods,
271
- ]
272
- )
273
- )
274
- );
233
+ ...methods
234
+ ])
235
+ )
236
+ );
275
237
  };
276
238
 
277
239
  const createPropertyFunctionWithObjectParamsForMessageComposer = (
278
- context: RenderContext,
279
- methodName: string,
280
- responseType: string,
281
- jsonschema: JSONSchema
240
+ context: RenderContext,
241
+ methodName: string,
242
+ responseType: string,
243
+ jsonschema: JSONSchema
282
244
  ) => {
283
- const obj = createTypedObjectParams(context, jsonschema);
284
- const fixedParams = [
285
- identifier('funds', t.tsTypeAnnotation(
286
- t.tsArrayType(
287
- t.tsTypeReference(
288
- t.identifier('Coin')
289
- )
290
- )
291
- ), true)
292
- ];
293
- const func = {
294
- type: 'TSFunctionType',
295
- typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(
296
- t.identifier(responseType)
297
- )),
298
- parameters: obj ? [
299
- obj,
300
- ...fixedParams
301
-
302
- ] : fixedParams
303
- }
245
+ const obj = createTypedObjectParams(context, jsonschema);
246
+ const fixedParams = [OPTIONAL_FUNDS_PARAM];
247
+ const func = {
248
+ type: 'TSFunctionType',
249
+ typeAnnotation: t.tsTypeAnnotation(
250
+ t.tsTypeReference(t.identifier(responseType))
251
+ ),
252
+ parameters: obj ? [obj, ...fixedParams] : fixedParams
253
+ };
304
254
 
305
- return t.tSPropertySignature(
306
- t.identifier(methodName),
307
- t.tsTypeAnnotation(
308
- // @ts-ignore:next-line
309
- func
310
- )
311
- );
255
+ return t.tSPropertySignature(
256
+ t.identifier(methodName),
257
+ t.tsTypeAnnotation(
258
+ // @ts-ignore:next-line
259
+ func
260
+ )
261
+ );
312
262
  };
313
-
@@ -5,8 +5,10 @@ import { ExecuteMsg, QueryMsg } from '../types';
5
5
  import {
6
6
  callExpression,
7
7
  createTypedObjectParams,
8
+ FIXED_EXECUTE_PARAMS,
8
9
  getMessageProperties,
9
10
  identifier,
11
+ OPTIONAL_FUNDS_PARAM,
10
12
  tsObjectPattern,
11
13
  tsPropertySignature
12
14
  } from '../utils';
@@ -24,8 +26,8 @@ import {
24
26
  } from '../utils/types';
25
27
  import { ReactQueryOptions, RenderContext } from '../context';
26
28
  import { JSONSchema } from '../types';
27
- import { FIXED_EXECUTE_PARAMS } from '../client';
28
29
  import { ArrowFunctionExpression, objectExpression } from '@babel/types';
30
+ import { OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM } from '../utils/constants';
29
31
 
30
32
  interface ReactQueryHookQuery {
31
33
  context: RenderContext;
@@ -120,7 +122,7 @@ export const createReactQueryHooks = ({
120
122
  context,
121
123
  queryFactoryName,
122
124
  queryKeysName,
123
- queryMsgs,
125
+ queryMsgs
124
126
  })
125
127
  );
126
128
  }
@@ -407,7 +409,7 @@ export const createReactQueryMutationArgsInterface = ({
407
409
  )
408
410
  ];
409
411
 
410
- const msgType = createTypedObjectParams(context, jsonschema)?.typeAnnotation
412
+ const msgType = createTypedObjectParams(context, jsonschema)?.typeAnnotation;
411
413
 
412
414
  if (msgType) {
413
415
  body.push(
@@ -415,7 +417,8 @@ export const createReactQueryMutationArgsInterface = ({
415
417
  t.identifier('msg'),
416
418
  // @ts-ignore
417
419
  msgType
418
- ));
420
+ )
421
+ );
419
422
  }
420
423
 
421
424
  context.addUtil('StdFee');
@@ -425,16 +428,11 @@ export const createReactQueryMutationArgsInterface = ({
425
428
  t.identifier('args'),
426
429
  t.tsTypeAnnotation(
427
430
  // @ts-ignore:next-line
428
- t.tsTypeLiteral(
429
- FIXED_EXECUTE_PARAMS.map((param) =>
430
- propertySignature(
431
- param.name,
432
- // @ts-ignore:next-line
433
- param.typeAnnotation,
434
- param.optional
435
- ) as t.TSTypeElement
436
- )
437
- )
431
+ t.tsTypeLiteral([
432
+ propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation, true),
433
+ propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation, true),
434
+ propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation, true)
435
+ ])
438
436
  )
439
437
  );
440
438
 
@@ -568,9 +566,11 @@ export const createReactQueryMutationHook = ({
568
566
  t.objectProperty(
569
567
  t.identifier('args'),
570
568
  t.assignmentPattern(
571
- t.objectPattern(
572
- FIXED_EXECUTE_PARAMS.map((param) => shorthandProperty(param.name))
573
- ),
569
+ t.objectPattern([
570
+ shorthandProperty('fee'),
571
+ shorthandProperty('memo'),
572
+ shorthandProperty('funds')
573
+ ]),
574
574
  t.objectExpression([])
575
575
  )
576
576
  )
@@ -606,11 +606,11 @@ export const createReactQueryMutationHook = ({
606
606
  t.identifier('client'),
607
607
  t.identifier(execMethodName)
608
608
  ),
609
- (hasMsg ? [t.identifier('msg')] : []).concat(
610
- FIXED_EXECUTE_PARAMS.map((param) =>
611
- t.identifier(param.name)
612
- )
613
- )
609
+ (hasMsg ? [t.identifier('msg')] : []).concat([
610
+ t.identifier('fee'),
611
+ t.identifier('memo'),
612
+ t.identifier('funds')
613
+ ])
614
614
  ),
615
615
  false // not async
616
616
  ),
@@ -787,7 +787,9 @@ function createReactQueryFactory({
787
787
  t.tsTypeReference(
788
788
  t.identifier(hookParamsTypeName),
789
789
  t.tsTypeParameterInstantiation([
790
- t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
790
+ t.tsTypeReference(
791
+ t.identifier(GENERIC_SELECT_RESPONSE_NAME)
792
+ )
791
793
  ])
792
794
  )
793
795
  )
@@ -840,7 +842,9 @@ function createReactQueryFactory({
840
842
  t.tsTypeParameterInstantiation([
841
843
  t.tsTypeReference(t.identifier(responseType)),
842
844
  t.tsTypeReference(t.identifier('Error')),
843
- t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
845
+ t.tsTypeReference(
846
+ t.identifier(GENERIC_SELECT_RESPONSE_NAME)
847
+ )
844
848
  ])
845
849
  )
846
850
  );
@@ -871,7 +875,6 @@ function createReactQueryHookGenericInterface({
871
875
  QueryClient,
872
876
  genericQueryInterfaceName
873
877
  }: ReactQueryHookGenericInterface) {
874
-
875
878
  const options = context.options.reactQuery;
876
879
  const genericResponseTypeName = 'TResponse';
877
880