Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cairo 1+ and OpenZeppelin Contracts for Cairo v0.8.0 #305

Merged
merged 29 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
89f588d
Support Cairo 1+ and Contracts for Cairo 0.8.0
ericglau Dec 4, 2023
8bf4776
Add reimplemented impls WIP
ericglau Dec 4, 2023
9620830
Handle returns WIP
ericglau Dec 4, 2023
41018a1
Reimplement safe allowance
ericglau Dec 4, 2023
c5f7f90
Fix function syntax
ericglau Dec 5, 2023
820d285
Update snapshots
ericglau Dec 5, 2023
a5575b9
Properly sanitize short strings
ericglau Dec 5, 2023
1194c62
Improve string handling
ericglau Dec 5, 2023
f176251
Add tests
ericglau Dec 5, 2023
38432df
Rename file
ericglau Dec 5, 2023
4fb644b
Rename file
ericglau Dec 5, 2023
2cac880
Reimplement ERC721Impl and CamelOnly
ericglau Dec 5, 2023
51ada35
Simplify reimplementing ERC20 impls
ericglau Dec 5, 2023
8597614
Update hover text and links
ericglau Dec 5, 2023
e6148f1
Handle decimals in erc20 premint
ericglau Dec 5, 2023
47b1e68
Import erc20 and erc721 interfaces
ericglau Dec 6, 2023
0726444
Use ExternalImpl
ericglau Dec 6, 2023
2de0194
Update snapshots
ericglau Dec 6, 2023
172ea65
Sort imports
ericglau Dec 6, 2023
23f129e
Update Download text
ericglau Dec 6, 2023
bd44ca3
Fix hyperlink for interface import
ericglau Dec 6, 2023
6126031
Update Download message
ericglau Dec 6, 2023
50b85f5
Use snake case for default admin
ericglau Dec 7, 2023
a8e3d2f
Improve error handling
ericglau Dec 7, 2023
0894e65
Make mintable pausable
ericglau Dec 7, 2023
089a192
Update changelog
ericglau Dec 7, 2023
f1f7085
Add safeMint
ericglau Dec 7, 2023
8324fcf
Reorder functions to make output more stable
ericglau Dec 7, 2023
4d9dce5
Use camel case for safeMint params
ericglau Dec 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
node-version: 18.x
cache: 'yarn'
- name: Install Foundry
if: matrix.package == 'core'
uses: foundry-rs/foundry-toolchain@v1
- name: Install dependencies
run: yarn install
Expand Down
10 changes: 10 additions & 0 deletions packages/core-cairo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased

- **Breaking changes**:
- Use Cairo 1+ and OpenZeppelin Contracts for Cairo v0.8.0.
- Remove functions for `getInitialSupply` and `toUint256`.
- Remove ERC1155.
- Role-Based Access Control adds separate constructor arguments to assign different users for different roles.
- Throws error if `name` results in an identifer that is empty or does not have valid characters.
- Throws error if `name` or `symbol` result in strings longer than 31 characters.

## 0.6.0 (2023-01-11)

