wasm-ast-types 0.11.3 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/main/client/test/ts-client.issue-71.test.js +103 -0
  2. package/main/context/context.js +2 -1
  3. package/main/utils/types.js +20 -7
  4. package/module/client/test/ts-client.issue-71.test.js +21 -0
  5. package/module/context/context.js +2 -1
  6. package/module/utils/types.js +20 -7
  7. package/package.json +5 -3
  8. package/src/client/client.ts +665 -0
  9. package/src/client/index.ts +1 -0
  10. package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +497 -0
  11. package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +452 -0
  12. package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +101 -0
  13. package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +141 -0
  14. package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +341 -0
  15. package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +20 -0
  16. package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +432 -0
  17. package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +984 -0
  18. package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +350 -0
  19. package/src/client/test/__snapshots__/ts-client.spec.ts.snap +723 -0
  20. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +337 -0
  21. package/src/client/test/ts-client.account-nfts.spec.ts +55 -0
  22. package/src/client/test/ts-client.arrays-ref.spec.ts +48 -0
  23. package/src/client/test/ts-client.arrays.spec.ts +58 -0
  24. package/src/client/test/ts-client.cw-named-groups.test.ts +48 -0
  25. package/src/client/test/ts-client.cw-proposal-single.test.ts +50 -0
  26. package/src/client/test/ts-client.empty-enums.spec.ts +28 -0
  27. package/src/client/test/ts-client.issue-71.test.ts +51 -0
  28. package/src/client/test/ts-client.issues.test.ts +52 -0
  29. package/src/client/test/ts-client.sg721.spec.ts +46 -0
  30. package/src/client/test/ts-client.spec.ts +166 -0
  31. package/src/client/test/ts-client.vectis.spec.ts +97 -0
  32. package/src/context/context.ts +134 -0
  33. package/src/context/imports.ts +126 -0
  34. package/src/context/index.ts +2 -0
  35. package/src/index.ts +7 -0
  36. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +271 -0
  37. package/src/message-composer/index.ts +1 -0
  38. package/src/message-composer/message-composer.spec.ts +25 -0
  39. package/src/message-composer/message-composer.ts +305 -0
  40. package/src/react-query/__snapshots__/react-query.spec.ts.snap +913 -0
  41. package/src/react-query/index.ts +1 -0
  42. package/src/react-query/react-query.spec.ts +75 -0
  43. package/src/react-query/react-query.ts +913 -0
  44. package/src/recoil/__snapshots__/recoil.spec.ts.snap +203 -0
  45. package/src/recoil/index.ts +1 -0
  46. package/src/recoil/recoil.spec.ts +38 -0
  47. package/src/recoil/recoil.ts +307 -0
  48. package/src/types.ts +44 -0
  49. package/src/utils/__snapshots__/babel.spec.ts.snap +75 -0
  50. package/src/utils/babel.spec.ts +511 -0
  51. package/src/utils/babel.ts +315 -0
  52. package/src/utils/index.ts +2 -0
  53. package/src/utils/types.ts +459 -0
  54. package/types/client/client.d.ts +1 -1
  55. package/types/context/context.d.ts +1 -0
