Skip to content

Commit bec86b5

Browse files
authored
[Cairo] with_components macro (#703)
1 parent 8a35c93 commit bec86b5

File tree

108 files changed

+12045
-1375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+12045
-1375
lines changed

.changeset/mean-shoes-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openzeppelin/wizard-common': patch
3+
---
4+
5+
Add macros descriptions for Cairo

.github/workflows/compile-cairo-alpha-project.yml

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,50 +47,52 @@ jobs:
4747
set -e
4848
4949
declare -a all_kinds=("ERC20" "ERC721" "ERC1155" "Account" "Multisig" "Governor" "Vesting" "Custom")
50+
declare -a all_macros_options=("none" "with_components")
5051
declare -a all_access_options=("disabled" "ownable" "roles" "roles-dar-default" "roles-dar-custom")
5152
declare -a all_royalty_options=("disabled" "enabled-default" "enabled-custom")
52-
for kind in "${all_kinds[@]}"; do
53-
scarb clean
5453
55-
if [[ "$kind" == "ERC721" || "$kind" == "ERC1155" ]]; then
56-
for access_option in "${all_access_options[@]}"; do
57-
for royalty_option in "${all_royalty_options[@]}"; do
58-
proj_name="'$kind, access: $access_option, royalty: $royalty_option' test project"
54+
for macros_option in "${all_macros_options[@]}"; do
55+
for kind in "${all_kinds[@]}"; do
56+
if [[ "$kind" == "ERC721" || "$kind" == "ERC1155" ]]; then
57+
for access_option in "${all_access_options[@]}"; do
58+
for royalty_option in "${all_royalty_options[@]}"; do
59+
proj_name="'$kind, macros: $macros_option, access: $access_option, royalty: $royalty_option' test project"
60+
echo "Generating $proj_name..."
61+
yarn run update_scarb_project --kind=$kind --macros=$macros_option --access=$access_option --royalty=$royalty_option
62+
63+
echo "Compiling $proj_name..."
64+
scarb build
65+
66+
echo "✅ Compiled $proj_name!"
67+
echo "---------------------------------"
68+
done
69+
done
70+
71+
elif [[ "$kind" == "ERC20" || "$kind" == "Custom" ]]; then
72+
for access_option in "${all_access_options[@]}"; do
73+
proj_name="'$kind, access: $access_option' test project"
5974
echo "Generating $proj_name..."
60-
yarn run update_scarb_project --kind=$kind --access=$access_option --royalty=$royalty_option
75+
yarn run update_scarb_project --kind=$kind --access=$access_option
6176
6277
echo "Compiling $proj_name..."
6378
scarb build
6479
6580
echo "✅ Compiled $proj_name!"
6681
echo "---------------------------------"
6782
done
68-
done
6983
70-
elif [[ "$kind" == "ERC20" || "$kind" == "Custom" ]]; then
71-
for access_option in "${all_access_options[@]}"; do
72-
proj_name="'$kind, access: $access_option' test project"
84+
else
85+
proj_name="'$kind' test project"
86+
7387
echo "Generating $proj_name..."
74-
yarn run update_scarb_project --kind=$kind --access=$access_option
88+
yarn run update_scarb_project --kind=$kind
7589
7690
echo "Compiling $proj_name..."
7791
scarb build
7892
7993
echo "✅ Compiled $proj_name!"
8094
echo "---------------------------------"
81-
done
82-
83-
else
84-
proj_name="'$kind' test project"
85-
86-
echo "Generating $proj_name..."
87-
yarn run update_scarb_project --kind=$kind
88-
89-
echo "Compiling $proj_name..."
90-
scarb build
91-
92-
echo "✅ Compiled $proj_name!"
93-
echo "---------------------------------"
94-
fi
95+
fi
96+
done
9597
9698
done

packages/common/src/ai/descriptions/cairo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export const cairoCommonDescriptions = {
2727
'Provides information for how much royalty is owed and to whom, based on a sale price. Follows ERC-2981 standard.',
2828
};
2929

30+
export const cairoMacrosDescriptions = {
31+
macros: 'The macros to use for the contract.',
32+
withComponents: 'Whether to use the "with_components" macro for simplified contract structure.',
33+
};
34+
3035
export const cairoAlphaAccessDescriptions = {
3136
accessType:
3237
'The type of access control to provision. Ownable is a simple mechanism with a single account authorized for all privileged actions. Roles is a flexible mechanism with a separate role for each privileged action. A role can have many authorized accounts. Roles (Default Admin Rules) provides additional enforced security measures on top of standard Roles mechanism for managing the most privileged role: default admin.',

packages/core/cairo_alpha/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Add support for `with_components` macro. ([#703](https://github.com/OpenZeppelin/contracts-wizard/pull/703))
56
- Add AccessControlDefaultAdminRules. ([#698](https://github.com/OpenZeppelin/contracts-wizard/pull/698))
67

78
- **Breaking changes**:

packages/core/cairo_alpha/ava.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
watchmode: {
55
ignoreChanges: ['contracts', 'artifacts', 'cache'],
66
},
7+
snapshotDir: '.',
78
timeout: '10m',
89
workerThreads: false,
910
};
-2.01 KB
Binary file not shown.

packages/core/cairo_alpha/src/account.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const defaults: Required<AccountOptions> = {
2121
outsideExecution: true,
2222
upgradeable: commonDefaults.upgradeable,
2323
info: commonDefaults.info,
24+
macros: commonDefaults.macros,
2425
} as const;
2526

2627
export function printAccount(opts: AccountOptions = defaults): string {
@@ -49,17 +50,16 @@ function withDefaults(opts: AccountOptions): Required<AccountOptions> {
4950

5051
export function buildAccount(opts: AccountOptions): Contract {
5152
const isAccount = true;
52-
const c = new ContractBuilder(opts.name, isAccount);
53-
5453
const allOpts = withDefaults(opts);
54+
const c = new ContractBuilder(allOpts.name, allOpts.macros, isAccount);
5555

5656
switch (allOpts.type) {
5757
case 'stark':
5858
c.addConstructorArgument({ name: 'public_key', type: 'felt252' });
5959
c.addComponent(components.AccountComponent, [{ lit: 'public_key' }], true);
6060
break;
6161
case 'eth':
62-
c.addUseClause('openzeppelin::interfaces::accounts', 'EthPublicKey');
62+
c.addUseClause('openzeppelin_interfaces::accounts', 'EthPublicKey');
6363
c.addConstructorArgument({ name: 'public_key', type: 'EthPublicKey' });
6464
c.addComponent(components.EthAccountComponent, [{ lit: 'public_key' }], true);
6565
break;
@@ -98,10 +98,12 @@ function addSRC6(c: ContractBuilder, accountType: Account) {
9898

9999
c.addImplToComponent(componentType, {
100100
name: 'SRC6Impl',
101+
embed: true,
101102
value: `${baseComponent}::SRC6Impl<ContractState>`,
102103
});
103104
c.addImplToComponent(componentType, {
104105
name: 'SRC6CamelOnlyImpl',
106+
embed: true,
105107
value: `${baseComponent}::SRC6CamelOnlyImpl<ContractState>`,
106108
});
107109

@@ -113,6 +115,7 @@ function addDeclarer(c: ContractBuilder, accountType: Account) {
113115

114116
c.addImplToComponent(componentType, {
115117
name: 'DeclarerImpl',
118+
embed: true,
116119
value: `${baseComponent}::DeclarerImpl<ContractState>`,
117120
});
118121
}
@@ -122,6 +125,7 @@ function addDeployer(c: ContractBuilder, accountType: Account) {
122125

123126
c.addImplToComponent(componentType, {
124127
name: 'DeployableImpl',
128+
embed: true,
125129
value: `${baseComponent}::DeployableImpl<ContractState>`,
126130
});
127131
}
@@ -131,16 +135,17 @@ function addPublicKey(c: ContractBuilder, accountType: Account) {
131135

132136
c.addImplToComponent(componentType, {
133137
name: 'PublicKeyImpl',
138+
embed: true,
134139
value: `${baseComponent}::PublicKeyImpl<ContractState>`,
135140
});
136141
c.addImplToComponent(componentType, {
137142
name: 'PublicKeyCamelImpl',
143+
embed: true,
138144
value: `${baseComponent}::PublicKeyCamelImpl<ContractState>`,
139145
});
140146
}
141147

142148
function addOutsideExecution(c: ContractBuilder) {
143-
c.addUseClause('openzeppelin::account::extensions', 'SRC9Component');
144149
c.addComponent(components.SRC9Component, [], true);
145150
}
146151

@@ -151,6 +156,7 @@ function addAccountMixin(c: ContractBuilder, accountType: Account) {
151156
c.addImplToComponent(componentType, {
152157
name: `${accountMixinImpl}`,
153158
value: `${baseComponent}::${accountMixinImpl}<ContractState>`,
159+
embed: true,
154160
});
155161

156162
c.addInterfaceFlag('ISRC5');
@@ -167,7 +173,7 @@ function getBaseCompAndCompType(accountType: Account): [string, typeof component
167173

168174
const components = defineComponents({
169175
AccountComponent: {
170-
path: 'openzeppelin::account',
176+
path: 'openzeppelin_account',
171177
substorage: {
172178
name: 'account',
173179
type: 'AccountComponent::Storage',
@@ -185,7 +191,7 @@ const components = defineComponents({
185191
],
186192
},
187193
EthAccountComponent: {
188-
path: 'openzeppelin::account::eth_account',
194+
path: 'openzeppelin_account::eth_account',
189195
substorage: {
190196
name: 'eth_account',
191197
type: 'EthAccountComponent::Storage',
@@ -203,7 +209,7 @@ const components = defineComponents({
203209
],
204210
},
205211
SRC9Component: {
206-
path: 'openzeppelin::account::extensions',
212+
path: 'openzeppelin_account::extensions',
207213
substorage: {
208214
name: 'src9',
209215
type: 'SRC9Component::Storage',
@@ -215,6 +221,7 @@ const components = defineComponents({
215221
impls: [
216222
{
217223
name: 'OutsideExecutionV2Impl',
224+
embed: true,
218225
value: 'SRC9Component::OutsideExecutionV2Impl<ContractState>',
219226
},
220227
{

packages/core/cairo_alpha/src/add-pausable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function addPausable(c: ContractBuilder, access: Access) {
1717

1818
const components = defineComponents({
1919
PausableComponent: {
20-
path: 'openzeppelin::security::pausable',
20+
path: 'openzeppelin_security::pausable',
2121
substorage: {
2222
name: 'pausable',
2323
type: 'PausableComponent::Storage',
@@ -29,6 +29,7 @@ const components = defineComponents({
2929
impls: [
3030
{
3131
name: 'PausableImpl',
32+
embed: true,
3233
value: 'PausableComponent::PausableImpl<ContractState>',
3334
},
3435
{

packages/core/cairo_alpha/src/common-components.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type Token = (typeof tokenTypes)[number];
66

77
const components = defineComponents({
88
SRC5Component: {
9-
path: 'openzeppelin::introspection::src5',
9+
path: 'openzeppelin_introspection::src5',
1010
substorage: {
1111
name: 'src5',
1212
type: 'SRC5Component::Storage',
@@ -19,7 +19,7 @@ const components = defineComponents({
1919
},
2020

2121
VotesComponent: {
22-
path: 'openzeppelin::governance::votes',
22+
path: 'openzeppelin_governance::votes',
2323
substorage: {
2424
name: 'votes',
2525
type: 'VotesComponent::Storage',
@@ -38,7 +38,7 @@ const components = defineComponents({
3838
},
3939

4040
NoncesComponent: {
41-
path: 'openzeppelin::utils::cryptography::nonces',
41+
path: 'openzeppelin_utils::cryptography::nonces',
4242
substorage: {
4343
name: 'nonces',
4444
type: 'NoncesComponent::Storage',
@@ -50,6 +50,7 @@ const components = defineComponents({
5050
impls: [
5151
{
5252
name: 'NoncesImpl',
53+
embed: true,
5354
value: 'NoncesComponent::NoncesImpl<ContractState>',
5455
},
5556
],
@@ -62,6 +63,7 @@ export function addSRC5Component(c: ContractBuilder, section?: string) {
6263
if (!c.interfaceFlags.has('ISRC5')) {
6364
c.addImplToComponent(components.SRC5Component, {
6465
name: 'SRC5Impl',
66+
embed: true,
6567
value: 'SRC5Component::SRC5Impl<ContractState>',
6668
section,
6769
});
@@ -75,13 +77,14 @@ export function addVotesComponent(c: ContractBuilder, name: string, version: str
7577
c.addComponent(components.VotesComponent, [], false);
7678
c.addImplToComponent(components.VotesComponent, {
7779
name: 'VotesImpl',
80+
embed: true,
7881
value: `VotesComponent::VotesImpl<ContractState>`,
7982
});
80-
c.addUseClause('openzeppelin::utils::contract_clock', 'ERC6372TimestampClock');
83+
c.addUseClause('openzeppelin_utils::contract_clock', 'ERC6372TimestampClock');
8184
}
8285

8386
export function addSNIP12Metadata(c: ContractBuilder, name: string, version: string, section?: string) {
84-
c.addUseClause('openzeppelin::utils::cryptography::snip12', 'SNIP12Metadata');
87+
c.addUseClause('openzeppelin_utils::cryptography::snip12', 'SNIP12Metadata');
8588

8689
const SNIP12Metadata: BaseImplementedTrait = {
8790
name: 'SNIP12MetadataImpl',

packages/core/cairo_alpha/src/common-options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { AccessControl } from './set-access-control';
44
import type { Info } from './set-info';
55
import { defaults as infoDefaults } from './set-info';
66
import type { Upgradeable } from './set-upgradeable';
7+
import type { MacrosOptions } from './set-macros';
8+
import { defaults as macrosDefaults } from './set-macros';
79

810
export const defaults: Required<CommonOptions> = {
911
upgradeable: true,
1012
info: infoDefaults,
13+
macros: macrosDefaults,
1114
} as const;
1215

1316
export const contractDefaults: Required<CommonContractOptions> = {
@@ -18,6 +21,7 @@ export const contractDefaults: Required<CommonContractOptions> = {
1821
export interface CommonOptions {
1922
upgradeable?: Upgradeable;
2023
info?: Info;
24+
macros?: MacrosOptions;
2125
}
2226

2327
export interface CommonContractOptions extends CommonOptions {
@@ -28,6 +32,7 @@ export function withCommonDefaults(opts: CommonOptions): Required<CommonOptions>
2832
return {
2933
upgradeable: opts.upgradeable ?? defaults.upgradeable,
3034
info: opts.info ?? defaults.info,
35+
macros: opts.macros ?? defaults.macros,
3136
};
3237
}
3338

0 commit comments

Comments
 (0)