@openzeppelin/wizard-stellar 0.1.1 → 0.2.1

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 (71) hide show
  1. package/README.md +1 -0
  2. package/dist/add-pausable.js +7 -7
  3. package/dist/add-pausable.js.map +1 -1
  4. package/dist/add-upgradeable.d.ts +4 -0
  5. package/dist/add-upgradeable.d.ts.map +1 -0
  6. package/dist/add-upgradeable.js +26 -0
  7. package/dist/add-upgradeable.js.map +1 -0
  8. package/dist/api.d.ts +3 -0
  9. package/dist/api.d.ts.map +1 -1
  10. package/dist/api.js +6 -1
  11. package/dist/api.js.map +1 -1
  12. package/dist/build-generic.d.ts +4 -0
  13. package/dist/build-generic.d.ts.map +1 -1
  14. package/dist/build-generic.js +5 -2
  15. package/dist/build-generic.js.map +1 -1
  16. package/dist/contract.d.ts +22 -11
  17. package/dist/contract.d.ts.map +1 -1
  18. package/dist/contract.js +58 -12
  19. package/dist/contract.js.map +1 -1
  20. package/dist/fungible.d.ts +1 -0
  21. package/dist/fungible.d.ts.map +1 -1
  22. package/dist/fungible.js +38 -32
  23. package/dist/fungible.js.map +1 -1
  24. package/dist/generate/fungible.d.ts.map +1 -1
  25. package/dist/generate/fungible.js +1 -0
  26. package/dist/generate/fungible.js.map +1 -1
  27. package/dist/generate/non-fungible.d.ts +3 -0
  28. package/dist/generate/non-fungible.d.ts.map +1 -0
  29. package/dist/generate/non-fungible.js +24 -0
  30. package/dist/generate/non-fungible.js.map +1 -0
  31. package/dist/generate/sources.d.ts.map +1 -1
  32. package/dist/generate/sources.js +6 -0
  33. package/dist/generate/sources.js.map +1 -1
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +3 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/kind.js +2 -1
  39. package/dist/kind.js.map +1 -1
  40. package/dist/non-fungible.d.ts +17 -0
  41. package/dist/non-fungible.d.ts.map +1 -0
  42. package/dist/non-fungible.js +354 -0
  43. package/dist/non-fungible.js.map +1 -0
  44. package/dist/print.d.ts.map +1 -1
  45. package/dist/print.js +53 -17
  46. package/dist/print.js.map +1 -1
  47. package/dist/set-access-control.d.ts +2 -2
  48. package/dist/set-access-control.d.ts.map +1 -1
  49. package/dist/set-access-control.js +2 -7
  50. package/dist/set-access-control.js.map +1 -1
  51. package/dist/test.js +3 -0
  52. package/dist/test.js.map +1 -1
  53. package/dist/utils/version.d.ts +3 -3
  54. package/dist/utils/version.js +2 -2
  55. package/package.json +3 -4
  56. package/src/add-pausable.ts +7 -7
  57. package/src/add-upgradeable.ts +30 -0
  58. package/src/api.ts +8 -0
  59. package/src/build-generic.ts +9 -2
  60. package/src/contract.ts +73 -21
  61. package/src/fungible.ts +38 -30
  62. package/src/generate/fungible.ts +1 -0
  63. package/src/generate/non-fungible.ts +24 -0
  64. package/src/generate/sources.ts +7 -0
  65. package/src/index.ts +1 -0
  66. package/src/kind.ts +2 -1
  67. package/src/non-fungible.ts +407 -0
  68. package/src/print.ts +77 -28
  69. package/src/set-access-control.ts +8 -9
  70. package/src/test.ts +4 -0
  71. package/src/utils/version.ts +2 -2
package/src/print.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Contract, Argument, ContractFunction, ImplementedTrait, UseClause } from './contract';
1
+ import type { Contract, Argument, ContractFunction, TraitImplBlock, UseClause } from './contract';
2
2
 
3
3
  import type { Lines } from './utils/format-lines';
4
4
  import { formatLines, spaceBetween } from './utils/format-lines';
@@ -31,7 +31,13 @@ export function printContract(contract: Contract): string {
31
31
  }
32
32
 
33
33
  function printContractStruct(contract: Contract): Lines[] {
34
- return ['#[contract]', `pub struct ${contract.name};`];
34
+ const lines = [];
35
+ if (contract.derives.length > 0) {
36
+ lines.push(`#[derive(${contract.derives.sort().join(', ')})]`);
37
+ }
38
+ lines.push('#[contract]');
39
+ lines.push(`pub struct ${contract.name};`);
40
+ return lines;
35
41
  }
36
42
 
