Skip to content

Commit e0eabc9

Browse files
authored
Use Cairo 1+ and OpenZeppelin Contracts for Cairo v0.8.0 (#305)
1 parent 61ef0af commit e0eabc9

Some content is hidden

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

68 files changed

+3422
-5685
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
node-version: 18.x
2222
cache: 'yarn'
2323
- name: Install Foundry
24+
if: matrix.package == 'core'
2425
uses: foundry-rs/foundry-toolchain@v1
2526
- name: Install dependencies
2627
run: yarn install

packages/core-cairo/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- **Breaking changes**:
6+
- Use Cairo 1+ and OpenZeppelin Contracts for Cairo v0.8.0.
7+
- Remove functions for `getInitialSupply` and `toUint256`.
8+
- Remove ERC1155.
9+
- Role-Based Access Control adds separate constructor arguments to assign different users for different roles.
10+
- Throws error if `name` results in an identifer that is empty or does not have valid characters.
11+
- Throws error if `name` or `symbol` result in strings longer than 31 characters.
12+
313
## 0.6.0 (2023-01-11)
414

515
- Add ERC1155. ([#167](https://github.com/OpenZeppelin/contracts-wizard/pull/167))

packages/core-cairo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openzeppelin/wizard-cairo",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "A boilerplate generator to get started with OpenZeppelin Contracts for Cairo",
55
"license": "MIT",
66
"repository": "github:OpenZeppelin/contracts-wizard",
@@ -20,7 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"@types/bn.js": "^5.1.0",
23-
"@types/node": "^10.17.51",
23+
"@types/node": "^18.0.0",
2424
"array.prototype.flat": "^1.2.4",
2525
"ava": "^5.0.0",
2626
"rimraf": "^5.0.0",
Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,62 @@
1-
import { withImplicitArgs } from './common-options';
2-
import type { ContractBuilder, BaseFunction } from './contract';
1+
import { getSelfArg } from './common-options';
2+
import type { BaseImplementedTrait, ContractBuilder, ContractFunction } from './contract';
33
import { Access, requireAccessControl } from './set-access-control';
44
import { defineFunctions } from './utils/define-functions';
5-
import { defineModules } from './utils/define-modules';
5+
import { defineComponents } from './utils/define-components';
6+
import { externalTrait } from './external-trait';
67

7-
export function addPausable(c: ContractBuilder, access: Access, pausableFns: BaseFunction[]) {
8-
c.addModule(modules.Pausable, [], [functions.pause, functions.unpause], false);
8+
export function addPausable(c: ContractBuilder, access: Access) {
9+
c.addComponent(components.PausableComponent, [], false);
910

10-
for (const fn of pausableFns) {
11-
setPausable(c, fn);
12-
}
13-
14-
c.addFunction(functions.paused);
15-
16-
requireAccessControl(c, functions.pause, access, 'PAUSER');
17-
requireAccessControl(c, functions.unpause, access, 'PAUSER');
11+
c.addFunction(externalTrait, functions.pause);
12+
c.addFunction(externalTrait, functions.unpause);
13+
requireAccessControl(c, externalTrait, functions.pause, access, 'PAUSER', 'pauser');
14+
requireAccessControl(c, externalTrait, functions.unpause, access, 'PAUSER', 'pauser');
1815
}
1916

20-
const modules = defineModules( {
21-
Pausable: {
22-
path: 'openzeppelin.security.pausable.library',
23-
useNamespace: true
17+
const components = defineComponents( {
18+
PausableComponent: {
19+
path: 'openzeppelin::security::pausable',
20+
substorage: {
21+
name: 'pausable',
22+
type: 'PausableComponent::Storage',
23+
},
24+
event: {
25+
name: 'PausableEvent',
26+
type: 'PausableComponent::Event',
27+
},
28+
impls: [
29+
{
30+
name: 'PausableImpl',
31+
value: 'PausableComponent::PausableImpl<ContractState>',
32+
},
33+
],
34+
internalImpl: {
35+
name: 'PausableInternalImpl',
36+
value: 'PausableComponent::InternalImpl<ContractState>',
37+
},
2438
},
2539
});
2640

2741
const functions = defineFunctions({
28-
29-
paused: {
30-
module: modules.Pausable,
31-
kind: 'view',
32-
implicitArgs: withImplicitArgs(),
33-
args: [],
34-
returns: [{ name: 'paused', type: 'felt' }],
35-
passthrough: true,
36-
parentFunctionName: 'is_paused',
37-
},
38-
3942
pause: {
40-
module: modules.Pausable,
41-
kind: 'external',
42-
implicitArgs: withImplicitArgs(),
43-
args: [],
44-
parentFunctionName: '_pause',
43+
args: [
44+
getSelfArg(),
45+
],
46+
code: [
47+
'self.pausable._pause()'
48+
]
4549
},
46-
4750
unpause: {
48-
module: modules.Pausable,
49-
kind: 'external',
50-
implicitArgs: withImplicitArgs(),
51-
args: [],
52-
parentFunctionName: '_unpause',
53-
},
54-
55-
// --- library-only calls ---
56-
57-
assert_not_paused: {
58-
module: modules.Pausable,
59-
args: [],
51+
args: [
52+
getSelfArg(),
53+
],
54+
code: [
55+
'self.pausable._unpause()'
56+
]
6057
},
61-
6258
});
6359

64-
export function setPausable(c: ContractBuilder, fn: BaseFunction) {
65-
c.addLibraryCall(functions.assert_not_paused, fn);
60+
export function setPausable(c: ContractBuilder, t: BaseImplementedTrait, fn: ContractFunction) {
61+
c.addFunctionCodeBefore(t, fn, 'self.pausable.assert_not_paused()');
6662
}

packages/core-cairo/src/api.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { CommonOptions } from './common-options';
2-
import { printERC20, defaults as erc20defaults, isAccessControlRequired as erc20IsAccessControlRequired, ERC20Options, getInitialSupply } from './erc20';
2+
import { printERC20, defaults as erc20defaults, isAccessControlRequired as erc20IsAccessControlRequired, ERC20Options } from './erc20';
33
import { printERC721, defaults as erc721defaults, isAccessControlRequired as erc721IsAccessControlRequired, ERC721Options } from './erc721';
4-
import { printERC1155, defaults as erc1155defaults, isAccessControlRequired as erc1155IsAccessControlRequired, ERC1155Options } from './erc1155';
54
import { printCustom, defaults as customDefaults, isAccessControlRequired as customIsAccessControlRequired, CustomOptions } from './custom';
6-
import { toUint256 } from './utils/uint256';
75

86
export interface WizardContractAPI<Options extends CommonOptions> {
97
/**
@@ -23,37 +21,22 @@ export interface WizardContractAPI<Options extends CommonOptions> {
2321
isAccessControlRequired: (opts: Partial<Options>) => boolean,
2422
}
2523

26-
export type ERC20 = WizardContractAPI<ERC20Options> & {
27-
/**
28-
* Calculates the initial supply that would be used in an ERC20 contract based on a given premint amount and number of decimals.
29-
*/
30-
getInitialSupply: (premint: string, decimals: number) => string;
31-
}
24+
export type ERC20 = WizardContractAPI<ERC20Options>;
3225
export type ERC721 = WizardContractAPI<ERC721Options>;
33-
export type ERC1155 = WizardContractAPI<ERC1155Options>;
3426
export type Custom = WizardContractAPI<CustomOptions>;
3527

3628
export const erc20: ERC20 = {
3729
print: printERC20,
3830
defaults: erc20defaults,
3931
isAccessControlRequired: erc20IsAccessControlRequired,
40-
getInitialSupply
4132
}
4233
export const erc721: ERC721 = {
4334
print: printERC721,
4435
defaults: erc721defaults,
4536
isAccessControlRequired: erc721IsAccessControlRequired
4637
}
47-
export const erc1155: ERC1155 = {
48-
print: printERC1155,
49-
defaults: erc1155defaults,
50-
isAccessControlRequired: erc1155IsAccessControlRequired
51-
}
5238
export const custom: Custom = {
5339
print: printCustom,
5440
defaults: customDefaults,
5541
isAccessControlRequired: customIsAccessControlRequired
5642
}
57-
export const utils = {
58-
toUint256
59-
}

packages/core-cairo/src/build-generic.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { ERC20Options, buildERC20 } from './erc20';
22
import { ERC721Options, buildERC721 } from './erc721';
3-
import { ERC1155Options, buildERC1155 } from './erc1155';
43
import { CustomOptions, buildCustom } from './custom';
54

65
export interface KindedOptions {
76
ERC20: { kind: 'ERC20' } & ERC20Options;
87
ERC721: { kind: 'ERC721' } & ERC721Options;
9-
ERC1155: { kind: 'ERC1155' } & ERC1155Options;
108
Custom: { kind: 'Custom' } & CustomOptions;
119
}
1210

@@ -20,9 +18,6 @@ export function buildGeneric(opts: GenericOptions) {
2018
case 'ERC721':
2119
return buildERC721(opts);
2220

23-
case 'ERC1155':
24-
return buildERC1155(opts);
25-
2621
case 'Custom':
2722
return buildCustom(opts);
2823

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { ContractBuilder } from "./contract";
2+
import { defineComponents } from "./utils/define-components";
3+
4+
const components = defineComponents( {
5+
SRC5Component: {
6+
path: 'openzeppelin::introspection::src5',
7+
substorage: {
8+
name: 'src5',
9+
type: 'SRC5Component::Storage',
10+
},
11+
event: {
12+
name: 'SRC5Event',
13+
type: 'SRC5Component::Event',
14+
},
15+
impls: [
16+
{
17+
name: 'SRC5Impl',
18+
value: 'SRC5Component::SRC5Impl<ContractState>',
19+
},
20+
],
21+
},
22+
})
23+
24+
export function addSRC5Component(c: ContractBuilder) {
25+
c.addComponent(components.SRC5Component, [], false);
26+
}

packages/core-cairo/src/common-functions.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export function withCommonDefaults(opts: CommonOptions): Required<CommonOptions>
2424
};
2525
}
2626

27-
export function withImplicitArgs(): Argument[] {
28-
return [
29-
{ name: 'syscall_ptr', type: 'felt*' },
30-
{ name: 'pedersen_ptr', type: 'HashBuiltin*' },
31-
{ name: 'range_check_ptr' }
32-
];
27+
export function getSelfArg(scope: 'external' | 'view' = 'external'): Argument {
28+
if (scope === 'view') {
29+
return { name: 'self', type: '@ContractState' };
30+
} else {
31+
return { name: 'ref self', type: 'ContractState' };
32+
}
3333
}

0 commit comments

Comments
 (0)