@taqueria/plugin-contract-types 0.0.0-pr-91-a8fa7172

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 (51) hide show
  1. package/Readme.md +212 -0
  2. package/example/contracts/example-contract-1.tz +1409 -0
  3. package/example/contracts/example-contract-2.tz +798 -0
  4. package/example/contracts/example-contract-3.json +7080 -0
  5. package/example/contracts/example-contract-4.tz +1374 -0
  6. package/example/contracts/example-contract-5.tz +1424 -0
  7. package/example/contracts/example-contract-6.tz +675 -0
  8. package/example/contracts/example-contract-7.tz +384 -0
  9. package/example/contracts/example-contract-8.tz +28 -0
  10. package/example/contracts/example-contract-9.tz +1010 -0
  11. package/example/example-usage-type-utilities.ts +35 -0
  12. package/example/example-usage.ts +410 -0
  13. package/example/types-file/example-contract-1.code.ts +6 -0
  14. package/example/types-file/example-contract-1.types.ts +87 -0
  15. package/example/types-file/example-contract-2.code.ts +6 -0
  16. package/example/types-file/example-contract-2.types.ts +122 -0
  17. package/example/types-file/example-contract-4.code.ts +6 -0
  18. package/example/types-file/example-contract-4.types.ts +97 -0
  19. package/example/types-file/example-contract-5.code.ts +6 -0
  20. package/example/types-file/example-contract-5.types.ts +173 -0
  21. package/example/types-file/example-contract-6.code.ts +6 -0
  22. package/example/types-file/example-contract-6.types.ts +122 -0
  23. package/example/types-file/example-contract-7.code.ts +6 -0
  24. package/example/types-file/example-contract-7.types.ts +78 -0
  25. package/example/types-file/example-contract-8.code.ts +6 -0
  26. package/example/types-file/example-contract-8.types.ts +86 -0
  27. package/example/types-file/example-contract-9.code.ts +6 -0
  28. package/example/types-file/example-contract-9.types.ts +29 -0
  29. package/example/types-file/type-aliases.ts +81 -0
  30. package/example/types-file/type-utils.ts +36 -0
  31. package/index.js +969 -0
  32. package/index.js.map +1 -0
  33. package/index.ts +32 -0
  34. package/package.json +55 -0
  35. package/run.ts +3 -0
  36. package/src/cli-process.ts +111 -0
  37. package/src/cli.ts +34 -0
  38. package/src/generator/common.ts +21 -0
  39. package/src/generator/contract-name.ts +6 -0
  40. package/src/generator/contract-parser.ts +358 -0
  41. package/src/generator/process.ts +66 -0
  42. package/src/generator/schema-output.ts +54 -0
  43. package/src/generator/typescript-output.ts +239 -0
  44. package/src/taquito-contract-type-generator.ts +4 -0
  45. package/src/type-aliases-file-content.ts +83 -0
  46. package/src/type-aliases.ts +80 -0
  47. package/src/type-utils-file-content.ts +38 -0
  48. package/src/type-utils.ts +35 -0
  49. package/tasks.ts +127 -0
  50. package/test/generator.spec.ts +69 -0
  51. package/tsconfig.json +13 -0
