@taqueria/plugin-contract-types 0.0.0-pr-726-e060c010 → 0.0.0-pr-575-850bfda7

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.
@@ -1,3 +1,5 @@
1
+ # Taqueria Contract Types Plugin
2
+
1
3
  This plugin provides a `taq generate types` command which will generate and export TypeScript types from compiled Michelson smart contracts. These generated types then work with your IDE and Taquito, providing type safety and an improved code authoring experience
2
4
 
3
5
  Benefits of using generated types:
@@ -57,6 +59,71 @@ The `generate types` command will accept the following optional parameters:
57
59
  |:-----:|:--------------|----------------------------------------------|
58
60
  | -t | typeAliasMode | Use type aliases in the generated types |
59
61
 
62
+
63
+ ## Taquito Workflow Improvements
64
+
65
+ The generated TS types can be used in a Taquito project which provides an improved developing experience, and simplifies the way types are provided to Taquito method calls. Some examples of how these changes are put into use are detailed below
66
+
67
+ > ### :page_with_curl: Note
68
+ > You can view the full example in the `example-usage.ts` file on Github: [taqueria/taqueria-plugin-contract-types/example](https://github.com/ecadlabs/taqueria/blob/main/taqueria-plugin-contract-types/example/example-usage.ts)
69
+
70
+ ### Calling the `.at` Method of a Contract
71
+
72
+ Traditionally, calling the `.at` method of a contract with Taquito required the developer to pass the parameter's type via a utility method:
73
+ ```ts Utility Method
74
+ const contract = await Tezos.contract.at(`tz123`, contractAbstractionComposer<TestContractType>());
75
+ ```
76
+ or a cast:
77
+ ```ts Cast
78
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
79
+ ```
80
+
81
+ When using generated types, the developer can now directly use the type in the call to `.at`:
82
+ ```ts
83
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
84
+ ```
85
+
86
+ ### Using a Wallet
87
+
88
+ Using a wallet is simplified in a similar way:
89
+ ```ts
90
+ const contract = await Tezos.wallet.at(`tz123`, walletAbstractionComposer<TestContractType>());
91
+ ```
92
+
93
+ Becomes:
94
+ ```ts
95
+ const contract = await Tezos.wallet.at<TestWalletContract>(`tz123`);
96
+ ```
97
+
98
+ ### Contract Origination
99
+
100
+ The Taquito contract origination did not have any way to provide a type, so this used to require a cast:
101
+ ```ts
102
+ const originationResult = await Tezos.contract.originate({...});
103
+ const contract = await originationResult.contract(5) as ContractProviderFromContractType<TestContractType2>;
104
+ ```
105
+
106
+ Now, it can directly accept a type:
107
+ ```ts
108
+ const originationResult = await Tezos.contract.originate({...});
109
+ const contract = await originationResult.contract<TestContract2>(5);
110
+ ```
111
+
112
+
113
+ ### Storage
114
+
115
+ When accessing storage, there was no way to pass the type through the contract class. This required providing the type a second time:
116
+ ```ts
117
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
118
+ const storage = await contract.storage<StorageFromContractType<TestContractType>>();
119
+ ```
120
+
121
+ Now, the contract type provides the default storage type:
122
+ ```ts
123
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
124
+ const storage = await contract.storage();
125
+ ```
126
+
60
127
  ## Examples
61
128
 
62
129
  ### Example Usage (based on an nft auction contract from open minter sdk)
@@ -187,6 +254,7 @@ type Methods = {
187
254
 
188
255
  See [example-usage.ts](example/example-usage.ts) for full example
189
256
 
257
+
190
258
  ### Before
191
259
 
192
260
  Using taquito with the generated contract types:
@@ -256,4 +324,3 @@ The contract type now also provides the default storage type:
256
324
  const contract = await Tezos.contract.at<TestContract>(`tz123`);
257
325
  const storage = await contract.storage();
258
326
  ```
259
-
package/_readme.eta ADDED
@@ -0,0 +1,333 @@
1
+ <% if (it.output == "github") { %>
2
+ # Taqueria Contract Types Plugin
3
+ <% } %>
4
+
5
+ This plugin provides a `taq generate types` command which will generate and export TypeScript types from compiled Michelson smart contracts. These generated types then work with your IDE and Taquito, providing type safety and an improved code authoring experience
6
+
7
+ Benefits of using generated types:
8
+ - Static types used to call smart contract methods are checked at compile time, improving code reliability
9
+ - Generated types enable auto-completion and syntax highlighting in your IDE
10
+ - Developing apps with Taquito is faster and more reliable
11
+ - The VS Code Extension provides tooltip hints for parameter types used to call a smart contract method
12
+ - Calling smart contract methods with types is done directly, removing the need for utility methods
13
+ - Simplifies your code and improves readability
14
+
15
+ ## Requirements
16
+
17
+ - Node JS v16 or later
18
+ - Taquito v11.2 or later (optional)
19
+
20
+ ## Installation
21
+
22
+ To install the Contract Types plugin on a Taqueria project, navigate to the project folder and run:
23
+ ```shell
24
+ taq install @taqueria/plugin-contract-types
25
+ ```
26
+
27
+ ## Configuration
28
+
29
+ This plugin will look for Michelson files according to the `artifactsDir` configured in `./.taq/config.json`. By default, this value is `/artifacts` but can be changed as needed
30
+
31
+ ## Usage
32
+
33
+ The plugin provides a single command to Taqueria: `taq generate types`
34
+
35
+ This will look for `.tz` files in the `/artifacts` directory and will generate a series of related `.ts` files in the `/types` directory. These files export type definitions for each method which can then be used by Taquito and your IDE
36
+
37
+ ### The `generate types` Command
38
+
39
+ #### Syntax
40
+ ```shell
41
+ taq generate types [typeOutputDir]
42
+ ```
43
+
44
+ #### Parameters
45
+
46
+ | parameter | required | description |
47
+ |:-------------:|:-----------|----------------------------------------------------|
48
+ | typeOutputDir | no | The output directory for the `.ts` files generated |
49
+
50
+ #### CLI Aliases
51
+
52
+ The following aliases are interchangable with `generate types`
53
+ - `gen`
54
+ - `gentypes`
55
+
56
+ #### Options
57
+
58
+ The `generate types` command will accept the following optional parameters:
59
+
60
+ | flag | name | description |
61
+ |:-----:|:--------------|----------------------------------------------|
62
+ | -t | typeAliasMode | Use type aliases in the generated types |
63
+
64
+
65
+ ## Taquito Workflow Improvements
66
+
67
+ The generated TS types can be used in a Taquito project which provides an improved developing experience, and simplifies the way types are provided to Taquito method calls. Some examples of how these changes are put into use are detailed below
68
+
69
+ <%~ it.noteOpenAdmonition %>
70
+ You can view the full example in the `example-usage.ts` file on Github: [taqueria/taqueria-plugin-contract-types/example](https://github.com/ecadlabs/taqueria/blob/main/taqueria-plugin-contract-types/example/example-usage.ts)
71
+ <%= it.closeAdmonition %>
72
+
73
+ ### Calling the `.at` Method of a Contract
74
+
75
+ Traditionally, calling the `.at` method of a contract with Taquito required the developer to pass the parameter's type via a utility method:
76
+ ```ts Utility Method
77
+ const contract = await Tezos.contract.at(`tz123`, contractAbstractionComposer<TestContractType>());
78
+ ```
79
+ or a cast:
80
+ ```ts Cast
81
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
82
+ ```
83
+
84
+ When using generated types, the developer can now directly use the type in the call to `.at`:
85
+ ```ts
86
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
87
+ ```
88
+
89
+ ### Using a Wallet
90
+
91
+ Using a wallet is simplified in a similar way:
92
+ ```ts
93
+ const contract = await Tezos.wallet.at(`tz123`, walletAbstractionComposer<TestContractType>());
94
+ ```
95
+
96
+ Becomes:
97
+ ```ts
98
+ const contract = await Tezos.wallet.at<TestWalletContract>(`tz123`);
99
+ ```
100
+
101
+ ### Contract Origination
102
+
103
+ The Taquito contract origination did not have any way to provide a type, so this used to require a cast:
104
+ ```ts
105
+ const originationResult = await Tezos.contract.originate({...});
106
+ const contract = await originationResult.contract(5) as ContractProviderFromContractType<TestContractType2>;
107
+ ```
108
+
109
+ Now, it can directly accept a type:
110
+ ```ts
111
+ const originationResult = await Tezos.contract.originate({...});
112
+ const contract = await originationResult.contract<TestContract2>(5);
113
+ ```
114
+
115
+
116
+ ### Storage
117
+
118
+ When accessing storage, there was no way to pass the type through the contract class. This required providing the type a second time:
119
+ ```ts
120
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
121
+ const storage = await contract.storage<StorageFromContractType<TestContractType>>();
122
+ ```
123
+
124
+ Now, the contract type provides the default storage type:
125
+ ```ts
126
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
127
+ const storage = await contract.storage();
128
+ ```
129
+
130
+ ## Examples
131
+
132
+ ### Example Usage (based on an nft auction contract from open minter sdk)
133
+
134
+ ```ts
135
+ export const exampleContractMethods1 = async () => {
136
+
137
+ const Tezos = new TezosToolkit(`https://YOUR_PREFERRED_RPC_URL`)
138
+
139
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
140
+
141
+ contract.methods.bid(tas.nat(0));
142
+ contract.methods.configure(
143
+ /*opening_price:*/ tas.mutez(10),
144
+ /*min_raise_percent:*/ tas.nat(10),
145
+ /*min_raise:*/ tas.mutez(10),
146
+ /*round_time:*/ tas.nat(10),
147
+ /*extend_time:*/ tas.nat(10),
148
+ /*asset:*/ [{
149
+ fa2_address: tas.address(`tz123`),
150
+ fa2_batch: [{
151
+ amount: tas.nat(100),
152
+ token_id: tas.nat(`100000000000000`),
153
+ }],
154
+ }],
155
+ /*start_time:*/ tas.timestamp(new Date()),
156
+ /*end_time:*/ tas.timestamp(`2020-01-01`),
157
+ );
158
+
159
+ // methodsObject
160
+ contract.methodsObject.bid(tas.nat(0));
161
+ contract.methodsObject.configure({
162
+ asset: [{
163
+ fa2_address: tas.address(`tz123`),
164
+ fa2_batch: [{
165
+ amount: tas.nat(100),
166
+ token_id: tas.nat(`100000000000000`),
167
+ }],
168
+ }],
169
+ start_time: tas.timestamp(new Date()),
170
+ end_time: tas.timestamp(`2020-01-01`),
171
+ extend_time: tas.nat(10),
172
+ min_raise: tas.mutez(10),
173
+ min_raise_percent: tas.nat(10),
174
+ opening_price: tas.mutez(10),
175
+ round_time: tas.nat(10),
176
+ });
177
+
178
+ };
179
+ ```
180
+
181
+ ### Example typegen task
182
+
183
+ ```console
184
+ $ taqueria typegen --typescriptDir ./types
185
+ generateTypes
186
+ {
187
+ "typescriptDir": "./types"
188
+ }
189
+ Generating Types: /home/rick/projects/crypto/taqueria/example/artifacts => /home/rick/projects/crypto/taqueria/example/types
190
+ Contracts Found:
191
+ - /home/rick/projects/crypto/taqueria/example/artifacts/example-contract-1.tz
192
+ Processing /example-contract-1.tz...example-contract-1.tz: Types generated
193
+ ```
194
+
195
+
196
+ ### Example type output
197
+
198
+ ```ts
199
+ type Storage = {
200
+ pauseable_admin?: {
201
+ admin: address;
202
+ paused: boolean;
203
+ pending_admin?: address;
204
+ };
205
+ current_id: nat;
206
+ max_auction_time: nat;
207
+ max_config_to_start_time: nat;
208
+ auctions: BigMap<nat, {
209
+ seller: address;
210
+ current_bid: mutez;
211
+ start_time: timestamp;
212
+ last_bid_time: timestamp;
213
+ round_time: int;
214
+ extend_time: int;
215
+ asset: Array<{
216
+ fa2_address: address;
217
+ fa2_batch: Array<{
218
+ token_id: nat;
219
+ amount: nat;
220
+ }>;
221
+ }>;
222
+ min_raise_percent: nat;
223
+ min_raise: mutez;
224
+ end_time: timestamp;
225
+ highest_bidder: address;
226
+ }>;
227
+ };
228
+
229
+ type Methods = {
230
+ confirm_admin: () => Promise<void>;
231
+ pause: (param: boolean) => Promise<void>;
232
+ set_admin: (param: address) => Promise<void>;
233
+ bid: (param: nat) => Promise<void>;
234
+ cancel: (param: nat) => Promise<void>;
235
+ configure: (
236
+ opening_price: mutez,
237
+ min_raise_percent: nat,
238
+ min_raise: mutez,
239
+ round_time: nat,
240
+ extend_time: nat,
241
+ asset: Array<{
242
+ fa2_address: address;
243
+ fa2_batch: Array<{
244
+ token_id: nat;
245
+ amount: nat;
246
+ }>;
247
+ }>,
248
+ start_time: timestamp,
249
+ end_time: timestamp,
250
+ ) => Promise<void>;
251
+ resolve: (param: nat) => Promise<void>;
252
+ };
253
+ ```
254
+
255
+
256
+ ## Taquito library changes
257
+
258
+ <% if (it.output == "github") { %>
259
+ See [example-usage.ts](example/example-usage.ts) for full example
260
+ <% } else { %>
261
+ See [example-usage.ts](https://github.com/ecadlabs/taqueria/blob/main/taqueria-plugin-contract-types/example/example-usage.ts) for full example
262
+ <% } %>
263
+
264
+
265
+ ### Before
266
+
267
+ Using taquito with the generated contract types:
268
+
269
+ The at method can be called providing a type with a utility method that can be provided:
270
+ ```ts
271
+ const contract = await Tezos.contract.at(`tz123`, contractAbstractionComposer<TestContractType>());
272
+
273
+ // methods can now use typed parameters
274
+ // methodsObject will be able to use type parameters
275
+ ```
276
+
277
+ This can work the same with a wallet
278
+ ```ts
279
+ const contract = await Tezos.wallet.at(`tz123`, walletAbstractionComposer<TestContractType>());
280
+ ```
281
+
282
+ Alternatively, this can be done with a cast:
283
+ ```ts
284
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
285
+ ```
286
+
287
+
288
+ The originate contract does not have any way to provide a type, so this requires a cast:
289
+ ```ts
290
+ const originationResult = await Tezos.contract.originate({...});
291
+ const contract = await originationResult.contract(5) as ContractProviderFromContractType<TestContractType2>;
292
+ ```
293
+
294
+
295
+ For accessing storage, there is no way to pass the type through the contract class,
296
+ so it requires providing the type again:
297
+ ```ts
298
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
299
+ const storage = await contract.storage<StorageFromContractType<TestContractType>>();
300
+ ```
301
+
302
+
303
+ ### After
304
+
305
+ Extending ContractAbstraction with additional generic types:
306
+
307
+ The at method can be called with the contract type provided:
308
+ ```ts
309
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
310
+
311
+ // methods can now use typed parameters
312
+ // methodsObject will be able to use type parameters
313
+ // storage will be able to use type parameters
314
+
315
+ ```
316
+
317
+ This can work the same with a wallet
318
+ ```ts
319
+ const contract = await Tezos.wallet.at<TestWalletContract>(`tz123`);
320
+ ```
321
+
322
+ The originate contract now accepts a type:
323
+ ```ts
324
+ const originationResult = await Tezos.contract.originate({...});
325
+ const contract = await originationResult.contract<TestContract2>(5);
326
+ ```
327
+
328
+
329
+ The contract type now also provides the default storage type:
330
+ ```ts
331
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
332
+ const storage = await contract.storage();
333
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taqueria/plugin-contract-types",
3
- "version": "0.0.0-pr-726-e060c010",
3
+ "version": "0.0.0-pr-575-850bfda7",
4
4
  "description": "",
5
5
  "targets": {
6
6
  "default": {