- Add ERC1155. ([#167](https://github.com/OpenZeppelin/contracts-wizard/pull/167))
Expand Down
4 changes: 2 additions & 2 deletions packages/core-cairo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openzeppelin/wizard-cairo",
"version": "0.6.0",
"version": "0.7.0",
"description": "A boilerplate generator to get started with OpenZeppelin Contracts for Cairo",
"license": "MIT",
"repository": "github:OpenZeppelin/contracts-wizard",
Expand All @@ -20,7 +20,7 @@
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/node": "^10.17.51",
"@types/node": "^18.0.0",
"array.prototype.flat": "^1.2.4",
"ava": "^5.0.0",
"rimraf": "^5.0.0",
Expand Down
94 changes: 45 additions & 49 deletions packages/core-cairo/src/add-pausable.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
import { withImplicitArgs } from './common-options';
import type { ContractBuilder, BaseFunction } from './contract';
import { getSelfArg } from './common-options';
import type { BaseImplementedTrait, ContractBuilder, ContractFunction } from './contract';
import { Access, requireAccessControl } from './set-access-control';
import { defineFunctions } from './utils/define-functions';
import { defineModules } from './utils/define-modules';
import { defineComponents } from './utils/define-components';
import { externalTrait } from './external-trait';

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

for (const fn of pausableFns) {
setPausable(c, fn);
}

c.addFunction(functions.paused);

requireAccessControl(c, functions.pause, access, 'PAUSER');
requireAccessControl(c, functions.unpause, access, 'PAUSER');
c.addFunction(externalTrait, functions.pause);
c.addFunction(externalTrait, functions.unpause);
requireAccessControl(c, externalTrait, functions.pause, access, 'PAUSER', 'pauser');
requireAccessControl(c, externalTrait, functions.unpause, access, 'PAUSER', 'pauser');
}

const modules = defineModules( {
Pausable: {
path: 'openzeppelin.security.pausable.library',
useNamespace: true
const components = defineComponents( {
PausableComponent: {
path: 'openzeppelin::security::pausable',
substorage: {
name: 'pausable',
type: 'PausableComponent::Storage',
},
event: {
name: 'PausableEvent',
type: 'PausableComponent::Event',
},
impls: [
{
name: 'PausableImpl',
value: 'PausableComponent::PausableImpl<ContractState>',
},
],
internalImpl: {
name: 'PausableInternalImpl',
value: 'PausableComponent::InternalImpl<ContractState>',
},
},
});

const functions = defineFunctions({

paused: {
module: modules.Pausable,
kind: 'view',
implicitArgs: withImplicitArgs(),
args: [],
returns: [{ name: 'paused', type: 'felt' }],
passthrough: true,
parentFunctionName: 'is_paused',
},

pause: {
module: modules.Pausable,
kind: 'external',
implicitArgs: withImplicitArgs(),
args: [],
parentFunctionName: '_pause',
args: [
getSelfArg(),
],
code: [
'self.pausable._pause()'
]
},

unpause: {
module: modules.Pausable,
kind: 'external',
implicitArgs: withImplicitArgs(),
args: [],
parentFunctionName: '_unpause',
},

// --- library-only calls ---

assert_not_paused: {
module: modules.Pausable,
args: [],
args: [
getSelfArg(),
],
code: [
'self.pausable._unpause()'
]
},

});

export function setPausable(c: ContractBuilder, fn: BaseFunction) {
c.addLibraryCall(functions.assert_not_paused, fn);
export function setPausable(c: ContractBuilder, t: BaseImplementedTrait, fn: ContractFunction) {
c.addFunctionCodeBefore(t, fn, 'self.pausable.assert_not_paused()');
}
21 changes: 2 additions & 19 deletions packages/core-cairo/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { CommonOptions } from './common-options';
import { printERC20, defaults as erc20defaults, isAccessControlRequired as erc20IsAccessControlRequired, ERC20Options, getInitialSupply } from './erc20';
import { printERC20, defaults as erc20defaults, isAccessControlRequired as erc20IsAccessControlRequired, ERC20Options } from './erc20';
import { printERC721, defaults as erc721defaults, isAccessControlRequired as erc721IsAccessControlRequired, ERC721Options } from './erc721';
import { printERC1155, defaults as erc1155defaults, isAccessControlRequired as erc1155IsAccessControlRequired, ERC1155Options } from './erc1155';
import { printCustom, defaults as customDefaults, isAccessControlRequired as customIsAccessControlRequired, CustomOptions } from './custom';
import { toUint256 } from './utils/uint256';

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

export type ERC20 = WizardContractAPI<ERC20Options> & {
/**
* Calculates the initial supply that would be used in an ERC20 contract based on a given premint amount and number of decimals.
*/
getInitialSupply: (premint: string, decimals: number) => string;
}
export type ERC20 = WizardContractAPI<ERC20Options>;
export type ERC721 = WizardContractAPI<ERC721Options>;
export type ERC1155 = WizardContractAPI<ERC1155Options>;
export type Custom = WizardContractAPI<CustomOptions>;

export const erc20: ERC20 = {
print: printERC20,
defaults: erc20defaults,
isAccessControlRequired: erc20IsAccessControlRequired,
getInitialSupply
}
export const erc721: ERC721 = {
print: printERC721,
defaults: erc721defaults,
isAccessControlRequired: erc721IsAccessControlRequired
}
export const erc1155: ERC1155 = {
print: printERC1155,
defaults: erc1155defaults,
isAccessControlRequired: erc1155IsAccessControlRequired
}
export const custom: Custom = {
print: printCustom,
defaults: customDefaults,
isAccessControlRequired: customIsAccessControlRequired
}
export const utils = {
toUint256
}
5 changes: 0 additions & 5 deletions packages/core-cairo/src/build-generic.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ERC20Options, buildERC20 } from './erc20';
import { ERC721Options, buildERC721 } from './erc721';
import { ERC1155Options, buildERC1155 } from './erc1155';
import { CustomOptions, buildCustom } from './custom';

export interface KindedOptions {
ERC20: { kind: 'ERC20' } & ERC20Options;
ERC721: { kind: 'ERC721' } & ERC721Options;
ERC1155: { kind: 'ERC1155' } & ERC1155Options;
Custom: { kind: 'Custom' } & CustomOptions;
}

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

case 'ERC1155':
return buildERC1155(opts);

case 'Custom':
return buildCustom(opts);

Expand Down
26 changes: 26 additions & 0 deletions packages/core-cairo/src/common-components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ContractBuilder } from "./contract";
import { defineComponents } from "./utils/define-components";

const components = defineComponents( {
SRC5Component: {
path: 'openzeppelin::introspection::src5',
substorage: {
name: 'src5',
type: 'SRC5Component::Storage',
},
event: {
name: 'SRC5Event',
type: 'SRC5Component::Event',
},
impls: [
{
name: 'SRC5Impl',
value: 'SRC5Component::SRC5Impl<ContractState>',
},
],
},
})

export function addSRC5Component(c: ContractBuilder) {
c.addComponent(components.SRC5Component, [], false);
}
45 changes: 0 additions & 45 deletions packages/core-cairo/src/common-functions.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/core-cairo/src/common-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export function withCommonDefaults(opts: CommonOptions): Required<CommonOptions>
};
}

export function withImplicitArgs(): Argument[] {
return [
{ name: 'syscall_ptr', type: 'felt*' },
{ name: 'pedersen_ptr', type: 'HashBuiltin*' },
{ name: 'range_check_ptr' }
];
export function getSelfArg(scope: 'external' | 'view' = 'external'): Argument {
if (scope === 'view') {
return { name: 'self', type: '@ContractState' };
} else {
return { name: 'ref self', type: 'ContractState' };
}
}
Loading