package/Readme.md ADDED
@@ -0,0 +1,212 @@
1
+ This plugin will generate typescript types for a contract.
2
+
3
+ In the simple case, it reads the `artifacts` folder for any `*.tz` Michelson files and will generate a types file with the matching filename for each.
4
+
5
+ ## Requirements
6
+
7
+ This is using a unmerged taquito PR:
8
+
9
+ - https://github.com/ecadlabs/taquito/pull/1343
10
+ - `npm i @taquito/taquito@11.1.0-6bfb6c08--1343 --registry https://npm.preview.tezostaquito.io/`
11
+
12
+ ## Examples
13
+
14
+ ### Example Usage (based on an nft auction contract from open minter sdk)
15
+
16
+ ```ts
17
+ export const exampleContractMethods1 = async () => {
18
+
19
+ const Tezos = new TezosToolkit(`https://YOUR_PREFERRED_RPC_URL`)
20
+
21
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
22
+
23
+ contract.methods.bid(tas.nat(0));
24
+ contract.methods.configure(
25
+ /*opening_price:*/ tas.mutez(10),
26
+ /*min_raise_percent:*/ tas.nat(10),
27
+ /*min_raise:*/ tas.mutez(10),
28
+ /*round_time:*/ tas.nat(10),
29
+ /*extend_time:*/ tas.nat(10),
30
+ /*asset:*/ [{
31
+ fa2_address: tas.address(`tz123`),
32
+ fa2_batch: [{
33
+ amount: tas.nat(100),
34
+ token_id: tas.nat(`100000000000000`),
35
+ }],
36
+ }],
37
+ /*start_time:*/ tas.timestamp(new Date()),
38
+ /*end_time:*/ tas.timestamp(`2020-01-01`),
39
+ );
40
+
41
+ // methodsObject
42
+ contract.methodsObject.bid(tas.nat(0));
43
+ contract.methodsObject.configure({
44
+ asset: [{
45
+ fa2_address: tas.address(`tz123`),
46
+ fa2_batch: [{
47
+ amount: tas.nat(100),
48
+ token_id: tas.nat(`100000000000000`),
49
+ }],
50
+ }],
51
+ start_time: tas.timestamp(new Date()),
52
+ end_time: tas.timestamp(`2020-01-01`),
53
+ extend_time: tas.nat(10),
54
+ min_raise: tas.mutez(10),
55
+ min_raise_percent: tas.nat(10),
56
+ opening_price: tas.mutez(10),
57
+ round_time: tas.nat(10),
58
+ });
59
+
60
+ };
61
+ ```
62
+
63
+
64
+ ### Example typegen task
65
+
66
+ ```console
67
+ $ taqueria typegen --typescriptDir ./types
68
+ generateTypes
69
+ {
70
+ "typescriptDir": "./types"
71
+ }
72
+ Generating Types: /home/rick/projects/crypto/taqueria/example/artifacts => /home/rick/projects/crypto/taqueria/example/types
73
+ Contracts Found:
74
+ - /home/rick/projects/crypto/taqueria/example/artifacts/example-contract-1.tz
75
+ Processing /example-contract-1.tz...example-contract-1.tz: Types generated
76
+ ```
77
+
78
+
79
+ ### Example type output
80
+
81
+ ```ts
82
+ type Storage = {
83
+ pauseable_admin?: {
84
+ admin: address;
85
+ paused: boolean;
86
+ pending_admin?: address;
87
+ };
88
+ current_id: nat;
89
+ max_auction_time: nat;
90
+ max_config_to_start_time: nat;
91
+ auctions: BigMap<nat, {
92
+ seller: address;
93
+ current_bid: mutez;
94
+ start_time: timestamp;
95
+ last_bid_time: timestamp;
96
+ round_time: int;
97
+ extend_time: int;
98
+ asset: Array<{
99
+ fa2_address: address;
100
+ fa2_batch: Array<{
101
+ token_id: nat;
102
+ amount: nat;
103
+ }>;
104
+ }>;
105
+ min_raise_percent: nat;
106
+ min_raise: mutez;
107
+ end_time: timestamp;
108
+ highest_bidder: address;
109
+ }>;
110
+ };
111
+
112
+ type Methods = {
113
+ confirm_admin: () => Promise<void>;
114
+ pause: (param: boolean) => Promise<void>;
115
+ set_admin: (param: address) => Promise<void>;
116
+ bid: (param: nat) => Promise<void>;
117
+ cancel: (param: nat) => Promise<void>;
118
+ configure: (
119
+ opening_price: mutez,
120
+ min_raise_percent: nat,
121
+ min_raise: mutez,
122
+ round_time: nat,
123
+ extend_time: nat,
124
+ asset: Array<{
125
+ fa2_address: address;
126
+ fa2_batch: Array<{
127
+ token_id: nat;
128
+ amount: nat;
129
+ }>;
130
+ }>,
131
+ start_time: timestamp,
132
+ end_time: timestamp,
133
+ ) => Promise<void>;
134
+ resolve: (param: nat) => Promise<void>;
135
+ };
136
+ ```
137
+
138
+
139
+ ## Taquito library changes
140
+
141
+ See [example-usage.ts](example/example-usage.ts) for full example
142
+
143
+ ### Before
144
+
145
+ Using taquito with the generated contract types:
146
+
147
+ The at method can be called providing a type with a utility method that can be provided:
148
+ ```ts
149
+ const contract = await Tezos.contract.at(`tz123`, contractAbstractionComposer<TestContractType>());
150
+
151
+ // methods can now use typed parameters
152
+ // methodsObject will be able to use type parameters
153
+ ```
154
+
155
+ This can work the same with a wallet
156
+ ```ts
157
+ const contract = await Tezos.wallet.at(`tz123`, walletAbstractionComposer<TestContractType>());
158
+ ```
159
+
160
+ Alternatively, this can be done with a cast:
161
+ ```ts
162
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
163
+ ```
164
+
165
+
166
+ The originate contract does not have any way to provide a type, so this requires a cast:
167
+ ```ts
168
+ const originationResult = await Tezos.contract.originate({...});
169
+ const contract = await originationResult.contract(5) as ContractProviderFromContractType<TestContractType2>;
170
+ ```
171
+
172
+
173
+ For accessing storage, there is no way to pass the type through the contract class,
174
+ so it requires providing the type again:
175
+ ```ts
176
+ const contract = await Tezos.contract.at(`tz123`) as ContractProviderFromContractType<TestContractType>;
177
+ const storage = await contract.storage<StorageFromContractType<TestContractType>>();
178
+ ```
179
+
180
+
181
+ ### After
182
+
183
+ Extending ContractAbstraction with additional generic types:
184
+
185
+ The at method can be called with the contract type provided:
186
+ ```ts
187
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
188
+
189
+ // methods can now use typed parameters
190
+ // methodsObject will be able to use type parameters
191
+ // storage will be able to use type parameters
192
+
193
+ ```
194
+
195
+ This can work the same with a wallet
196
+ ```ts
197
+ const contract = await Tezos.wallet.at<TestWalletContract>(`tz123`);
198
+ ```
199
+
200
+ The originate contract now accepts a type:
201
+ ```ts
202
+ const originationResult = await Tezos.contract.originate({...});
203
+ const contract = await originationResult.contract<TestContract2>(5);
204
+ ```
205
+
206
+
207
+ The contract type now also provides the default storage type:
208
+ ```ts
209
+ const contract = await Tezos.contract.at<TestContract>(`tz123`);
210
+ const storage = await contract.storage();
211
+ ```
212
+