37
43
  function printContractErrors(contract: Contract): Lines[] {
@@ -151,28 +157,25 @@ function printImplementedTraits(contract: Contract): Lines[] {
151
157
  if (a.tags.length !== b.tags.length) {
152
158
  return a.tags.length - b.tags.length;
153
159
  }
154
- return a.name.localeCompare(b.name);
160
+ return a.traitName.localeCompare(b.traitName);
155
161
  });
156
162
 
157
163
  // group by section
158
- const grouped = sortedTraits.reduce(
159
- (result: { [section: string]: ImplementedTrait[] }, current: ImplementedTrait) => {
160
- // default to no section
161
- const section = current.section ?? DEFAULT_SECTION;
162
- (result[section] = result[section] || []).push(current);
163
- return result;
164
- },
165
- {},
166
- );
164
+ const grouped = sortedTraits.reduce((result: { [section: string]: TraitImplBlock[] }, current: TraitImplBlock) => {
165
+ // default to no section
166
+ const section = current.section ?? DEFAULT_SECTION;
167
+ (result[section] = result[section] || []).push(current);
168
+ return result;
169
+ }, {});
167
170
 
168
171
  const sections = Object.entries(grouped)
169
172
  .sort((a, b) => a[0].localeCompare(b[0]))
170
- .map(([section, impls]) => printImplementedTraitsSection(section, impls as ImplementedTrait[]));
173
+ .map(([section, impls]) => printImplementedTraitsSection(section, impls as TraitImplBlock[]));
171
174
 
172
175
  return spaceBetween(...sections);
173
176
  }
174
177
 
