Skip to content

Commit

Permalink
Support Cairo 1+ and Contracts for Cairo 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Dec 4, 2023
1 parent d55ffdd commit 89f588d
Show file tree
Hide file tree
Showing 62 changed files with 2,364 additions and 5,752 deletions.
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
8 changes: 8 additions & 0 deletions packages/core-cairo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# 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.

## 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 { 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, fn: ContractFunction) {
c.addFunctionCodeBefore(externalTrait, 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.

8 changes: 2 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,6 @@ 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(): Argument {
return { name: 'ref self', type: 'ContractState' };
}
Loading

0 comments on commit 89f588d

Please sign in to comment.