Skip to content

Commit f98a8bd

Browse files
committed
Generate parameters for tuple types
1 parent dccd0ed commit f98a8bd

File tree

14 files changed

+528
-87
lines changed

14 files changed

+528
-87
lines changed

__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/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
]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`execute interfaces no extends 1`] = `
4+
"export interface OwnershipInstance {
5+
contractAddress: string;
6+
sender: string;
7+
setFactory: ({
8+
newFactory
9+
}: {
10+
newFactory: string;
11+
}, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
12+
updateOwnership: (action: Action, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
13+
}"
14+
`;
15+
16+
exports[`ownership client with tuple 1`] = `
17+
"export class OwnershipClient implements OwnershipInstance {
18+
client: SigningCosmWasmClient;
19+
sender: string;
20+
contractAddress: string;
21+
22+
constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) {
23+
this.client = client;
24+
this.sender = sender;
25+
this.contractAddress = contractAddress;
26+
this.setFactory = this.setFactory.bind(this);
27+
this.updateOwnership = this.updateOwnership.bind(this);
28+
}
29+
30+
setFactory = async ({
31+
newFactory
32+
}: {
33+
newFactory: string;
34+
}, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
35+
return await this.client.execute(this.sender, this.contractAddress, {
36+
set_factory: {
37+
new_factory: newFactory
38+
}
39+
}, fee, memo, funds);
40+
};
41+
updateOwnership = async (action: Action, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
42+
return await this.client.execute(this.sender, this.contractAddress, {
43+
update_ownership: action
44+
}, fee, memo, funds);
45+
};
46+
}"
47+
`;

packages/wasm-ast-types/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,29 @@ exports[`query classes 1`] = `
193193
this.wasm = this.wasm.bind(this);
194194
}
195195
196-
bank = async (): Promise<BankResponse> => {
196+
bank = async (bankMsg: BankMsg): Promise<BankResponse> => {
197197
return this.client.queryContractSmart(this.contractAddress, {
198-
bank: {}
198+
bank: bankMsg
199199
});
200200
};
201201
custom = async (): Promise<CustomResponse> => {
202202
return this.client.queryContractSmart(this.contractAddress, {
203203
custom: {}
204204
});
205205
};
206-
staking = async (): Promise<StakingResponse> => {
206+
staking = async (stakingMsg: StakingMsg): Promise<StakingResponse> => {
207207
return this.client.queryContractSmart(this.contractAddress, {
208-
staking: {}
208+
staking: stakingMsg
209209
});
210210
};
211-
distribution = async (): Promise<DistributionResponse> => {
211+
distribution = async (distributionMsg: DistributionMsg): Promise<DistributionResponse> => {
212212
return this.client.queryContractSmart(this.contractAddress, {
213-
distribution: {}
213+
distribution: distributionMsg
214214
});
215215
};
216-
wasm = async (): Promise<WasmResponse> => {
216+
wasm = async (wasmMsg: WasmMsg): Promise<WasmResponse> => {
217217
return this.client.queryContractSmart(this.contractAddress, {
218-
wasm: {}
218+
wasm: wasmMsg
219219
});
220220
};
221221
}"

0 commit comments

Comments
 (0)