175
- function printImplementedTraitsSection(section: string, impls: ImplementedTrait[]): Lines[] {
178
+ function printImplementedTraitsSection(section: string, impls: TraitImplBlock[]): Lines[] {
176
179
  const lines = [];
177
180
  const isDefaultSection = section === DEFAULT_SECTION;
178
181
  if (!isDefaultSection) {
@@ -189,13 +192,33 @@ function printImplementedTraitsSection(section: string, impls: ImplementedTrait[
189
192
  return lines;
190
193
  }
191
194
 
192
- function printImplementedTrait(trait: ImplementedTrait): Lines[] {
195
+ function printImplementedTrait(trait: TraitImplBlock): Lines[] {
193
196
  const implLines = [];
194
197
  implLines.push(...trait.tags.map(t => `#[${t}]`));
195
- implLines.push(`impl ${trait.name} for ${trait.for} {`);
196
- const fns = trait.functions.map(fn => printFunction(fn));
197
- implLines.push(spaceBetween(...fns));
198
- implLines.push('}');
198
+
199
+ const head = `impl ${trait.traitName} for ${trait.structName}`;
200
+
201
+ const assocTypeLines =
202
+ trait.assocType !== undefined && trait.assocType.trim().length > 0 ? [TAB + trait.assocType.trim(), ''] : [];
203
+
204
+ const functionLines = trait.functions.map(fn => printFunction(fn));
205
+
206
+ const hasBodyContent = assocTypeLines.length > 0 || functionLines.length > 0;
207
+
208
+ if (!hasBodyContent) {
209
+ // Empty impl
210
+ implLines.push(`${head} {}`);
211
+ } else {
212
+ implLines.push(`${head} {`);
213
+ if (assocTypeLines.length > 0) {
214
+ implLines.push(...assocTypeLines);
215
+ }
216
+ if (functionLines.length > 0) {
217
+ implLines.push(spaceBetween(...functionLines));
218
+ }
219
+ implLines.push('}');
220
+ }
221
+
199
222
  return implLines;
200
223
  }
201
224
 
@@ -216,26 +239,45 @@ function printFunction(fn: ContractFunction): Lines[] {
216
239
  }
217
240
  }
218
241
 
219
- return printFunction2(head, args, fn.tag, fn.returns, undefined, codeLines);
242
+ return printFunction2(fn.pub, head, args, fn.tag, fn.returns, undefined, codeLines);
220
243
  }
221
244
 
222
245
  function printContractFunctions(contract: Contract): Lines[] {
223
- const implLines = [];
224
- implLines.push('#[contractimpl]');
225
- implLines.push(`impl ${contract.name} {`);
226
- implLines.push(printConstructor(contract));
227
- implLines.push('}');
228
- return implLines;
246
+ const constructorLines = printConstructor(contract);
247
+ const freeFunctionLines = contract.freeFunctions.map(fn => printFunction(fn));
248
+
249
+ if (constructorLines.length === 0 && freeFunctionLines.length === 0) {
250
+ return [];
251
+ }
252
+
253
+ const implBlock: Lines[] = ['#[contractimpl]', `impl ${contract.name} {`];
254
+
255
+ if (constructorLines.length > 0) {
256
+ implBlock.push(constructorLines);
257
+ }
258
+
259
+ if (constructorLines.length > 0 && freeFunctionLines.length > 0) {
260
+ // Add line break between constructor and first free function
261
+ implBlock.push('');
262
+ }
263
+
264
+ if (freeFunctionLines.length > 0) {
265
+ implBlock.push(spaceBetween(...freeFunctionLines));
266
+ }
267
+
268
+ implBlock.push('}');
269
+ return implBlock;
229
270
  }
230
271
 
231
272
  function printConstructor(contract: Contract): Lines[] {
232
273
  if (contract.constructorCode.length > 0) {
233
- const head = 'pub fn __constructor';
274
+ const head = 'fn __constructor';
234
275
  const args = [getSelfArg(), ...contract.constructorArgs];
235
276
 
236
277
  const body = spaceBetween(withSemicolons(contract.constructorCode));
237
278
 
238
279
  const constructor = printFunction2(
280
+ true,
239
281
  head,
240
282
  args.map(a => printArgument(a)),
241
283
  undefined,
@@ -252,6 +294,7 @@ function printConstructor(contract: Contract): Lines[] {
252
294
  // generic for functions and constructors
253
295
  // kindedName = 'fn foo'
254
296
  function printFunction2(
297
+ pub: boolean | undefined,
255
298
  kindedName: string,
256
299
  args: string[],
257
300
  tag: string | undefined,
@@ -265,7 +308,13 @@ function printFunction2(
265
308
  fn.push(`#[${tag}]`);
266
309
  }
267
310
 
268
- let accum = `${kindedName}(`;
311
+ let accum = '';
312
+
313
+ if (pub) {
314
+ accum += 'pub ';
315
+ }
316
+
317
+ accum += `${kindedName}(`;
269
318
 
270
319
  if (args.length > 0) {
271
320
  const formattedArgs = args.join(', ');
@@ -1,4 +1,4 @@
1
- import type { BaseFunction, BaseImplementedTrait, ContractBuilder } from './contract';
1
+ import type { BaseFunction, BaseTraitImplBlock, ContractBuilder } from './contract';
2
2
 
3
3
  export const accessOptions = [false, 'ownable'] as const;
4
4
  export const DEFAULT_ACCESS_CONTROL = 'ownable';
@@ -37,7 +37,7 @@ export function setAccessControl(c: ContractBuilder, access: Access): void {
37
37
  */
38
38
  export function requireAccessControl(
39
39
  c: ContractBuilder,
40
- trait: BaseImplementedTrait,
40
+ trait: BaseTraitImplBlock | undefined,
41
41
  fn: BaseFunction,
42
42
  access: Access,
43
43
  caller?: string,
@@ -54,14 +54,13 @@ export function requireAccessControl(
54
54
  if (caller) {
55
55
  c.addUseClause('soroban_sdk', 'panic_with_error');
56
56
  c.addError('Unauthorized', 1); // TODO: Ensure there are no conflicts in error codes
57
- c.addFunctionCodeBefore(trait, fn, [
58
- getOwner,
59
- 'if owner != caller {',
60
- ` panic_with_error!(e, ${c.name}Error::Unauthorized)`,
61
- '}',
62
- ]);
57
+ c.addFunctionCodeBefore(
58
+ fn,
59
+ [getOwner, `if owner != ${caller} {`, ` panic_with_error!(e, ${c.name}Error::Unauthorized)`, '}'],
60
+ trait,
61
+ );
63
62
  } else {
64
- c.addFunctionCodeBefore(trait, fn, [getOwner, 'owner.require_auth();']);
63
+ c.addFunctionCodeBefore(fn, [getOwner, 'owner.require_auth();'], trait);
65
64
  }
66
65
  break;
67
66
  }
package/src/test.ts CHANGED
@@ -17,6 +17,10 @@ test.serial('fungible result generated', async t => {
17
17
  await testGenerate(t, 'Fungible');
18
18
  });
19
19
 
20
+ test.serial('non-fungible result generated', async t => {
21
+ await testGenerate(t, 'NonFungible');
22
+ });
23
+
20
24
  async function testGenerate(t: ExecutionContext<Context>, kind: keyof KindedOptions) {
21
25
  const generatedSourcesPath = path.join(os.tmpdir(), 'oz-wizard-stellar');
22
26
  await fs.rm(generatedSourcesPath, { force: true, recursive: true });
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * The actual latest version to use in links.
3
3
  */
4
- export const contractsVersion = '0.1.0';
4
+ export const contractsVersion = '0.2.0';
5
5
  export const contractsVersionTag = `v${contractsVersion}`;
6
6
 
7
7
  /**
8
8
  * Semantic version string representing of the minimum compatible version of Contracts to display in output.
9
9
  */
10
- export const compatibleContractsSemver = '^0.1.0';
10
+ export const compatibleContractsSemver = '^0.2.0';