@@ -0,0 +1,271 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`createMessageComposerInterface 1`] = `
4
+ "export interface SG721Message {
5
+ contractAddress: string;
6
+ sender: string;
7
+ transferNft: ({
8
+ recipient,
9
+ tokenId
10
+ }: {
11
+ recipient: string;
12
+ tokenId: string;
13
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
14
+ sendNft: ({
15
+ contract,
16
+ msg,
17
+ tokenId
18
+ }: {
19
+ contract: string;
20
+ msg: Binary;
21
+ tokenId: string;
22
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
23
+ approve: ({
24
+ expires,
25
+ spender,
26
+ tokenId
27
+ }: {
28
+ expires?: Expiration;
29
+ spender: string;
30
+ tokenId: string;
31
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
32
+ revoke: ({
33
+ spender,
34
+ tokenId
35
+ }: {
36
+ spender: string;
37
+ tokenId: string;
38
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
39
+ approveAll: ({
40
+ expires,
41
+ operator
42
+ }: {
43
+ expires?: Expiration;
44
+ operator: string;
45
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
46
+ revokeAll: ({
47
+ operator
48
+ }: {
49
+ operator: string;
50
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
51
+ mint: ({
52
+ extension,
53
+ owner,
54
+ tokenId,
55
+ tokenUri
56
+ }: {
57
+ extension: Empty;
58
+ owner: string;
59
+ tokenId: string;
60
+ tokenUri?: string;
61
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
62
+ burn: ({
63
+ tokenId
64
+ }: {
65
+ tokenId: string;
66
+ }, funds?: Coin[]) => MsgExecuteContractEncodeObject;
67
+ }"
68
+ `;
69
+
70
+ exports[`execute classes 1`] = `
71
+ "export class SG721MessageComposer implements SG721Message {
72
+ sender: string;
73
+ contractAddress: string;
74
+
75
+ constructor(sender: string, contractAddress: string) {
76
+ this.sender = sender;
77
+ this.contractAddress = contractAddress;
78
+ this.transferNft = this.transferNft.bind(this);
79
+ this.sendNft = this.sendNft.bind(this);
80
+ this.approve = this.approve.bind(this);
81
+ this.revoke = this.revoke.bind(this);
82
+ this.approveAll = this.approveAll.bind(this);
83
+ this.revokeAll = this.revokeAll.bind(this);
84
+ this.mint = this.mint.bind(this);
85
+ this.burn = this.burn.bind(this);
86
+ }
87
+
88
+ transferNft = ({
89
+ recipient,
90
+ tokenId
91
+ }: {
92
+ recipient: string;
93
+ tokenId: string;
94
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
95
+ return {
96
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
97
+ value: MsgExecuteContract.fromPartial({
98
+ sender: this.sender,
99
+ contract: this.contractAddress,
100
+ msg: toUtf8(JSON.stringify({
101
+ transfer_nft: {
102
+ recipient,
103
+ token_id: tokenId
104
+ }
105
+ })),
106
+ funds
107
+ })
108
+ };
109
+ };
110
+ sendNft = ({
111
+ contract,
112
+ msg,
113
+ tokenId
114
+ }: {
115
+ contract: string;
116
+ msg: Binary;
117
+ tokenId: string;
118
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
119
+ return {
120
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
121
+ value: MsgExecuteContract.fromPartial({
122
+ sender: this.sender,
123
+ contract: this.contractAddress,
124
+ msg: toUtf8(JSON.stringify({
125
+ send_nft: {
126
+ contract,
127
+ msg,
128
+ token_id: tokenId
129
+ }
130
+ })),
131
+ funds
132
+ })
133
+ };
134
+ };
135
+ approve = ({
136
+ expires,
137
+ spender,
138
+ tokenId
139
+ }: {
140
+ expires?: Expiration;
141
+ spender: string;
142
+ tokenId: string;
143
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
144
+ return {
145
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
146
+ value: MsgExecuteContract.fromPartial({
147
+ sender: this.sender,
148
+ contract: this.contractAddress,
149
+ msg: toUtf8(JSON.stringify({
150
+ approve: {
151
+ expires,
152
+ spender,
153
+ token_id: tokenId
154
+ }
155
+ })),
156
+ funds
157
+ })
158
+ };
159
+ };
160
+ revoke = ({
161
+ spender,
162
+ tokenId
163
+ }: {
164
+ spender: string;
165
+ tokenId: string;
166
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
167
+ return {
168
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
169
+ value: MsgExecuteContract.fromPartial({
170
+ sender: this.sender,
171
+ contract: this.contractAddress,
172
+ msg: toUtf8(JSON.stringify({
173
+ revoke: {
174
+ spender,
175
+ token_id: tokenId
176
+ }
177
+ })),
178
+ funds
179
+ })
180
+ };
181
+ };
182
+ approveAll = ({
183
+ expires,
184
+ operator
185
+ }: {
186
+ expires?: Expiration;
187
+ operator: string;
188
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
189
+ return {
190
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
191
+ value: MsgExecuteContract.fromPartial({
192
+ sender: this.sender,
193
+ contract: this.contractAddress,
194
+ msg: toUtf8(JSON.stringify({
195
+ approve_all: {
196
+ expires,
197
+ operator
198
+ }
199
+ })),
200
+ funds
201
+ })
202
+ };
203
+ };
204
+ revokeAll = ({
205
+ operator
206
+ }: {
207
+ operator: string;
208
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
209
+ return {
210
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
211
+ value: MsgExecuteContract.fromPartial({
212
+ sender: this.sender,
213
+ contract: this.contractAddress,
214
+ msg: toUtf8(JSON.stringify({
215
+ revoke_all: {
216
+ operator
217
+ }
218
+ })),
219
+ funds
220
+ })
221
+ };
222
+ };
223
+ mint = ({
224
+ extension,
225
+ owner,
226
+ tokenId,
227
+ tokenUri
228
+ }: {
229
+ extension: Empty;
230
+ owner: string;
231
+ tokenId: string;
232
+ tokenUri?: string;
233
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
234
+ return {
235
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
236
+ value: MsgExecuteContract.fromPartial({
237
+ sender: this.sender,
238
+ contract: this.contractAddress,
239
+ msg: toUtf8(JSON.stringify({
240
+ mint: {
241
+ extension,
242
+ owner,
243
+ token_id: tokenId,
244
+ token_uri: tokenUri
245
+ }
246
+ })),
247
+ funds
248
+ })
249
+ };
250
+ };
251
+ burn = ({
252
+ tokenId
253
+ }: {
254
+ tokenId: string;
255
+ }, funds?: Coin[]): MsgExecuteContractEncodeObject => {
256
+ return {
257
+ typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
258
+ value: MsgExecuteContract.fromPartial({
259
+ sender: this.sender,
260
+ contract: this.contractAddress,
261
+ msg: toUtf8(JSON.stringify({
262
+ burn: {
263
+ token_id: tokenId
264
+ }
265
+ })),
266
+ funds
267
+ })
268
+ };
269
+ };
270
+ }"
271
+ `;
@@ -0,0 +1 @@
1
+ export * from './message-composer';
@@ -0,0 +1,25 @@
1
+ import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
2
+ import {
3
+ createMessageComposerClass,
4
+ createMessageComposerInterface
5
+ } from './message-composer'
6
+ import { expectCode, makeContext } from '../../test-utils';
7
+
8
+ it('execute classes', () => {
9
+ const ctx = makeContext(execute_msg);
10
+ expectCode(createMessageComposerClass(
11
+ ctx,
12
+ 'SG721MessageComposer',
13
+ 'SG721Message',
14
+ execute_msg
15
+ ))
16
+ });
17
+
18
+ it('createMessageComposerInterface', () => {
19
+ const ctx = makeContext(execute_msg);
20
+ expectCode(createMessageComposerInterface(
21
+ ctx,
22
+ 'SG721Message',
23
+ execute_msg
24
+ ))
25
+ });
@@ -0,0 +1,305 @@
1
+ import * as t from '@babel/types';
2
+ import { camel } from 'case';
3
+ import {
4
+ bindMethod,
5
+ typedIdentifier,
6
+ classDeclaration,
7
+ classProperty,
8
+ arrowFunctionExpression,
9
+ getMessageProperties
10
+ } from '../utils'
11
+ import { ExecuteMsg } from '../types';
12
+ import { createTypedObjectParams } from '../utils/types';
13
+ import { JSONSchema } from '../types';
14
+ import { RenderContext } from '../context';
15
+ import { identifier } from '../utils/babel';
16
+ import { getWasmMethodArgs } from '../client/client';
17
+
18
+ const createWasmExecMethodMessageComposer = (
19
+ context: RenderContext,
20
+ jsonschema: any
21
+ ) => {
22
+
23
+ context.addUtil('Coin');
24
+ context.addUtil('MsgExecuteContractEncodeObject');
25
+ context.addUtil('MsgExecuteContract');
26
+ context.addUtil('toUtf8');
27
+
28
+ const underscoreName = Object.keys(jsonschema.properties)[0];
29
+ const methodName = camel(underscoreName);
30
+ const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
31
+ const args = getWasmMethodArgs(
32
+ context,
33
+ jsonschema.properties[underscoreName]
34
+ );
35
+
36
+ const constantParams = [
37
+ identifier('funds', t.tsTypeAnnotation(
38
+ t.tsArrayType(
39
+ t.tsTypeReference(
40
+ t.identifier('Coin')
41
+ )
42
+ )
43
+ ), true)
44
+ ];
45
+
46
+ return t.classProperty(
47
+ t.identifier(methodName),
48
+ arrowFunctionExpression(
49
+ obj ? [
50
+ // props
51
+ obj,
52
+ ...constantParams
53
+ ] : constantParams,
54
+ t.blockStatement(
55
+ [
56
+ t.returnStatement(
57
+
58
+ t.objectExpression([
59
+ t.objectProperty(
60
+ t.identifier('typeUrl'),
61
+ t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')
62
+ ),
63
+ t.objectProperty(
64
+ t.identifier('value'),
65
+ t.callExpression(
66
+ t.memberExpression(
67
+ t.identifier('MsgExecuteContract'),
68
+ t.identifier('fromPartial')
69
+ ),
70
+ [
71
+ t.objectExpression([
72
+ t.objectProperty(
73
+ t.identifier('sender'),
74
+ t.memberExpression(
75
+ t.thisExpression(),
76
+ t.identifier('sender')
77
+ )
78
+ ),
79
+ t.objectProperty(
80
+ t.identifier('contract'),
81
+ t.memberExpression(
82
+ t.thisExpression(),
83
+ t.identifier('contractAddress')
84
+ )
85
+ ),
86
+ t.objectProperty(
87
+ t.identifier('msg'),
88
+ t.callExpression(
89
+ t.identifier('toUtf8'),
90
+ [
91
+ t.callExpression(
92
+ t.memberExpression(
93
+ t.identifier('JSON'),
94
+ t.identifier('stringify')
95
+ ),
96
+ [
97
+ t.objectExpression(
98
+
99
+ [
100
+ t.objectProperty(
101
+ t.identifier(underscoreName), t.objectExpression(args)
102
+ )
103
+ ]
104
+
105
+ )
106
+ ]
107
+ )
108
+ ]
109
+ )
110
+ ),
111
+ t.objectProperty(
112
+ t.identifier('funds'),
113
+ t.identifier('funds'),
114
+ false,
115
+ true
116
+ ),
117
+ ])
118
+ ]
119
+ )
120
+ )
121
+ ])
122
+ )
123
+ ]
124
+ ),
125
+ // return type
126
+ t.tsTypeAnnotation(
127
+ t.tsTypeReference(
128
+ t.identifier('MsgExecuteContractEncodeObject')
129
+ )
130
+ ),
131
+ false
132
+ )
133
+ );
134
+
135
+ }
136
+
137
+ export const createMessageComposerClass = (
138
+ context: RenderContext,
139
+ className: string,
140
+ implementsClassName: string,
141
+ execMsg: ExecuteMsg
142
+ ) => {
143
+
144
+ const propertyNames = getMessageProperties(execMsg)
145
+ .map(method => Object.keys(method.properties)?.[0])
146
+ .filter(Boolean);
147
+
148
+ const bindings = propertyNames
149
+ .map(camel)
150
+ .map(bindMethod);
151
+
152
+ const methods = getMessageProperties(execMsg)
153
+ .map(schema => {
154
+ return createWasmExecMethodMessageComposer(context, schema)
155
+ });
156
+
157
+ const blockStmt = [];
158
+
159
+ [].push.apply(blockStmt, [
160
+ t.expressionStatement(
161
+ t.assignmentExpression(
162
+ '=',
163
+ t.memberExpression(
164
+ t.thisExpression(),
165
+ t.identifier('sender')
166
+ ),
167
+ t.identifier('sender')
168
+ )
169
+ ),
170
+ t.expressionStatement(
171
+ t.assignmentExpression(
172
+ '=',
173
+ t.memberExpression(
174
+ t.thisExpression(),
175
+ t.identifier('contractAddress')
176
+ ),
177
+ t.identifier('contractAddress')
178
+ )
179
+ ),
180
+ ...bindings
181
+ ]);
182
+
183
+ return t.exportNamedDeclaration(
184
+ classDeclaration(className,
185
+ [
186
+ // sender
187
+ classProperty('sender', t.tsTypeAnnotation(
188
+ t.tsStringKeyword()
189
+ )),
190
+
191
+ // contractAddress
192
+ classProperty('contractAddress', t.tsTypeAnnotation(
193
+ t.tsStringKeyword()
194
+ )),
195
+
196
+ // constructor
197
+ t.classMethod('constructor',
198
+ t.identifier('constructor'),
199
+ [
200
+ typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
201
+ typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))
202
+ ],
203
+ t.blockStatement(
204
+ blockStmt
205
+ )),
206
+ ...methods
207
+ ],
208
+ [
209
+ t.tSExpressionWithTypeArguments(
210
+ t.identifier(implementsClassName)
211
+ )
212
+ ],
213
+ null
214
+ )
215
+ );
216
+ }
217
+
218
+ export const createMessageComposerInterface = (
219
+ context: RenderContext,
220
+ className: string,
221
+ execMsg: ExecuteMsg
222
+ ) => {
223
+
224
+ const methods = getMessageProperties(execMsg)
225
+ .map(jsonschema => {
226
+ const underscoreName = Object.keys(jsonschema.properties)[0];
227
+ const methodName = camel(underscoreName);
228
+ return createPropertyFunctionWithObjectParamsForMessageComposer(
229
+ context,
230
+ methodName,
231
+ 'MsgExecuteContractEncodeObject',
232
+ jsonschema.properties[underscoreName]
233
+ );
234
+ });
235
+
236
+ const extendsAst = [];
237
+
238
+ return t.exportNamedDeclaration(
239
+ t.tsInterfaceDeclaration(
240
+ t.identifier(className),
241
+ null,
242
+ extendsAst,
243
+ t.tSInterfaceBody(
244
+ [
245
+
246
+ // contract address
247
+ t.tSPropertySignature(
248
+ t.identifier('contractAddress'),
249
+ t.tsTypeAnnotation(
250
+ t.tsStringKeyword()
251
+ )
252
+ ),
253
+
254
+ // contract address
255
+ t.tSPropertySignature(
256
+ t.identifier('sender'),
257
+ t.tsTypeAnnotation(
258
+ t.tsStringKeyword()
259
+ )
260
+ ),
261
+
262
+ ...methods,
263
+ ]
264
+ )
265
+ )
266
+ );
267
+ };
268
+
269
+ const createPropertyFunctionWithObjectParamsForMessageComposer = (
270
+ context: RenderContext,
271
+ methodName: string,
272
+ responseType: string,
273
+ jsonschema: JSONSchema
274
+ ) => {
275
+ const obj = createTypedObjectParams(context, jsonschema);
276
+ const fixedParams = [
277
+ identifier('funds', t.tsTypeAnnotation(
278
+ t.tsArrayType(
279
+ t.tsTypeReference(
280
+ t.identifier('Coin')
281
+ )
282
+ )
283
+ ), true)
284
+ ];
285
+ const func = {
286
+ type: 'TSFunctionType',
287
+ typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(
288
+ t.identifier(responseType)
289
+ )),
290
+ parameters: obj ? [
291
+ obj,
292
+ ...fixedParams
293
+
294
+ ] : fixedParams
295
+ }
296
+
297
+ return t.tSPropertySignature(
298
+ t.identifier(methodName),
299
+ t.tsTypeAnnotation(
300
+ // @ts-ignore:next-line
301
+ func
302
+ )
303
+ );
304
+ };
305
+